page_manager.module 39 KB

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