Projet

Général

Profil

Paste
Télécharger (11,2 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Core Panels API include file containing various display-editing functions.
6
 * This includes all the basic editing forms (content, layout, layout settings)
7
 * as well as the ajax modal forms associated with them.
8
 */
9

    
10
/**
11
 * Handle calling and processing of the form for editing display content.
12
 *
13
 * Helper function for panels_edit().
14
 *
15
 * @see panels_edit() for details on the various behaviors of this function.
16
 */
17
function _panels_edit($display, $destination, $content_types, $title = FALSE) {
18
  $did = $display->did;
19
  if (!$did) {
20
    $display->did = $did = 'new';
21
  }
22

    
23
  // Load the display being edited from cache, if possible.
24
  if (!empty($_POST) && is_object($cache = panels_edit_cache_get($did))) {
25
    $display = $cache->display;
26
  }
27
  else {
28
    $cache = panels_edit_cache_get_default($display, $content_types, $title);
29
  }
30

    
31
  // Get a renderer.
32
  $renderer = panels_get_renderer_handler('editor', $display);
33
  $renderer->cache = $cache;
34

    
35
  $output = $renderer->edit();
36
  if (is_object($output) && $destination) {
37
    return panels_goto($destination);
38
  }
39
  return $output;
40
}
41

    
42
/**
43
 * Form definition for the panels display editor.
44
 *
45
 * No validation function is necessary, as all 'validation' is handled
46
 * either in the lead-up to form rendering (through the selection of
47
 * specified content types) or by the validation functions specific to
48
 * the ajax modals & content types.
49
 *
50
 * @ingroup forms
51
 *
52
 * @see panels_edit_display_submit()
53
 */
54
function panels_edit_display_form($form, &$form_state) {
55
  $display = &$form_state['display'];
56
  $renderer = &$form_state['renderer'];
57

    
58
  // Make sure there is a valid cache key.
59
  $cache_key = isset($display->cache_key) ? $display->cache_key : $display->did;
60
  $display->cache_key = $cache_key;
61

    
62
  // Annoyingly, theme doesn't have access to form_state so we have to do this.
63
  $form['#display'] = $display;
64

    
65
  // The flexible layout maker wants to be able to edit a display without
66
  // actually editing a display, so we provide this 'setting' to allow
67
  // that to go away.
68
  if (empty($form_state['no display settings'])) {
69
    $links = $renderer->get_display_links();
70
  }
71
  else {
72
    $renderer->no_edit_links = TRUE;
73
    $links = '';
74
  }
75
  $form['hide']['display-settings'] = array(
76
    '#markup' => $links,
77
  );
78

    
79
  $form += panels_edit_display_settings_form($form, $form_state);
80

    
81
  $form['panel'] = array('#tree' => TRUE);
82
  $form['panel']['pane'] = array('#tree' => TRUE);
83

    
84
  $form['display'] = array(
85
    '#markup' => $renderer->render(),
86
  );
87

    
88
  foreach ($renderer->plugins['layout']['regions'] as $region_id => $title) {
89
    // Make sure we at least have an empty array for all possible locations.
90
    if (!isset($display->panels[$region_id])) {
91
      $display->panels[$region_id] = array();
92
    }
93

    
94
    $form['panel']['pane'][$region_id] = array(
95
      // Use 'hidden' instead of 'value' so the js can access it.
96
      '#type' => 'hidden',
97
      '#default_value' => implode(',', (array) $display->panels[$region_id]),
98
    );
99
  }
100

    
101
  if (empty($form_state['no buttons'])) {
102
    $form['buttons']['submit'] = array(
103
      '#type' => 'submit',
104
      '#value' => t('Save'),
105
      '#id' => 'panels-dnd-save',
106
      '#submit' => array('panels_edit_display_form_submit'),
107
      '#save-display' => TRUE,
108
    );
109
    $form['buttons']['cancel'] = array(
110
      '#type' => 'submit',
111
      '#value' => t('Cancel'),
112
    );
113
  }
114

    
115
  // Build up the preview portion of the form, if necessary.
116
  if (empty($form_state['no preview'])) {
117
    $form['preview'] = array(
118
      '#tree' => TRUE,
119
      '#prefix' => '<h2>' . t('Live preview') . '</h2>' . '<div id="panels-live-preview">',
120
      '#suffix' => '</div>',
121
    );
122

    
123
    ctools_context_replace_form($form['preview'], $display->context);
124
    $form['preview']['button'] = array(
125
      '#type' => 'submit',
126
      '#value' => t('Preview'),
127
      '#attributes' => array('class' => array('use-ajax-submit')),
128
      '#id' => 'panels-live-preview-button',
129
      '#submit' => array('panels_edit_display_form_submit', 'panels_edit_display_form_preview'),
130
    );
131
  }
132

    
133
  return $form;
134
}
135

    
136
/**
137
 * Handle form validation of the display content editor.
138
 */
139
function panels_edit_display_form_validate($form, &$form_state) {
140
  panels_edit_display_settings_form_validate($form, $form_state);
141
}
142

    
143
/**
144
 * Handle form submission of the display content editor.
145
 *
146
 * This reads the location of the various panes from the form, which will
147
 * have been modified from the ajax, rearranges them and then saves
148
 * the display.
149
 */
150
function panels_edit_display_form_submit($form, &$form_state) {
151
  $display = &$form_state['display'];
152

    
153
  $old_content = $display->content;
154
  $display->content = array();
155

    
156
  if (!empty($form_state['values']['panel']['pane'])) {
157
    foreach ($form_state['values']['panel']['pane'] as $region_id => $panes) {
158
      $display->panels[$region_id] = array();
159
      if ($panes) {
160
        $pids = explode(',', $panes);
161
        // Need to filter the array, b/c passing it in a hidden field can generate trash.
162
        foreach (array_filter($pids) as $pid) {
163
          if ($old_content[$pid]) {
164
            $display->panels[$region_id][] = $pid;
165
            $old_content[$pid]->panel = $region_id;
166
            $display->content[$pid] = $old_content[$pid];
167

    
168
            // If the panel has region locking, make sure that the region
169
            // the panel is in is applicable. This can happen if the panel
170
            // was moved and then the lock changed and the server didn't
171
            // know.
172
            if (!empty($old_content[$pid]->locks) && $old_content[$pid]->locks['type'] == 'regions') {
173
              $old_content[$pid]->locks['regions'][$region_id] = $region_id;
174
            }
175
          }
176
        }
177
      }
178
    }
179
  }
180

    
181
  panels_edit_display_settings_form_submit($form, $form_state);
182
}
183

    
184
/**
185
 * Submission of the preview button. Render the preview and put it into
186
 * the preview widget area.
187
 */
188
function panels_edit_display_form_preview(&$form, &$form_state) {
189
  $display = &$form_state['display'];
190
  ctools_include('ajax');
191

    
192
  $display->context = ctools_context_replace_placeholders($display->context, $form_state['values']['preview']);
193
  $display->skip_cache = TRUE;
194
  $output = panels_render_display($display);
195

    
196
  // Add any extra CSS that some layouts may have added specifically for this.
197
  if (!empty($display->add_css)) {
198
    $output = "<style type=\"text/css\">\n$display->add_css</style>\n" . $output;
199
  }
200

    
201
  $commands = array();
202
  $commands[] = array(
203
    'command' => 'panel_preview',
204
    'output' => $output,
205
  );
206

    
207
  print ajax_render($commands);
208
  ajax_footer();
209
  exit;
210
}
211

    
212

    
213
/**
214
 * Form for display settings.
215
 */
216
function panels_edit_display_settings_form($form, &$form_state) {
217
  $display = &$form_state['display'];
218

    
219
  $layout = panels_get_layout($display->layout);
220
  $form_state['layout'] = $layout;
221

    
222
  ctools_include('dependent');
223

    
224
  if ($form_state['display_title']) {
225
    $form['display_title'] = array(
226
      '#tree' => TRUE,
227
    );
228

    
229
    $form['display_title']['hide_title'] = array(
230
      '#type' => 'select',
231
      '#title' => t('Title type'),
232
      '#default_value' => (int) $display->hide_title,
233
      '#options' => array(
234
        PANELS_TITLE_NONE => t('No title'),
235
        PANELS_TITLE_FIXED => t('Manually set'),
236
        PANELS_TITLE_PANE => t('From pane'),
237
      ),
238
    );
239

    
240
    $form['display_title']['title'] = array(
241
      '#type' => 'textfield',
242
      '#default_value' => $display->title,
243
      '#title' => t('Title'),
244
      '#description' => t('The title of this panel. If left blank, a default title may be used. If you want the title actually to be blank, change the "Title type" dropdown from "Manually Set" to "No Title".'),
245
      '#process' => array('ctools_dependent_process'),
246
      '#dependency' => array('edit-display-title-hide-title' => array(PANELS_TITLE_FIXED)),
247
      '#maxlength' => 255,
248
    );
249

    
250
    if (!empty($display->context)) {
251
      $form['display_title']['title']['#description'] .= ' ' . t('You may use substitutions in this title.');
252

    
253
      // We have to create a manual fieldset because fieldsets do not support IDs.
254
      // Use 'hidden' instead of 'markup' so that the process will run.
255
      // Add js for collapsible fieldsets manually
256
      //      drupal_add_js('misc/form.js');
257
      //      drupal_add_js('misc/collapse.js');
258
      //      $form['display_title']['contexts_prefix'] = array(
259
      //        '#type' => 'hidden',
260
      //        '#id' => 'edit-display-substitutions',
261
      //        '#prefix' => '<div><fieldset id="edit-display-substitutions" class="collapsed collapsible"><legend>' . t('Substitutions') . '</legend><div class="fieldset-wrapper">',
262
      //        '#process' => array('ctools_dependent_process'),
263
      //        '#dependency' => array('edit-display-title-hide-title' => array(PANELS_TITLE_FIXED)),
264
      //      );
265
      $rows = array();
266
      foreach ($display->context as $context) {
267
        foreach (ctools_context_get_converters('%' . check_plain($context->keyword) . ':', $context) as $keyword => $title) {
268
          $rows[] = array(
269
            check_plain($keyword),
270
            t('@identifier: @title', array('@title' => $title, '@identifier' => $context->identifier)),
271
          );
272
        }
273
      }
274

    
275
      $header = array(t('Keyword'), t('Value'));
276
      $form['display_title']['contexts'] = array(
277
        '#type' => 'fieldset',
278
        '#title' => t('Substitutions'),
279
        '#collapsible' => TRUE,
280
        '#collapsed' => TRUE,
281
        '#value' => theme('table', array('header' => $header, 'rows' => $rows)),
282
      // '#process' => array('form_process_fieldset', 'ctools_dependent_process'),      //        '#id' => 'edit-display-title-context',
283
      //        '#dependency' => array('edit-display-title-hide-title' => array(PANELS_TITLE_FIXED)),
284
      );
285
      // $form['display_title']['contexts_suffix'] = array(      //        '#value' => '</div></fieldset></div>',
286
      //      );.
287
    }
288
  }
289

    
290
  // TODO doc the ability to do this as part of the API.
291
  if (!empty($layout['settings form']) && function_exists($layout['settings form'])) {
292
    $form['layout_settings'] = $layout['settings form']($display, $layout, $display->layout_settings);
293
  }
294
  $form['layout_settings']['#tree'] = TRUE;
295

    
296
  return $form;
297
}
298

    
299
/**
300
 * Validate the layout settings form.
301
 */
302
function panels_edit_display_settings_form_validate($form, &$form_state) {
303
  if ($function = panels_plugin_get_function('layouts', $form_state['layout'], 'settings validate')) {
304
    $function($form_state['values']['layout_settings'], $form['layout_settings'], $form_state['display'], $form_state['layout'], $form_state['display']->layout_settings);
305
  }
306
}
307

    
308
/**
309
 * Store changes from the layout settings form.
310
 */
311
function panels_edit_display_settings_form_submit($form, &$form_state) {
312
  $display = &$form_state['display'];
313
  if ($function = panels_plugin_get_function('layouts', $form_state['layout'], 'settings submit')) {
314
    $function($form_state['values']['layout_settings'], $display, $form_state['layout'], $display->layout_settings);
315
  }
316

    
317
  // Since not all layouts have layout settings, check here in case of notices.
318
  if (isset($form_state['values']['layout_settings'])) {
319
    $display->layout_settings = $form_state['values']['layout_settings'];
320
  }
321

    
322
  if (isset($form_state['values']['display_title']['title'])) {
323
    $display->title = $form_state['values']['display_title']['title'];
324
    $display->hide_title = $form_state['values']['display_title']['hide_title'];
325
  }
326
}