12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349 |
- <?php
- /**
- * @file
- * The page manager module provides a UI and API to manage pages.
- *
- * It defines pages, both for system pages, overrides of system pages, and
- * custom pages using Drupal's normal menu system. It allows complex
- * manipulations of these pages, their content, and their hierarchy within
- * the site. These pages can be exported to code for superior revision
- * control.
- */
- /**
- * Bit flag on the 'changed' value to tell us if an item was moved.
- */
- define('PAGE_MANAGER_CHANGED_MOVED', 0x01);
- /**
- * Bit flag on the 'changed' value to tell us if an item edited or added.
- */
- define('PAGE_MANAGER_CHANGED_CACHED', 0x02);
- /**
- * Bit flag on the 'changed' value to tell us if an item deleted.
- */
- define('PAGE_MANAGER_CHANGED_DELETED', 0x04);
- /**
- * Bit flag on the 'changed' value to tell us if an item has had its disabled status changed.
- */
- define('PAGE_MANAGER_CHANGED_STATUS', 0x08);
- // --------------------------------------------------------------------------
- // Drupal hooks
- /**
- * Implements hook_permission().
- */
- function page_manager_permission() {
- return array(
- 'use page manager' => array(
- 'title' => t('Use Page Manager'),
- '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."),
- 'restrict access' => TRUE,
- ),
- 'administer page manager' => array(
- 'title' => t('Administer Page Manager'),
- 'description' => t('Allows complete control over Page Manager, i.e., complete control over your site. Grant with extreme caution.'),
- 'restrict access' => TRUE,
- ),
- );
- }
- /**
- * Implements hook_ctools_plugin_directory() to let the system know
- * where our task and task_handler plugins are.
- */
- function page_manager_ctools_plugin_directory($owner, $plugin_type) {
- if ($owner == 'page_manager') {
- return 'plugins/' . $plugin_type;
- }
- if ($owner == 'ctools' && $plugin_type == 'cache') {
- return 'plugins/' . $plugin_type;
- }
- }
- /**
- * Implements hook_ctools_plugin_type() to inform the plugin system that Page
- * Manager owns task, task_handler, and page_wizard plugin types.
- *
- * All of these are empty because the defaults all work.
- */
- function page_manager_ctools_plugin_type() {
- return array(
- 'tasks' => array(),
- 'task_handlers' => array(),
- 'page_wizards' => array(),
- );
- }
- /**
- * Delegated implementation of hook_menu().
- */
- function page_manager_menu() {
- // For some reason, some things can activate modules without satisfying
- // dependencies. I don't know how, but this helps prevent things from
- // whitescreening when this happens.
- if (!module_exists('ctools')) {
- return;
- }
- $items = array();
- $base = array(
- 'access arguments' => array('use page manager'),
- 'file' => 'page_manager.admin.inc',
- 'theme callback' => 'ajax_base_page_theme',
- );
- $items['admin/structure/pages'] = array(
- 'title' => 'Pages',
- 'description' => 'Add, edit and remove overridden system pages and user defined pages from the system.',
- 'page callback' => 'page_manager_list_page',
- ) + $base;
- $items['admin/structure/pages/list'] = array(
- 'title' => 'List',
- 'page callback' => 'page_manager_list_page',
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- 'weight' => -10,
- ) + $base;
- $items['admin/structure/pages/edit/%page_manager_cache'] = array(
- 'title' => 'Edit',
- 'page callback' => 'page_manager_edit_page',
- 'page arguments' => array(4),
- 'type' => MENU_NORMAL_ITEM,
- ) + $base;
- $items['admin/structure/pages/%ctools_js/operation/%page_manager_cache'] = array(
- 'page callback' => 'page_manager_edit_page_operation',
- 'page arguments' => array(3, 5),
- 'type' => MENU_NORMAL_ITEM,
- ) + $base;
- $items['admin/structure/pages/%ctools_js/enable/%page_manager_cache'] = array(
- 'page callback' => 'page_manager_enable_page',
- 'page arguments' => array(FALSE, 3, 5),
- 'type' => MENU_CALLBACK,
- ) + $base;
- $items['admin/structure/pages/%ctools_js/disable/%page_manager_cache'] = array(
- 'page callback' => 'page_manager_enable_page',
- 'page arguments' => array(TRUE, 3, 5),
- 'type' => MENU_CALLBACK,
- ) + $base;
- $tasks = page_manager_get_tasks();
- // Provide menu items for each task.
- foreach ($tasks as $task_id => $task) {
- // Allow the task to add its own menu items.
- if ($function = ctools_plugin_get_function($task, 'hook menu')) {
- $function($items, $task);
- }
- // And for those that provide subtasks, provide menu items for them, as well.
- foreach (page_manager_get_task_subtasks($task) as $subtask_id => $subtask) {
- // Allow the task to add its own menu items.
- if ($function = ctools_plugin_get_function($task, 'hook menu')) {
- $function($items, $subtask);
- }
- }
- }
- return $items;
- }
- function page_manager_admin_paths() {
- /* @todo FIX ME this is a major resource suck. */
- return;
- $items = array();
- ctools_include('page', 'page_manager', 'plugins/tasks');
- $pages = page_manager_page_load_all();
- foreach ($pages as $page) {
- // Make sure the page we're on is set to be an administrative path and that
- // it is not set to be a frontpage path.
- if ((isset($page->conf['admin_paths']) && $page->conf['admin_paths']) && (!isset($page->make_frontpage) || !$page->make_frontpage)) {
- $path_parts = explode('/', $page->path);
- foreach ($path_parts as $key => $part) {
- if (strpos($part, '%') !== FALSE || strpos($part, '!') !== FALSE) {
- $path_parts[$key] = '*';
- }
- }
- $path = implode('/', $path_parts);
- if ($page->menu['type'] == 'default tab') {
- array_pop($path_parts);
- $parent_path = implode('/', $path_parts);
- $items[$parent_path] = TRUE;
- }
- $items[$path] = TRUE;
- }
- }
- return $items;
- }
- /**
- * Implements hook_menu_alter.
- *
- * Get a list of all tasks and delegate to them.
- */
- function page_manager_menu_alter(&$items) {
- // For some reason, some things can activate modules without satisfying
- // dependencies. I don't know how, but this helps prevent things from
- // whitescreening when this happens.
- if (!module_exists('ctools')) {
- return;
- }
- $tasks = page_manager_get_tasks();
- foreach ($tasks as $task) {
- if ($function = ctools_plugin_get_function($task, 'hook menu alter')) {
- $function($items, $task);
- }
- // let the subtasks alter the menu items too.
- foreach (page_manager_get_task_subtasks($task) as $subtask_id => $subtask) {
- if ($function = ctools_plugin_get_function($subtask, 'hook menu alter')) {
- $function($items, $subtask);
- }
- }
- }
- // Override the core node revisions display to use the configured Page
- // display handler.
- if (!variable_get('page_manager_node_view_disabled', TRUE) && isset($items['node/%node/revisions/%/view'])) {
- // Abstract the basic settings.
- $item = array(
- // Handle the page arguments.
- 'load arguments' => array(3),
- 'page arguments' => array(1, TRUE),
- // Replace the normal node_show call with Page Manager's node view.
- 'page callback' => 'page_manager_node_view_page',
- // Provide the correct path to the Page Manager file.
- 'file' => 'node_view.inc',
- 'file path' => drupal_get_path('module', 'page_manager') . '/plugins/tasks',
- );
- // Re-build the menu item using the normal values from node.module.
- $items['node/%node/revisions/%/view'] = array(
- 'title' => 'Revisions',
- 'access callback' => '_node_revision_access',
- 'access arguments' => array(1),
- ) + $item;
- }
- return $items;
- }
- /*
- * Implements hook_theme()
- */
- function page_manager_theme() {
- // For some reason, some things can activate modules without satisfying
- // dependencies. I don't know how, but this helps prevent things from
- // whitescreening when this happens.
- if (!module_exists('ctools')) {
- return;
- }
- $base = array(
- 'path' => drupal_get_path('module', 'page_manager') . '/theme',
- 'file' => 'page_manager.theme.inc',
- );
- $items = array(
- 'page_manager_handler_rearrange' => array(
- 'render element' => 'form',
- ) + $base,
- 'page_manager_edit_page' => array(
- 'template' => 'page-manager-edit-page',
- 'variables' => array('page' => NULL, 'save' => NULL, 'operations' => array(), 'content' => array()),
- ) + $base,
- 'page_manager_lock' => array(
- 'variables' => array('page' => array()),
- ) + $base,
- 'page_manager_changed' => array(
- 'variables' => array('text' => NULL, 'description' => NULL),
- ) + $base,
- );
- // Allow task plugins to have theme registrations by passing through:
- $tasks = page_manager_get_tasks();
- // Provide menu items for each task.
- foreach ($tasks as $task_id => $task) {
- if ($function = ctools_plugin_get_function($task, 'hook theme')) {
- $function($items, $task);
- }
- }
- return $items;
- }
- // --------------------------------------------------------------------------
- // Page caching
- //
- // The page cache is used to store a page temporarily, using the ctools object
- // cache. When loading from the page cache, it will either load the cached
- // version, or if there is not one, load the real thing and create a cache
- // object which can then be easily stored.
- /**
- * Get the cached changes to a given task handler.
- */
- function page_manager_get_page_cache($task_name) {
- $caches = drupal_static(__FUNCTION__, array());
- if (!isset($caches[$task_name])) {
- ctools_include('object-cache');
- $cache = ctools_object_cache_get('page_manager_page', $task_name);
- if (!$cache) {
- $cache = new stdClass();
- $cache->task_name = $task_name;
- list($cache->task_id, $cache->subtask_id) = page_manager_get_task_id($cache->task_name);
- $cache->task = page_manager_get_task($cache->task_id);
- if (empty($cache->task)) {
- return FALSE;
- }
- if ($cache->subtask_id) {
- $cache->subtask = page_manager_get_task_subtask($cache->task, $cache->subtask_id);
- if (empty($cache->subtask)) {
- return FALSE;
- }
- }
- else {
- $cache->subtask = $cache->task;
- $cache->subtask['name'] = '';
- }
- $cache->handlers = page_manager_load_sorted_handlers($cache->task, $cache->subtask_id);
- $cache->handler_info = array();
- foreach ($cache->handlers as $id => $handler) {
- $cache->handler_info[$id] = array(
- 'weight' => $handler->weight,
- 'changed' => FALSE,
- 'name' => $id,
- );
- }
- }
- else {
- // ensure the task is loaded.
- page_manager_get_task($cache->task_id);
- }
- if ($task_name != '::new') {
- $cache->locked = ctools_object_cache_test('page_manager_page', $task_name);
- }
- else {
- $cache->locked = FALSE;
- }
- $caches[$task_name] = $cache;
- }
- return $caches[$task_name];
- }
- /**
- * Store changes to a task handler in the object cache.
- */
- function page_manager_set_page_cache($page) {
- if (!empty($page->locked)) {
- return;
- }
- if (empty($page->task_name)) {
- return;
- }
- ctools_include('object-cache');
- $page->changed = TRUE;
- $cache = ctools_object_cache_set('page_manager_page', $page->task_name, $page);
- }
- /**
- * Remove an item from the object cache.
- */
- function page_manager_clear_page_cache($name) {
- ctools_include('object-cache');
- ctools_object_cache_clear('page_manager_page', $name);
- }
- /**
- * Write all changes from the page cache and clear it out.
- */
- function page_manager_save_page_cache($cache) {
- // Save the subtask:
- if ($function = ctools_plugin_get_function($cache->task, 'save subtask callback')) {
- $function($cache->subtask, $cache);
- }
- // Iterate through handlers and save/delete/update as necessary.
- // Go through each of the task handlers, check to see if it needs updating,
- // and update it if so.
- foreach ($cache->handler_info as $id => $info) {
- $handler = &$cache->handlers[$id];
- // If it has been marked for deletion, delete it.
- if ($info['changed'] & PAGE_MANAGER_CHANGED_DELETED) {
- page_manager_delete_task_handler($handler);
- }
- // If it has been somehow edited (or added), write the cached version
- elseif ($info['changed'] & PAGE_MANAGER_CHANGED_CACHED) {
- // Make sure we get updated weight from the form for this.
- $handler->weight = $info['weight'];
- page_manager_save_task_handler($handler);
- }
- // Otherwise, check to see if it has moved and, if so, update the weight.
- elseif ($info['weight'] != $handler->weight) {
- // Theoretically we could only do this for in code objects, but since our
- // load mechanism checks for all, this is less database work.
- page_manager_update_task_handler_weight($handler, $info['weight']);
- }
- // Set enable/disabled status.
- if ($info['changed'] & PAGE_MANAGER_CHANGED_STATUS) {
- ctools_include('export');
- ctools_export_set_object_status($cache->handlers[$id], $info['disabled']);
- }
- }
- page_manager_clear_page_cache($cache->task_name);
- if (!empty($cache->path_changed) || !empty($cache->new)) {
- // Force a menu rebuild to make sure the menu entries are set.
- menu_rebuild();
- }
- cache_clear_all();
- }
- /**
- * Menu callback to load a page manager cache object for menu callbacks.
- */
- function page_manager_cache_load($task_name) {
- // load context plugin as there may be contexts cached here.
- ctools_include('context');
- return page_manager_get_page_cache($task_name);
- }
- /**
- * Generate a unique name for a task handler.
- *
- * Task handlers need to be named but they aren't allowed to set their own
- * names. Instead, they are named based upon their parent task and type.
- */
- function page_manager_handler_get_name($task_name, $handlers, $handler) {
- $base = str_replace('-', '_', $task_name);
- $name = '';
- // Optional machine name.
- if (!empty($handler->conf['name'])) {
- $name = $base . '__' . $handler->conf['name'];
- if (count(ctools_export_load_object('page_manager_handlers', 'names', array($name)))) {
- $name = '';
- }
- }
- // If no machine name was provided or the name is in use, generate a unique name.
- if (empty($name)) {
- $base .= '__' . $handler->handler;
- // Use the ctools uuid generator to generate a unique id.
- $name = $base . '_' . ctools_uuid_generate();
- }
- return $name;
- }
- /**
- * Import a handler into a page.
- *
- * This is used by both import and clone, since clone just exports the
- * handler and immediately imports it.
- */
- function page_manager_handler_add_to_page(&$page, &$handler, $title = NULL) {
- $last = end($page->handler_info);
- $handler->weight = $last ? $last['weight'] + 1 : 0;
- $handler->task = $page->task_id;
- $handler->subtask = $page->subtask_id;
- $handler->export_type = EXPORT_IN_DATABASE;
- $handler->type = t('Normal');
- if ($title) {
- $handler->conf['title'] = $title;
- $handler->conf['name'] = trim(preg_replace('/[^a-z0-9_]+/', '-', strtolower($title)), '-');
- }
- else {
- $handler->conf['name'] = '';
- }
- $name = page_manager_handler_get_name($page->task_name, $page->handlers, $handler);
- $handler->name = $name;
- $page->handlers[$name] = $handler;
- $page->handler_info[$name] = array(
- 'weight' => $handler->weight,
- 'name' => $handler->name,
- 'changed' => PAGE_MANAGER_CHANGED_CACHED,
- );
- }
- // --------------------------------------------------------------------------
- // Database routines
- //
- // This includes fetching plugins and plugin info as well as specialized
- // fetch methods to get groups of task handlers per task.
- /**
- * Load a single task handler by name.
- *
- * Handlers can come from multiple sources; either the database or by normal
- * export method, which is handled by the ctools library, but handlers can
- * also be bundled with task/subtask. We have to check there and perform
- * overrides as appropriate.
- *
- * Handlers bundled with the task are of a higher priority than default
- * handlers provided by normal code, and are of a lower priority than
- * the database, so we have to check the source of handlers when we have
- * multiple to choose from.
- */
- function page_manager_load_task_handler($task, $subtask_id, $name) {
- ctools_include('export');
- $result = ctools_export_load_object('page_manager_handlers', 'names', array($name));
- $handlers = page_manager_get_default_task_handlers($task, $subtask_id);
- return page_manager_compare_task_handlers($result, $handlers, $name);
- }
- /**
- * Load all task handlers for a given task/subtask.
- */
- function page_manager_load_task_handlers($task, $subtask_id = NULL, $default_handlers = NULL) {
- ctools_include('export');
- $conditions = array(
- 'task' => $task['name'],
- );
- if (isset($subtask_id)) {
- $conditions['subtask'] = $subtask_id;
- }
- $handlers = ctools_export_load_object('page_manager_handlers', 'conditions', $conditions);
- $defaults = isset($default_handlers) ? $default_handlers : page_manager_get_default_task_handlers($task, $subtask_id);
- foreach ($defaults as $name => $default) {
- $result = page_manager_compare_task_handlers($handlers, $defaults, $name);
- if ($result) {
- $handlers[$name] = $result;
- // Ensure task and subtask are correct, because it's easy to change task
- // names when editing a default and fail to do it on the associated handlers.
- $result->task = $task['name'];
- $result->subtask = $subtask_id;
- }
- }
- // Override weights from the weight table.
- if ($handlers) {
- $names = array();
- $placeholders = array();
- foreach ($handlers as $handler) {
- $names[] = $handler->name;
- $placeholders[] = "'%s'";
- }
- $result = db_query('SELECT name, weight FROM {page_manager_weights} WHERE name IN (:names)', array(':names' => $names));
- foreach ($result as $weight) {
- $handlers[$weight->name]->weight = $weight->weight;
- }
- }
- return $handlers;
- }
- /**
- * Get the default task handlers from a task, if they exist.
- *
- * Tasks can contain 'default' task handlers which are provided by the
- * default task. Because these can come from either the task or the
- * subtask, the logic is abstracted to reduce code duplication.
- */
- function page_manager_get_default_task_handlers($task, $subtask_id) {
- // Load default handlers that are provied by the task/subtask itself.
- $handlers = array();
- if ($subtask_id) {
- $subtask = page_manager_get_task_subtask($task, $subtask_id);
- if (isset($subtask['default handlers'])) {
- $handlers = $subtask['default handlers'];
- }
- }
- else if (isset($task['default handlers'])) {
- $handlers = $task['default handlers'];
- }
- return $handlers;
- }
- /**
- * Compare a single task handler from two lists and provide the correct one.
- *
- * Task handlers can be gotten from multiple sources. As exportable objects,
- * they can be provided by default hooks and the database. But also, because
- * they are tightly bound to tasks, they can also be provided by default
- * tasks. This function reconciles where to pick up a task handler between
- * the exportables list and the defaults provided by the task itself.
- *
- * @param $result
- * A list of handlers provided by export.inc
- * @param $handlers
- * A list of handlers provided by the default task.
- * @param $name
- * Which handler to compare.
- * @return
- * Which handler to use, if any. May be NULL.
- */
- function page_manager_compare_task_handlers($result, $handlers, $name) {
- // Compare our special default handler against the actual result, if
- // any, and do the right thing.
- if (!isset($result[$name]) && isset($handlers[$name])) {
- $handlers[$name]->type = t('Default');
- $handlers[$name]->export_type = EXPORT_IN_CODE;
- return $handlers[$name];
- }
- else if (isset($result[$name]) && !isset($handlers[$name])) {
- return $result[$name];
- }
- else if (isset($result[$name]) && isset($handlers[$name])) {
- if ($result[$name]->export_type & EXPORT_IN_DATABASE) {
- $result[$name]->type = t('Overridden');
- $result[$name]->export_type = $result[$name]->export_type | EXPORT_IN_CODE;
- return $result[$name];
- }
- else {
- // In this case, our default is a higher priority than the standard default.
- $handlers[$name]->type = t('Default');
- $handlers[$name]->export_type = EXPORT_IN_CODE;
- return $handlers[$name];
- }
- }
- }
- /**
- * Load all task handlers for a given task and subtask and sort them.
- */
- function page_manager_load_sorted_handlers($task, $subtask_id = NULL, $enabled = FALSE) {
- $handlers = page_manager_load_task_handlers($task, $subtask_id);
- if ($enabled) {
- foreach ($handlers as $id => $handler) {
- if (!empty($handler->disabled)) {
- unset($handlers[$id]);
- }
- }
- }
- uasort($handlers, 'page_manager_sort_task_handlers');
- return $handlers;
- }
- /**
- * Callback for uasort to sort task handlers.
- *
- * Task handlers are sorted by weight then by name.
- */
- function page_manager_sort_task_handlers($a, $b) {
- if ($a->weight < $b->weight) {
- return -1;
- }
- elseif ($a->weight > $b->weight) {
- return 1;
- }
- elseif ($a->name < $b->name) {
- return -1;
- }
- elseif ($a->name > $b->name) {
- return 1;
- }
- return 0;
- }
- /**
- * Write a task handler to the database.
- */
- function page_manager_save_task_handler(&$handler) {
- $update = (isset($handler->did)) ? array('did') : array();
- // Let the task handler respond to saves:
- if ($function = ctools_plugin_load_function('page_manager', 'task_handlers', $handler->handler, 'save')) {
- $function($handler, $update);
- }
- drupal_write_record('page_manager_handlers', $handler, $update);
- db_delete('page_manager_weights')
- ->condition('name', $handler->name)
- ->execute();
- // If this was previously a default handler, we may have to write task handlers.
- if (!$update) {
- // @todo wtf was I going to do here?
- }
- return $handler;
- }
- /**
- * Remove a task handler.
- */
- function page_manager_delete_task_handler($handler) {
- // Let the task handler respond to saves:
- if ($function = ctools_plugin_load_function('page_manager', 'task_handlers', $handler->handler, 'delete')) {
- $function($handler);
- }
- db_delete('page_manager_handlers')
- ->condition('name', $handler->name)
- ->execute();
- db_delete('page_manager_weights')
- ->condition('name', $handler->name)
- ->execute();
- }
- /**
- * Export a task handler into code suitable for import or use as a default
- * task handler.
- */
- function page_manager_export_task_handler($handler, $indent = '') {
- ctools_include('export');
- ctools_include('plugins');
- $handler = clone $handler;
- $append = '';
- if ($function = ctools_plugin_load_function('page_manager', 'task_handlers', $handler->handler, 'export')) {
- $append = $function($handler, $indent);
- }
- $output = ctools_export_object('page_manager_handlers', $handler, $indent);
- $output .= $append;
- return $output;
- }
- /**
- * Loads page manager handler for export.
- *
- * Callback to load page manager handler within ctools_export_crud_load().
- *
- * @param string $name
- * The name of the handler to load.
- *
- * @return
- * Loaded page manager handler object, extended with external properties.
- */
- function page_manager_export_task_handler_load($name) {
- $table = 'page_manager_handlers';
- $schema = ctools_export_get_schema($table);
- $export = $schema['export'];
- $result = ctools_export_load_object($table, 'names', array($name));
- if (isset($result[$name])) {
- $handler = $result[$name];
- // Weight is stored in additional table so that in-code task handlers
- // don't need to get written to the database just because they have their
- // weight changed. Therefore, handler could have no correspondent database
- // entry. Revert will not be performed for this handler and the weight
- // will not be reverted. To make possible revert of the weight field
- // export_type must simulate that the handler is stored in the database.
- $handler->export_type = EXPORT_IN_DATABASE;
- // Also, page manager handler weight should be overriden with correspondent
- // weight from page_manager_weights table, if there is one.
- $result = db_query('SELECT weight FROM {page_manager_weights} WHERE name = (:names)', array(':names' => $handler->name))->fetchField();
- if (is_numeric($result)) {
- $handler->weight = $result;
- }
- return $handler;
- }
- }
- /**
- * Create a new task handler object.
- *
- * @param $plugin
- * The plugin this task handler is created from.
- */
- function page_manager_new_task_handler($plugin) {
- // Generate a unique name. Unlike most named objects, we don't let people choose
- // names for task handlers because they mostly don't make sense.
- // Create a new, empty handler object.
- $handler = new stdClass;
- $handler->title = $plugin['title'];
- $handler->task = NULL;
- $handler->subtask = NULL;
- $handler->name = NULL;
- $handler->handler = $plugin['name'];
- $handler->weight = 0;
- $handler->conf = array();
- // These are provided by the core export API provided by ctools and we
- // set defaults here so that we don't cause notices. Perhaps ctools should
- // provide a way to do this for us so we don't have to muck with it.
- $handler->export_type = EXPORT_IN_DATABASE;
- $handler->type = t('Local');
- if (isset($plugin['default conf'])) {
- if (is_array($plugin['default conf'])) {
- $handler->conf = $plugin['default conf'];
- }
- else if (function_exists($plugin['default conf'])) {
- $handler->conf = $plugin['default conf']($handler);
- }
- }
- return $handler;
- }
- /**
- * Set an overidden weight for a task handler.
- *
- * We do this so that in-code task handlers don't need to get written
- * to the database just because they have their weight changed.
- */
- function page_manager_update_task_handler_weight($handler, $weight) {
- db_delete('page_manager_weights')
- ->condition('name', $handler->name)
- ->execute();
- db_insert('page_manager_weights')
- ->fields(array(
- 'name' => $handler->name,
- 'weight' => $weight,
- ))
- ->execute();
- }
- /**
- * Shortcut function to get task plugins.
- */
- function page_manager_get_tasks() {
- ctools_include('plugins');
- return ctools_get_plugins('page_manager', 'tasks');
- }
- /**
- * Shortcut function to get a task plugin.
- */
- function page_manager_get_task($id) {
- ctools_include('plugins');
- return ctools_get_plugins('page_manager', 'tasks', $id);
- }
- /**
- * Get all tasks for a given type.
- */
- function page_manager_get_tasks_by_type($type) {
- ctools_include('plugins');
- $all_tasks = ctools_get_plugins('page_manager', 'tasks');
- $tasks = array();
- foreach ($all_tasks as $id => $task) {
- if (isset($task['task type']) && $task['task type'] == $type) {
- $tasks[$id] = $task;
- }
- }
- return $tasks;
- }
- /**
- * Fetch all subtasks for a page managertask.
- *
- * @param $task
- * A loaded $task plugin object.
- */
- function page_manager_get_task_subtasks($task) {
- if (empty($task['subtasks'])) {
- return array();
- }
- if ($function = ctools_plugin_get_function($task, 'subtasks callback')) {
- $retval = $function($task);
- if (is_array($retval)) {
- return $retval;
- }
- }
- return array();
- }
- /**
- * Fetch all subtasks for a page managertask.
- *
- * @param $task
- * A loaded $task plugin object.
- * @param $subtask_id
- * The subtask ID to load.
- */
- function page_manager_get_task_subtask($task, $subtask_id) {
- if (empty($task['subtasks'])) {
- return;
- }
- if ($function = ctools_plugin_get_function($task, 'subtask callback')) {
- return $function($task, $subtask_id);
- }
- }
- /**
- * Shortcut function to get task handler plugins.
- */
- function page_manager_get_task_handlers() {
- ctools_include('plugins');
- return ctools_get_plugins('page_manager', 'task_handlers');
- }
- /**
- * Shortcut function to get a task handler plugin.
- */
- function page_manager_get_task_handler($id) {
- ctools_include('plugins');
- return ctools_get_plugins('page_manager', 'task_handlers', $id);
- }
- /**
- * Retrieve a list of all applicable task handlers for a given task.
- *
- * This looks at the $task['handler type'] and compares that to $task_handler['handler type'].
- * If the task has no type, the id of the task is used instead.
- */
- function page_manager_get_task_handler_plugins($task, $all = FALSE) {
- $type = isset($task['handler type']) ? $task['handler type'] : $task['name'];
- $name = $task['name'];
- $handlers = array();
- $task_handlers = page_manager_get_task_handlers();
- foreach ($task_handlers as $id => $handler) {
- $task_type = is_array($handler['handler type']) ? $handler['handler type'] : array($handler['handler type']);
- if (in_array($type, $task_type) || in_array($name, $task_type)) {
- if ($all || !empty($handler['visible'])) {
- $handlers[$id] = $handler;
- }
- }
- }
- return $handlers;
- }
- /**
- * Get the title for a given handler.
- *
- * If the plugin has no 'admin title' function, the generic title of the
- * plugin is used instead.
- */
- function page_manager_get_handler_title($plugin, $handler, $task, $subtask_id) {
- $function = ctools_plugin_get_function($plugin, 'admin title');
- if ($function) {
- return $function($handler, $task, $subtask_id);
- }
- else {
- return $plugin['title'];
- }
- }
- /**
- * Get the admin summary (additional info) for a given handler.
- */
- function page_manager_get_handler_summary($plugin, $handler, $page, $title = TRUE) {
- if ($function = ctools_plugin_get_function($plugin, 'admin summary')) {
- return $function($handler, $page->task, $page->subtask, $page, $title);
- }
- }
- /**
- * Get the admin summary (additional info) for a given page.
- */
- function page_manager_get_page_summary($task, $subtask) {
- if ($function = ctools_plugin_get_function($subtask, 'admin summary')) {
- return $function($task, $subtask);
- }
- }
- /**
- * Split a task name into a task id and subtask id, if applicable.
- */
- function page_manager_get_task_id($task_name) {
- if (strpos($task_name, '-') !== FALSE) {
- return explode('-', $task_name, 2);
- }
- else {
- return array($task_name, NULL);
- }
- }
- /**
- * Turn a task id + subtask_id into a task name.
- */
- function page_manager_make_task_name($task_id, $subtask_id) {
- if ($subtask_id) {
- return $task_id . '-' . $subtask_id;
- }
- else {
- return $task_id;
- }
- }
- /**
- * Get the render function for a handler.
- */
- function page_manager_get_renderer($handler) {
- return ctools_plugin_load_function('page_manager', 'task_handlers', $handler->handler, 'render');
- }
- // --------------------------------------------------------------------------
- // Functions existing on behalf of tasks and task handlers
- /**
- * Page manager arg load function because menu system will not load extra
- * files for these; they must be in a .module.
- */
- function pm_arg_load($value, $subtask, $argument) {
- page_manager_get_task('page');
- return _pm_arg_load($value, $subtask, $argument);
- }
- /**
- * Special arg_load function to use %menu_tail like functionality to
- * get everything after the arg together as a single value.
- */
- function pm_arg_tail_load($value, $subtask, $argument, $map) {
- $value = implode('/', array_slice($map, $argument));
- page_manager_get_task('page');
- return _pm_arg_load($value, $subtask, $argument);
- }
- /**
- * Special menu _load() function for the user:uid argument.
- *
- * This is just the normal page manager argument. It only exists so that
- * the to_arg can exist.
- */
- function pm_uid_arg_load($value, $subtask, $argument) {
- page_manager_get_task('page');
- return _pm_arg_load($value, $subtask, $argument);
- }
- /**
- * to_arg function for the user:uid argument to provide the arg for the
- * current global user.
- */
- function pm_uid_arg_to_arg($arg) {
- return user_uid_optional_to_arg($arg);
- }
- /**
- * Callback for access control ajax form on behalf of page.inc task.
- *
- * Returns the cached access config and contexts used.
- */
- function page_manager_page_ctools_access_get($argument) {
- $page = page_manager_get_page_cache($argument);
- $contexts = array();
- // Load contexts based on argument data:
- if ($arguments = _page_manager_page_get_arguments($page->subtask['subtask'])) {
- $contexts = ctools_context_get_placeholders_from_argument($arguments);
- }
- return array($page->subtask['subtask']->access, $contexts);
- }
- /**
- * Callback for access control ajax form on behalf of page.inc task.
- *
- * Writes the changed access to the cache.
- */
- function page_manager_page_ctools_access_set($argument, $access) {
- $page = page_manager_get_page_cache($argument);
- $page->subtask['subtask']->access = $access;
- page_manager_set_page_cache($page);
- }
- /**
- * Callback for access control ajax form on behalf of context task handler.
- *
- * Returns the cached access config and contexts used.
- */
- function page_manager_task_handler_ctools_access_get($argument) {
- list($task_name, $name) = explode('*', $argument);
- $page = page_manager_get_page_cache($task_name);
- if (empty($name)) {
- $handler = &$page->new_handler;
- }
- else {
- $handler = &$page->handlers[$name];
- }
- if (!isset($handler->conf['access'])) {
- $handler->conf['access'] = array();
- }
- ctools_include('context-task-handler');
- $contexts = ctools_context_handler_get_all_contexts($page->task, $page->subtask, $handler);
- return array($handler->conf['access'], $contexts);
- }
- /**
- * Callback for access control ajax form on behalf of context task handler.
- *
- * Writes the changed access to the cache.
- */
- function page_manager_task_handler_ctools_access_set($argument, $access) {
- list($task_name, $name) = explode('*', $argument);
- $page = page_manager_get_page_cache($task_name);
- if (empty($name)) {
- $handler = &$page->new_handler;
- }
- else {
- $handler = &$page->handlers[$name];
- }
- $handler->conf['access'] = $access;
- page_manager_set_page_cache($page);
- }
- /**
- * Form a URL to edit a given page given the trail.
- */
- function page_manager_edit_url($task_name, $trail = array()) {
- if (!is_array($trail)) {
- $trail = array($trail);
- }
- if (empty($trail) || $trail == array('summary')) {
- return "admin/structure/pages/edit/$task_name";
- }
- return 'admin/structure/pages/nojs/operation/' . $task_name . '/' . implode('/', $trail);
- }
- /**
- * Watch menu links during the menu rebuild, and re-parent things if we need to.
- */
- function page_manager_menu_link_alter(&$item) {
- return;
- /** -- disabled, concept code --
- static $mlids = array();
- // Keep an array of mlids as links are saved that we can use later.
- if (isset($item['mlid'])) {
- $mlids[$item['path']] = $item['mlid'];
- }
- if (isset($item['parent_path'])) {
- if (isset($mlids[$item['parent_path']])) {
- $item['plid'] = $mlids[$item['parent_path']];
- }
- else {
- // Since we didn't already see an mlid, let's check the database for one.
- $mlid = db_query('SELECT mlid FROM {menu_links} WHERE router_path = :path', array('path' => $item['parent_path']))->fetchField();
- if ($mlid) {
- $item['plid'] = $mlid;
- }
- }
- }
- */
- }
- /**
- * Callback to list handlers available for export.
- */
- function page_manager_page_manager_handlers_list() {
- $list = $types = array();
- $tasks = page_manager_get_tasks();
- foreach ($tasks as $type => $info) {
- if (empty($info['non-exportable'])) {
- $types[] = $type;
- }
- }
- $handlers = ctools_export_load_object('page_manager_handlers');
- foreach ($handlers as $handler) {
- if (in_array($handler->task, $types)) {
- $plugin = page_manager_get_task_handler($handler->handler);
- $title = page_manager_get_handler_title($plugin, $handler, $tasks[$handler->task], $handler->subtask);
- if ($title) {
- $list[$handler->name] = check_plain("$handler->task: $title ($handler->name)");
- }
- else {
- $list[$handler->name] = check_plain("$handler->task: ($handler->name)");
- }
- }
- }
- return $list;
- }
- /**
- * Callback to bulk export page manager pages.
- */
- function page_manager_page_manager_pages_to_hook_code($names = array(), $name = 'foo') {
- $schema = ctools_export_get_schema('page_manager_pages');
- $export = $schema['export'];
- $objects = ctools_export_load_object('page_manager_pages', 'names', array_values($names));
- if ($objects) {
- $code = "/**\n";
- $code .= " * Implements hook_{$export['default hook']}()\n";
- $code .= " */\n";
- $code .= "function " . $name . "_{$export['default hook']}() {\n";
- foreach ($objects as $object) {
- // Have to implement our own because this export func sig requires it
- $code .= $export['export callback']($object, TRUE, ' ');
- $code .= " \${$export['identifier']}s['" . check_plain($object->{$export['key']}) . "'] = \${$export['identifier']};\n\n";
- }
- $code .= " return \${$export['identifier']}s;\n";
- $code .= "}\n";
- return $code;
- }
- }
- /**
- * Get the current page information.
- *
- * @return $page
- * An array containing the following information.
- *
- * - 'name': The name of the page as used in the page manager admin UI.
- * - 'task': The plugin for the task in use. If this is a system page it
- * will contain information about that page, such as what functions
- * it uses.
- * - 'subtask': The plugin for the subtask. If this is a custom page, this
- * will contain information about that custom page. See 'subtask' in this
- * array to get the actual page object.
- * - 'handler': The actual handler object used. If using panels, see
- * $page['handler']->conf['display'] for the actual panels display
- * used to render.
- * - 'contexts': The context objects used to render this page.
- * - 'arguments': The raw arguments from the URL used on this page.
- */
- function page_manager_get_current_page($page = NULL) {
- static $current = array();
- if (isset($page)) {
- $current = $page;
- }
- return $current;
- }
- /**
- * Implementation of hook_panels_dashboard_blocks().
- *
- * Adds page information to the Panels dashboard.
- */
- function page_manager_panels_dashboard_blocks(&$vars) {
- $vars['links']['page_manager'] = array(
- 'weight' => -100,
- 'title' => l(t('Panel page'), 'admin/structure/pages/add'),
- 'description' => t('Panel pages can be used as landing pages. They have a URL path, accept arguments and can have menu entries.'),
- );
- module_load_include('inc', 'page_manager', 'page_manager.admin');
- $tasks = page_manager_get_tasks_by_type('page');
- $pages = array('operations' => array());
- page_manager_get_pages($tasks, $pages);
- $count = 0;
- $rows = array();
- foreach ($pages['rows'] as $id => $info) {
- $rows[] = array(
- 'data' => array(
- $info['data']['title'],
- $info['data']['operations'],
- ),
- 'class' => $info['class'],
- );
- // Only show 10.
- if (++$count >= 10) {
- break;
- }
- }
- $vars['blocks']['page_manager'] = array(
- 'weight' => -100,
- 'title' => t('Manage pages'),
- 'link' => l(t('Go to list'), 'admin/structure/pages'),
- 'content' => theme('table', array('header' => array(), 'rows' => $rows, 'attributes' => array('class' => 'panels-manage'))),
- 'class' => 'dashboard-pages',
- 'section' => 'right',
- );
- }
- /**
- * Implement pseudo-hook to fetch addressable content.
- *
- * For Page Manager, the address will be an array. The first
- * element will be the $task and the second element will be the
- * $task_handler. The third elements will be the arguments
- * provided.
- */
- function page_manager_addressable_content($address, $type) {
- if (count($address) < 3) {
- return;
- }
- $task_name = array_shift($address);
- $subtask_name = array_shift($address);
- $handler_name = array_shift($address);
- if ($address) {
- $arguments = array_shift($address);
- }
- // Since $arguments is an array of arbitrary size, we need to implode it:
- if (!empty($arguments)) {
- // The only choices we have for separators since :: is already
- // used involve ., - or _. Since - and _ are more common than .
- // in URLs, let's try .. as an argument separator.
- $arguments = explode('..', $arguments);
- }
- else {
- // implode does not return an empty array on an empty
- // string so do it specifically.
- $arguments = array();
- }
- $task = page_manager_get_task($task_name);
- if (!$task) {
- return;
- }
- $handler = page_manager_load_task_handler($task, $subtask_name, $handler_name);
- if (!$handler) {
- return;
- }
- $handler_plugin = page_manager_get_task_handler($handler->handler);
- if (!$handler_plugin) {
- return;
- }
- // Load the contexts for the task.
- ctools_include('context');
- ctools_include('context-task-handler');
- $contexts = ctools_context_handler_get_task_contexts($task, $subtask_name, $arguments);
- // With contexts loaded, ensure the task is accessible. Tasks without a callback
- // are automatically accessible.
- $function = ctools_plugin_get_function($task, 'access callback');
- if ($function && !$function($task, $subtask_name, $contexts)) {
- return;
- }
- $function = ctools_plugin_get_function($handler_plugin, 'addressable callback');
- if ($function) {
- return $function($task, $subtask_name, $handler, $address, $contexts, $arguments, $type);
- }
- }
|