display-layout.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * @file
  4. * Handle the forms for changing a display's layout.
  5. */
  6. /**
  7. * Handle calling and processing of the form for editing display layouts.
  8. *
  9. * Helper function for panels_edit_layout().
  10. *
  11. * @see panels_edit_layout() for details on the various behaviors of this function.
  12. */
  13. function _panels_edit_layout($display, $finish, $destination, $allowed_layouts) {
  14. ctools_include('common', 'panels');
  15. $form_state = array(
  16. 'display' => &$display,
  17. 'finish' => $finish,
  18. 'destination' => $destination,
  19. 'allowed_layouts' => $allowed_layouts,
  20. 're_render' => FALSE,
  21. 'no_redirect' => TRUE,
  22. );
  23. $change_form_state = $form_state;
  24. $change_form = FALSE;
  25. // Examine $_POST to see which form they're currently using.
  26. if (empty($_POST) || empty($_POST['form_id']) || $_POST['form_id'] != 'panels_change_layout') {
  27. $output = drupal_build_form('panels_choose_layout', $form_state);
  28. if (!empty($form_state['executed'])) {
  29. // Upon submission go to next form.
  30. $change_form_state['layout'] = $_SESSION['layout'][$display->did] = $form_state['layout'];
  31. $change_form = TRUE;
  32. }
  33. }
  34. else {
  35. $change_form_state['layout'] = $_SESSION['layout'][$display->did];
  36. $change_form = TRUE;
  37. }
  38. if ($change_form) {
  39. $output = drupal_build_form('panels_change_layout', $change_form_state);
  40. if (!empty($change_form_state['executed'])) {
  41. if (isset($change_form_state['back'])) {
  42. unset($_POST);
  43. return _panels_edit_layout($display, $finish, $destination, $allowed_layouts);
  44. }
  45. if (!empty($change_form_state['clicked_button']['#save-display'])) {
  46. drupal_set_message(t('Panel layout has been updated.'));
  47. panels_save_display($display);
  48. }
  49. if ($destination) {
  50. return panels_goto($destination);
  51. }
  52. return $change_form_state['display'];
  53. }
  54. }
  55. return $output;
  56. }
  57. /**
  58. * Form definition for the display layout editor.
  59. *
  60. * @ingroup forms
  61. */
  62. function panels_choose_layout($form, &$form_state) {
  63. $display = &$form_state['display'];
  64. ctools_include('common', 'panels');
  65. ctools_include('cleanstring');
  66. $layouts = panels_common_get_allowed_layouts($form_state['allowed_layouts']);
  67. $categories = array();
  68. $current = '';
  69. foreach ($layouts as $id => $layout) {
  70. $category = ctools_cleanstring($layout['category']);
  71. // Default category to first in case layout doesn't exist or there isn't one.
  72. if (empty($current)) {
  73. $current = $category;
  74. }
  75. $categories[$category] = $layout['category'];
  76. $options[$category][$id] = panels_print_layout_icon($id, $layout, check_plain($layout['title']));
  77. // Set current category to what is chosen.
  78. if ($id == $display->layout) {
  79. $current = $category;
  80. }
  81. }
  82. ctools_add_js('panels-base', 'panels');
  83. ctools_add_js('layout', 'panels');
  84. $form['categories'] = array(
  85. '#title' => t('Category'),
  86. '#type' => 'select',
  87. '#options' => $categories,
  88. '#default_value' => $current,
  89. );
  90. $form['layout'] = array(
  91. '#prefix' => '<div class="panels-choose-layout panels-layouts-checkboxes clearfix">',
  92. '#suffix' => '</div>',
  93. );
  94. // We set up the dependencies manually because these aren't really form
  95. // items. It's possible there's a simpler way to do this, but I could not
  96. // think of one at the time.
  97. $dependencies = array();
  98. foreach ($options as $category => $radios) {
  99. $dependencies['panels-layout-category-' . $category] = array(
  100. 'values' => array('edit-categories' => array($category)),
  101. 'num' => 1,
  102. 'type' => 'hide',
  103. );
  104. $form['layout'][$category] = array(
  105. '#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>',
  106. '#suffix' => '</div></div>',
  107. );
  108. foreach ($radios as $key => $choice) {
  109. // Set the first available layout as default value.
  110. if (empty($display->layout)) {
  111. $display->layout = $key;
  112. }
  113. // Generate the parents as the autogenerator does, so we will have a
  114. // unique id for each radio button.
  115. $form['layout'][$category][$key] = array(
  116. '#type' => 'radio',
  117. '#title' => $choice,
  118. '#parents' => array('layout'),
  119. '#id' => drupal_clean_css_identifier('edit-layout-' . $key),
  120. '#return_value' => check_plain($key),
  121. '#default_value' => in_array($display->layout, array_keys($layouts)) ? $display->layout : NULL,
  122. );
  123. }
  124. }
  125. ctools_add_js('dependent');
  126. $js['CTools']['dependent'] = $dependencies;
  127. drupal_add_js($js, 'setting');
  128. if (empty($form_state['no buttons'])) {
  129. $form['submit'] = array(
  130. '#type' => 'submit',
  131. '#value' => t('Next'),
  132. );
  133. }
  134. return $form;
  135. }
  136. /**
  137. * Handle form submission of the display layout editor.
  138. */
  139. function panels_choose_layout_submit($form, &$form_state) {
  140. $form_state['layout'] = $form_state['values']['layout'];
  141. }
  142. /**
  143. * Form definition for the display layout converter.
  144. *
  145. * This form is only triggered if the user attempts to change the layout
  146. * for a display that has already had content assigned to it. It allows
  147. * the user to select where the panes located in to-be-deleted panels should
  148. * be relocated to.
  149. *
  150. * @ingroup forms
  151. *
  152. * @param array $form
  153. * A structured FAPI $form array.
  154. * @param &$form_state
  155. * The Drupal $form_state
  156. */
  157. function panels_change_layout($form, &$form_state) {
  158. // Provide a temporary display and renderer.
  159. $form_state['layout_display'] = $display = panels_new_display();
  160. if (isset($form_state['cache_key'])) {
  161. $display->cache_key = $form_state['cache_key'];
  162. }
  163. $new_layout = panels_get_layout($form_state['layout']);
  164. $new_layout_regions = panels_get_regions($new_layout, $display);
  165. $old_layout = panels_get_layout($form_state['display']->layout);
  166. $old_layout_regions = panels_get_regions($old_layout, $form_state['display']);
  167. $display->layout = $form_state['layout'];
  168. $renderer = panels_get_renderer_handler('editor', $display);
  169. $renderer->meta_location = 'inline';
  170. ctools_add_css('panels_admin', 'panels');
  171. ctools_add_css('panels_dnd', 'panels');
  172. ctools_add_css('dropdown');
  173. // For every region that had content in the old layout, create a custom pane
  174. // in the new layout that represents that region.
  175. $keys = array_keys($new_layout_regions);
  176. $default_region = reset($keys);
  177. foreach ($old_layout_regions as $region_id => $region_name) {
  178. if (!empty($form_state['display']->panels[$region_id])) {
  179. $pane = panels_new_pane('custom', 'custom', TRUE);
  180. $pane->pid = $region_id;
  181. $pane->configuration['title'] = t('Panes');
  182. $pane->configuration['admin_title'] = $region_name;
  183. // Get a list of pane titles used.
  184. $titles = array();
  185. foreach ($form_state['display']->panels[$region_id] as $pid) {
  186. $content_type = ctools_get_content_type($form_state['display']->content[$pid]->type);
  187. $titles[$pid] = ctools_content_admin_title($content_type, $form_state['display']->content[$pid]->subtype, $form_state['display']->content[$pid]->configuration, $form_state['display']->context);
  188. }
  189. $pane->configuration['body'] = '<ul><li>' . implode('</li><li>', $titles) . '</li></ul>';
  190. // If the region id matches, make it the same; otherwise, put it
  191. // in the default region.
  192. $pane->panel = empty($new_layout_regions[$region_id]) ? $default_region : $region_id;
  193. // Add the pane to the approprate spots.
  194. $display->content[$pane->pid] = $pane;
  195. $display->panels[$pane->panel][] = $pane->pid;
  196. }
  197. }
  198. $form['container'] = array(
  199. '#prefix' => '<div class="change-layout-display clearfix">',
  200. '#suffix' => '</div>',
  201. );
  202. $form['container']['old_layout'] = array(
  203. '#markup' => panels_print_layout_icon($form_state['display']->layout, $old_layout, check_plain($old_layout['title'])),
  204. );
  205. $form['container']['right_arrow'] = array(
  206. '#markup' => theme('image', array('path' => drupal_get_path('module', 'panels') . '/images/go-right.png')),
  207. );
  208. $form['container']['new_layout'] = array(
  209. '#markup' => panels_print_layout_icon($form_state['layout'], $new_layout, check_plain($new_layout['title'])),
  210. );
  211. $edit_form_state = array(
  212. 'display' => $display,
  213. 'renderer' => $renderer,
  214. 'no buttons' => TRUE,
  215. 'no preview' => TRUE,
  216. 'no display settings' => TRUE,
  217. 'display_title' => '',
  218. );
  219. ctools_include('display-edit', 'panels');
  220. $form = panels_edit_display_form($form, $edit_form_state);
  221. if (empty($form_state['no buttons'])) {
  222. $form['back'] = array(
  223. '#type' => 'submit',
  224. '#value' => t('Back'),
  225. '#submit' => array('panels_choose_layout_back'),
  226. );
  227. $form['submit'] = array(
  228. '#type' => 'submit',
  229. '#value' => $form_state['finish'],
  230. '#submit' => array('panels_change_layout_submit'),
  231. '#save-display' => TRUE,
  232. );
  233. }
  234. return $form;
  235. }
  236. /**
  237. * Handle submission of the change layout form.
  238. *
  239. * This submit handler will move panes around and save the display.
  240. */
  241. function panels_change_layout_submit($form, &$form_state) {
  242. $display = $form_state['display'];
  243. $layout_display = $form_state['layout_display'];
  244. $switch = array();
  245. // Calculate the pids submitted by the display and make a list of
  246. // translation to the regions. Remember the 'pid' of the pane
  247. // is the region id in the old layout.
  248. if (!empty($form_state['values']['panel']['pane'])) {
  249. foreach ($form_state['values']['panel']['pane'] as $region_id => $panes) {
  250. if ($panes) {
  251. $pids = explode(',', $panes);
  252. // Need to filter the array, b/c passing it in a hidden field can generate trash.
  253. foreach (array_filter($pids) as $pid) {
  254. $switch[$pid] = $region_id;
  255. }
  256. }
  257. }
  258. }
  259. $content = array();
  260. foreach ($switch as $region_id => $new_region_id) {
  261. if (isset($display->panels[$region_id])) {
  262. if (!isset($content[$new_region_id])) {
  263. $content[$new_region_id] = array();
  264. }
  265. $content[$new_region_id] = array_merge($content[$new_region_id], $display->panels[$region_id]);
  266. }
  267. }
  268. // Go through each pane and make sure its region id is correct.
  269. foreach ($content as $region_id => $region) {
  270. foreach ($region as $pid) {
  271. $display->content[$pid]->panel = $region_id;
  272. }
  273. }
  274. $display->panels = $content;
  275. $display->layout = $form_state['layout'];
  276. panels_edit_display_settings_form_submit($form, $form_state);
  277. }
  278. /**
  279. * Handle submission of the change layout form.
  280. *
  281. * This submit handler sets a flag on the form state, which is then used
  282. * by the calling wrapper to restart the process.
  283. */
  284. function panels_choose_layout_back($form, &$form_state) {
  285. $form_state['back'] = TRUE;
  286. }