panel_context.inc 33 KB

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