panel_context.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. <?php
  2. /**
  3. * @file
  4. * This is the task handler plugin to handle attaching a panel to any
  5. * task that advertises itself as a 'context' type, which all of the
  6. * basic page tasks provided by page_manager.module do by default.
  7. */
  8. // Plugin definition.
  9. $plugin = array(
  10. // Is a 'context' handler type, meaning it supports the API of the
  11. // context handlers provided by ctools context plugins.
  12. 'handler type' => 'context',
  13. 'visible' => TRUE, // may be added up front.
  14. // Administrative fields.
  15. 'title' => t('Panel'),
  16. 'admin summary' => 'panels_panel_context_admin_summary',
  17. 'admin title' => 'panels_panel_context_title',
  18. 'operations' => array(
  19. 'settings' => array(
  20. 'title' => t('General'),
  21. 'description' => t('Change general settings about this variant.'),
  22. 'form' => 'panels_panel_context_edit_settings',
  23. ),
  24. 'criteria' => array(
  25. 'title' => t('Selection rules'),
  26. 'description' => t('Control the criteria used to decide whether or not this variant is used.'),
  27. 'ajax' => FALSE,
  28. 'form' => array(
  29. 'order' => array(
  30. 'form' => t('Selection rules'),
  31. ),
  32. 'forms' => array(
  33. 'form' => array(
  34. 'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
  35. 'form id' => 'ctools_context_handler_edit_criteria',
  36. ),
  37. ),
  38. ),
  39. ),
  40. 'context' => array(
  41. 'title' => t('Contexts'),
  42. 'ajax' => FALSE,
  43. 'description' => t('Add additional context objects to this variant that can be used by the content.'),
  44. 'form' => array(
  45. 'order' => array(
  46. 'form' => t('Context'),
  47. ),
  48. 'forms' => array(
  49. 'form' => array(
  50. 'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
  51. 'form id' => 'ctools_context_handler_edit_context',
  52. ),
  53. ),
  54. ),
  55. ),
  56. 'layout' => array(
  57. 'title' => t('Layout'),
  58. 'description' => t('Change the layout of this panel.'),
  59. // No AJAX so we get our CSS loaded.
  60. 'ajax' => FALSE,
  61. 'form' => array(
  62. 'order' => array(
  63. 'choose' => t('Change layout'),
  64. 'move' => t('Move content from old layout'),
  65. ),
  66. 'forms' => array(
  67. 'choose' => array(
  68. 'form id' => 'panels_panel_context_edit_layout',
  69. ),
  70. 'move' => array(
  71. 'include' => array(
  72. drupal_get_path('module', 'panels') . '/includes/display-layout.inc',
  73. ),
  74. 'form id' => 'panels_panel_context_edit_move',
  75. 'submit' => 'panels_change_layout_submit',
  76. ),
  77. ),
  78. ),
  79. ),
  80. 'content' => array(
  81. 'title' => t('Content'),
  82. 'description' => t('Add content items and change their location with a drag and drop interface.'),
  83. 'ajax' => FALSE,
  84. 'form' => array(
  85. 'order' => array(
  86. 'form' => t('Content'),
  87. ),
  88. 'forms' => array(
  89. 'form' => array(
  90. 'include' => array(
  91. drupal_get_path('module', 'panels') . '/includes/display-edit.inc',
  92. ),
  93. 'form id' => 'panels_panel_context_edit_content',
  94. 'no blocks' => TRUE,
  95. ),
  96. ),
  97. ),
  98. ),
  99. 'preview' => array(
  100. 'title' => t('Preview'),
  101. 'description' => t('Get a preview of what this variant will look like.'),
  102. 'form' => 'panels_panel_context_edit_preview',
  103. 'ajax' => FALSE,
  104. 'silent' => TRUE,
  105. 'form info' => array('finish text' => t('Preview')),
  106. 'no update and save' => TRUE,
  107. ),
  108. ),
  109. 'tab operation' => 'panels_panel_context_tab_operation',
  110. // Callback to render the data.
  111. 'render' => 'panels_panel_context_render',
  112. // Callback to return addressable data.
  113. 'addressable callback' => 'panels_panel_context_get_addressable',
  114. // Various callbacks for operations performed on the handler to ensure
  115. // related data is updated properly.
  116. 'save' => 'panels_panel_context_save',
  117. 'delete' => 'panels_panel_context_delete',
  118. 'export' => 'panels_panel_context_export',
  119. 'clone' => 'panels_panel_context_clone',
  120. 'add features' => array(
  121. 'criteria' => t('Selection rules'),
  122. 'context' => t('Contexts'),
  123. ),
  124. // Where to go when finished.
  125. 'add finish' => 'content',
  126. 'required forms' => array(
  127. 'choose' => t('Choose layout'),
  128. 'settings' => t('Panel settings'),
  129. 'content' => t('Panel content'),
  130. ),
  131. 'edit forms' => array(
  132. 'content' => t('Panel content'),
  133. 'criteria' => t('Selection rules'),
  134. 'settings' => t('General'),
  135. 'context' => t('Contexts'),
  136. 'layout' => t('Change layout'),
  137. 'move' => '', // no title makes it a 'hidden' edit form.
  138. ),
  139. 'forms' => array(
  140. 'settings' => array(
  141. 'form id' => 'panels_panel_context_edit_settings',
  142. ),
  143. 'choose' => array(
  144. 'form id' => 'panels_panel_context_edit_choose',
  145. 'no back validate' => TRUE,
  146. ),
  147. 'layout' => array(
  148. 'no return' => TRUE,
  149. 'form id' => 'panels_panel_context_edit_layout',
  150. ),
  151. 'move' => array(
  152. 'include' => array(
  153. drupal_get_path('module', 'panels') . '/includes/display-layout.inc',
  154. ),
  155. 'form id' => 'panels_panel_context_edit_move',
  156. 'submit' => 'panels_change_layout_submit',
  157. ),
  158. 'content' => array(
  159. 'include' => array(
  160. drupal_get_path('module', 'panels') . '/includes/display-edit.inc',
  161. ),
  162. 'form id' => 'panels_panel_context_edit_content',
  163. 'no blocks' => TRUE,
  164. ),
  165. 'context' => array(
  166. 'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
  167. 'form id' => 'ctools_context_handler_edit_context',
  168. ),
  169. 'criteria' => array(
  170. 'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
  171. 'form id' => 'ctools_context_handler_edit_criteria',
  172. ),
  173. ),
  174. 'default conf' => array(
  175. 'title' => t('Panel'),
  176. 'no_blocks' => FALSE,
  177. 'pipeline' => variable_get('panels_renderer_default', 'standard'),
  178. 'body_classes_to_remove' => '',
  179. 'body_classes_to_add' => '',
  180. 'css_id' => '',
  181. 'css' => '',
  182. 'contexts' => array(),
  183. 'relationships' => array(),
  184. ),
  185. );
  186. /**
  187. * Provide the operation trail for the 'Edit panel' link.
  188. *
  189. * When editing the panel, go directly to the content tab.
  190. */
  191. function panels_panel_context_tab_operation($handler, $contexts, $args) {
  192. return array('handlers', $handler->name, 'content');
  193. }
  194. /**
  195. * Get the display for a task handler.
  196. *
  197. * There are three methods that the display can be found.
  198. * - In the database. $handler->conf['did'] will be set in this case,
  199. * and $handler->conf['display'] won't be.
  200. * - In $handler->conf['display'], with $handler->conf['did'] empty. This
  201. * will be true for a default/imported task handler as well as a handler
  202. * that has just been created but has not yet been saved.
  203. * - in $handler->conf['display'] with $handler->conf['did' populated. This
  204. * simply means that the display has been modified and is awaiting
  205. * save. The modified one should always be used for editing purposes.
  206. * - If none of the above is true, then a new display needs to be created
  207. * for the handler and pla.
  208. */
  209. function &panels_panel_context_get_display(&$handler) {
  210. if (!isset($handler->conf['display'])) {
  211. if (isset($handler->conf['did'])) {
  212. $handler->conf['display'] = panels_load_display($handler->conf['did']);
  213. }
  214. // Check for again for a valid display. If no valid display could be loaded,
  215. // something is wrong and we'll create a new one.
  216. if (empty($handler->conf['display'])) {
  217. $handler->conf['display'] = panels_new_display();
  218. }
  219. }
  220. $display =& $handler->conf['display'];
  221. $display->storage_type = 'page_manager';
  222. $display->storage_id = !empty($handler->name) ? $handler->name : 'new';
  223. return $display;
  224. }
  225. /**
  226. * Build the cache key so that the editor and IPE can properly find
  227. * everything needed for this display.
  228. */
  229. function panels_panel_context_cache_key($task_name, $handler_id, $args) {
  230. $arguments = array();
  231. foreach ($args as $arg) {
  232. // Sadly things like panels everywhere actually use non-string arguments
  233. // and they basically can't be represented here. Luckily, PE also does
  234. // not use a system where this matters, so replace its args with a 0
  235. // for a placeholder.
  236. if (is_string($arg)) {
  237. $arguments[] = $arg;
  238. }
  239. else {
  240. $arguments[] = '0';
  241. }
  242. }
  243. $cache_key = 'panel_context:' . $task_name . '::' . $handler_id . '::' . implode('\\', $arguments) . '::';
  244. return $cache_key;
  245. }
  246. /**
  247. * Check selection rules and, if passed, render the contexts.
  248. */
  249. function panels_panel_context_render($handler, $base_contexts, $args, $test = TRUE) {
  250. // Go through arguments and see if they match.
  251. ctools_include('context');
  252. ctools_include('context-task-handler');
  253. ctools_include('plugins', 'panels');
  254. // Add my contexts.
  255. $contexts = ctools_context_handler_get_handler_contexts($base_contexts, $handler);
  256. // Test.
  257. if ($test && !ctools_context_handler_select($handler, $contexts)) {
  258. return;
  259. }
  260. if (isset($handler->handler)) {
  261. ctools_context_handler_pre_render($handler, $contexts, $args);
  262. }
  263. // Load the display.
  264. $display = panels_panel_context_get_display($handler);
  265. $display->context = $contexts;
  266. $display->args = $args;
  267. $page_css_id = ctools_context_keyword_substitute($handler->conf['css_id'], array(), $contexts);
  268. $display->css_id = check_plain($page_css_id);
  269. $task_name = page_manager_make_task_name($handler->task, $handler->subtask);
  270. $display->cache_key = panels_panel_context_cache_key($task_name, $handler->name, $args);
  271. // Check to see if there is any CSS.
  272. if (!empty($handler->conf['css'])) {
  273. ctools_include('css');
  274. $css_id = 'panel_context:' . $handler->name;
  275. $filename = ctools_css_retrieve($css_id);
  276. if (!$filename) {
  277. // Add keywords from context.
  278. $css = ctools_context_keyword_substitute($handler->conf['css'], array(), $contexts, array('css safe' => TRUE));
  279. $filename = ctools_css_store($css_id, $css);
  280. }
  281. drupal_add_css($filename);
  282. }
  283. // With an argument, this actually sets the display.
  284. panels_get_current_page_display($display);
  285. $renderer = panels_get_renderer($handler->conf['pipeline'], $display);
  286. // If the IPE is enabled, but the user does not have access to edit
  287. // load the standard renderer instead.
  288. $parents = class_parents($renderer);
  289. if (!empty($parents['panels_renderer_editor']) && !user_access('user page manager') && !user_access('use ipe with page manager')) {
  290. $renderer = panels_get_renderer_handler('standard', $display);
  291. }
  292. // Remove and add body element classes.
  293. $panel_body_css = &drupal_static('panel_body_css');
  294. if (isset($handler->conf['body_classes_to_remove'])) {
  295. $classes = ctools_context_keyword_substitute($handler->conf['body_classes_to_remove'], array(), $contexts, array('css safe' => TRUE));
  296. if (!isset($panel_body_css['body_classes_to_remove'])) {
  297. $panel_body_css['body_classes_to_remove'] = check_plain($classes);
  298. }
  299. else {
  300. $panel_body_css['body_classes_to_remove'] .= ' ' . check_plain($classes);
  301. }
  302. }
  303. if (isset($handler->conf['body_classes_to_add'])) {
  304. $classes = ctools_context_keyword_substitute($handler->conf['body_classes_to_add'], array(), $contexts, array('css safe' => TRUE));
  305. if (!isset($panel_body_css['body_classes_to_add'])) {
  306. $panel_body_css['body_classes_to_add'] = check_plain($classes);
  307. }
  308. else {
  309. $panel_body_css['body_classes_to_add'] .= ' ' . check_plain($classes);
  310. }
  311. }
  312. $info = array(
  313. 'content' => panels_render_display($display, $renderer),
  314. 'no_blocks' => !empty($handler->conf['no_blocks']),
  315. );
  316. $info['title'] = $display->get_title();
  317. return $info;
  318. }
  319. /**
  320. * Callback to allow the handler to react to being saved.
  321. *
  322. * When a handler with a display is saved, two things have to happen.
  323. * First, we have to save the display so that it becomes a real display,
  324. * not the fake one we started with. Second, we have to cache
  325. * any CSS that the display is using. This CSS can get re-cached
  326. * later if the file disappears, but it's imperative that we do it here
  327. * to make sure that old, dirty CSS cache gets removed.
  328. */
  329. function panels_panel_context_save(&$handler, $update) {
  330. // Only save the display if we believe it has been modified.
  331. if (isset($handler->conf['display'])) {
  332. panels_save_display($handler->conf['display']);
  333. $handler->conf['did'] = $handler->conf['display']->did;
  334. unset($handler->conf['display']);
  335. }
  336. // Delete any previous CSS cache file.
  337. ctools_include('css');
  338. ctools_css_clear('panel_context:' . $handler->name);
  339. if (isset($handler->conf['temp_layout'])) {
  340. unset($handler->conf['temp_layout']);
  341. }
  342. }
  343. /**
  344. * Special handling for exporting a panel task handler.
  345. *
  346. * When a panel is exported, we need to export the display separately
  347. * rather than just letting its object be unpacked, which does not work
  348. * very well.
  349. */
  350. function panels_panel_context_export(&$handler, $indent) {
  351. $display = panels_panel_context_get_display($handler);
  352. foreach (array('display', 'did', 'css_cache', 'temp_layout') as $item) {
  353. if (isset($handler->conf[$item])) {
  354. unset($handler->conf[$item]);
  355. }
  356. }
  357. $output = panels_export_display($display, $indent);
  358. $output .= $indent . '$handler->conf[\'display\'] = $display' . ";\n";
  359. return $output;
  360. }
  361. /**
  362. * When a handler is cloned, we have to clone the display.
  363. */
  364. function panels_panel_context_clone(&$handler) {
  365. $old_display = panels_panel_context_get_display($handler);
  366. $code = panels_export_display($old_display);
  367. eval($code);
  368. foreach (array('display', 'did', 'css_cache', 'temp_layout') as $item) {
  369. if (isset($handler->conf[$item])) {
  370. unset($handler->conf[$item]);
  371. }
  372. }
  373. $display = (object) array(
  374. 'did' => 'new',
  375. 'uuid' => ctools_uuid_generate(),
  376. );
  377. $handler->conf['display'] = $display;
  378. }
  379. /**
  380. * Callback to delete the display when a handler is deleted.
  381. */
  382. function panels_panel_context_delete(&$handler) {
  383. if (!empty($handler->conf['did'])) {
  384. panels_delete_display($handler->conf['did']);
  385. }
  386. }
  387. /**
  388. * Set up a title for the panel based upon the selection rules.
  389. */
  390. function panels_panel_context_title($handler, $task, $subtask) {
  391. if (isset($handler->conf['title'])) {
  392. return check_plain($handler->conf['title']);
  393. }
  394. else {
  395. return t('Panel');
  396. }
  397. }
  398. /**
  399. * Provide a nice little summary of what's in a panel.
  400. *
  401. * The task handler manager provides a summary of a given handler in a
  402. * collapsible div. This callback provides that. For a Panel, we
  403. * provide a summary of the layout type and content on one side, and
  404. * a summary of the contexts in use on the other.
  405. */
  406. function panels_panel_context_admin_summary($handler, $task, $subtask, $page, $show_title = TRUE) {
  407. $task_name = page_manager_make_task_name($task['name'], $subtask['name']);
  408. $output = '';
  409. $display = panels_panel_context_get_display($handler);
  410. ctools_include('plugins', 'panels');
  411. ctools_include('context');
  412. ctools_include('context-task-handler');
  413. // Get the operations.
  414. $operations = page_manager_get_operations($page);
  415. // Get operations for just this handler.
  416. $operations = $operations['handlers']['children'][$handler->name]['children']['actions']['children'];
  417. $args = array('handlers', $handler->name, 'actions');
  418. $rendered_operations = page_manager_render_operations($page, $operations, array(), array('class' => array('actions')), 'actions', $args);
  419. $layout = panels_get_layout($display->layout);
  420. $plugin = page_manager_get_task_handler($handler->handler);
  421. $object = ctools_context_handler_get_task_object($task, $subtask, $handler);
  422. $display->context = ctools_context_load_contexts($object, TRUE);
  423. $access = ctools_access_group_summary(!empty($handler->conf['access']) ? $handler->conf['access'] : array(), $display->context);
  424. if ($access) {
  425. $access = t('This panel will be selected if @conditions.', array('@conditions' => $access));
  426. }
  427. else {
  428. $access = t('This panel will always be selected.');
  429. }
  430. $rows = array();
  431. $type = $handler->type == t('Default') ? t('In code') : $handler->type;
  432. $rows[] = array(
  433. array('class' => t('page-summary-label'), 'data' => t('Storage')),
  434. array('class' => t('page-summary-data'), 'data' => $type),
  435. array('class' => t('page-summary-operation'), 'data' => ''),
  436. );
  437. if (!empty($handler->disabled)) {
  438. $link = l(t('Enable'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'actions', 'enable')));
  439. $text = t('Disabled');
  440. }
  441. else {
  442. $link = l(t('Disable'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'actions', 'disable')));
  443. $text = t('Enabled');
  444. }
  445. $rows[] = array(
  446. array('class' => t('page-summary-label'), 'data' => t('Status')),
  447. array('class' => t('page-summary-data'), 'data' => $text),
  448. array('class' => t('page-summary-operation'), 'data' => $link),
  449. );
  450. $link = l(t('Edit'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'criteria')));
  451. $rows[] = array(
  452. array('class' => t('page-summary-label'), 'data' => t('Selection rule')),
  453. array('class' => t('page-summary-data'), 'data' => $access),
  454. array('class' => t('page-summary-operation'), 'data' => $link),
  455. );
  456. $link = l(t('Change layout'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'layout')));
  457. $link .= '<br />' . l(t('Edit content'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'content')));
  458. $link .= '<br />' . l(t('Preview'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'preview')));
  459. $rows[] = array(
  460. array('class' => t('page-summary-label'), 'data' => t('Layout')),
  461. array('class' => t('page-summary-data'), 'data' => check_plain($layout['title'])),
  462. array('class' => t('page-summary-operation'), 'data' => $link),
  463. );
  464. $content_link = ' [' . l(t('Edit'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'content'))) . ']';
  465. $context_link = ' [' . l(t('Edit'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'context'))) . ']';
  466. $info = theme('table', array('rows' => $rows, 'attributes' => array('class' => 'page-manager-handler-summary')));
  467. $title = $handler->conf['title'];
  468. if ($title != t('Panel')) {
  469. $title = t('Panel: @title', array('@title' => $title));
  470. }
  471. $output .= '<div class="clearfix">';
  472. if ($show_title) {
  473. $output .= '<div class="handler-title clearfix">';
  474. $output .= '<div class="actions handler-actions">' . $rendered_operations['actions'] . '</div>';
  475. $output .= '<span class="title-label">' . $title . '</span>';
  476. }
  477. $output .= '</div>';
  478. $output .= $info;
  479. $output .= '</div>';
  480. /*
  481. $output .= '<div class="right-container">';
  482. $output .= '<h3 class="context-title">' . t('Contexts') . $context_link . '</h3>';
  483. $output .= $contexts;
  484. $output .= '</div>';
  485. $output .= '<div class="left-container">';
  486. // $output .= $icon;
  487. $output .= '<h3 class="handler-title">' . t('Content') . $content_link . '</h3>';
  488. $output .= $content;
  489. $output .= '</div>';
  490. */
  491. return $output;
  492. }
  493. // --------------------------------------------------------------------------
  494. // Forms.
  495. /**
  496. * General notes about forms: The handler is automatically cached by the form
  497. * wizard, so anything we store on $form_state['handler'] anywhere will get
  498. * saved and appear on the next form. The cache is a 'working' cache and
  499. * if the user hits cancel on any page of the multi-page wizard, all
  500. * changes since the last 'update/finish' click will be flushed away.
  501. *
  502. * Many of the Panels forms call through to the real Panels cousins. These
  503. * forms are smart enough to know that they're being wrapped in another
  504. * form and act appropriately. Some of them are so smart that we just let
  505. * their submit and validate handlers do the work rather than writing
  506. * additional ones here.
  507. */
  508. /**
  509. * Choose a layout for this panel.
  510. *
  511. * This is only called during 'add', when we know that there isn't a
  512. * previous layout to choose from. a different, only slightly different
  513. * variant is called to change a pre-existing layout.
  514. */
  515. function panels_panel_context_edit_choose($form, &$form_state) {
  516. ctools_include('common', 'panels');
  517. ctools_include('display-layout', 'panels');
  518. ctools_include('plugins', 'panels');
  519. // @todo -- figure out where/how to deal with this.
  520. $form_state['allowed_layouts'] = 'panels_page';
  521. $form_state['display'] = &panels_panel_context_get_display($form_state['handler']);
  522. // Tell the Panels form not to display buttons.
  523. $form_state['no buttons'] = TRUE;
  524. // Change the #id of the form so the CSS applies properly.
  525. $form['#id'] = 'panels-choose-layout';
  526. $form = panels_choose_layout($form, $form_state);
  527. return $form;
  528. }
  529. /**
  530. * Validate that a layout was chosen.
  531. */
  532. function panels_panel_context_edit_choose_validate(&$form, &$form_state) {
  533. if (empty($form_state['values']['layout'])) {
  534. form_error($form['layout'], t('You must select a layout.'));
  535. }
  536. }
  537. /**
  538. * A layout has been selected, set it up.
  539. */
  540. function panels_panel_context_edit_choose_submit(&$form, &$form_state) {
  541. $form_state['display']->layout = $form_state['values']['layout'];
  542. $form_state['handler']->conf['display'] = $form_state['display'];
  543. if (isset($form_state['page']->display_cache[$form_state['handler_id']])) {
  544. $form_state['page']->display_cache[$form_state['handler_id']]->display = $form_state['display'];
  545. }
  546. }
  547. /**
  548. * Change the layout for this panel.
  549. *
  550. * This form is only used if a layout already exists and the user wants
  551. * to change to a different one. The submit handler changes the next form
  552. * to the move content form, which is 'hidden' so it won't be accessed
  553. * directly.
  554. */
  555. function panels_panel_context_edit_layout($form, &$form_state) {
  556. ctools_include('common', 'panels');
  557. ctools_include('display-layout', 'panels');
  558. ctools_include('plugins', 'panels');
  559. // @todo -- figure out where/how to deal with this.
  560. $form_state['allowed_layouts'] = 'panels_page';
  561. $form_state['display'] = &panels_panel_context_get_display($form_state['handler']);
  562. // Tell the Panels form not to display buttons.
  563. $form_state['no buttons'] = TRUE;
  564. // Change the #id of the form so the CSS applies properly.
  565. $form['#id'] = 'panels-choose-layout';
  566. $form = panels_choose_layout($form, $form_state);
  567. return $form;
  568. }
  569. /**
  570. * Validate that a layout was chosen.
  571. */
  572. function panels_panel_context_edit_layout_validate(&$form, &$form_state) {
  573. $display = &panels_panel_context_get_display($form_state['handler']);
  574. if (empty($form_state['values']['layout'])) {
  575. form_error($form['layout'], t('You must select a layout.'));
  576. }
  577. if ($form_state['values']['layout'] == $display->layout) {
  578. form_error($form['layout'], t('You must select a different layout if you wish to change layouts.'));
  579. }
  580. }
  581. /**
  582. * A layout has been selected, set it up.
  583. */
  584. function panels_panel_context_edit_layout_submit(&$form, &$form_state) {
  585. $display = &panels_panel_context_get_display($form_state['handler']);
  586. if ($form_state['values']['layout'] != $display->layout) {
  587. $form_state['handler']->conf['temp_layout'] = $form_state['values']['layout'];
  588. }
  589. }
  590. /**
  591. * When a layout is changed, the user is given the opportunity to move content.
  592. */
  593. function panels_panel_context_edit_move($form, &$form_state) {
  594. $form_state['display'] = &panels_panel_context_get_display($form_state['handler']);
  595. $form_state['layout'] = $form_state['handler']->conf['temp_layout'];
  596. $form_state['cache_key'] = panels_panel_context_cache_key($form_state['task_name'], $form_state['handler_id'], array());
  597. ctools_include('common', 'panels');
  598. ctools_include('display-layout', 'panels');
  599. ctools_include('plugins', 'panels');
  600. // Tell the Panels form not to display buttons.
  601. $form_state['no buttons'] = TRUE;
  602. // Change the #id of the form so the CSS applies properly.
  603. $form = panels_change_layout($form, $form_state);
  604. // Change the 'back' button to just go directly to the previous form
  605. // $task_id = $form_state['task']['name'];
  606. // $handler_id = $form_state['handler']->handler;
  607. // $name = $form_state['handler']->name;
  608. // This form is outside the normal wizard list, so we need to specify the
  609. // previous/next forms.
  610. $form['buttons']['previous']['#next'] = 'layout';
  611. $form['buttons']['next']['#next'] = 'content';
  612. $form_state['form_info']['return path'] = page_manager_edit_url($form_state['page']->task_name, array('handlers', $form_state['handler_id'], 'content'));
  613. return $form;
  614. }
  615. /**
  616. * Present the panels drag & drop editor to edit the display attached
  617. * to the task handler.
  618. */
  619. function panels_panel_context_edit_content($form, &$form_state) {
  620. ctools_include('ajax');
  621. ctools_include('plugins', 'panels');
  622. ctools_include('common', 'panels');
  623. ctools_include('context');
  624. ctools_include('context-task-handler');
  625. $cache = panels_edit_cache_get(panels_panel_context_cache_key($form_state['task_name'], $form_state['handler_id'], array()));
  626. $form_state['renderer'] = panels_get_renderer_handler('editor', $cache->display);
  627. $form_state['renderer']->cache = &$cache;
  628. $form_state['display'] = &$cache->display;
  629. $form_state['content_types'] = $cache->content_types;
  630. // Tell the Panels form not to display buttons.
  631. $form_state['no buttons'] = TRUE;
  632. $form_state['display_title'] = !empty($cache->display_title);
  633. $form_state['no preview'] = TRUE;
  634. $form_state['page']->display_cache[$form_state['handler_id']] = $cache;
  635. $form = panels_edit_display_form($form, $form_state);
  636. if (!isset($form_state['type']) || $form_state['type'] != 'add' && !empty($form_state['handler_id']) && !empty($form['buttons'])) {
  637. $form['buttons']['preview'] = $form['buttons']['return'];
  638. $form['buttons']['preview']['#value'] = t('Update and preview');
  639. }
  640. return $form;
  641. }
  642. /**
  643. * Validate changes to the panel content form.
  644. */
  645. function panels_panel_context_edit_content_validate(&$form, &$form_state) {
  646. panels_edit_display_form_validate($form, $form_state);
  647. }
  648. function panels_panel_context_edit_content_submit(&$form, &$form_state) {
  649. // Update the storage_id if this is a new variant before saving.
  650. if ($form_state['display']->storage_id == 'new') {
  651. $form_state['display']->storage_id = $form_state['handler_id'];
  652. }
  653. panels_edit_display_form_submit($form, $form_state);
  654. $handler = &$form_state['handler'];
  655. // Update the cached display:
  656. $display = $form_state['page']->display_cache[$form_state['handler_id']]->display;
  657. $handler->conf['display'] = $display;
  658. unset($form_state['page']->display_cache[$form_state['handler_id']]);
  659. if ($form_state['clicked_button']['#value'] == t('Update and preview')) {
  660. $form_state['new trail'] = array('handlers', $form_state['handler_id'], 'preview');
  661. }
  662. }
  663. /**
  664. * General settings for the panel.
  665. */
  666. function panels_panel_context_edit_settings($form, &$form_state) {
  667. $conf = $form_state['handler']->conf;
  668. $form['conf']['title'] = array(
  669. '#type' => 'textfield',
  670. '#default_value' => $conf['title'],
  671. '#title' => t('Administrative title'),
  672. '#description' => t('Administrative title of this variant.'),
  673. );
  674. $form['conf']['no_blocks'] = array(
  675. '#type' => 'checkbox',
  676. '#default_value' => $conf['no_blocks'],
  677. '#title' => t('Disable Drupal blocks/regions'),
  678. '#description' => t('Check this to have the page disable all regions displayed in the theme. Note that some themes support this setting better than others. If in doubt, try with stock themes to see.'),
  679. );
  680. $form['conf']['body_classes_to_remove'] = array(
  681. '#type' => 'textfield',
  682. '#size' => 128,
  683. '#default_value' => empty($conf['body_classes_to_remove']) ? '' : $conf['body_classes_to_remove'],
  684. '#title' => t('Remove body CSS classes'),
  685. '#description' => t('The CSS classes to remove from the body element of this page. Separated by a space. For example: no-sidebars one-sidebar sidebar-first sidebar-second two-sidebars. Keywords from context are allowed.'),
  686. );
  687. $form['conf']['body_classes_to_add'] = array(
  688. '#type' => 'textfield',
  689. '#size' => 128,
  690. '#default_value' => empty($conf['body_classes_to_add']) ? '' : $conf['body_classes_to_add'],
  691. '#title' => t('Add body CSS classes'),
  692. '#description' => t('The CSS classes to add to the body element of this page. Separated by a space. For example: no-sidebars one-sidebar sidebar-first sidebar-second two-sidebars. Keywords from context are allowed.'),
  693. );
  694. ctools_include('plugins', 'panels');
  695. $pipelines = panels_get_renderer_pipelines();
  696. // Handle backward compatibility with the IPE checkbox.
  697. if (empty($conf['pipeline'])) {
  698. $conf['pipeline'] = !empty($conf['use_ipe']) ? 'ipe' : 'standard';
  699. }
  700. // If there are no pipelines, that probably means we're operating in
  701. // legacy mode.
  702. if (empty($pipelines)) {
  703. // We retain the original pipeline so we don't wreck things by installing
  704. // old modules.
  705. $form['conf']['pipeline'] = array(
  706. '#type' => 'value',
  707. '#value' => $conf['pipeline'],
  708. );
  709. }
  710. else {
  711. $options = array();
  712. foreach ($pipelines as $name => $pipeline) {
  713. $options[$name] = check_plain($pipeline->admin_title) . '<div class="description">' . check_plain($pipeline->admin_description) . '</div>';
  714. }
  715. $form['conf']['pipeline'] = array(
  716. '#type' => 'radios',
  717. '#options' => $options,
  718. '#title' => t('Renderer'),
  719. '#default_value' => $conf['pipeline'],
  720. );
  721. }
  722. $form['conf']['css_id'] = array(
  723. '#type' => 'textfield',
  724. '#size' => 35,
  725. '#default_value' => $conf['css_id'],
  726. '#title' => t('CSS ID'),
  727. '#description' => t('The CSS ID to apply to this page. Keywords from context are allowed.'),
  728. );
  729. $form['conf']['css'] = array(
  730. '#type' => 'textarea',
  731. '#title' => t('CSS code'),
  732. '#description' => t('Enter well-formed CSS code here; this code will be embedded into the page, and should only be used for minor adjustments; it is usually better to try to put CSS for the page into the theme if possible. This CSS will be filtered for safety so some CSS may not work. Keywords from context are allowed.'),
  733. '#default_value' => $conf['css'],
  734. );
  735. return $form;
  736. }
  737. /**
  738. * Submit handler for general settings form.
  739. */
  740. function panels_panel_context_edit_settings_submit(&$form, &$form_state) {
  741. $form_state['handler']->conf['no_blocks'] = $form_state['values']['no_blocks'];
  742. $form_state['handler']->conf['body_classes_to_remove'] = $form_state['values']['body_classes_to_remove'];
  743. $form_state['handler']->conf['body_classes_to_add'] = $form_state['values']['body_classes_to_add'];
  744. $form_state['handler']->conf['pipeline'] = $form_state['values']['pipeline'];
  745. $form_state['handler']->conf['css_id'] = $form_state['values']['css_id'];
  746. $form_state['handler']->conf['css'] = $form_state['values']['css'];
  747. $form_state['handler']->conf['title'] = $form_state['values']['title'];
  748. // Unset the old checkbox so we don't store needless data.
  749. if (isset($form_state['handler']->conf['use_ipe'])) {
  750. unset($form_state['handler']->conf['use_ipe']);
  751. }
  752. }
  753. /**
  754. * Form to show a nice preview.
  755. */
  756. function panels_panel_context_edit_preview($form, &$form_state) {
  757. ctools_include('context');
  758. ctools_include('context-task-handler');
  759. $contexts = ctools_context_handler_get_all_contexts($form_state['task'], $form_state['subtask'], $form_state['handler']);
  760. $form['preview'] = array();
  761. ctools_context_replace_form($form['preview'], $contexts);
  762. // Automatically preview if there are no argument placeholders.
  763. if (empty($form['preview'])) {
  764. $display = panels_panel_context_get_display($form_state['handler']);
  765. $display->context = $contexts;
  766. $display->skip_cache = TRUE;
  767. $output = panels_render_display($display);
  768. if (isset($form['buttons'])) {
  769. unset($form['buttons']);
  770. }
  771. }
  772. else {
  773. $form['preview']['#tree'] = TRUE;
  774. $form_state['contexts'] = $contexts;
  775. }
  776. if (!empty($output)) {
  777. $form['output'] = array(
  778. '#markup' => $output,
  779. );
  780. }
  781. $form_state['do not cache'] = TRUE;
  782. return $form;
  783. }
  784. /**
  785. * Display a preview upon submit if arguments were needed.
  786. */
  787. function panels_panel_context_edit_preview_submit(&$form, &$form_state) {
  788. $display = panels_panel_context_get_display($form_state['handler']);
  789. $display->context = ctools_context_replace_placeholders($form_state['contexts'], $form_state['values']['preview']);
  790. $form_state['content'] = panels_render_display($display);
  791. $form_state['redirect'] = FALSE;
  792. $form_state['rerender'] = TRUE;
  793. }
  794. function panels_panel_context_get_addressable($task, $subtask_name, $handler, $address, $contexts, $arguments, $type) {
  795. // Load the display.
  796. $display = panels_panel_context_get_display($handler);
  797. $display->context = $contexts;
  798. $display->args = $arguments;
  799. $display->css_id = $handler->conf['css_id'];
  800. $display->cache_key = panels_panel_context_cache_key($task->name, $handler->name, $arguments);
  801. $renderer = panels_get_renderer($handler->conf['pipeline'], $display);
  802. $renderer->prepare();
  803. if ($address) {
  804. $pid = array_shift($address);
  805. if (!empty($renderer->prepared['panes'][$pid])) {
  806. if ($type == 'content') {
  807. return $renderer->render_pane($renderer->prepared['panes'][$pid]);
  808. }
  809. elseif ($type == 'pane') {
  810. return $renderer->prepared['panes'][$pid];
  811. }
  812. }
  813. }
  814. else {
  815. if ($type == 'content') {
  816. return $renderer->render();
  817. }
  818. elseif ($type == 'renderer') {
  819. return $renderer;
  820. }
  821. }
  822. }