Projet

Général

Profil

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

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

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('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

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

    
157
  return $form;
158
}
159

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
286
  $switch = array();
287

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

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

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

    
320
  $display->panels = $content;
321

    
322
  $display->layout = $form_state['layout'];
323
}
324

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