page_manager.module 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. <?php
  2. /**
  3. * @file
  4. * The page manager module provides a UI and API to manage pages.
  5. *
  6. * It defines pages, both for system pages, overrides of system pages, and
  7. * custom pages using Drupal's normal menu system. It allows complex
  8. * manipulations of these pages, their content, and their hierarchy within
  9. * the site. These pages can be exported to code for superior revision
  10. * control.
  11. */
  12. /**
  13. * Bit flag on the 'changed' value to tell us if an item was moved.
  14. */
  15. define('PAGE_MANAGER_CHANGED_MOVED', 0x01);
  16. /**
  17. * Bit flag on the 'changed' value to tell us if an item edited or added.
  18. */
  19. define('PAGE_MANAGER_CHANGED_CACHED', 0x02);
  20. /**
  21. * Bit flag on the 'changed' value to tell us if an item deleted.
  22. */
  23. define('PAGE_MANAGER_CHANGED_DELETED', 0x04);
  24. /**
  25. * Bit flag on the 'changed' value to tell us if an item has had its disabled status changed.
  26. */
  27. define('PAGE_MANAGER_CHANGED_STATUS', 0x08);
  28. // --------------------------------------------------------------------------
  29. // Drupal hooks
  30. /**
  31. * Implements hook_permission().
  32. */
  33. function page_manager_permission() {
  34. return array(
  35. 'use page manager' => array(
  36. 'title' => t('Use Page Manager'),
  37. 'description' => t("Allows users to use most of Page Manager's features, though restricts some of the most powerful, potentially site-damaging features. Note that even the reduced featureset still allows for enormous control over your website."),
  38. 'restrict access' => TRUE,
  39. ),
  40. 'administer page manager' => array(
  41. 'title' => t('Administer Page Manager'),
  42. 'description' => t('Allows complete control over Page Manager, i.e., complete control over your site. Grant with extreme caution.'),
  43. 'restrict access' => TRUE,
  44. ),
  45. );
  46. }
  47. /**
  48. * Implements hook_ctools_plugin_directory() to let the system know
  49. * where our task and task_handler plugins are.
  50. */
  51. function page_manager_ctools_plugin_directory($owner, $plugin_type) {
  52. if ($owner == 'page_manager') {
  53. return 'plugins/' . $plugin_type;
  54. }
  55. if ($owner == 'ctools' && $plugin_type == 'cache') {
  56. return 'plugins/' . $plugin_type;
  57. }
  58. }
  59. /**
  60. * Implements hook_ctools_plugin_type() to inform the plugin system that Page
  61. * Manager owns task, task_handler, and page_wizard plugin types.
  62. *
  63. * All of these are empty because the defaults all work.
  64. */
  65. function page_manager_ctools_plugin_type() {
  66. return array(
  67. 'tasks' => array(),
  68. 'task_handlers' => array(),
  69. 'page_wizards' => array(),
  70. );
  71. }
  72. /**
  73. * Delegated implementation of hook_menu().
  74. */
  75. function page_manager_menu() {
  76. // For some reason, some things can activate modules without satisfying
  77. // dependencies. I don't know how, but this helps prevent things from
  78. // whitescreening when this happens.
  79. if (!module_exists('ctools')) {
  80. return;
  81. }
  82. $items = array();
  83. $base = array(
  84. 'access arguments' => array('use page manager'),
  85. 'file' => 'page_manager.admin.inc',
  86. 'theme callback' => 'ajax_base_page_theme',
  87. );
  88. $items['admin/structure/pages'] = array(
  89. 'title' => 'Pages',
  90. 'description' => 'Add, edit and remove overridden system pages and user defined pages from the system.',
  91. 'page callback' => 'page_manager_list_page',
  92. ) + $base;
  93. $items['admin/structure/pages/list'] = array(
  94. 'title' => 'List',
  95. 'page callback' => 'page_manager_list_page',
  96. 'type' => MENU_DEFAULT_LOCAL_TASK,
  97. 'weight' => -10,
  98. ) + $base;
  99. $items['admin/structure/pages/edit/%page_manager_cache'] = array(
  100. 'title' => 'Edit',
  101. 'page callback' => 'page_manager_edit_page',
  102. 'page arguments' => array(4),
  103. 'type' => MENU_NORMAL_ITEM,
  104. ) + $base;
  105. $items['admin/structure/pages/%ctools_js/operation/%page_manager_cache'] = array(
  106. 'page callback' => 'page_manager_edit_page_operation',
  107. 'page arguments' => array(3, 5),
  108. 'type' => MENU_NORMAL_ITEM,
  109. ) + $base;
  110. $items['admin/structure/pages/%ctools_js/enable/%page_manager_cache'] = array(
  111. 'page callback' => 'page_manager_enable_page',
  112. 'page arguments' => array(FALSE, 3, 5),
  113. 'type' => MENU_CALLBACK,
  114. ) + $base;
  115. $items['admin/structure/pages/%ctools_js/disable/%page_manager_cache'] = array(
  116. 'page callback' => 'page_manager_enable_page',
  117. 'page arguments' => array(TRUE, 3, 5),
  118. 'type' => MENU_CALLBACK,
  119. ) + $base;
  120. $tasks = page_manager_get_tasks();
  121. // Provide menu items for each task.
  122. foreach ($tasks as $task_id => $task) {
  123. // Allow the task to add its own menu items.
  124. if ($function = ctools_plugin_get_function($task, 'hook menu')) {
  125. $function($items, $task);
  126. }
  127. // And for those that provide subtasks, provide menu items for them, as well.
  128. foreach (page_manager_get_task_subtasks($task) as $subtask_id => $subtask) {
  129. // Allow the task to add its own menu items.
  130. if ($function = ctools_plugin_get_function($task, 'hook menu')) {
  131. $function($items, $subtask);
  132. }
  133. }
  134. }
  135. return $items;
  136. }
  137. function page_manager_admin_paths() {
  138. /* @todo FIX ME this is a major resource suck. */
  139. return;
  140. $items = array();
  141. ctools_include('page', 'page_manager', 'plugins/tasks');
  142. $pages = page_manager_page_load_all();
  143. foreach ($pages as $page) {
  144. // Make sure the page we're on is set to be an administrative path and that
  145. // it is not set to be a frontpage path.
  146. if ((isset($page->conf['admin_paths']) && $page->conf['admin_paths']) && (!isset($page->make_frontpage) || !$page->make_frontpage)) {
  147. $path_parts = explode('/', $page->path);
  148. foreach ($path_parts as $key => $part) {
  149. if (strpos($part, '%') !== FALSE || strpos($part, '!') !== FALSE) {
  150. $path_parts[$key] = '*';
  151. }
  152. }
  153. $path = implode('/', $path_parts);
  154. if ($page->menu['type'] == 'default tab') {
  155. array_pop($path_parts);
  156. $parent_path = implode('/', $path_parts);
  157. $items[$parent_path] = TRUE;
  158. }
  159. $items[$path] = TRUE;
  160. }
  161. }
  162. return $items;
  163. }
  164. /**
  165. * Implements hook_menu_alter.
  166. *
  167. * Get a list of all tasks and delegate to them.
  168. */
  169. function page_manager_menu_alter(&$items) {
  170. // For some reason, some things can activate modules without satisfying
  171. // dependencies. I don't know how, but this helps prevent things from
  172. // whitescreening when this happens.
  173. if (!module_exists('ctools')) {
  174. return;
  175. }
  176. $tasks = page_manager_get_tasks();
  177. foreach ($tasks as $task) {
  178. if ($function = ctools_plugin_get_function($task, 'hook menu alter')) {
  179. $function($items, $task);
  180. }
  181. // let the subtasks alter the menu items too.
  182. foreach (page_manager_get_task_subtasks($task) as $subtask_id => $subtask) {
  183. if ($function = ctools_plugin_get_function($subtask, 'hook menu alter')) {
  184. $function($items, $subtask);
  185. }
  186. }
  187. }
  188. // Override the core node revisions display to use the configured Page
  189. // display handler.
  190. if (!variable_get('page_manager_node_view_disabled', TRUE) && isset($items['node/%node/revisions/%/view'])) {
  191. // Abstract the basic settings.
  192. $item = array(
  193. // Handle the page arguments.
  194. 'load arguments' => array(3),
  195. 'page arguments' => array(1, TRUE),
  196. // Replace the normal node_show call with Page Manager's node view.
  197. 'page callback' => 'page_manager_node_view_page',
  198. // Provide the correct path to the Page Manager file.
  199. 'file' => 'node_view.inc',
  200. 'file path' => drupal_get_path('module', 'page_manager') . '/plugins/tasks',
  201. );
  202. // Re-build the menu item using the normal values from node.module.
  203. $items['node/%node/revisions/%/view'] = array(
  204. 'title' => 'Revisions',
  205. 'access callback' => '_node_revision_access',
  206. 'access arguments' => array(1),
  207. ) + $item;
  208. }
  209. return $items;
  210. }
  211. /*
  212. * Implements hook_theme()
  213. */
  214. function page_manager_theme() {
  215. // For some reason, some things can activate modules without satisfying
  216. // dependencies. I don't know how, but this helps prevent things from
  217. // whitescreening when this happens.
  218. if (!module_exists('ctools')) {
  219. return;
  220. }
  221. $base = array(
  222. 'path' => drupal_get_path('module', 'page_manager') . '/theme',
  223. 'file' => 'page_manager.theme.inc',
  224. );
  225. $items = array(
  226. 'page_manager_handler_rearrange' => array(
  227. 'render element' => 'form',
  228. ) + $base,
  229. 'page_manager_edit_page' => array(
  230. 'template' => 'page-manager-edit-page',
  231. 'variables' => array('page' => NULL, 'save' => NULL, 'operations' => array(), 'content' => array()),
  232. ) + $base,
  233. 'page_manager_lock' => array(
  234. 'variables' => array('page' => array()),
  235. ) + $base,
  236. 'page_manager_changed' => array(
  237. 'variables' => array('text' => NULL, 'description' => NULL),
  238. ) + $base,
  239. );
  240. // Allow task plugins to have theme registrations by passing through:
  241. $tasks = page_manager_get_tasks();
  242. // Provide menu items for each task.
  243. foreach ($tasks as $task_id => $task) {
  244. if ($function = ctools_plugin_get_function($task, 'hook theme')) {
  245. $function($items, $task);
  246. }
  247. }
  248. return $items;
  249. }
  250. // --------------------------------------------------------------------------
  251. // Page caching
  252. //
  253. // The page cache is used to store a page temporarily, using the ctools object
  254. // cache. When loading from the page cache, it will either load the cached
  255. // version, or if there is not one, load the real thing and create a cache
  256. // object which can then be easily stored.
  257. /**
  258. * Get the cached changes to a given task handler.
  259. */
  260. function page_manager_get_page_cache($task_name) {
  261. $caches = drupal_static(__FUNCTION__, array());
  262. if (!isset($caches[$task_name])) {
  263. ctools_include('object-cache');
  264. $cache = ctools_object_cache_get('page_manager_page', $task_name);
  265. if (!$cache) {
  266. $cache = new stdClass();
  267. $cache->task_name = $task_name;
  268. list($cache->task_id, $cache->subtask_id) = page_manager_get_task_id($cache->task_name);
  269. $cache->task = page_manager_get_task($cache->task_id);
  270. if (empty($cache->task)) {
  271. return FALSE;
  272. }
  273. if ($cache->subtask_id) {
  274. $cache->subtask = page_manager_get_task_subtask($cache->task, $cache->subtask_id);
  275. if (empty($cache->subtask)) {
  276. return FALSE;
  277. }
  278. }
  279. else {
  280. $cache->subtask = $cache->task;
  281. $cache->subtask['name'] = '';
  282. }
  283. $cache->handlers = page_manager_load_sorted_handlers($cache->task, $cache->subtask_id);
  284. $cache->handler_info = array();
  285. foreach ($cache->handlers as $id => $handler) {
  286. $cache->handler_info[$id] = array(
  287. 'weight' => $handler->weight,
  288. 'changed' => FALSE,
  289. 'name' => $id,
  290. );
  291. }
  292. }
  293. else {
  294. // ensure the task is loaded.
  295. page_manager_get_task($cache->task_id);
  296. }
  297. if ($task_name != '::new') {
  298. $cache->locked = ctools_object_cache_test('page_manager_page', $task_name);
  299. }
  300. else {
  301. $cache->locked = FALSE;
  302. }
  303. $caches[$task_name] = $cache;
  304. }
  305. return $caches[$task_name];
  306. }
  307. /**
  308. * Store changes to a task handler in the object cache.
  309. */
  310. function page_manager_set_page_cache($page) {
  311. if (!empty($page->locked)) {
  312. return;
  313. }
  314. if (empty($page->task_name)) {
  315. return;
  316. }
  317. ctools_include('object-cache');
  318. $page->changed = TRUE;
  319. $cache = ctools_object_cache_set('page_manager_page', $page->task_name, $page);
  320. }
  321. /**
  322. * Remove an item from the object cache.
  323. */
  324. function page_manager_clear_page_cache($name) {
  325. ctools_include('object-cache');
  326. ctools_object_cache_clear('page_manager_page', $name);
  327. }
  328. /**
  329. * Write all changes from the page cache and clear it out.
  330. */
  331. function page_manager_save_page_cache($cache) {
  332. // Save the subtask:
  333. if ($function = ctools_plugin_get_function($cache->task, 'save subtask callback')) {
  334. $function($cache->subtask, $cache);
  335. }
  336. // Iterate through handlers and save/delete/update as necessary.
  337. // Go through each of the task handlers, check to see if it needs updating,
  338. // and update it if so.
  339. foreach ($cache->handler_info as $id => $info) {
  340. $handler = &$cache->handlers[$id];
  341. // If it has been marked for deletion, delete it.
  342. if ($info['changed'] & PAGE_MANAGER_CHANGED_DELETED) {
  343. page_manager_delete_task_handler($handler);
  344. }
  345. // If it has been somehow edited (or added), write the cached version
  346. elseif ($info['changed'] & PAGE_MANAGER_CHANGED_CACHED) {
  347. // Make sure we get updated weight from the form for this.
  348. $handler->weight = $info['weight'];
  349. page_manager_save_task_handler($handler);
  350. }
  351. // Otherwise, check to see if it has moved and, if so, update the weight.
  352. elseif ($info['weight'] != $handler->weight) {
  353. // Theoretically we could only do this for in code objects, but since our
  354. // load mechanism checks for all, this is less database work.
  355. page_manager_update_task_handler_weight($handler, $info['weight']);
  356. }
  357. // Set enable/disabled status.
  358. if ($info['changed'] & PAGE_MANAGER_CHANGED_STATUS) {
  359. ctools_include('export');
  360. ctools_export_set_object_status($cache->handlers[$id], $info['disabled']);
  361. }
  362. }
  363. page_manager_clear_page_cache($cache->task_name);
  364. if (!empty($cache->path_changed) || !empty($cache->new)) {
  365. // Force a menu rebuild to make sure the menu entries are set.
  366. menu_rebuild();
  367. }
  368. cache_clear_all();
  369. }
  370. /**
  371. * Menu callback to load a page manager cache object for menu callbacks.
  372. */
  373. function page_manager_cache_load($task_name) {
  374. // load context plugin as there may be contexts cached here.
  375. ctools_include('context');
  376. return page_manager_get_page_cache($task_name);
  377. }
  378. /**
  379. * Generate a unique name for a task handler.
  380. *
  381. * Task handlers need to be named but they aren't allowed to set their own
  382. * names. Instead, they are named based upon their parent task and type.
  383. */
  384. function page_manager_handler_get_name($task_name, $handlers, $handler) {
  385. $base = str_replace('-', '_', $task_name);
  386. // Optional machine name.
  387. if (!empty($handler->conf['name'])) {
  388. $name = $base . '__' . $handler->conf['name'];
  389. }
  390. // If no machine name was provided, generate a unique name.
  391. else {
  392. $base .= '__' . $handler->handler;
  393. // Use the ctools uuid generator to generate a unique id.
  394. $name = $base . '_' . ctools_uuid_generate();
  395. }
  396. return $name;
  397. }
  398. /**
  399. * Import a handler into a page.
  400. *
  401. * This is used by both import and clone, since clone just exports the
  402. * handler and immediately imports it.
  403. */
  404. function page_manager_handler_add_to_page(&$page, &$handler, $title = NULL) {
  405. $last = end($page->handler_info);
  406. $handler->weight = $last ? $last['weight'] + 1 : 0;
  407. $handler->task = $page->task_id;
  408. $handler->subtask = $page->subtask_id;
  409. $handler->export_type = EXPORT_IN_DATABASE;
  410. $handler->type = t('Normal');
  411. if ($title) {
  412. $handler->conf['title'] = $title;
  413. }
  414. $name = page_manager_handler_get_name($page->task_name, $page->handlers, $handler);
  415. $handler->name = $name;
  416. $page->handlers[$name] = $handler;
  417. $page->handler_info[$name] = array(
  418. 'weight' => $handler->weight,
  419. 'name' => $handler->name,
  420. 'changed' => PAGE_MANAGER_CHANGED_CACHED,
  421. );
  422. }
  423. // --------------------------------------------------------------------------
  424. // Database routines
  425. //
  426. // This includes fetching plugins and plugin info as well as specialized
  427. // fetch methods to get groups of task handlers per task.
  428. /**
  429. * Load a single task handler by name.
  430. *
  431. * Handlers can come from multiple sources; either the database or by normal
  432. * export method, which is handled by the ctools library, but handlers can
  433. * also be bundled with task/subtask. We have to check there and perform
  434. * overrides as appropriate.
  435. *
  436. * Handlers bundled with the task are of a higher priority than default
  437. * handlers provided by normal code, and are of a lower priority than
  438. * the database, so we have to check the source of handlers when we have
  439. * multiple to choose from.
  440. */
  441. function page_manager_load_task_handler($task, $subtask_id, $name) {
  442. ctools_include('export');
  443. $result = ctools_export_load_object('page_manager_handlers', 'names', array($name));
  444. $handlers = page_manager_get_default_task_handlers($task, $subtask_id);
  445. return page_manager_compare_task_handlers($result, $handlers, $name);
  446. }
  447. /**
  448. * Load all task handlers for a given task/subtask.
  449. */
  450. function page_manager_load_task_handlers($task, $subtask_id = NULL, $default_handlers = NULL) {
  451. ctools_include('export');
  452. $conditions = array(
  453. 'task' => $task['name'],
  454. );
  455. if (isset($subtask_id)) {
  456. $conditions['subtask'] = $subtask_id;
  457. }
  458. $handlers = ctools_export_load_object('page_manager_handlers', 'conditions', $conditions);
  459. $defaults = isset($default_handlers) ? $default_handlers : page_manager_get_default_task_handlers($task, $subtask_id);
  460. foreach ($defaults as $name => $default) {
  461. $result = page_manager_compare_task_handlers($handlers, $defaults, $name);
  462. if ($result) {
  463. $handlers[$name] = $result;
  464. // Ensure task and subtask are correct, because it's easy to change task
  465. // names when editing a default and fail to do it on the associated handlers.
  466. $result->task = $task['name'];
  467. $result->subtask = $subtask_id;
  468. }
  469. }
  470. // Override weights from the weight table.
  471. if ($handlers) {
  472. $names = array();
  473. $placeholders = array();
  474. foreach ($handlers as $handler) {
  475. $names[] = $handler->name;
  476. $placeholders[] = "'%s'";
  477. }
  478. $result = db_query('SELECT name, weight FROM {page_manager_weights} WHERE name IN (:names)', array(':names' => $names));
  479. foreach ($result as $weight) {
  480. $handlers[$weight->name]->weight = $weight->weight;
  481. }
  482. }
  483. return $handlers;
  484. }
  485. /**
  486. * Get the default task handlers from a task, if they exist.
  487. *
  488. * Tasks can contain 'default' task handlers which are provided by the
  489. * default task. Because these can come from either the task or the
  490. * subtask, the logic is abstracted to reduce code duplication.
  491. */
  492. function page_manager_get_default_task_handlers($task, $subtask_id) {
  493. // Load default handlers that are provied by the task/subtask itself.
  494. $handlers = array();
  495. if ($subtask_id) {
  496. $subtask = page_manager_get_task_subtask($task, $subtask_id);
  497. if (isset($subtask['default handlers'])) {
  498. $handlers = $subtask['default handlers'];
  499. }
  500. }
  501. else if (isset($task['default handlers'])) {
  502. $handlers = $task['default handlers'];
  503. }
  504. return $handlers;
  505. }
  506. /**
  507. * Compare a single task handler from two lists and provide the correct one.
  508. *
  509. * Task handlers can be gotten from multiple sources. As exportable objects,
  510. * they can be provided by default hooks and the database. But also, because
  511. * they are tightly bound to tasks, they can also be provided by default
  512. * tasks. This function reconciles where to pick up a task handler between
  513. * the exportables list and the defaults provided by the task itself.
  514. *
  515. * @param $result
  516. * A list of handlers provided by export.inc
  517. * @param $handlers
  518. * A list of handlers provided by the default task.
  519. * @param $name
  520. * Which handler to compare.
  521. * @return
  522. * Which handler to use, if any. May be NULL.
  523. */
  524. function page_manager_compare_task_handlers($result, $handlers, $name) {
  525. // Compare our special default handler against the actual result, if
  526. // any, and do the right thing.
  527. if (!isset($result[$name]) && isset($handlers[$name])) {
  528. $handlers[$name]->type = t('Default');
  529. $handlers[$name]->export_type = EXPORT_IN_CODE;
  530. return $handlers[$name];
  531. }
  532. else if (isset($result[$name]) && !isset($handlers[$name])) {
  533. return $result[$name];
  534. }
  535. else if (isset($result[$name]) && isset($handlers[$name])) {
  536. if ($result[$name]->export_type & EXPORT_IN_DATABASE) {
  537. $result[$name]->type = t('Overridden');
  538. $result[$name]->export_type = $result[$name]->export_type | EXPORT_IN_CODE;
  539. return $result[$name];
  540. }
  541. else {
  542. // In this case, our default is a higher priority than the standard default.
  543. $handlers[$name]->type = t('Default');
  544. $handlers[$name]->export_type = EXPORT_IN_CODE;
  545. return $handlers[$name];
  546. }
  547. }
  548. }
  549. /**
  550. * Load all task handlers for a given task and subtask and sort them.
  551. */
  552. function page_manager_load_sorted_handlers($task, $subtask_id = NULL, $enabled = FALSE) {
  553. $handlers = page_manager_load_task_handlers($task, $subtask_id);
  554. if ($enabled) {
  555. foreach ($handlers as $id => $handler) {
  556. if (!empty($handler->disabled)) {
  557. unset($handlers[$id]);
  558. }
  559. }
  560. }
  561. uasort($handlers, 'page_manager_sort_task_handlers');
  562. return $handlers;
  563. }
  564. /**
  565. * Callback for uasort to sort task handlers.
  566. *
  567. * Task handlers are sorted by weight then by name.
  568. */
  569. function page_manager_sort_task_handlers($a, $b) {
  570. if ($a->weight < $b->weight) {
  571. return -1;
  572. }
  573. elseif ($a->weight > $b->weight) {
  574. return 1;
  575. }
  576. elseif ($a->name < $b->name) {
  577. return -1;
  578. }
  579. elseif ($a->name > $b->name) {
  580. return 1;
  581. }
  582. return 0;
  583. }
  584. /**
  585. * Write a task handler to the database.
  586. */
  587. function page_manager_save_task_handler(&$handler) {
  588. $update = (isset($handler->did)) ? array('did') : array();
  589. // Let the task handler respond to saves:
  590. if ($function = ctools_plugin_load_function('page_manager', 'task_handlers', $handler->handler, 'save')) {
  591. $function($handler, $update);
  592. }
  593. drupal_write_record('page_manager_handlers', $handler, $update);
  594. db_delete('page_manager_weights')
  595. ->condition('name', $handler->name)
  596. ->execute();
  597. // If this was previously a default handler, we may have to write task handlers.
  598. if (!$update) {
  599. // @todo wtf was I going to do here?
  600. }
  601. return $handler;
  602. }
  603. /**
  604. * Remove a task handler.
  605. */
  606. function page_manager_delete_task_handler($handler) {
  607. // Let the task handler respond to saves:
  608. if ($function = ctools_plugin_load_function('page_manager', 'task_handlers', $handler->handler, 'delete')) {
  609. $function($handler);
  610. }
  611. db_delete('page_manager_handlers')
  612. ->condition('name', $handler->name)
  613. ->execute();
  614. db_delete('page_manager_weights')
  615. ->condition('name', $handler->name)
  616. ->execute();
  617. }
  618. /**
  619. * Export a task handler into code suitable for import or use as a default
  620. * task handler.
  621. */
  622. function page_manager_export_task_handler($handler, $indent = '') {
  623. ctools_include('export');
  624. ctools_include('plugins');
  625. $handler = clone $handler;
  626. $append = '';
  627. if ($function = ctools_plugin_load_function('page_manager', 'task_handlers', $handler->handler, 'export')) {
  628. $append = $function($handler, $indent);
  629. }
  630. $output = ctools_export_object('page_manager_handlers', $handler, $indent);
  631. $output .= $append;
  632. return $output;
  633. }
  634. /**
  635. * Loads page manager handler for export.
  636. *
  637. * Callback to load page manager handler within ctools_export_crud_load().
  638. *
  639. * @param string $name
  640. * The name of the handler to load.
  641. *
  642. * @return
  643. * Loaded page manager handler object, extended with external properties.
  644. */
  645. function page_manager_export_task_handler_load($name) {
  646. $table = 'page_manager_handlers';
  647. $schema = ctools_export_get_schema($table);
  648. $export = $schema['export'];
  649. $result = ctools_export_load_object($table, 'names', array($name));
  650. if (isset($result[$name])) {
  651. $handler = $result[$name];
  652. // Weight is stored in additional table so that in-code task handlers
  653. // don't need to get written to the database just because they have their
  654. // weight changed. Therefore, handler could have no correspondent database
  655. // entry. Revert will not be performed for this handler and the weight
  656. // will not be reverted. To make possible revert of the weight field
  657. // export_type must simulate that the handler is stored in the database.
  658. $handler->export_type = EXPORT_IN_DATABASE;
  659. // Also, page manager handler weight should be overriden with correspondent
  660. // weight from page_manager_weights table, if there is one.
  661. $result = db_query('SELECT weight FROM {page_manager_weights} WHERE name = (:names)', array(':names' => $handler->name))->fetchField();
  662. if (is_numeric($result)) {
  663. $handler->weight = $result;
  664. }
  665. return $handler;
  666. }
  667. }
  668. /**
  669. * Create a new task handler object.
  670. *
  671. * @param $plugin
  672. * The plugin this task handler is created from.
  673. */
  674. function page_manager_new_task_handler($plugin) {
  675. // Generate a unique name. Unlike most named objects, we don't let people choose
  676. // names for task handlers because they mostly don't make sense.
  677. // Create a new, empty handler object.
  678. $handler = new stdClass;
  679. $handler->title = $plugin['title'];
  680. $handler->task = NULL;
  681. $handler->subtask = NULL;
  682. $handler->name = NULL;
  683. $handler->handler = $plugin['name'];
  684. $handler->weight = 0;
  685. $handler->conf = array();
  686. // These are provided by the core export API provided by ctools and we
  687. // set defaults here so that we don't cause notices. Perhaps ctools should
  688. // provide a way to do this for us so we don't have to muck with it.
  689. $handler->export_type = EXPORT_IN_DATABASE;
  690. $handler->type = t('Local');
  691. if (isset($plugin['default conf'])) {
  692. if (is_array($plugin['default conf'])) {
  693. $handler->conf = $plugin['default conf'];
  694. }
  695. else if (function_exists($plugin['default conf'])) {
  696. $handler->conf = $plugin['default conf']($handler);
  697. }
  698. }
  699. return $handler;
  700. }
  701. /**
  702. * Set an overidden weight for a task handler.
  703. *
  704. * We do this so that in-code task handlers don't need to get written
  705. * to the database just because they have their weight changed.
  706. */
  707. function page_manager_update_task_handler_weight($handler, $weight) {
  708. db_delete('page_manager_weights')
  709. ->condition('name', $handler->name)
  710. ->execute();
  711. db_insert('page_manager_weights')
  712. ->fields(array(
  713. 'name' => $handler->name,
  714. 'weight' => $weight,
  715. ))
  716. ->execute();
  717. }
  718. /**
  719. * Shortcut function to get task plugins.
  720. */
  721. function page_manager_get_tasks() {
  722. ctools_include('plugins');
  723. return ctools_get_plugins('page_manager', 'tasks');
  724. }
  725. /**
  726. * Shortcut function to get a task plugin.
  727. */
  728. function page_manager_get_task($id) {
  729. ctools_include('plugins');
  730. return ctools_get_plugins('page_manager', 'tasks', $id);
  731. }
  732. /**
  733. * Get all tasks for a given type.
  734. */
  735. function page_manager_get_tasks_by_type($type) {
  736. ctools_include('plugins');
  737. $all_tasks = ctools_get_plugins('page_manager', 'tasks');
  738. $tasks = array();
  739. foreach ($all_tasks as $id => $task) {
  740. if (isset($task['task type']) && $task['task type'] == $type) {
  741. $tasks[$id] = $task;
  742. }
  743. }
  744. return $tasks;
  745. }
  746. /**
  747. * Fetch all subtasks for a page managertask.
  748. *
  749. * @param $task
  750. * A loaded $task plugin object.
  751. */
  752. function page_manager_get_task_subtasks($task) {
  753. if (empty($task['subtasks'])) {
  754. return array();
  755. }
  756. if ($function = ctools_plugin_get_function($task, 'subtasks callback')) {
  757. $retval = $function($task);
  758. if (is_array($retval)) {
  759. return $retval;
  760. }
  761. }
  762. return array();
  763. }
  764. /**
  765. * Fetch all subtasks for a page managertask.
  766. *
  767. * @param $task
  768. * A loaded $task plugin object.
  769. * @param $subtask_id
  770. * The subtask ID to load.
  771. */
  772. function page_manager_get_task_subtask($task, $subtask_id) {
  773. if (empty($task['subtasks'])) {
  774. return;
  775. }
  776. if ($function = ctools_plugin_get_function($task, 'subtask callback')) {
  777. return $function($task, $subtask_id);
  778. }
  779. }
  780. /**
  781. * Shortcut function to get task handler plugins.
  782. */
  783. function page_manager_get_task_handlers() {
  784. ctools_include('plugins');
  785. return ctools_get_plugins('page_manager', 'task_handlers');
  786. }
  787. /**
  788. * Shortcut function to get a task handler plugin.
  789. */
  790. function page_manager_get_task_handler($id) {
  791. ctools_include('plugins');
  792. return ctools_get_plugins('page_manager', 'task_handlers', $id);
  793. }
  794. /**
  795. * Retrieve a list of all applicable task handlers for a given task.
  796. *
  797. * This looks at the $task['handler type'] and compares that to $task_handler['handler type'].
  798. * If the task has no type, the id of the task is used instead.
  799. */
  800. function page_manager_get_task_handler_plugins($task, $all = FALSE) {
  801. $type = isset($task['handler type']) ? $task['handler type'] : $task['name'];
  802. $name = $task['name'];
  803. $handlers = array();
  804. $task_handlers = page_manager_get_task_handlers();
  805. foreach ($task_handlers as $id => $handler) {
  806. $task_type = is_array($handler['handler type']) ? $handler['handler type'] : array($handler['handler type']);
  807. if (in_array($type, $task_type) || in_array($name, $task_type)) {
  808. if ($all || !empty($handler['visible'])) {
  809. $handlers[$id] = $handler;
  810. }
  811. }
  812. }
  813. return $handlers;
  814. }
  815. /**
  816. * Get the title for a given handler.
  817. *
  818. * If the plugin has no 'admin title' function, the generic title of the
  819. * plugin is used instead.
  820. */
  821. function page_manager_get_handler_title($plugin, $handler, $task, $subtask_id) {
  822. $function = ctools_plugin_get_function($plugin, 'admin title');
  823. if ($function) {
  824. return $function($handler, $task, $subtask_id);
  825. }
  826. else {
  827. return $plugin['title'];
  828. }
  829. }
  830. /**
  831. * Get the admin summary (additional info) for a given handler.
  832. */
  833. function page_manager_get_handler_summary($plugin, $handler, $page, $title = TRUE) {
  834. if ($function = ctools_plugin_get_function($plugin, 'admin summary')) {
  835. return $function($handler, $page->task, $page->subtask, $page, $title);
  836. }
  837. }
  838. /**
  839. * Get the admin summary (additional info) for a given page.
  840. */
  841. function page_manager_get_page_summary($task, $subtask) {
  842. if ($function = ctools_plugin_get_function($subtask, 'admin summary')) {
  843. return $function($task, $subtask);
  844. }
  845. }
  846. /**
  847. * Split a task name into a task id and subtask id, if applicable.
  848. */
  849. function page_manager_get_task_id($task_name) {
  850. if (strpos($task_name, '-') !== FALSE) {
  851. return explode('-', $task_name, 2);
  852. }
  853. else {
  854. return array($task_name, NULL);
  855. }
  856. }
  857. /**
  858. * Turn a task id + subtask_id into a task name.
  859. */
  860. function page_manager_make_task_name($task_id, $subtask_id) {
  861. if ($subtask_id) {
  862. return $task_id . '-' . $subtask_id;
  863. }
  864. else {
  865. return $task_id;
  866. }
  867. }
  868. /**
  869. * Get the render function for a handler.
  870. */
  871. function page_manager_get_renderer($handler) {
  872. return ctools_plugin_load_function('page_manager', 'task_handlers', $handler->handler, 'render');
  873. }
  874. // --------------------------------------------------------------------------
  875. // Functions existing on behalf of tasks and task handlers
  876. /**
  877. * Page manager arg load function because menu system will not load extra
  878. * files for these; they must be in a .module.
  879. */
  880. function pm_arg_load($value, $subtask, $argument) {
  881. page_manager_get_task('page');
  882. return _pm_arg_load($value, $subtask, $argument);
  883. }
  884. /**
  885. * Special arg_load function to use %menu_tail like functionality to
  886. * get everything after the arg together as a single value.
  887. */
  888. function pm_arg_tail_load($value, $subtask, $argument, $map) {
  889. $value = implode('/', array_slice($map, $argument));
  890. page_manager_get_task('page');
  891. return _pm_arg_load($value, $subtask, $argument);
  892. }
  893. /**
  894. * Special menu _load() function for the user:uid argument.
  895. *
  896. * This is just the normal page manager argument. It only exists so that
  897. * the to_arg can exist.
  898. */
  899. function pm_uid_arg_load($value, $subtask, $argument) {
  900. page_manager_get_task('page');
  901. return _pm_arg_load($value, $subtask, $argument);
  902. }
  903. /**
  904. * to_arg function for the user:uid argument to provide the arg for the
  905. * current global user.
  906. */
  907. function pm_uid_arg_to_arg($arg) {
  908. return user_uid_optional_to_arg($arg);
  909. }
  910. /**
  911. * Callback for access control ajax form on behalf of page.inc task.
  912. *
  913. * Returns the cached access config and contexts used.
  914. */
  915. function page_manager_page_ctools_access_get($argument) {
  916. $page = page_manager_get_page_cache($argument);
  917. $contexts = array();
  918. // Load contexts based on argument data:
  919. if ($arguments = _page_manager_page_get_arguments($page->subtask['subtask'])) {
  920. $contexts = ctools_context_get_placeholders_from_argument($arguments);
  921. }
  922. return array($page->subtask['subtask']->access, $contexts);
  923. }
  924. /**
  925. * Callback for access control ajax form on behalf of page.inc task.
  926. *
  927. * Writes the changed access to the cache.
  928. */
  929. function page_manager_page_ctools_access_set($argument, $access) {
  930. $page = page_manager_get_page_cache($argument);
  931. $page->subtask['subtask']->access = $access;
  932. page_manager_set_page_cache($page);
  933. }
  934. /**
  935. * Callback for access control ajax form on behalf of context task handler.
  936. *
  937. * Returns the cached access config and contexts used.
  938. */
  939. function page_manager_task_handler_ctools_access_get($argument) {
  940. list($task_name, $name) = explode('*', $argument);
  941. $page = page_manager_get_page_cache($task_name);
  942. if (empty($name)) {
  943. $handler = &$page->new_handler;
  944. }
  945. else {
  946. $handler = &$page->handlers[$name];
  947. }
  948. if (!isset($handler->conf['access'])) {
  949. $handler->conf['access'] = array();
  950. }
  951. ctools_include('context-task-handler');
  952. $contexts = ctools_context_handler_get_all_contexts($page->task, $page->subtask, $handler);
  953. return array($handler->conf['access'], $contexts);
  954. }
  955. /**
  956. * Callback for access control ajax form on behalf of context task handler.
  957. *
  958. * Writes the changed access to the cache.
  959. */
  960. function page_manager_task_handler_ctools_access_set($argument, $access) {
  961. list($task_name, $name) = explode('*', $argument);
  962. $page = page_manager_get_page_cache($task_name);
  963. if (empty($name)) {
  964. $handler = &$page->new_handler;
  965. }
  966. else {
  967. $handler = &$page->handlers[$name];
  968. }
  969. $handler->conf['access'] = $access;
  970. page_manager_set_page_cache($page);
  971. }
  972. /**
  973. * Form a URL to edit a given page given the trail.
  974. */
  975. function page_manager_edit_url($task_name, $trail = array()) {
  976. if (!is_array($trail)) {
  977. $trail = array($trail);
  978. }
  979. if (empty($trail) || $trail == array('summary')) {
  980. return "admin/structure/pages/edit/$task_name";
  981. }
  982. return 'admin/structure/pages/nojs/operation/' . $task_name . '/' . implode('/', $trail);
  983. }
  984. /**
  985. * Watch menu links during the menu rebuild, and re-parent things if we need to.
  986. */
  987. function page_manager_menu_link_alter(&$item) {
  988. return;
  989. /** -- disabled, concept code --
  990. static $mlids = array();
  991. // Keep an array of mlids as links are saved that we can use later.
  992. if (isset($item['mlid'])) {
  993. $mlids[$item['path']] = $item['mlid'];
  994. }
  995. if (isset($item['parent_path'])) {
  996. if (isset($mlids[$item['parent_path']])) {
  997. $item['plid'] = $mlids[$item['parent_path']];
  998. }
  999. else {
  1000. // Since we didn't already see an mlid, let's check the database for one.
  1001. $mlid = db_query('SELECT mlid FROM {menu_links} WHERE router_path = :path', array('path' => $item['parent_path']))->fetchField();
  1002. if ($mlid) {
  1003. $item['plid'] = $mlid;
  1004. }
  1005. }
  1006. }
  1007. */
  1008. }
  1009. /**
  1010. * Callback to list handlers available for export.
  1011. */
  1012. function page_manager_page_manager_handlers_list() {
  1013. $list = $types = array();
  1014. $tasks = page_manager_get_tasks();
  1015. foreach ($tasks as $type => $info) {
  1016. if (empty($info['non-exportable'])) {
  1017. $types[] = $type;
  1018. }
  1019. }
  1020. $handlers = ctools_export_load_object('page_manager_handlers');
  1021. foreach ($handlers as $handler) {
  1022. if (in_array($handler->task, $types)) {
  1023. $plugin = page_manager_get_task_handler($handler->handler);
  1024. $title = page_manager_get_handler_title($plugin, $handler, $tasks[$handler->task], $handler->subtask);
  1025. if ($title) {
  1026. $list[$handler->name] = check_plain("$handler->task: $title ($handler->name)");
  1027. }
  1028. else {
  1029. $list[$handler->name] = check_plain("$handler->task: ($handler->name)");
  1030. }
  1031. }
  1032. }
  1033. return $list;
  1034. }
  1035. /**
  1036. * Callback to bulk export page manager pages.
  1037. */
  1038. function page_manager_page_manager_pages_to_hook_code($names = array(), $name = 'foo') {
  1039. $schema = ctools_export_get_schema('page_manager_pages');
  1040. $export = $schema['export'];
  1041. $objects = ctools_export_load_object('page_manager_pages', 'names', array_values($names));
  1042. if ($objects) {
  1043. $code = "/**\n";
  1044. $code .= " * Implements hook_{$export['default hook']}()\n";
  1045. $code .= " */\n";
  1046. $code .= "function " . $name . "_{$export['default hook']}() {\n";
  1047. foreach ($objects as $object) {
  1048. // Have to implement our own because this export func sig requires it
  1049. $code .= $export['export callback']($object, TRUE, ' ');
  1050. $code .= " \${$export['identifier']}s['" . check_plain($object->$export['key']) . "'] = \${$export['identifier']};\n\n";
  1051. }
  1052. $code .= " return \${$export['identifier']}s;\n";
  1053. $code .= "}\n";
  1054. return $code;
  1055. }
  1056. }
  1057. /**
  1058. * Get the current page information.
  1059. *
  1060. * @return $page
  1061. * An array containing the following information.
  1062. *
  1063. * - 'name': The name of the page as used in the page manager admin UI.
  1064. * - 'task': The plugin for the task in use. If this is a system page it
  1065. * will contain information about that page, such as what functions
  1066. * it uses.
  1067. * - 'subtask': The plugin for the subtask. If this is a custom page, this
  1068. * will contain information about that custom page. See 'subtask' in this
  1069. * array to get the actual page object.
  1070. * - 'handler': The actual handler object used. If using panels, see
  1071. * $page['handler']->conf['display'] for the actual panels display
  1072. * used to render.
  1073. * - 'contexts': The context objects used to render this page.
  1074. * - 'arguments': The raw arguments from the URL used on this page.
  1075. */
  1076. function page_manager_get_current_page($page = NULL) {
  1077. static $current = array();
  1078. if (isset($page)) {
  1079. $current = $page;
  1080. }
  1081. return $current;
  1082. }
  1083. /**
  1084. * Implementation of hook_panels_dashboard_blocks().
  1085. *
  1086. * Adds page information to the Panels dashboard.
  1087. */
  1088. function page_manager_panels_dashboard_blocks(&$vars) {
  1089. $vars['links']['page_manager'] = array(
  1090. 'weight' => -100,
  1091. 'title' => l(t('Panel page'), 'admin/structure/pages/add'),
  1092. 'description' => t('Panel pages can be used as landing pages. They have a URL path, accept arguments and can have menu entries.'),
  1093. );
  1094. module_load_include('inc', 'page_manager', 'page_manager.admin');
  1095. $tasks = page_manager_get_tasks_by_type('page');
  1096. $pages = array('operations' => array());
  1097. page_manager_get_pages($tasks, $pages);
  1098. $count = 0;
  1099. $rows = array();
  1100. foreach ($pages['rows'] as $id => $info) {
  1101. $rows[] = array(
  1102. 'data' => array(
  1103. $info['data']['title'],
  1104. $info['data']['operations'],
  1105. ),
  1106. 'class' => $info['class'],
  1107. );
  1108. // Only show 10.
  1109. if (++$count >= 10) {
  1110. break;
  1111. }
  1112. }
  1113. $vars['blocks']['page_manager'] = array(
  1114. 'weight' => -100,
  1115. 'title' => t('Manage pages'),
  1116. 'link' => l(t('Go to list'), 'admin/structure/pages'),
  1117. 'content' => theme('table', array('header' => array(), 'rows' => $rows, 'attributes' => array('class' => 'panels-manage'))),
  1118. 'class' => 'dashboard-pages',
  1119. 'section' => 'right',
  1120. );
  1121. }
  1122. /**
  1123. * Implement pseudo-hook to fetch addressable content.
  1124. *
  1125. * For Page Manager, the address will be an array. The first
  1126. * element will be the $task and the second element will be the
  1127. * $task_handler. The third elements will be the arguments
  1128. * provided.
  1129. */
  1130. function page_manager_addressable_content($address, $type) {
  1131. if (count($address) < 3) {
  1132. return;
  1133. }
  1134. $task_name = array_shift($address);
  1135. $subtask_name = array_shift($address);
  1136. $handler_name = array_shift($address);
  1137. if ($address) {
  1138. $arguments = array_shift($address);
  1139. }
  1140. // Since $arguments is an array of arbitrary size, we need to implode it:
  1141. if (!empty($arguments)) {
  1142. // The only choices we have for separators since :: is already
  1143. // used involve ., - or _. Since - and _ are more common than .
  1144. // in URLs, let's try .. as an argument separator.
  1145. $arguments = explode('..', $arguments);
  1146. }
  1147. else {
  1148. // implode does not return an empty array on an empty
  1149. // string so do it specifically.
  1150. $arguments = array();
  1151. }
  1152. $task = page_manager_get_task($task_name);
  1153. if (!$task) {
  1154. return;
  1155. }
  1156. $handler = page_manager_load_task_handler($task, $subtask_name, $handler_name);
  1157. if (!$handler) {
  1158. return;
  1159. }
  1160. $handler_plugin = page_manager_get_task_handler($handler->handler);
  1161. if (!$handler_plugin) {
  1162. return;
  1163. }
  1164. // Load the contexts for the task.
  1165. ctools_include('context');
  1166. ctools_include('context-task-handler');
  1167. $contexts = ctools_context_handler_get_task_contexts($task, $subtask_name, $arguments);
  1168. // With contexts loaded, ensure the task is accessible. Tasks without a callback
  1169. // are automatically accessible.
  1170. $function = ctools_plugin_get_function($task, 'access callback');
  1171. if ($function && !$function($task, $subtask_name, $contexts)) {
  1172. return;
  1173. }
  1174. $function = ctools_plugin_get_function($handler_plugin, 'addressable callback');
  1175. if ($function) {
  1176. return $function($task, $subtask_name, $handler, $address, $contexts, $arguments, $type);
  1177. }
  1178. }