common.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. <?php
  2. /**
  3. * @file
  4. * Functions used by more than one panels client module.
  5. */
  6. /**
  7. * Class definition for the allowed layouts governing structure.
  8. *
  9. * @ingroup mainapi
  10. *
  11. * This class is designed to handle panels allowed layouts data from start to finish, and sees
  12. * action at two times:\n
  13. * - When a client module wants to generate a form allowing an admin to create or edit a set
  14. * of allowed layouts. In this case, either a new panels_allowed_layouts object is created
  15. * or one is retrieved from storage and panels_allowed_layouts::set_allowed() is called to
  16. * generate the allowed layouts form. \n
  17. * - When a client module is calling panels_edit_layout(), a saved instantiation of this object
  18. * can be called up and passed in to the fourth parameter, and only the allowed layouts saved
  19. * in that object will be displayed on the form. \n
  20. * Because the panels API does not impose a data structure on the allowed_layouts data, client
  21. * modules can create as many of these objects as they want, and organize them around any concept:
  22. * node types, date published, author roles...anything.
  23. *
  24. * To call the settings form, instantiate this class - or, if your client module's needs are
  25. * heavy-duty, extend this class and instantiate your subclass - assign values to any relevant
  26. * desired members, and call panels_allowed_layouts::set_allowed(). See the documentation on
  27. * that method for a sample implementation.
  28. *
  29. * Note that when unserializing saved tokens of this class, you must
  30. * run panels_load_include('common') before unserializing in order to ensure
  31. * that the object is properly loaded.
  32. *
  33. * Client modules extending this class should implement a save() method and use it for
  34. * their custom data storage routine. You'll need to rewrite other class methods if
  35. * you choose to go another route.
  36. *
  37. * @see panels_edit_layout()
  38. * @see _panels_edit_layout()
  39. *
  40. */
  41. class panels_allowed_layouts {
  42. /**
  43. * Specifies whether newly-added layouts (as in, new .inc files) should be automatically
  44. * allowed (TRUE) or disallowed (FALSE) for $this. Defaults to TRUE, which is more
  45. * permissive but less of an administrative hassle if/when you add new layouts. Note
  46. * that this parameter will be derived from $allowed_layouts if a value is passed in.
  47. */
  48. var $allow_new = TRUE;
  49. /**
  50. * Optional member. If provided, the Panels API will generate a drupal variable using
  51. * variable_set($module_name . 'allowed_layouts', serialize($this)), thereby handling the
  52. * storage of this object entirely within the Panels API. This object will be
  53. * called and rebuilt by panels_edit_layout() if the same $module_name string is passed in
  54. * for the $allowed_types parameter. \n
  55. * This is primarily intended for convenience - client modules doing heavy-duty implementations
  56. * of the Panels API will probably want to create their own storage method.
  57. * @see panels_edit_layout()
  58. */
  59. var $module_name = NULL;
  60. /**
  61. * An associative array of all available layouts, keyed by layout name (as defined
  62. * in the corresponding layout plugin definition), with value = 1 if the layout is
  63. * allowed, and value = 0 if the layout is not allowed.
  64. * Calling array_filter(panels_allowed_layouts::$allowed_layout_settings) will return an associative array
  65. * containing only the allowed layouts, and wrapping that in array_keys() will
  66. * return an indexed version of that array.
  67. */
  68. var $allowed_layout_settings = array();
  69. /**
  70. * Hack-imitation of D6's $form_state. Used by the panels_common_set_allowed_types()
  71. * form to indicate whether the returned value is in its 'render', 'failed-validate',
  72. * or 'submit' stage.
  73. */
  74. var $form_state;
  75. /**
  76. * Constructor function; loads the $allowed_layout_settings array with initial values according
  77. * to $start_allowed
  78. *
  79. * @param bool $start_allowed
  80. * $start_allowed determines whether all available layouts will be marked
  81. * as allowed or not allowed on the initial call to panels_allowed_layouts::set_allowed()
  82. *
  83. */
  84. function __construct($start_allowed = TRUE) {
  85. // TODO would be nice if there was a way to just fetch the names easily
  86. foreach ($this->list_layouts() as $layout_name) {
  87. $this->allowed_layout_settings[$layout_name] = $start_allowed ? 1 : 0;
  88. }
  89. }
  90. /**
  91. * Manage panels_common_set_allowed_layouts(), the FAPI code for selecting allowed layouts.
  92. *
  93. * MAKE SURE to set panels_allowed_layouts::allow_new before calling this method. If you want the panels API
  94. * to handle saving these allowed layout settings, panels_allowed_layouts::module_name must also be set.
  95. *
  96. * Below is a sample implementation; refer to the rest of the class documentation to understand all the
  97. * specific pieces. Values that are intended to be replaced are wrapped with <>.
  98. *
  99. * \n @code
  100. * function docdemo_allowed_layouts() {
  101. * ctools_include('common', 'panels');
  102. * if (!is_a($allowed_layouts = unserialize(variable_get('panels_common_allowed_layouts', serialize(''))), 'panels_allowed_layouts')) {
  103. * $allowed_layouts = new panels_allowed_layouts();
  104. * $allowed_layouts->allow_new = TRUE;
  105. * $allowed_layouts->module_name = '<client_module_name>';
  106. * }
  107. * $result = $allowed_layouts->set_allowed('<Desired client module form title>');
  108. * if (in_array($allowed_layouts->form_state, array('failed-validate', 'render'))) {
  109. * return $result;
  110. * }
  111. * elseif ($allowed_layouts->form_state == 'submit') {
  112. * drupal_goto('</path/to/desired/redirect>');
  113. * }
  114. * }
  115. * @endcode \n
  116. *
  117. * If $allowed_layouts->form_state == 'failed-validate' || 'render', then you'll need to return
  118. * $result as it contains the structured form HTML generated by drupal_render_form() and is ready
  119. * to be passed through index.php's call to theme('page', ...).
  120. *
  121. * However, if $allowed_layouts->form_state == 'submit', then the form has been submitted and we should
  122. * react. It's really up to your client module how you handle the rest; panels_allowed_layouts::save() (or
  123. * panels_allowed_layouts::api_save(), if that's the route you're going) will have already been called,
  124. * so if those methods handle your save routine, then all there is left to do is handle redirects, if you
  125. * want. The current implementation of the allowed layouts form currently never redirects, so it's up to
  126. * you to control where the user ends up next.
  127. *
  128. * @param string $title
  129. * Used to set the title of the allowed layouts form. If no value is given, defaults to
  130. * 'Panels: Allowed Layouts'.
  131. *
  132. * @return mixed $result
  133. * - On the first passthrough when the form is being rendered, $result is the form's structured
  134. * HTML, ready to be pushed to the screen with a call to theme('page', ...).
  135. * - A successful second passthrough indicates a successful submit, and
  136. * $result === panels_allowed_layouts::allowed_layout_settings. Returning it is simply for convenience.
  137. */
  138. function set_allowed($title = 'Panels: Allowed Layouts') {
  139. $this->sync_with_available();
  140. $form_id = 'panels_common_set_allowed_layouts';
  141. // TODO switch to drupal_build_form(); need to pass by ref
  142. $form = drupal_retrieve_form($form_id, $this, $title);
  143. if ($result = drupal_process_form($form_id, $form)) {
  144. // successful submit
  145. $this->form_state = 'submit';
  146. return $result;
  147. }
  148. $this->form_state = isset($_POST['op']) ? 'failed-validate' : 'render';
  149. $result = drupal_render_form($form_id, $form);
  150. return $result;
  151. }
  152. /**
  153. * Checks for newly-added layouts and deleted layouts. If any are found, updates panels_allowed_layouts::allowed_layout_settings;
  154. * new additions are made according to panels_allowed_layouts::allow_new, while deletions are unset().
  155. *
  156. * Note that any changes made by this function are not saved in any permanent location.
  157. */
  158. function sync_with_available() {
  159. $layouts = $this->list_layouts();
  160. foreach (array_diff($layouts, array_keys($this->allowed_layout_settings)) as $new_layout) {
  161. $this->allowed_layout_settings[$new_layout] = $this->allow_new ? 1 : 0;
  162. }
  163. foreach (array_diff(array_keys($this->allowed_layout_settings), $layouts) as $deleted_layout) {
  164. unset($this->allowed_layout_settings[$deleted_layout]);
  165. }
  166. }
  167. /**
  168. * Use panels_allowed_layouts::module_name to generate a variable for variable_set(), in which
  169. * a serialized version of $this will be stored.
  170. *
  171. * Does nothing if panels_allowed_layouts::module_name is not set.
  172. *
  173. * IMPORTANT NOTE: if you use variable_get() in a custom client module save() method, you MUST
  174. * wrap $this in serialize(), then unserialize() what you get from variable_get(). Failure to
  175. * do so will result in an incomplete object. The following code will work:
  176. * @code
  177. * $allowed_layouts = unserialize(variable_get('your_variable_name', serialize(''));
  178. * @endcode
  179. *
  180. * If you don't serialize the second parameter of variable_get() and the variable name you provide
  181. * can't be found, an E_STRICT warning will be generated for trying to unserialize an entity
  182. * that has not been serialized.
  183. *
  184. */
  185. function save() {
  186. if (!is_null($this->module_name)) {
  187. variable_set($this->module_name . '_allowed_layouts', serialize($this));
  188. }
  189. }
  190. /**
  191. * Snag a list of the current layouts for internal use.
  192. *
  193. * Data is not saved in a class member in order to ensure that it's
  194. * fresh.
  195. *
  196. * @return array $layouts
  197. * An indexed array of the system names for all currently available layouts.
  198. */
  199. function list_layouts() {
  200. static $layouts = array();
  201. if (empty($layouts)) {
  202. ctools_include('plugins', 'panels');
  203. $layouts = array_keys(panels_get_layouts());
  204. }
  205. return $layouts;
  206. }
  207. }
  208. /**
  209. * A common settings page for Panels modules, because this code is relevant to
  210. * any modules that don't already have special requirements.
  211. */
  212. function panels_common_settings($form, &$form_state, $module_name = 'panels_common') {
  213. ctools_include('plugins', 'panels');
  214. ctools_include('content');
  215. $content_types = ctools_get_content_types();
  216. $skip = FALSE;
  217. $default_types = variable_get($module_name . '_default', NULL);
  218. if (!isset($default_types)) {
  219. $default_types = array('other' => TRUE);
  220. $skip = TRUE;
  221. }
  222. foreach ($content_types as $id => $info) {
  223. if (empty($info['single'])) {
  224. $default_options[$id] = t('New @s', array('@s' => $info['title']));
  225. if ($skip) {
  226. $default_types[$id] = TRUE;
  227. }
  228. }
  229. }
  230. $default_options['other'] = t('New content of other types');
  231. $form['additional_settings'] = array(
  232. '#type' => 'vertical_tabs',
  233. );
  234. $form['common'] = array(
  235. '#type' => 'fieldset',
  236. '#title' => t('New content behavior'),
  237. '#group' => 'additional_settings',
  238. '#weight' => -10,
  239. );
  240. $form['common']['panels_common_default'] = array(
  241. '#type' => 'checkboxes',
  242. '#description' => t('Select the default behavior of new content added to the system. If checked, new content will automatically be immediately available to be added to Panels pages. If not checked, new content will not be available until specifically allowed here.'),
  243. '#options' => $default_options,
  244. '#default_value' => array_keys(array_filter($default_types)),
  245. );
  246. $form_state['skip'] = $skip;
  247. if ($skip) {
  248. $form['markup'] = array('#value' => t('<p>Click Submit to be presented with a complete list of available content types set to the defaults you selected.</p>'));
  249. }
  250. else {
  251. // Rebuild the entire list, setting appropriately from defaults. Give
  252. // each type its own checkboxes set unless it's 'single' in which
  253. // case it can go into our fake other set.
  254. $available_content_types = ctools_content_get_all_types();
  255. $allowed_content_types = db_select('panels_allowed_types', 'pat')
  256. ->fields('pat', array('type', 'allowed'))
  257. ->condition('module', $module_name)
  258. ->execute()
  259. ->fetchAllKeyed();
  260. foreach ($available_content_types as $id => $types) {
  261. foreach ($types as $type => $info) {
  262. $key = $id . '-' . $type;
  263. $checkboxes = empty($content_types[$id]['single']) ? $id : 'other';
  264. $options[$checkboxes][$key] = $info['title'];
  265. if (!isset($allowed_content_types[$key])) {
  266. $allowed[$checkboxes][$key] = isset($default_types[$id]) ? $default_types[$id] : $default_types['other'];
  267. }
  268. else {
  269. $allowed[$checkboxes][$key] = $allowed_content_types[$key];
  270. }
  271. }
  272. }
  273. $form['content_types'] = array(
  274. '#tree' => TRUE,
  275. );
  276. // cheat a bit
  277. $content_types['other'] = array('title' => t('Other'), 'weight' => 10);
  278. foreach ($content_types as $id => $info) {
  279. if (isset($allowed[$id])) {
  280. $form['content_types'][$id] = array(
  281. '#type' => 'fieldset',
  282. '#group' => 'additional_settings',
  283. '#title' => t('Allowed @s content', array('@s' => $info['title'])),
  284. );
  285. $form['content_types'][$id]['options'] = array(
  286. '#prefix' => '<div class="panels-page-type-container">',
  287. '#suffix' => '</div>',
  288. '#type' => 'checkboxes',
  289. '#options' => $options[$id],
  290. '#default_value' => array_keys(array_filter($allowed[$id])),
  291. '#checkall' => TRUE,
  292. );
  293. }
  294. }
  295. }
  296. // Layout selection.
  297. panels_common_allowed_layouts_form($form, $form_state, $module_name);
  298. $form['allowed'] = array(
  299. '#type' => 'value',
  300. '#value' => isset($allowed) ? array_keys($allowed) : array(),
  301. );
  302. $form['module_name'] = array(
  303. '#type' => 'value',
  304. '#value' => $module_name,
  305. );
  306. $form['submit'] = array(
  307. '#type' => 'submit',
  308. '#value' => t('Save'),
  309. );
  310. ctools_add_css('panels_page', 'panels');
  311. return $form;
  312. }
  313. /**
  314. * Submit hook for panels_common_settings
  315. */
  316. function panels_common_settings_validate($form, &$form_state) {
  317. panels_common_allowed_layouts_form_validate($form, $form_state);
  318. }
  319. /**
  320. * Submit hook for panels_common_settings
  321. */
  322. function panels_common_settings_submit($form, &$form_state) {
  323. panels_common_allowed_layouts_form_submit($form, $form_state);
  324. $module_name = $form_state['values']['module_name'];
  325. variable_set($module_name . '_default', $form_state['values']['panels_common_default']);
  326. if (!$form_state['skip']) {
  327. // Merge the broken apart array neatly back together.
  328. $allowed_content_types = array();
  329. $content_types = $form_state['values']['allowed'];
  330. foreach ($content_types as $content_type) {
  331. $allowed_content_types = array_merge($allowed_content_types, $form_state['values']['content_types'][$content_type]['options']);
  332. foreach ($allowed_content_types as $type => $allowed) {
  333. $allowed = empty($allowed) ? 0 : 1;
  334. db_merge('panels_allowed_types')
  335. ->key(array('module' => $module_name, 'type' => $type))
  336. ->fields(array(
  337. 'module' => $module_name,
  338. 'type' => $type,
  339. 'allowed' => $allowed,
  340. ))
  341. ->execute();
  342. }
  343. }
  344. }
  345. drupal_set_message(t('Your changes have been saved.'));
  346. }
  347. /**
  348. * Based upon the settings, get the allowed types for this node.
  349. */
  350. function panels_common_get_allowed_types($module, $contexts = array(), $has_content = FALSE, $default_defaults = array(), $default_allowed_types = array()) {
  351. // Get a list of all types that are available
  352. $default_types = variable_get($module . '_default', $default_defaults);
  353. $allowed_types = db_select('panels_allowed_types', 'pat')
  354. ->fields('pat', array('type', 'allowed'))
  355. ->condition('module', $module)
  356. ->execute()
  357. ->fetchAllKeyed();
  358. $allowed_types = !empty($allowed_types) ? $allowed_types : $default_allowed_types;
  359. // By default, if they haven't gone and done the initial setup here,
  360. // let all 'other' types (which will be all types) be available.
  361. if (!isset($default_types['other'])) {
  362. $default_types['other'] = TRUE;
  363. }
  364. ctools_include('content');
  365. $content_types = ctools_content_get_available_types($contexts, $has_content, $allowed_types, $default_types);
  366. return $content_types;
  367. }
  368. /**
  369. * The FAPI code for generating an 'allowed layouts' selection form.
  370. *
  371. * NOTE: Because the Panels API does not guarantee a particular method of storing the data on allowed layouts,
  372. * it is not_possible for the Panels API to implement any checks that determine whether reductions in
  373. * the set of allowed layouts conflict with pre-existing layout selections. $displays in that category
  374. * will continue to function with their current layout as normal until the user/owner/admin attempts
  375. * to change layouts on that display, at which point they will have to select from the new set of
  376. * allowed layouts. If this is not the desired behavior for your client module, it's up to you to
  377. * write a validation routine that determines what should be done with conflicting layouts.
  378. *
  379. * Remember that changing layouts where panes have already been created can result in data loss;
  380. * consult panels_change_layout() to see how the Panels API handles that process. Running
  381. * drupal_execute('panels_change_layout', ...) is one possible starting point.
  382. *
  383. * @ingroup forms
  384. *
  385. * @param array $allowed_layouts
  386. * The set of allowed layouts that should be used as the default values
  387. * for this form. If none is provided, then by default no layouts will be restricted.
  388. */
  389. function panels_common_allowed_layouts_form(&$form, &$form_state, $module_name) {
  390. // Fetch our allowed layouts from variables.
  391. $allowed_layouts = panels_common_get_allowed_layout_object($module_name);
  392. $layouts = panels_get_layouts();
  393. foreach ($layouts as $id => $layout) {
  394. $options[$id] = panels_print_layout_icon($id, $layout, check_plain($layout['title']));
  395. }
  396. $form_state['allowed_layouts'] = &$allowed_layouts;
  397. ctools_add_js('layout', 'panels');
  398. $form['layout_selection'] = array(
  399. '#type' => 'fieldset',
  400. '#title' => t('Select allowed layouts'),
  401. '#group' => 'additional_settings',
  402. '#weight' => 10,
  403. );
  404. $form['layout_selection']['layouts'] = array(
  405. '#type' => 'checkboxes',
  406. '#options' => $options,
  407. '#description' => t('Check the boxes for all layouts you want to allow users choose from when picking a layout. You must allow at least one layout.'),
  408. '#default_value' => array_keys(array_filter($allowed_layouts->allowed_layout_settings)),
  409. '#prefix' => '<div class="clearfix panels-layouts-checkboxes">',
  410. '#suffix' => '</div>',
  411. '#checkall' => TRUE,
  412. );
  413. return $form;
  414. }
  415. function panels_common_allowed_layouts_form_validate($form, &$form_state) {
  416. $selected = array_filter($form_state['values']['layouts']);
  417. if (empty($selected)) {
  418. form_set_error('layouts', 'You must choose at least one layout to allow.');
  419. }
  420. }
  421. function panels_common_allowed_layouts_form_submit($form, &$form_state) {
  422. foreach ($form_state['values']['layouts'] as $layout => $setting) {
  423. $form_state['allowed_layouts']->allowed_layout_settings[$layout] = (bool) $setting;
  424. }
  425. $form_state['allowed_layouts']->save();
  426. }
  427. /**
  428. * Get the allowed layout object for the given module.
  429. */
  430. function panels_common_get_allowed_layout_object($module_name) {
  431. $allowed_layouts = unserialize(variable_get($module_name . '_allowed_layouts', serialize('')));
  432. // if no parameter was provided, or the variable_get failed
  433. if (!$allowed_layouts) {
  434. // still no dice. simply creates a dummy version where all layouts
  435. // are allowed.
  436. $allowed_layouts = new panels_allowed_layouts();
  437. $allowed_layouts->allow_new = TRUE;
  438. $allowed_layouts->module_name = $module_name;
  439. }
  440. // sanitize allowed layout listing; this is redundant if the
  441. // $allowed_layouts param was null, but the data is cached anyway
  442. $allowed_layouts->sync_with_available();
  443. return $allowed_layouts;
  444. }
  445. /**
  446. * Get the allowed layouts for the given module.
  447. */
  448. function panels_common_get_allowed_layouts($module_name) {
  449. ctools_include('plugins', 'panels');
  450. $available_layouts = panels_get_layouts();
  451. if (empty($module_name)) {
  452. return $available_layouts;
  453. }
  454. else if (is_object($module_name)) {
  455. $allowed_layouts = $module_name;
  456. }
  457. else {
  458. $allowed_layouts = panels_common_get_allowed_layout_object($module_name);
  459. }
  460. $allowed = array_filter($allowed_layouts->allowed_layout_settings);
  461. $order = array();
  462. foreach ($available_layouts as $name => $plugin) {
  463. if (!empty($allowed[$name])) {
  464. $order[$name] = $plugin['category'] . ':' . $plugin['title'];
  465. }
  466. }
  467. // Sort
  468. $layouts = array();
  469. asort($order);
  470. foreach ($order as $name => $junk) {
  471. $layouts[$name] = $available_layouts[$name];
  472. }
  473. return $layouts;
  474. }
  475. /**
  476. * Create a visible list of content in a display.
  477. * Note that the contexts must be pre-loaded.
  478. */
  479. function theme_panels_common_content_list($vars) {
  480. $display = $vars['display'];
  481. $layout = panels_get_layout($display->layout);
  482. $content = '<dl class="content-list">';
  483. foreach (panels_get_regions($layout, $display) as $panel_id => $title) {
  484. $content .= "<dt>$title</dt><dd>";
  485. if (!empty($display->panels[$panel_id])) {
  486. $content .= '<ol>';
  487. foreach ($display->panels[$panel_id] as $pid) {
  488. $content .= '<li>' . panels_get_pane_title($display->content[$pid], $display->context) . '</li>';
  489. }
  490. $content .= '</ol>';
  491. }
  492. else {
  493. $content .= t('Empty');
  494. }
  495. $content .= '</dd>';
  496. }
  497. $content .= '</dl>';
  498. return $content;
  499. }
  500. /**
  501. * Print a selector of layouts, each linked to the next step.
  502. *
  503. * Most operations use radio buttons for selecting layouts, but some will
  504. * give each layout as a link that goes to the next step. This function
  505. * makes it easy to simply provide a list of allowed layouts and the base
  506. * path.
  507. *
  508. * One limitation is that it will only append the layout name to the end, so
  509. * if the actual layout name is needed in the middle, that can't happen.
  510. *
  511. * @return
  512. * The rendered output.
  513. */
  514. function panels_common_print_layout_links($layouts, $base_path, $link_options = array(), $current_layout = NULL) {
  515. $output = '';
  516. $categories = array();
  517. ctools_include('cleanstring');
  518. $default_category = '';
  519. foreach ($layouts as $id => $layout) {
  520. $category = ctools_cleanstring($layout['category']);
  521. $categories[$category] = $layout['category'];
  522. if ($id == $current_layout) {
  523. $default_category = $category;
  524. }
  525. $options[$category][$id] = panels_print_layout_link($id, $layout, $base_path . '/' . $id, $link_options, $current_layout);
  526. }
  527. $form = array();
  528. $form['categories'] = array(
  529. '#title' => t('Category'),
  530. '#type' => 'select',
  531. '#options' => $categories,
  532. '#name' => 'categories',
  533. '#id' => 'edit-categories',
  534. '#value' => $default_category,
  535. '#parents' => array('categories'),
  536. '#access' => (count($categories) > 1) ? TRUE : FALSE,
  537. );
  538. $output .= drupal_render($form);
  539. $output .= '<div class="panels-choose-layout panels-layouts-checkboxes clearfix">';
  540. // We're doing these dependencies completely manualy, which is unusual, but
  541. // the process code only supports doing them in a form.
  542. // @todo modify dependent.inc to make this easier.
  543. $dependencies = array();
  544. foreach ($options as $category => $links) {
  545. $dependencies['panels-layout-category-' . $category] = array(
  546. 'values' => array('edit-categories' => array($category)),
  547. 'num' => 1,
  548. 'type' => 'hide',
  549. );
  550. $output .= '<div id="panels-layout-category-' . $category . '-wrapper">';
  551. $output .= '<div id="panels-layout-category-' . $category . '" class="form-checkboxes clearfix">';
  552. $output .= (count($categories) > 1) ? '<div class="panels-layouts-category">' . $categories[$category] . '</div>' : '';
  553. foreach ($links as $key => $link) {
  554. $output .= $link;
  555. }
  556. $output .= '</div></div>';
  557. }
  558. $output .= '</div>';
  559. ctools_add_js('dependent');
  560. $js['CTools']['dependent'] = $dependencies;
  561. drupal_add_js($js, 'setting');
  562. return $output;
  563. }