display-layout.inc 11 KB

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