panel_context.inc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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. return $handler->conf['display'];
  213. }
  214. if (isset($handler->conf['did'])) {
  215. $handler->conf['display'] = panels_load_display($handler->conf['did']);
  216. // Check for a valid display. If no valid display can be loaded, something
  217. // is wrong and we'll create a new one.
  218. if (!empty($handler->conf['display'])) {
  219. return $handler->conf['display'];
  220. }
  221. }
  222. $handler->conf['display'] = panels_new_display();
  223. return $handler->conf['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($page->conf['temp_layout'])) {
  340. unset($page->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. // Grab the storage_type and storage_id and inject it into the display.
  523. if (empty($form_state['display']->storage_type)) {
  524. if (!isset($form_state[$form_state['task_id']]->storage_type)) {
  525. watchdog('panels', "Unable to find the storage type for specified storage. Read 'Upgrading task handlers' in CHANGELOG.txt", array(), WATCHDOG_ERROR);
  526. $form_state['display']->storage_type = 'unknown';
  527. }
  528. else {
  529. $form_state['display']->storage_type = $form_state[$form_state['task_id']]->storage_type;
  530. }
  531. // When adding variants, we don't know the handler id yet. In that case,
  532. // Mark it as new. We'll assign it later.
  533. $form_state['display']->storage_id = !empty($form_state['handler_id']) ? $form_state['handler_id'] : 'new';
  534. }
  535. // Tell the Panels form not to display buttons.
  536. $form_state['no buttons'] = TRUE;
  537. // Change the #id of the form so the CSS applies properly.
  538. $form['#id'] = 'panels-choose-layout';
  539. $form = panels_choose_layout($form, $form_state);
  540. return $form;
  541. }
  542. /**
  543. * Validate that a layout was chosen.
  544. */
  545. function panels_panel_context_edit_choose_validate(&$form, &$form_state) {
  546. if (empty($form_state['values']['layout'])) {
  547. form_error($form['layout'], t('You must select a layout.'));
  548. }
  549. }
  550. /**
  551. * A layout has been selected, set it up.
  552. */
  553. function panels_panel_context_edit_choose_submit(&$form, &$form_state) {
  554. $form_state['display']->layout = $form_state['values']['layout'];
  555. $form_state['handler']->conf['display'] = $form_state['display'];
  556. if (isset($form_state['page']->display_cache[$form_state['handler_id']])) {
  557. $form_state['page']->display_cache[$form_state['handler_id']]->display = $form_state['display'];
  558. }
  559. }
  560. /**
  561. * Change the layout for this panel.
  562. *
  563. * This form is only used if a layout already exists and the user wants
  564. * to change to a different one. The submit handler changes the next form
  565. * to the move content form, which is 'hidden' so it won't be accessed
  566. * directly.
  567. */
  568. function panels_panel_context_edit_layout($form, &$form_state) {
  569. ctools_include('common', 'panels');
  570. ctools_include('display-layout', 'panels');
  571. ctools_include('plugins', 'panels');
  572. // @todo -- figure out where/how to deal with this.
  573. $form_state['allowed_layouts'] = 'panels_page';
  574. $form_state['display'] = &panels_panel_context_get_display($form_state['handler']);
  575. // Tell the Panels form not to display buttons.
  576. $form_state['no buttons'] = TRUE;
  577. // Change the #id of the form so the CSS applies properly.
  578. $form['#id'] = 'panels-choose-layout';
  579. $form = panels_choose_layout($form, $form_state);
  580. return $form;
  581. }
  582. /**
  583. * Validate that a layout was chosen.
  584. */
  585. function panels_panel_context_edit_layout_validate(&$form, &$form_state) {
  586. $display = &panels_panel_context_get_display($form_state['handler']);
  587. if (empty($form_state['values']['layout'])) {
  588. form_error($form['layout'], t('You must select a layout.'));
  589. }
  590. if ($form_state['values']['layout'] == $display->layout) {
  591. form_error($form['layout'], t('You must select a different layout if you wish to change layouts.'));
  592. }
  593. }
  594. /**
  595. * A layout has been selected, set it up.
  596. */
  597. function panels_panel_context_edit_layout_submit(&$form, &$form_state) {
  598. $display = &panels_panel_context_get_display($form_state['handler']);
  599. if ($form_state['values']['layout'] != $display->layout) {
  600. $form_state['handler']->conf['temp_layout'] = $form_state['values']['layout'];
  601. }
  602. }
  603. /**
  604. * When a layout is changed, the user is given the opportunity to move content.
  605. */
  606. function panels_panel_context_edit_move($form, &$form_state) {
  607. $form_state['display'] = &panels_panel_context_get_display($form_state['handler']);
  608. $form_state['layout'] = $form_state['handler']->conf['temp_layout'];
  609. $form_state['cache_key'] = panels_panel_context_cache_key($form_state['task_name'], $form_state['handler_id'], array());
  610. ctools_include('common', 'panels');
  611. ctools_include('display-layout', 'panels');
  612. ctools_include('plugins', 'panels');
  613. // Tell the Panels form not to display buttons.
  614. $form_state['no buttons'] = TRUE;
  615. // Change the #id of the form so the CSS applies properly.
  616. $form = panels_change_layout($form, $form_state);
  617. // Change the 'back' button to just go directly to the previous form
  618. // $task_id = $form_state['task']['name'];
  619. // $handler_id = $form_state['handler']->handler;
  620. // $name = $form_state['handler']->name;
  621. // This form is outside the normal wizard list, so we need to specify the
  622. // previous/next forms.
  623. $form['buttons']['previous']['#next'] = 'layout';
  624. $form['buttons']['next']['#next'] = 'content';
  625. $form_state['form_info']['return path'] = page_manager_edit_url($form_state['page']->task_name, array('handlers', $form_state['handler_id'], 'content'));
  626. return $form;
  627. }
  628. /**
  629. * Present the panels drag & drop editor to edit the display attached
  630. * to the task handler.
  631. */
  632. function panels_panel_context_edit_content($form, &$form_state) {
  633. ctools_include('ajax');
  634. ctools_include('plugins', 'panels');
  635. ctools_include('common', 'panels');
  636. ctools_include('context');
  637. ctools_include('context-task-handler');
  638. $cache = panels_edit_cache_get(panels_panel_context_cache_key($form_state['task_name'], $form_state['handler_id'], array()));
  639. $form_state['renderer'] = panels_get_renderer_handler('editor', $cache->display);
  640. $form_state['renderer']->cache = &$cache;
  641. $form_state['display'] = &$cache->display;
  642. $form_state['content_types'] = $cache->content_types;
  643. // Tell the Panels form not to display buttons.
  644. $form_state['no buttons'] = TRUE;
  645. $form_state['display_title'] = !empty($cache->display_title);
  646. $form_state['no preview'] = TRUE;
  647. $form_state['page']->display_cache[$form_state['handler_id']] = $cache;
  648. $form = panels_edit_display_form($form, $form_state);
  649. if (!isset($form_state['type']) || $form_state['type'] != 'add' && !empty($form_state['handler_id']) && !empty($form['buttons'])) {
  650. $form['buttons']['preview'] = $form['buttons']['return'];
  651. $form['buttons']['preview']['#value'] = t('Update and preview');
  652. }
  653. return $form;
  654. }
  655. /**
  656. * Validate changes to the panel content form.
  657. */
  658. function panels_panel_context_edit_content_validate(&$form, &$form_state) {
  659. panels_edit_display_form_validate($form, $form_state);
  660. }
  661. function panels_panel_context_edit_content_submit(&$form, &$form_state) {
  662. // Update the storage_id if this is a new variant before saving.
  663. if ($form_state['display']->storage_id == 'new') {
  664. $form_state['display']->storage_id = $form_state['handler_id'];
  665. }
  666. panels_edit_display_form_submit($form, $form_state);
  667. $handler = &$form_state['handler'];
  668. // update the cached display:
  669. $display = $form_state['page']->display_cache[$form_state['handler_id']]->display;
  670. $handler->conf['display'] = $display;
  671. unset($form_state['page']->display_cache[$form_state['handler_id']]);
  672. if ($form_state['clicked_button']['#value'] == t('Update and preview')) {
  673. $form_state['new trail'] = array('handlers', $form_state['handler_id'], 'preview');
  674. }
  675. }
  676. /**
  677. * General settings for the panel
  678. */
  679. function panels_panel_context_edit_settings($form, &$form_state) {
  680. $conf = $form_state['handler']->conf;
  681. $form['conf']['title'] = array(
  682. '#type' => 'textfield',
  683. '#default_value' => $conf['title'],
  684. '#title' => t('Administrative title'),
  685. '#description' => t('Administrative title of this variant.'),
  686. );
  687. $form['conf']['no_blocks'] = array(
  688. '#type' => 'checkbox',
  689. '#default_value' => $conf['no_blocks'],
  690. '#title' => t('Disable Drupal blocks/regions'),
  691. '#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.'),
  692. );
  693. $form['conf']['body_classes_to_remove'] = array(
  694. '#type' => 'textfield',
  695. '#size' => 128,
  696. '#default_value' => empty($conf['body_classes_to_remove']) ? '' : $conf['body_classes_to_remove'],
  697. '#title' => t('Remove body CSS classes'),
  698. '#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.'),
  699. );
  700. $form['conf']['body_classes_to_add'] = array(
  701. '#type' => 'textfield',
  702. '#size' => 128,
  703. '#default_value' => empty($conf['body_classes_to_add']) ? '' : $conf['body_classes_to_add'],
  704. '#title' => t('Add body CSS classes'),
  705. '#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.'),
  706. );
  707. ctools_include('plugins', 'panels');
  708. $pipelines = panels_get_renderer_pipelines();
  709. // Handle backward compatibility with the IPE checkbox.
  710. if (empty($conf['pipeline'])) {
  711. $conf['pipeline'] = !empty($conf['use_ipe']) ? 'ipe' : 'standard';
  712. }
  713. // If there are no pipelines, that probably means we're operating in
  714. // legacy mode.
  715. if (empty($pipelines)) {
  716. // We retain the original pipeline so we don't wreck things by installing
  717. // old modules.
  718. $form['conf']['pipeline'] = array(
  719. '#type' => 'value',
  720. '#value' => $conf['pipeline'],
  721. );
  722. }
  723. else {
  724. $options = array();
  725. foreach ($pipelines as $name => $pipeline) {
  726. $options[$name] = check_plain($pipeline->admin_title) . '<div class="description">' . check_plain($pipeline->admin_description) . '</div>';
  727. }
  728. $form['conf']['pipeline'] = array(
  729. '#type' => 'radios',
  730. '#options' => $options,
  731. '#title' => t('Renderer'),
  732. '#default_value' => $conf['pipeline'],
  733. );
  734. }
  735. $form['conf']['css_id'] = array(
  736. '#type' => 'textfield',
  737. '#size' => 35,
  738. '#default_value' => $conf['css_id'],
  739. '#title' => t('CSS ID'),
  740. '#description' => t('The CSS ID to apply to this page. Keywords from context are allowed.'),
  741. );
  742. $form['conf']['css'] = array(
  743. '#type' => 'textarea',
  744. '#title' => t('CSS code'),
  745. '#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.'),
  746. '#default_value' => $conf['css'],
  747. );
  748. return $form;
  749. }
  750. /**
  751. * Submit handler for general settings form.
  752. */
  753. function panels_panel_context_edit_settings_submit(&$form, &$form_state) {
  754. $form_state['handler']->conf['no_blocks'] = $form_state['values']['no_blocks'];
  755. $form_state['handler']->conf['body_classes_to_remove'] = $form_state['values']['body_classes_to_remove'];
  756. $form_state['handler']->conf['body_classes_to_add'] = $form_state['values']['body_classes_to_add'];
  757. $form_state['handler']->conf['pipeline'] = $form_state['values']['pipeline'];
  758. $form_state['handler']->conf['css_id'] = $form_state['values']['css_id'];
  759. $form_state['handler']->conf['css'] = $form_state['values']['css'];
  760. $form_state['handler']->conf['title'] = $form_state['values']['title'];
  761. // Unset the old checkbox so we don't store needless data.
  762. if (isset($form_state['handler']->conf['use_ipe'])) {
  763. unset($form_state['handler']->conf['use_ipe']);
  764. }
  765. }
  766. /**
  767. * Form to show a nice preview.
  768. */
  769. function panels_panel_context_edit_preview($form, &$form_state) {
  770. ctools_include('context');
  771. ctools_include('context-task-handler');
  772. $contexts = ctools_context_handler_get_all_contexts($form_state['task'], $form_state['subtask'], $form_state['handler']);
  773. $form['preview'] = array();
  774. ctools_context_replace_form($form['preview'], $contexts);
  775. // automatically preview if there are no argument placeholders.
  776. if (empty($form['preview'])) {
  777. $display = panels_panel_context_get_display($form_state['handler']);
  778. $display->context = $contexts;
  779. $display->skip_cache = TRUE;
  780. $output = panels_render_display($display);
  781. if (isset($form['buttons'])) {
  782. unset($form['buttons']);
  783. }
  784. }
  785. else {
  786. $form['preview']['#tree'] = TRUE;
  787. $form_state['contexts'] = $contexts;
  788. }
  789. if (!empty($output)) {
  790. $form['output'] = array(
  791. '#markup' => $output,
  792. );
  793. }
  794. $form_state['do not cache'] = TRUE;
  795. return $form;
  796. }
  797. /**
  798. * Display a preview upon submit if arguments were needed.
  799. */
  800. function panels_panel_context_edit_preview_submit(&$form, &$form_state) {
  801. $display = panels_panel_context_get_display($form_state['handler']);
  802. $display->context = ctools_context_replace_placeholders($form_state['contexts'], $form_state['values']['preview']);
  803. $form_state['content'] = panels_render_display($display);
  804. $form_state['redirect'] = FALSE;
  805. $form_state['rerender'] = TRUE;
  806. }
  807. function panels_panel_context_get_addressable($task, $subtask_name, $handler, $address, $contexts, $arguments, $type) {
  808. // Load the display
  809. $display = panels_panel_context_get_display($handler);
  810. $display->context = $contexts;
  811. $display->args = $arguments;
  812. $display->css_id = $handler->conf['css_id'];
  813. $display->cache_key = panels_panel_context_cache_key($task->name, $handler->name, $arguments);
  814. $renderer = panels_get_renderer($handler->conf['pipeline'], $display);
  815. $renderer->prepare();
  816. if ($address) {
  817. $pid = array_shift($address);
  818. if (!empty($renderer->prepared['panes'][$pid])) {
  819. if ($type == 'content') {
  820. return $renderer->render_pane($renderer->prepared['panes'][$pid]);
  821. }
  822. elseif ($type == 'pane') {
  823. return $renderer->prepared['panes'][$pid];
  824. }
  825. }
  826. }
  827. else {
  828. if ($type == 'content') {
  829. return $renderer->render();
  830. }
  831. elseif ($type == 'renderer') {
  832. return $renderer;
  833. }
  834. }
  835. }