Projet

Général

Profil

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

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

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
 * @see panels_edit_display_submit()
52
 */
53
function panels_edit_display_form($form, &$form_state) {
54
  $display = &$form_state['display'];
55
  $renderer = &$form_state['renderer'];
56

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

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

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

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

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

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

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

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

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

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

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

    
132
  return $form;
133
}
134

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

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

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

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

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

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

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

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

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

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

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

    
211

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

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

    
221
  ctools_include('dependent');
222

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

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

    
239
    $form['display_title']['title'] = array(
240
      '#type' => 'textfield',
241
      '#default_value' => $display->title,
242
      '#title' => t('Title'),
243
      '#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".'),
244
      '#process' => array('ctools_dependent_process'),
245
      '#dependency' => array('edit-display-title-hide-title' => array(PANELS_TITLE_FIXED)),
246
      '#maxlength' => 255,
247
    );
248

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

    
252
      // We have to create a manual fieldset because fieldsets do not support IDs.
253
      // Use 'hidden' instead of 'markup' so that the process will run.
254
      // Add js for collapsible fieldsets manually
255
//      drupal_add_js('misc/form.js');
256
//      drupal_add_js('misc/collapse.js');
257
//      $form['display_title']['contexts_prefix'] = array(
258
//        '#type' => 'hidden',
259
//        '#id' => 'edit-display-substitutions',
260
//        '#prefix' => '<div><fieldset id="edit-display-substitutions" class="collapsed collapsible"><legend>' . t('Substitutions') . '</legend><div class="fieldset-wrapper">',
261
//        '#process' => array('ctools_dependent_process'),
262
//        '#dependency' => array('edit-display-title-hide-title' => array(PANELS_TITLE_FIXED)),
263
//      );
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'),
283
//        '#id' => 'edit-display-title-context',
284
//        '#dependency' => array('edit-display-title-hide-title' => array(PANELS_TITLE_FIXED)),
285
      );
286
//      $form['display_title']['contexts_suffix'] = array(
287
//        '#value' => '</div></fieldset></div>',
288
//      );
289
    }
290
  }
291

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

    
298
  return $form;
299
}
300

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

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

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

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