display-edit.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /**
  3. * @file
  4. * Core Panels API include file containing various display-editing functions.
  5. * This includes all the basic editing forms (content, layout, layout settings)
  6. * as well as the ajax modal forms associated with them.
  7. */
  8. /**
  9. * Handle calling and processing of the form for editing display content.
  10. *
  11. * Helper function for panels_edit().
  12. *
  13. * @see panels_edit() for details on the various behaviors of this function.
  14. */
  15. function _panels_edit($display, $destination, $content_types, $title = FALSE) {
  16. $did = $display->did;
  17. if (!$did) {
  18. $display->did = $did = 'new';
  19. }
  20. // Load the display being edited from cache, if possible.
  21. if (!empty($_POST) && is_object($cache = panels_edit_cache_get($did))) {
  22. $display = $cache->display;
  23. }
  24. else {
  25. $cache = panels_edit_cache_get_default($display, $content_types, $title);
  26. }
  27. // Get a renderer.
  28. $renderer = panels_get_renderer_handler('editor', $display);
  29. $renderer->cache = $cache;
  30. $output = $renderer->edit();
  31. if (is_object($output) && $destination) {
  32. return panels_goto($destination);
  33. }
  34. return $output;
  35. }
  36. /**
  37. * Form definition for the panels display editor.
  38. *
  39. * No validation function is necessary, as all 'validation' is handled
  40. * either in the lead-up to form rendering (through the selection of
  41. * specified content types) or by the validation functions specific to
  42. * the ajax modals & content types.
  43. *
  44. * @ingroup forms
  45. *
  46. * @see panels_edit_display_submit()
  47. */
  48. function panels_edit_display_form($form, &$form_state) {
  49. $display = &$form_state['display'];
  50. $renderer = &$form_state['renderer'];
  51. // Make sure there is a valid cache key.
  52. $cache_key = isset($display->cache_key) ? $display->cache_key : $display->did;
  53. $display->cache_key = $cache_key;
  54. // Annoyingly, theme doesn't have access to form_state so we have to do this.
  55. $form['#display'] = $display;
  56. // The flexible layout maker wants to be able to edit a display without
  57. // actually editing a display, so we provide this 'setting' to allow
  58. // that to go away.
  59. if (empty($form_state['no display settings'])) {
  60. $links = $renderer->get_display_links();
  61. }
  62. else {
  63. $renderer->no_edit_links = TRUE;
  64. $links = '';
  65. }
  66. $form['hide']['display-settings'] = array(
  67. '#markup' => $links,
  68. );
  69. $form += panels_edit_display_settings_form($form, $form_state);
  70. $form['panel'] = array('#tree' => TRUE);
  71. $form['panel']['pane'] = array('#tree' => TRUE);
  72. $form['display'] = array(
  73. '#markup' => $renderer->render(),
  74. );
  75. foreach ($renderer->plugins['layout']['regions'] as $region_id => $title) {
  76. // Make sure we at least have an empty array for all possible locations.
  77. if (!isset($display->panels[$region_id])) {
  78. $display->panels[$region_id] = array();
  79. }
  80. $form['panel']['pane'][$region_id] = array(
  81. // Use 'hidden' instead of 'value' so the js can access it.
  82. '#type' => 'hidden',
  83. '#default_value' => implode(',', (array) $display->panels[$region_id]),
  84. );
  85. }
  86. if (empty($form_state['no buttons'])) {
  87. $form['buttons']['submit'] = array(
  88. '#type' => 'submit',
  89. '#value' => t('Save'),
  90. '#id' => 'panels-dnd-save',
  91. '#submit' => array('panels_edit_display_form_submit'),
  92. '#save-display' => TRUE,
  93. );
  94. $form['buttons']['cancel'] = array(
  95. '#type' => 'submit',
  96. '#value' => t('Cancel'),
  97. );
  98. }
  99. // Build up the preview portion of the form, if necessary.
  100. if (empty($form_state['no preview'])) {
  101. $form['preview'] = array(
  102. '#tree' => TRUE,
  103. '#prefix' => '<h2>' . t('Live preview') . '</h2>' . '<div id="panels-live-preview">',
  104. '#suffix' => '</div>',
  105. );
  106. ctools_context_replace_form($form['preview'], $display->context);
  107. $form['preview']['button'] = array(
  108. '#type' => 'submit',
  109. '#value' => t('Preview'),
  110. '#attributes' => array('class' => array('use-ajax-submit')),
  111. '#id' => 'panels-live-preview-button',
  112. '#submit' => array('panels_edit_display_form_submit', 'panels_edit_display_form_preview'),
  113. );
  114. }
  115. return $form;
  116. }
  117. /**
  118. * Handle form validation of the display content editor.
  119. */
  120. function panels_edit_display_form_validate($form, &$form_state) {
  121. panels_edit_display_settings_form_validate($form, $form_state);
  122. }
  123. /**
  124. * Handle form submission of the display content editor.
  125. *
  126. * This reads the location of the various panes from the form, which will
  127. * have been modified from the ajax, rearranges them and then saves
  128. * the display.
  129. */
  130. function panels_edit_display_form_submit($form, &$form_state) {
  131. $display = &$form_state['display'];
  132. $old_content = $display->content;
  133. $display->content = array();
  134. if (!empty($form_state['values']['panel']['pane'])) {
  135. foreach ($form_state['values']['panel']['pane'] as $region_id => $panes) {
  136. $display->panels[$region_id] = array();
  137. if ($panes) {
  138. $pids = explode(',', $panes);
  139. // Need to filter the array, b/c passing it in a hidden field can generate trash.
  140. foreach (array_filter($pids) as $pid) {
  141. if ($old_content[$pid]) {
  142. $display->panels[$region_id][] = $pid;
  143. $old_content[$pid]->panel = $region_id;
  144. $display->content[$pid] = $old_content[$pid];
  145. // If the panel has region locking, make sure that the region
  146. // the panel is in is applicable. This can happen if the panel
  147. // was moved and then the lock changed and the server didn't
  148. // know.
  149. if (!empty($old_content[$pid]->locks) && $old_content[$pid]->locks['type'] == 'regions') {
  150. $old_content[$pid]->locks['regions'][$region_id] = $region_id;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. panels_edit_display_settings_form_submit($form, $form_state);
  158. }
  159. /**
  160. * Submission of the preview button. Render the preview and put it into
  161. * the preview widget area.
  162. */
  163. function panels_edit_display_form_preview(&$form, &$form_state) {
  164. $display = &$form_state['display'];
  165. ctools_include('ajax');
  166. $display->context = ctools_context_replace_placeholders($display->context, $form_state['values']['preview']);
  167. $display->skip_cache = TRUE;
  168. $output = panels_render_display($display);
  169. // Add any extra CSS that some layouts may have added specifically for this.
  170. if (!empty($display->add_css)) {
  171. $output = "<style type=\"text/css\">\n$display->add_css</style>\n" . $output;
  172. }
  173. $commands = array();
  174. $commands[] = array(
  175. 'command' => 'panel_preview',
  176. 'output' => $output,
  177. );
  178. print ajax_render($commands);
  179. ajax_footer();
  180. exit;
  181. }
  182. /**
  183. * Form for display settings.
  184. */
  185. function panels_edit_display_settings_form($form, &$form_state) {
  186. $display = &$form_state['display'];
  187. $layout = panels_get_layout($display->layout);
  188. $form_state['layout'] = $layout;
  189. ctools_include('dependent');
  190. if ($form_state['display_title']) {
  191. $form['display_title'] = array(
  192. '#tree' => TRUE,
  193. );
  194. $form['display_title']['hide_title'] = array(
  195. '#type' => 'select',
  196. '#title' => t('Title type'),
  197. '#default_value' => (int) $display->hide_title,
  198. '#options' => array(
  199. PANELS_TITLE_NONE => t('No title'),
  200. PANELS_TITLE_FIXED => t('Manually set'),
  201. PANELS_TITLE_PANE => t('From pane'),
  202. ),
  203. );
  204. $form['display_title']['title'] = array(
  205. '#type' => 'textfield',
  206. '#default_value' => $display->title,
  207. '#title' => t('Title'),
  208. '#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".'),
  209. '#process' => array('ctools_dependent_process'),
  210. '#dependency' => array('edit-display-title-hide-title' => array(PANELS_TITLE_FIXED)),
  211. '#maxlength' => 255,
  212. );
  213. if (!empty($display->context)) {
  214. $form['display_title']['title']['#description'] .= ' ' . t('You may use substitutions in this title.');
  215. // We have to create a manual fieldset because fieldsets do not support IDs.
  216. // Use 'hidden' instead of 'markup' so that the process will run.
  217. // Add js for collapsible fieldsets manually
  218. // drupal_add_js('misc/form.js');
  219. // drupal_add_js('misc/collapse.js');
  220. // $form['display_title']['contexts_prefix'] = array(
  221. // '#type' => 'hidden',
  222. // '#id' => 'edit-display-substitutions',
  223. // '#prefix' => '<div><fieldset id="edit-display-substitutions" class="collapsed collapsible"><legend>' . t('Substitutions') . '</legend><div class="fieldset-wrapper">',
  224. // '#process' => array('ctools_dependent_process'),
  225. // '#dependency' => array('edit-display-title-hide-title' => array(PANELS_TITLE_FIXED)),
  226. // );
  227. $rows = array();
  228. foreach ($display->context as $context) {
  229. foreach (ctools_context_get_converters('%' . check_plain($context->keyword) . ':', $context) as $keyword => $title) {
  230. $rows[] = array(
  231. check_plain($keyword),
  232. t('@identifier: @title', array('@title' => $title, '@identifier' => $context->identifier)),
  233. );
  234. }
  235. }
  236. $header = array(t('Keyword'), t('Value'));
  237. $form['display_title']['contexts'] = array(
  238. '#type' => 'fieldset',
  239. '#title' => t('Substitutions'),
  240. '#collapsible' => TRUE,
  241. '#collapsed' => TRUE,
  242. '#value' => theme('table', array('header' => $header, 'rows' => $rows)),
  243. // '#process' => array('form_process_fieldset', 'ctools_dependent_process'), // '#id' => 'edit-display-title-context',
  244. // '#dependency' => array('edit-display-title-hide-title' => array(PANELS_TITLE_FIXED)),
  245. );
  246. // $form['display_title']['contexts_suffix'] = array( // '#value' => '</div></fieldset></div>',
  247. // );.
  248. }
  249. }
  250. // TODO doc the ability to do this as part of the API.
  251. if (!empty($layout['settings form']) && function_exists($layout['settings form'])) {
  252. $form['layout_settings'] = $layout['settings form']($display, $layout, $display->layout_settings);
  253. }
  254. $form['layout_settings']['#tree'] = TRUE;
  255. return $form;
  256. }
  257. /**
  258. * Validate the layout settings form.
  259. */
  260. function panels_edit_display_settings_form_validate($form, &$form_state) {
  261. if ($function = panels_plugin_get_function('layouts', $form_state['layout'], 'settings validate')) {
  262. $function($form_state['values']['layout_settings'], $form['layout_settings'], $form_state['display'], $form_state['layout'], $form_state['display']->layout_settings);
  263. }
  264. }
  265. /**
  266. * Store changes from the layout settings form.
  267. */
  268. function panels_edit_display_settings_form_submit($form, &$form_state) {
  269. $display = &$form_state['display'];
  270. if ($function = panels_plugin_get_function('layouts', $form_state['layout'], 'settings submit')) {
  271. $function($form_state['values']['layout_settings'], $display, $form_state['layout'], $display->layout_settings);
  272. }
  273. // Since not all layouts have layout settings, check here in case of notices.
  274. if (isset($form_state['values']['layout_settings'])) {
  275. $display->layout_settings = $form_state['values']['layout_settings'];
  276. }
  277. if (isset($form_state['values']['display_title']['title'])) {
  278. $display->title = $form_state['values']['display_title']['title'];
  279. $display->hide_title = $form_state['values']['display_title']['hide_title'];
  280. }
  281. }