1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
*
|
6
|
* Handle the forms for changing a display's layout.
|
7
|
*/
|
8
|
|
9
|
/**
|
10
|
* Handle calling and processing of the form for editing display layouts.
|
11
|
*
|
12
|
* Helper function for panels_edit_layout().
|
13
|
*
|
14
|
* @see panels_edit_layout() for details on the various behaviors of this function.
|
15
|
*/
|
16
|
function _panels_edit_layout($display, $finish, $destination, $allowed_layouts) {
|
17
|
ctools_include('common', 'panels');
|
18
|
|
19
|
$form_state = array(
|
20
|
'display' => &$display,
|
21
|
'finish' => $finish,
|
22
|
'destination' => $destination,
|
23
|
'allowed_layouts' => $allowed_layouts,
|
24
|
're_render' => FALSE,
|
25
|
'no_redirect' => TRUE,
|
26
|
);
|
27
|
|
28
|
$change_form_state = $form_state;
|
29
|
|
30
|
$change_form = FALSE;
|
31
|
|
32
|
// Examine $_POST to see which form they're currently using.
|
33
|
if (empty($_POST) || empty($_POST['form_id']) || $_POST['form_id'] != 'panels_change_layout') {
|
34
|
$output = drupal_build_form('panels_choose_layout', $form_state);
|
35
|
if (!empty($form_state['executed'])) {
|
36
|
// upon submission go to next form.
|
37
|
$change_form_state['layout'] = $_SESSION['layout'][$display->did] = $form_state['layout'];
|
38
|
$change_form = TRUE;
|
39
|
}
|
40
|
}
|
41
|
else {
|
42
|
$change_form_state['layout'] = $_SESSION['layout'][$display->did];
|
43
|
$change_form = TRUE;
|
44
|
}
|
45
|
|
46
|
if ($change_form) {
|
47
|
$output = drupal_build_form('panels_change_layout', $change_form_state);
|
48
|
if (!empty($change_form_state['executed'])) {
|
49
|
if (isset($change_form_state['back'])) {
|
50
|
unset($_POST);
|
51
|
return _panels_edit_layout($display, $finish, $destination, $allowed_layouts);
|
52
|
}
|
53
|
|
54
|
if (!empty($change_form_state['clicked_button']['#save-display'])) {
|
55
|
drupal_set_message(t('Panel layout has been updated.'));
|
56
|
panels_save_display($display);
|
57
|
}
|
58
|
|
59
|
if ($destination) {
|
60
|
return panels_goto($destination);
|
61
|
}
|
62
|
return $change_form_state['display'];
|
63
|
}
|
64
|
}
|
65
|
return $output;
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Form definition for the display layout editor.
|
70
|
*
|
71
|
* @ingroup forms
|
72
|
*/
|
73
|
function panels_choose_layout($form, &$form_state) {
|
74
|
$display = &$form_state['display'];
|
75
|
ctools_include('common', 'panels');
|
76
|
ctools_include('cleanstring');
|
77
|
|
78
|
$layouts = panels_common_get_allowed_layouts($form_state['allowed_layouts']);
|
79
|
$categories = array();
|
80
|
$current = '';
|
81
|
foreach ($layouts as $id => $layout) {
|
82
|
$category = ctools_cleanstring($layout['category']);
|
83
|
// Default category to first in case layout doesn't exist or there isn't one.
|
84
|
if (empty($current)) {
|
85
|
$current = $category;
|
86
|
}
|
87
|
|
88
|
$categories[$category] = $layout['category'];
|
89
|
$options[$category][$id] = panels_print_layout_icon($id, $layout, check_plain($layout['title']));
|
90
|
|
91
|
// Set current category to what is chosen.
|
92
|
if ($id == $display->layout) {
|
93
|
$current = $category;
|
94
|
}
|
95
|
}
|
96
|
|
97
|
ctools_add_js('panels-base', 'panels');
|
98
|
ctools_add_js('layout', 'panels');
|
99
|
|
100
|
$form['categories'] = array(
|
101
|
'#title' => t('Category'),
|
102
|
'#type' => 'select',
|
103
|
'#options' => $categories,
|
104
|
'#default_value' => $current,
|
105
|
);
|
106
|
|
107
|
$form['layout'] = array(
|
108
|
'#prefix' => '<div class="panels-choose-layout panels-layouts-checkboxes clearfix">',
|
109
|
'#suffix' => '</div>',
|
110
|
);
|
111
|
|
112
|
// We set up the dependencies manually because these aren't really form
|
113
|
// items. It's possible there's a simpler way to do this, but I could not
|
114
|
// think of one at the time.
|
115
|
$dependencies = array();
|
116
|
foreach ($options as $category => $radios) {
|
117
|
$dependencies['panels-layout-category-' . $category] = array(
|
118
|
'values' => array('edit-categories' => array($category)),
|
119
|
'num' => 1,
|
120
|
'type' => 'hide',
|
121
|
);
|
122
|
|
123
|
$form['layout'][$category] = array(
|
124
|
'#prefix' => '<div id="panels-layout-category-' . $category . '-wrapper"><div id="panels-layout-category-' . $category . '" class="form-checkboxes clearfix"><div class="panels-layouts-category">' . $categories[$category] . '</div>',
|
125
|
'#suffix' => '</div></div>',
|
126
|
);
|
127
|
|
128
|
foreach ($radios as $key => $choice) {
|
129
|
// Set the first available layout as default value.
|
130
|
if (empty($display->layout)) {
|
131
|
$display->layout = $key;
|
132
|
}
|
133
|
// Generate the parents as the autogenerator does, so we will have a
|
134
|
// unique id for each radio button.
|
135
|
$form['layout'][$category][$key] = array(
|
136
|
'#type' => 'radio',
|
137
|
'#title' => $choice,
|
138
|
'#parents' => array('layout'),
|
139
|
'#id' => drupal_clean_css_identifier('edit-layout-' . $key),
|
140
|
'#return_value' => check_plain($key),
|
141
|
'#default_value' => in_array($display->layout, array_keys($layouts)) ? $display->layout : NULL,
|
142
|
);
|
143
|
}
|
144
|
}
|
145
|
|
146
|
ctools_add_js('dependent');
|
147
|
$js['CTools']['dependent'] = $dependencies;
|
148
|
drupal_add_js($js, 'setting');
|
149
|
|
150
|
|
151
|
if (empty($form_state['no buttons'])) {
|
152
|
$form['submit'] = array(
|
153
|
'#type' => 'submit',
|
154
|
'#value' => t('Next'),
|
155
|
);
|
156
|
}
|
157
|
|
158
|
return $form;
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* Handle form submission of the display layout editor.
|
163
|
*/
|
164
|
function panels_choose_layout_submit($form, &$form_state) {
|
165
|
$form_state['layout'] = $form_state['values']['layout'];
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* Form definition for the display layout converter.
|
170
|
*
|
171
|
* This form is only triggered if the user attempts to change the layout
|
172
|
* for a display that has already had content assigned to it. It allows
|
173
|
* the user to select where the panes located in to-be-deleted panels should
|
174
|
* be relocated to.
|
175
|
*
|
176
|
* @ingroup forms
|
177
|
*
|
178
|
* @param array $form
|
179
|
* A structured FAPI $form array.
|
180
|
* @param &$form_state
|
181
|
* The Drupal $form_state
|
182
|
*/
|
183
|
function panels_change_layout($form, &$form_state) {
|
184
|
// Provide a temporary display and renderer.
|
185
|
$form_state['layout_display'] = $display = panels_new_display();
|
186
|
if (isset($form_state['cache_key'])) {
|
187
|
$display->cache_key = $form_state['cache_key'];
|
188
|
}
|
189
|
|
190
|
$new_layout = panels_get_layout($form_state['layout']);
|
191
|
$new_layout_regions = panels_get_regions($new_layout, $display);
|
192
|
|
193
|
$old_layout = panels_get_layout($form_state['display']->layout);
|
194
|
$old_layout_regions = panels_get_regions($old_layout, $form_state['display']);
|
195
|
|
196
|
$display->layout = $form_state['layout'];
|
197
|
$renderer = panels_get_renderer_handler('editor', $display);
|
198
|
|
199
|
$renderer->meta_location = 'inline';
|
200
|
|
201
|
ctools_add_css('panels_admin', 'panels');
|
202
|
ctools_add_css('panels_dnd', 'panels');
|
203
|
ctools_add_css('dropdown');
|
204
|
|
205
|
// For every region that had content in the old layout, create a custom pane
|
206
|
// in the new layout that represents that region.
|
207
|
$keys = array_keys($new_layout_regions);
|
208
|
$default_region = reset($keys);
|
209
|
foreach ($old_layout_regions as $region_id => $region_name) {
|
210
|
if (!empty($form_state['display']->panels[$region_id])) {
|
211
|
$pane = panels_new_pane('custom', 'custom', TRUE);
|
212
|
$pane->pid = $region_id;
|
213
|
$pane->configuration['title'] = t('Panes');
|
214
|
$pane->configuration['admin_title'] = $region_name;
|
215
|
// Get a list of pane titles used.
|
216
|
$titles = array();
|
217
|
foreach ($form_state['display']->panels[$region_id] as $pid) {
|
218
|
$content_type = ctools_get_content_type($form_state['display']->content[$pid]->type);
|
219
|
$titles[$pid] = ctools_content_admin_title($content_type, $form_state['display']->content[$pid]->subtype, $form_state['display']->content[$pid]->configuration, $form_state['display']->context);
|
220
|
}
|
221
|
$pane->configuration['body'] = '<ul><li>' . implode('</li><li>', $titles) . '</li></ul>';
|
222
|
|
223
|
// If the region id matches, make it the same; otherwise, put it
|
224
|
// in the default region.
|
225
|
$pane->panel = empty($new_layout_regions[$region_id]) ? $default_region : $region_id;
|
226
|
|
227
|
// Add the pane to the approprate spots.
|
228
|
$display->content[$pane->pid] = $pane;
|
229
|
$display->panels[$pane->panel][] = $pane->pid;
|
230
|
}
|
231
|
}
|
232
|
|
233
|
$form['container'] = array(
|
234
|
'#prefix' => '<div class="change-layout-display clearfix">',
|
235
|
'#suffix' => '</div>',
|
236
|
);
|
237
|
|
238
|
$form['container']['old_layout'] = array(
|
239
|
'#markup' => panels_print_layout_icon($form_state['display']->layout, $old_layout, check_plain($old_layout['title'])),
|
240
|
);
|
241
|
|
242
|
$form['container']['right_arrow'] = array(
|
243
|
'#markup' => theme('image', array('path' => drupal_get_path('module', 'panels') . '/images/go-right.png')),
|
244
|
);
|
245
|
$form['container']['new_layout'] = array(
|
246
|
'#markup' => panels_print_layout_icon($form_state['layout'], $new_layout, check_plain($new_layout['title'])),
|
247
|
);
|
248
|
|
249
|
$edit_form_state = array(
|
250
|
'display' => $display,
|
251
|
'renderer' => $renderer,
|
252
|
'no buttons' => TRUE,
|
253
|
'no preview' => TRUE,
|
254
|
'no display settings' => TRUE,
|
255
|
'display_title' => '',
|
256
|
);
|
257
|
|
258
|
ctools_include('display-edit', 'panels');
|
259
|
$form = panels_edit_display_form($form, $edit_form_state);
|
260
|
|
261
|
if (empty($form_state['no buttons'])) {
|
262
|
$form['back'] = array(
|
263
|
'#type' => 'submit',
|
264
|
'#value' => t('Back'),
|
265
|
'#submit' => array('panels_choose_layout_back'),
|
266
|
);
|
267
|
|
268
|
$form['submit'] = array(
|
269
|
'#type' => 'submit',
|
270
|
'#value' => $form_state['finish'],
|
271
|
'#submit' => array('panels_change_layout_submit'),
|
272
|
'#save-display' => TRUE,
|
273
|
);
|
274
|
}
|
275
|
return $form;
|
276
|
}
|
277
|
|
278
|
/**
|
279
|
* Handle submission of the change layout form.
|
280
|
*
|
281
|
* This submit handler will move panes around and save the display.
|
282
|
*/
|
283
|
function panels_change_layout_submit($form, &$form_state) {
|
284
|
$display = $form_state['display'];
|
285
|
$layout_display = $form_state['layout_display'];
|
286
|
|
287
|
$switch = array();
|
288
|
|
289
|
// Calculate the pids submitted by the display and make a list of
|
290
|
// translation to the regions. Remember the 'pid' of the pane
|
291
|
// is the region id in the old layout.
|
292
|
if (!empty($form_state['values']['panel']['pane'])) {
|
293
|
foreach ($form_state['values']['panel']['pane'] as $region_id => $panes) {
|
294
|
if ($panes) {
|
295
|
$pids = explode(',', $panes);
|
296
|
// need to filter the array, b/c passing it in a hidden field can generate trash
|
297
|
foreach (array_filter($pids) as $pid) {
|
298
|
$switch[$pid] = $region_id;
|
299
|
}
|
300
|
}
|
301
|
}
|
302
|
}
|
303
|
|
304
|
$content = array();
|
305
|
foreach ($switch as $region_id => $new_region_id) {
|
306
|
if (isset($display->panels[$region_id])) {
|
307
|
if (!isset($content[$new_region_id])) {
|
308
|
$content[$new_region_id] = array();
|
309
|
}
|
310
|
$content[$new_region_id] = array_merge($content[$new_region_id], $display->panels[$region_id]);
|
311
|
}
|
312
|
}
|
313
|
|
314
|
// Go through each pane and make sure its region id is correct.
|
315
|
foreach ($content as $region_id => $region) {
|
316
|
foreach ($region as $pid) {
|
317
|
$display->content[$pid]->panel = $region_id;
|
318
|
}
|
319
|
}
|
320
|
|
321
|
$display->panels = $content;
|
322
|
|
323
|
$display->layout = $form_state['layout'];
|
324
|
|
325
|
panels_edit_display_settings_form_submit($form, $form_state);
|
326
|
}
|
327
|
|
328
|
/**
|
329
|
* Handle submission of the change layout form.
|
330
|
*
|
331
|
* This submit handler sets a flag on the form state, which is then used
|
332
|
* by the calling wrapper to restart the process.
|
333
|
*/
|
334
|
function panels_choose_layout_back($form, &$form_state) {
|
335
|
$form_state['back'] = TRUE;
|
336
|
}
|