[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/panels/includes/ -> display-edit.inc (source)

   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  /**
  12   * Handle calling and processing of the form for editing display content.
  13   *
  14   * Helper function for panels_edit().
  15   *
  16   * @see panels_edit() for details on the various behaviors of this function.
  17   */
  18  function _panels_edit($display, $destination, $content_types, $title = FALSE) {
  19    $did = $display->did;
  20    if (!$did) {
  21      $display->did = $did = 'new';
  22    }
  23  
  24    // Load the display being edited from cache, if possible.
  25    if (!empty($_POST) && is_object($cache = panels_edit_cache_get($did))) {
  26      $display = $cache->display;
  27    }
  28    else {
  29      $cache = panels_edit_cache_get_default($display, $content_types, $title);
  30    }
  31  
  32    // Get a renderer.
  33    $renderer = panels_get_renderer_handler('editor', $display);
  34    $renderer->cache = $cache;
  35  
  36    $output = $renderer->edit();
  37    if (is_object($output) && $destination) {
  38      return panels_goto($destination);
  39    }
  40    return $output;
  41  }
  42  
  43  /**
  44   * Form definition for the panels display editor
  45   *
  46   * No validation function is necessary, as all 'validation' is handled
  47   * either in the lead-up to form rendering (through the selection of
  48   * specified content types) or by the validation functions specific to
  49   * the ajax modals & content types.
  50   *
  51   * @ingroup forms
  52   * @see panels_edit_display_submit()
  53   */
  54  function panels_edit_display_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      $links = '';
  73    }
  74    $form['hide']['display-settings'] = array(
  75      '#value' => $links,
  76    );
  77  
  78    $form += panels_edit_display_settings_form($form_state);
  79  
  80    $form['panel'] = array('#tree' => TRUE);
  81    $form['panel']['pane'] = array('#tree' => TRUE);
  82  
  83    $form['display'] = array(
  84      '#value' => $renderer->render(),
  85    );
  86  
  87    foreach ($renderer->plugins['layout']['panels'] 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' => 'ctools-use-ajax'),
 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 submission of the display content editor.
 137   *
 138   * This reads the location of the various panes from the form, which will
 139   * have been modified from the ajax, rearranges them and then saves
 140   * the display.
 141   */
 142  function panels_edit_display_form_submit($form, &$form_state) {
 143    $display = &$form_state['display'];
 144  
 145    $old_content = $display->content;
 146    $display->content = array();
 147  
 148    if (!empty($form_state['values']['panel']['pane'])) {
 149      foreach ($form_state['values']['panel']['pane'] as $panel_id => $panes) {
 150        $display->panels[$panel_id] = array();
 151        if ($panes) {
 152          $pids = explode(',', $panes);
 153          // need to filter the array, b/c passing it in a hidden field can generate trash
 154          foreach (array_filter($pids) as $pid) {
 155            if ($old_content[$pid]) {
 156              $display->panels[$panel_id][] = $pid;
 157              $old_content[$pid]->panel = $panel_id;
 158              $display->content[$pid] = $old_content[$pid];
 159            }
 160          }
 161        }
 162      }
 163    }
 164  
 165    panels_edit_display_settings_form_submit($form, $form_state);
 166  }
 167  
 168  /**
 169   * Submission of the preview button. Render the preview and put it into
 170   * the preview widget area.
 171   */
 172  function panels_edit_display_form_preview(&$form, &$form_state) {
 173    $display = &$form_state['display'];
 174    ctools_include('ajax');
 175  
 176    $display->context = ctools_context_replace_placeholders($display->context, $form_state['values']['preview']);
 177    $display->skip_cache = TRUE;
 178    $output = panels_render_display($display);
 179  
 180    // Add any extra CSS that some layouts may have added specifically for this.
 181    if (!empty($display->add_css)) {
 182      $output = "<style type=\"text/css\">\n$display->add_css</style>\n" . $output;
 183    }
 184  
 185    $commands = array();
 186    $commands[] = array(
 187      'command' => 'panel_preview',
 188      'output' => $output,
 189    );
 190  
 191    ctools_ajax_render($commands);
 192  }
 193  
 194  
 195  /**
 196   * Form for display settings.
 197   */
 198  function panels_edit_display_settings_form(&$form_state) {
 199    $form = array();
 200    $display = &$form_state['display'];
 201  
 202    $layout = panels_get_layout($display->layout);
 203    $form_state['layout'] = $layout;
 204  
 205    ctools_include('dependent');
 206  
 207    if ($form_state['display_title']) {
 208      $form['display_title'] = array (
 209        '#tree' => TRUE,
 210      );
 211  
 212      $form['display_title']['hide_title'] = array(
 213        '#type' => 'select',
 214        '#title' => t('Title type'),
 215        '#default_value' => (int) $display->hide_title,
 216        '#options' => array(
 217          PANELS_TITLE_NONE => t('No title'),
 218          PANELS_TITLE_FIXED => t('Manually set'),
 219          PANELS_TITLE_PANE => t('From pane'),
 220        ),
 221      );
 222  
 223      $form['display_title']['title'] = array(
 224        '#type' => 'textfield',
 225        '#default_value' => $display->title,
 226        '#title' => t('Title'),
 227        '#description' => t('The title of this panel. If left blank, a default title may be used. Set to No Title if you want the title to actually be blank.'),
 228        '#process' => array('ctools_dependent_process'),
 229        '#dependency' => array('edit-display-title-hide-title' => array(PANELS_TITLE_FIXED)),
 230      );
 231  
 232      if (!empty($display->context)) {
 233        $form['display_title']['title']['#description'] .= ' ' . t('You may use substitutions in this title.');
 234  
 235        // We have to create a manual fieldset because fieldsets do not support IDs.
 236        // Use 'hidden' instead of 'markup' so that the process will run.
 237        // Add js for collapsible fieldsets manually
 238        drupal_add_js('misc/collapse.js');
 239        $form['display_title']['contexts_prefix'] = array(
 240          '#type' => 'hidden',
 241          '#id' => 'edit-display-substitutions',
 242          '#prefix' => '<div><fieldset id="edit-display-substitutions" class="collapsed collapsible"><legend>' . t('Substitutions') . '</legend>',
 243          '#process' => array('ctools_dependent_process'),
 244          '#dependency' => array('edit-display-title-hide-title' => array(PANELS_TITLE_FIXED)),
 245        );
 246  
 247        $rows = array();
 248        foreach ($display->context as $context) {
 249          foreach (ctools_context_get_converters('%' . check_plain($context->keyword) . ':', $context) as $keyword => $title) {
 250            $rows[] = array(
 251              check_plain($keyword),
 252              t('@identifier: @title', array('@title' => $title, '@identifier' => $context->identifier)),
 253            );
 254          }
 255        }
 256  
 257        $header = array(t('Keyword'), t('Value'));
 258        $form['display_title']['contexts'] = array(
 259          '#value' => theme('table', $header, $rows),
 260        );
 261        $form['display_title']['contexts_suffix'] = array(
 262          '#value' => '</fieldset></div>',
 263        );
 264      }
 265    }
 266  
 267    // TODO doc the ability to do this as part of the API
 268    if (!empty($layout['settings form']) && function_exists($layout['settings form'])) {
 269      $form['layout_settings'] = $layout['settings form']($display, $layout, $display->layout_settings);
 270    }
 271    $form['layout_settings']['#tree'] = TRUE;
 272  
 273    return $form;
 274  }
 275  
 276  /**
 277   * Validate the layout settings form.
 278   */
 279  function panels_edit_display_settings_form_validate($form, &$form_state) {
 280    if ($function = panels_plugin_get_function('layout', $form_state['layout'], 'settings validate')) {
 281      $function($form_state['values']['layout_settings'], $form['layout_settings'], $form_state['display'], $form_state['layout'], $form_state['display']->layout_settings);
 282    }
 283  }
 284  
 285  /**
 286   * Store changes from the layout settings form.
 287   */
 288  function panels_edit_display_settings_form_submit($form, &$form_state) {
 289    $display = &$form_state['display'];
 290    if ($function = panels_plugin_get_function('layout', $form_state['layout'], 'settings submit')) {
 291      $function($form_state['values']['layout_settings'], $display, $form_state['layout'], $display->layout_settings);
 292    }
 293  
 294    // Since not all layouts have layout settings, check here in case of notices.
 295    if (isset($form_state['values']['layout_settings'])) {
 296      $display->layout_settings = $form_state['values']['layout_settings'];
 297    }
 298  
 299    if (isset($form_state['values']['display_title']['title'])) {
 300      $display->title = $form_state['values']['display_title']['title'];
 301      $display->hide_title = $form_state['values']['display_title']['hide_title'];
 302    }
 303  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7