Projet

Général

Profil

Paste
Télécharger (10,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / panels / includes / display-layout.inc @ 64156087

1
<?php
2

    
3
/**
4
 * @file
5
 * Handle the forms for changing a display's layout.
6
 */
7

    
8
/**
9
 * Handle calling and processing of the form for editing display layouts.
10
 *
11
 * Helper function for panels_edit_layout().
12
 *
13
 * @see panels_edit_layout() for details on the various behaviors of this function.
14
 */
15
function _panels_edit_layout($display, $finish, $destination, $allowed_layouts) {
16
  ctools_include('common', 'panels');
17

    
18
  $form_state = array(
19
    'display' => &$display,
20
    'finish' => $finish,
21
    'destination' => $destination,
22
    'allowed_layouts' => $allowed_layouts,
23
    're_render' => FALSE,
24
    'no_redirect' => TRUE,
25
  );
26

    
27
  $change_form_state = $form_state;
28

    
29
  $change_form = FALSE;
30

    
31
  // Examine $_POST to see which form they're currently using.
32
  if (empty($_POST) || empty($_POST['form_id']) || $_POST['form_id'] != 'panels_change_layout') {
33
    $output = drupal_build_form('panels_choose_layout', $form_state);
34
    if (!empty($form_state['executed'])) {
35
      // Upon submission go to next form.
36
      $change_form_state['layout'] = $_SESSION['layout'][$display->did] = $form_state['layout'];
37
      $change_form = TRUE;
38
    }
39
  }
40
  else {
41
    $change_form_state['layout'] = $_SESSION['layout'][$display->did];
42
    $change_form = TRUE;
43
  }
44

    
45
  if ($change_form) {
46
    $output = drupal_build_form('panels_change_layout', $change_form_state);
47
    if (!empty($change_form_state['executed'])) {
48
      if (isset($change_form_state['back'])) {
49
        unset($_POST);
50
        return _panels_edit_layout($display, $finish, $destination, $allowed_layouts);
51
      }
52

    
53
      if (!empty($change_form_state['clicked_button']['#save-display'])) {
54
        drupal_set_message(t('Panel layout has been updated.'));
55
        panels_save_display($display);
56
      }
57

    
58
      if ($destination) {
59
        return panels_goto($destination);
60
      }
61
      return $change_form_state['display'];
62
    }
63
  }
64
  return $output;
65
}
66

    
67
/**
68
 * Form definition for the display layout editor.
69
 *
70
 * @ingroup forms
71
 */
72
function panels_choose_layout($form, &$form_state) {
73
  $display = &$form_state['display'];
74
  ctools_include('common', 'panels');
75
  ctools_include('cleanstring');
76

    
77
  $layouts = panels_common_get_allowed_layouts($form_state['allowed_layouts']);
78
  $categories = array();
79
  $current = '';
80
  foreach ($layouts as $id => $layout) {
81
    $category = ctools_cleanstring($layout['category']);
82
    // Default category to first in case layout doesn't exist or there isn't one.
83
    if (empty($current)) {
84
      $current = $category;
85
    }
86

    
87
    $categories[$category] = $layout['category'];
88
    $options[$category][$id] = panels_print_layout_icon($id, $layout, check_plain($layout['title']));
89

    
90
    // Set current category to what is chosen.
91
    if ($id == $display->layout) {
92
      $current = $category;
93
    }
94
  }
95

    
96
  ctools_add_js('panels-base', 'panels');
97
  ctools_add_js('layout', 'panels');
98

    
99
  $form['categories'] = array(
100
    '#title' => t('Category'),
101
    '#type' => 'select',
102
    '#options' => $categories,
103
    '#default_value' => $current,
104
  );
105

    
106
  $form['layout'] = array(
107
    '#prefix' => '<div class="panels-choose-layout panels-layouts-checkboxes clearfix">',
108
    '#suffix' => '</div>',
109
  );
110

    
111
  // We set up the dependencies manually because these aren't really form
112
  // items. It's possible there's a simpler way to do this, but I could not
113
  // think of one at the time.
114
  $dependencies = array();
115
  foreach ($options as $category => $radios) {
116
    $dependencies['panels-layout-category-' . $category] = array(
117
      'values' => array('edit-categories' => array($category)),
118
      'num' => 1,
119
      'type' => 'hide',
120
    );
121

    
122
    $form['layout'][$category] = array(
123
      '#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>',
124
      '#suffix' => '</div></div>',
125
    );
126

    
127
    foreach ($radios as $key => $choice) {
128
      // Set the first available layout as default value.
129
      if (empty($display->layout)) {
130
        $display->layout = $key;
131
      }
132
      // Generate the parents as the autogenerator does, so we will have a
133
      // unique id for each radio button.
134
      $form['layout'][$category][$key] = array(
135
        '#type' => 'radio',
136
        '#title' => $choice,
137
        '#parents' => array('layout'),
138
        '#id' => drupal_clean_css_identifier('edit-layout-' . $key),
139
        '#return_value' => check_plain($key),
140
        '#default_value' => in_array($display->layout, array_keys($layouts)) ? $display->layout : NULL,
141
      );
142
    }
143
  }
144

    
145
  ctools_add_js('dependent');
146
  $js['CTools']['dependent'] = $dependencies;
147
  drupal_add_js($js, 'setting');
148

    
149
  if (empty($form_state['no buttons'])) {
150
    $form['submit'] = array(
151
      '#type' => 'submit',
152
      '#value' => t('Next'),
153
    );
154
  }
155

    
156
  return $form;
157
}
158

    
159
/**
160
 * Handle form submission of the display layout editor.
161
 */
162
function panels_choose_layout_submit($form, &$form_state) {
163
  $form_state['layout'] = $form_state['values']['layout'];
164
}
165

    
166
/**
167
 * Form definition for the display layout converter.
168
 *
169
 * This form is only triggered if the user attempts to change the layout
170
 * for a display that has already had content assigned to it. It allows
171
 * the user to select where the panes located in to-be-deleted panels should
172
 * be relocated to.
173
 *
174
 * @ingroup forms
175
 *
176
 * @param array $form
177
 *   A structured FAPI $form array.
178
 * @param &$form_state
179
 *   The Drupal $form_state
180
 */
181
function panels_change_layout($form, &$form_state) {
182
  // Provide a temporary display and renderer.
183
  $form_state['layout_display'] = $display = panels_new_display();
184
  if (isset($form_state['cache_key'])) {
185
    $display->cache_key = $form_state['cache_key'];
186
  }
187

    
188
  $new_layout = panels_get_layout($form_state['layout']);
189
  $new_layout_regions = panels_get_regions($new_layout, $display);
190

    
191
  $old_layout = panels_get_layout($form_state['display']->layout);
192
  $old_layout_regions = panels_get_regions($old_layout, $form_state['display']);
193

    
194
  $display->layout = $form_state['layout'];
195
  $renderer = panels_get_renderer_handler('editor', $display);
196

    
197
  $renderer->meta_location = 'inline';
198

    
199
  ctools_add_css('panels_admin', 'panels');
200
  ctools_add_css('panels_dnd', 'panels');
201
  ctools_add_css('dropdown');
202

    
203
  // For every region that had content in the old layout, create a custom pane
204
  // in the new layout that represents that region.
205
  $keys = array_keys($new_layout_regions);
206
  $default_region = reset($keys);
207
  foreach ($old_layout_regions as $region_id => $region_name) {
208
    if (!empty($form_state['display']->panels[$region_id])) {
209
      $pane = panels_new_pane('custom', 'custom', TRUE);
210
      $pane->pid = $region_id;
211
      $pane->configuration['title'] = t('Panes');
212
      $pane->configuration['admin_title'] = $region_name;
213
      // Get a list of pane titles used.
214
      $titles = array();
215
      foreach ($form_state['display']->panels[$region_id] as $pid) {
216
        $content_type = ctools_get_content_type($form_state['display']->content[$pid]->type);
217
        $titles[$pid] = ctools_content_admin_title($content_type, $form_state['display']->content[$pid]->subtype, $form_state['display']->content[$pid]->configuration, $form_state['display']->context);
218
      }
219
      $pane->configuration['body'] = '<ul><li>' . implode('</li><li>', $titles) . '</li></ul>';
220

    
221
      // If the region id matches, make it the same; otherwise, put it
222
      // in the default region.
223
      $pane->panel = empty($new_layout_regions[$region_id]) ? $default_region : $region_id;
224

    
225
      // Add the pane to the approprate spots.
226
      $display->content[$pane->pid] = $pane;
227
      $display->panels[$pane->panel][] = $pane->pid;
228
    }
229
  }
230

    
231
  $form['container'] = array(
232
    '#prefix' => '<div class="change-layout-display clearfix">',
233
    '#suffix' => '</div>',
234
  );
235

    
236
  $form['container']['old_layout'] = array(
237
    '#markup' => panels_print_layout_icon($form_state['display']->layout, $old_layout, check_plain($old_layout['title'])),
238
  );
239

    
240
  $form['container']['right_arrow'] = array(
241
    '#markup' => theme('image', array('path' => drupal_get_path('module', 'panels') . '/images/go-right.png')),
242
  );
243
  $form['container']['new_layout'] = array(
244
    '#markup' => panels_print_layout_icon($form_state['layout'], $new_layout, check_plain($new_layout['title'])),
245
  );
246

    
247
  $edit_form_state = array(
248
    'display' => $display,
249
    'renderer' => $renderer,
250
    'no buttons' => TRUE,
251
    'no preview' => TRUE,
252
    'no display settings' => TRUE,
253
    'display_title' => '',
254
  );
255

    
256
  ctools_include('display-edit', 'panels');
257
  $form = panels_edit_display_form($form, $edit_form_state);
258

    
259
  if (empty($form_state['no buttons'])) {
260
    $form['back'] = array(
261
      '#type' => 'submit',
262
      '#value' => t('Back'),
263
      '#submit' => array('panels_choose_layout_back'),
264
    );
265

    
266
    $form['submit'] = array(
267
      '#type' => 'submit',
268
      '#value' => $form_state['finish'],
269
      '#submit' => array('panels_change_layout_submit'),
270
      '#save-display' => TRUE,
271
    );
272
  }
273
  return $form;
274
}
275

    
276
/**
277
 * Handle submission of the change layout form.
278
 *
279
 * This submit handler will move panes around and save the display.
280
 */
281
function panels_change_layout_submit($form, &$form_state) {
282
  $display = $form_state['display'];
283
  $layout_display = $form_state['layout_display'];
284

    
285
  $switch = array();
286

    
287
  // Calculate the pids submitted by the display and make a list of
288
  // translation to the regions. Remember the 'pid' of the pane
289
  // is the region id in the old layout.
290
  if (!empty($form_state['values']['panel']['pane'])) {
291
    foreach ($form_state['values']['panel']['pane'] as $region_id => $panes) {
292
      if ($panes) {
293
        $pids = explode(',', $panes);
294
        // Need to filter the array, b/c passing it in a hidden field can generate trash.
295
        foreach (array_filter($pids) as $pid) {
296
          $switch[$pid] = $region_id;
297
        }
298
      }
299
    }
300
  }
301

    
302
  $content = array();
303
  foreach ($switch as $region_id => $new_region_id) {
304
    if (isset($display->panels[$region_id])) {
305
      if (!isset($content[$new_region_id])) {
306
        $content[$new_region_id] = array();
307
      }
308
      $content[$new_region_id] = array_merge($content[$new_region_id], $display->panels[$region_id]);
309
    }
310
  }
311

    
312
  // Go through each pane and make sure its region id is correct.
313
  foreach ($content as $region_id => $region) {
314
    foreach ($region as $pid) {
315
      $display->content[$pid]->panel = $region_id;
316
    }
317
  }
318

    
319
  $display->panels = $content;
320

    
321
  $display->layout = $form_state['layout'];
322

    
323
  panels_edit_display_settings_form_submit($form, $form_state);
324
}
325

    
326
/**
327
 * Handle submission of the change layout form.
328
 *
329
 * This submit handler sets a flag on the form state, which is then used
330
 * by the calling wrapper to restart the process.
331
 */
332
function panels_choose_layout_back($form, &$form_state) {
333
  $form_state['back'] = TRUE;
334
}