5 * Handle the 'page' task, which creates pages with arbitrary tasks and lets
6 * handlers decide how they will be rendered.
8 * This creates subtasks and stores them in the page_manager_pages table. These
9 * are exportable objects, too.
11 * The render callback for this task type has $handler, $page, $contexts as
16 * Specialized implementation of hook_page_manager_task_tasks(). See api-task.html for
19 function page_manager_page_page_manager_tasks() {
21 'title' => t('Custom pages'),
22 'description' => t('Administrator created pages that have a URL path, access control and entries in the Drupal menu system.'),
23 'non-exportable' => TRUE,
25 'subtask callback' => 'page_manager_page_subtask',
26 'subtasks callback' => 'page_manager_page_subtasks',
27 'save subtask callback' => 'page_manager_page_save_subtask',
28 'access callback' => 'page_manager_page_access_check',
30 'file' => 'page.admin.inc',
31 'path' => drupal_get_path('module', 'page_manager') . '/plugins/tasks',
32 'function' => 'page_manager_page_menu',
34 'hook theme' => 'page_manager_page_theme',
36 'task type' => 'page',
37 'page operations' => array(
39 'title' => ' » ' . t('Create a new page'),
40 'href' => 'admin/structure/pages/add',
46 'label' => t('Storage'),
47 'class' => 'page-manager-page-storage',
50 'page type' => 'custom',
53 'handler type' => 'context',
54 'get arguments' => array(
55 'file' => 'page.admin.inc',
56 'path' => drupal_get_path('module', 'page_manager') . '/plugins/tasks',
57 'function' => 'page_manager_page_get_arguments',
59 'get context placeholders' => 'page_manager_page_get_contexts',
60 'access restrictions' => 'page_manager_page_access_restrictions',
61 'uses handlers' => TRUE,
66 * Task callback to get all subtasks.
68 * Return a list of all subtasks.
70 function page_manager_page_subtasks($task) {
71 $pages = page_manager_page_load_all($task['name']);
73 foreach ($pages as $name => $page) {
74 $return[$name] = page_manager_page_build_subtask($task, $page);
81 * Callback to return a single subtask.
83 function page_manager_page_subtask($task, $subtask_id) {
84 $page = page_manager_page_load($subtask_id);
86 return page_manager_page_build_subtask($task, $page);
91 * Call back from the administrative system to save a page.
93 * We get the $subtask as created by page_manager_page_build_subtask.
95 function page_manager_page_save_subtask($subtask) {
96 $page = &$subtask['subtask'];
98 // Ensure $page->arguments contains only real arguments:
99 $arguments = page_manager_page_get_named_arguments($page->path);
101 foreach ($arguments as $keyword => $position) {
102 if (isset($page->arguments[$keyword])) {
103 $args[$keyword] = $page->arguments[$keyword];
106 $args[$keyword] = array(
110 'settings' => array(),
114 page_manager_page_recalculate_arguments($page);
115 // Create a real object from the cache
116 page_manager_page_save($page);
118 // Check to see if we should make this the site frontpage.
119 if (!empty($page->make_frontpage)) {
121 foreach (explode('/', $page->path) as $bit) {
122 if ($bit[0] != '!') {
127 $path = implode('/', $path);
128 $front = variable_get('site_frontpage', 'node');
129 if ($path != $front) {
130 variable_set('site_frontpage', $path);
136 * Build a subtask array for a given page.
138 function page_manager_page_build_subtask($task, $page) {
139 $operations = array();
140 $operations['settings'] = array(
142 'class' => array('operations-settings'),
143 'title' => t('Settings'),
144 'children' => array(),
147 $settings = &$operations['settings']['children'];
149 $settings['basic'] = array(
150 'title' => t('Basic'),
151 'description' => t('Edit name, path and other basic settings for the page.'),
152 'form' => 'page_manager_page_form_basic',
155 $arguments = page_manager_page_get_named_arguments($page->path);
157 $settings['argument'] = array(
158 'title' => t('Arguments'),
159 'description' => t('Set up contexts for the arguments on this page.'),
160 'form' => 'page_manager_page_form_argument',
164 $settings['access'] = array(
165 'title' => t('Access'),
166 'description' => t('Control what users can access this page.'),
167 'admin description' => t('Access rules are used to test if the page is accessible and any menu items associated with it are visible.'),
168 'form' => 'page_manager_page_form_access',
171 $settings['menu'] = array(
172 'title' => t('Menu'),
173 'description' => t('Provide this page a visible menu or a menu tab.'),
174 'form' => 'page_manager_page_form_menu',
177 $operations['actions']['children']['clone'] = array(
178 'title' => t('Clone'),
179 'description' => t('Make a copy of this page'),
180 'form' => 'page_manager_page_form_clone',
182 $operations['actions']['children']['export'] = array(
183 'title' => t('Export'),
184 'description' => t('Export this page as code that can be imported or embedded into a module.'),
185 'form' => 'page_manager_page_form_export',
187 if ($page->export_type == (EXPORT_IN_CODE | EXPORT_IN_DATABASE)) {
188 $operations['actions']['children']['delete'] = array(
189 'title' => t('Revert'),
190 'description' => t('Remove all changes to this page and revert to the version in code.'),
191 'form' => 'page_manager_page_form_delete',
194 else if ($page->export_type != EXPORT_IN_CODE) {
195 $operations['actions']['children']['delete'] = array(
196 'title' => t('Delete'),
197 'description' => t('Remove this page from your system completely.'),
198 'form' => 'page_manager_page_form_delete',
203 'name' => $page->name,
204 'admin title' => check_plain($page->admin_title),
205 'admin description' => filter_xss_admin($page->admin_description),
206 'admin summary' => 'page_manager_page_admin_summary',
207 'admin path' => $page->path,
208 'admin type' => t('Custom'),
210 'operations' => $operations,
211 'operations include' => array(
212 'file' => 'page.admin.inc',
213 'path' => drupal_get_path('module', 'page_manager') . '/plugins/tasks',
215 'single task' => empty($page->multiple),
216 'row class' => empty($page->disabled) ? 'page-manager-enabled' : 'page-manager-disabled',
217 'storage' => $page->type == t('Default') ? t('In code') : $page->type,
218 'disabled' => !empty($page->disabled),
219 // This works for both enable AND disable
220 'enable callback' => 'page_manager_page_enable',
223 // default handlers may appear from a default subtask.
224 if (isset($page->default_handlers)) {
225 $subtask['default handlers'] = $page->default_handlers;
231 * Delegated implementation of hook_theme().
233 function page_manager_page_theme(&$items, $task) {
235 'file' => 'page.admin.inc',
236 'path' => drupal_get_path('module', 'page_manager') . '/plugins/tasks',
238 $items['page_manager_page_form_argument_table'] = $base + array(
239 'render element' => 'form',
241 $items['page_manager_page_lock'] = $base + array(
242 'variables' => array('lock' => array(), 'task_name' => NULL),
244 $items['page_manager_page_changed'] = $base + array(
245 'variables' => array(),
249 // --------------------------------------------------------------------------
250 // Page execution functions
253 * Execute a page task.
255 * This is the callback to entries in the Drupal menu system created by the
259 * The name of the page task used.
261 * A number of context objects as specified by the user when
262 * creating named arguments in the path.
264 function page_manager_page_execute($subtask_id) {
265 $page = page_manager_page_load($subtask_id);
266 $task = page_manager_get_task($page->task);
267 $subtask = page_manager_get_task_subtask($task, $subtask_id);
269 // Turn the contexts into a properly keyed array.
272 foreach (func_get_args() as $count => $arg) {
273 if (is_object($arg) && get_class($arg) == 'ctools_context') {
274 $contexts[$arg->id] = $arg;
275 $args[] = $arg->original_argument;
283 $names = page_manager_page_get_named_arguments($page->path);
284 $bits = explode('/', $page->path);
286 if ($page->arguments) {
287 foreach ($page->arguments as $name => $argument) {
288 // Optional arguments must be converted to contexts too, if they exist.
289 if ($bits[$names[$name]][0] == '!') {
290 ctools_include('context');
291 $argument['keyword'] = $name;
292 if (isset($args[$count])) {
293 // Hack: use a special argument config variable to learn if we need
294 // to use menu_tail style behavior:
295 if (empty($argument['settings']['use_tail'])) {
296 $value = $args[$count];
299 $value = implode('/', array_slice($args, $count));
302 $context = ctools_context_get_context_from_argument($argument, $value);
305 // make sure there is a placeholder context for missing optional contexts.
306 $context = ctools_context_get_context_from_argument($argument, NULL, TRUE);
307 // Force the title to blank for replacements
310 $contexts[$context->id] = $context;
317 if ($function = ctools_plugin_get_function($task, 'page callback')) {
318 return call_user_func_array($function, array($page, $contexts, $args));
321 ctools_include('context-task-handler');
322 $output = ctools_context_handler_render($task, $subtask, $contexts, $args);
323 if ($output === FALSE) {
324 return MENU_NOT_FOUND;
330 // --------------------------------------------------------------------------
331 // Context type callbacks
334 * Return a list of arguments used by this task.
336 function page_manager_page_get_arguments($task, $subtask) {
337 return _page_manager_page_get_arguments($subtask['subtask']);
340 function _page_manager_page_get_arguments($page) {
341 $arguments = array();
342 if (!empty($page->arguments)) {
343 foreach ($page->arguments as $keyword => $argument) {
344 if (isset($argument['name'])) {
345 $argument['keyword'] = $keyword;
346 $arguments[$keyword] = $argument;
354 * Get a group of context placeholders for the arguments.
356 function page_manager_page_get_contexts($task, $subtask) {
357 ctools_include('context');
358 return ctools_context_get_placeholders_from_argument(page_manager_page_get_arguments($task, $subtask));
362 * Return a list of arguments used by this task.
364 function page_manager_page_access_restrictions($task, $subtask, $contexts) {
365 $page = $subtask['subtask'];
366 return ctools_access_add_restrictions($page->access, $contexts);
369 // --------------------------------------------------------------------------
370 // Page task database info.
373 * Create a new page with defaults appropriately set from schema.
375 function page_manager_page_new() {
376 ctools_include('export');
377 return ctools_export_new_object('page_manager_pages');
381 * Load a single page subtask.
383 function page_manager_page_load($name) {
384 ctools_include('export');
385 $result = ctools_export_load_object('page_manager_pages', 'names', array($name));
386 if (isset($result[$name])) {
387 return $result[$name];
392 * Load all page subtasks.
394 function page_manager_page_load_all($task = NULL) {
395 ctools_include('export');
398 return ctools_export_load_object('page_manager_pages');
401 return ctools_export_load_object('page_manager_pages', 'conditions', array('task' => $task));
406 * Write a page subtask to the database.
408 function page_manager_page_save(&$page) {
409 $update = (isset($page->pid)) ? array('pid') : array();
410 $task = page_manager_get_task($page->task);
412 if ($function = ctools_plugin_get_function($task, 'save')) {
413 $function($page, $update);
415 drupal_write_record('page_manager_pages', $page, $update);
417 // If this was a default page we may need to write default task
418 // handlers that we provided as well.
419 if (!$update && isset($page->default_handlers)) {
420 $handlers = page_manager_load_task_handlers(page_manager_get_task('page'), $page->name);
421 foreach ($page->default_handlers as $name => $handler) {
422 if (!isset($handlers[$name]) || !($handlers[$name]->export_type & EXPORT_IN_DATABASE)) {
423 // Make sure this is right, as exports can wander a bit.
424 $handler->subtask = $page->name;
425 page_manager_save_task_handler($handler);
433 * Remove a page subtask.
435 function page_manager_page_delete($page) {
436 $task = page_manager_get_task($page->task);
437 if ($function = ctools_plugin_get_function($task, 'delete')) {
440 if (!empty($task['uses handlers'])) {
441 $handlers = page_manager_load_task_handlers($task, $page->name);
442 foreach ($handlers as $handler) {
443 page_manager_delete_task_handler($handler);
446 db_delete('page_manager_pages')
447 ->condition('name', $page->name)
449 // Make sure that the cache is reset so that the menu rebuild does not
450 // rebuild this page again.
451 ctools_include('export');
452 ctools_export_load_object_reset('page_manager_pages');
457 * Export a page subtask.
459 function page_manager_page_export($page, $with_handlers = FALSE, $indent = '') {
460 $task = page_manager_get_task($page->task);
463 if ($function = ctools_plugin_get_function($task, 'export')) {
464 $append = $function($page, $indent);
467 ctools_include('export');
468 $output = ctools_export_object('page_manager_pages', $page, $indent);
471 if ($with_handlers) {
472 if (is_array($with_handlers)) {
473 $handlers = $with_handlers;
476 $handlers = page_manager_load_task_handlers(page_manager_get_task('page'), $page->name);
478 $output .= $indent . '$page->default_handlers = array();' . "\n";
479 foreach ($handlers as $handler) {
480 $output .= page_manager_export_task_handler($handler, $indent);
481 $output .= $indent . '$page->default_handlers[$handler->name] = $handler;' . "\n";
488 * Get a list of named arguments in a page manager path.
491 * A normal Drupal path.
494 * An array of % marked variable arguments, keyed by the argument's name.
495 * The value will be the position of the argument so that it can easily
496 * be found. Items with a position of -1 have multiple positions.
498 function page_manager_page_get_named_arguments($path) {
499 $arguments = array();
500 $bits = explode('/', $path);
501 foreach ($bits as $position => $bit) {
502 if ($bit && ($bit[0] == '%' || $bit[0] == '!')) {
503 // special handling for duplicate path items and substr to remove the %
504 $arguments[substr($bit, 1)] = isset($arguments[$bit]) ? -1 : $position;
512 * Load a context from an argument for a given page task.
514 * Helper function for pm_arg_load(), which is in page_manager.module because
515 * drupal's menu system does not allow loader functions to reside in separate
519 * The incoming argument value.
523 * The numeric position of the argument in the path, counting from 0.
526 * A context item if one is configured, the argument if one is not, or
527 * FALSE if restricted or invalid.
529 function _pm_arg_load($value, $subtask, $argument) {
530 $page = page_manager_page_load($subtask);
535 $path = explode('/', $page->path);
536 if (empty($path[$argument])) {
540 $keyword = substr($path[$argument], 1);
541 if (empty($page->arguments[$keyword])) {
545 $page->arguments[$keyword]['keyword'] = $keyword;
547 ctools_include('context');
548 $context = ctools_context_get_context_from_argument($page->arguments[$keyword], $value);
550 // convert false equivalents to false.
551 return $context ? $context : FALSE;
555 * Provide a nice administrative summary of the page so an admin can see at a
556 * glance what this page does and how it is configured.
558 function page_manager_page_admin_summary($task, $subtask) {
559 $task_name = page_manager_make_task_name($task['name'], $subtask['name']);
560 $page = $subtask['subtask'];
566 array('class' => array('page-summary-label'), 'data' => t('Storage')),
567 array('class' => array('page-summary-data'), 'data' => $subtask['storage']),
568 array('class' => array('page-summary-operation'), 'data' => ''),
571 if (!empty($page->disabled)) {
572 $link = l(t('Enable'), page_manager_edit_url($task_name, array('handlers', $page->name, 'actions', 'enable')));
573 $text = t('Disabled');
576 $link = l(t('Disable'), page_manager_edit_url($task_name, array('handlers', $page->name, 'actions', 'disable')));
577 $text = t('Enabled');
581 array('class' => array('page-summary-label'), 'data' => t('Status')),
582 array('class' => array('page-summary-data'), 'data' => $text),
583 array('class' => array('page-summary-operation'), 'data' => $link),
588 foreach (explode('/', $page->path) as $bit) {
589 if ($bit[0] != '!') {
594 $path = implode('/', $path);
595 $front = variable_get('site_frontpage', 'node');
597 $link = l(t('Edit'), page_manager_edit_url($task_name, array('settings', 'basic')));
599 if ($path == $front) {
600 $message = t('This is your site home page.');
602 else if (!empty($page->make_frontpage)) {
603 $message = t('This page is set to become your site home page.');
608 array('class' => array('page-summary-data'), 'data' => $message, 'colspan' => 2),
609 array('class' => array('page-summary-operation'), 'data' => $link),
613 if (strpos($path, '%') === FALSE) {
614 $path = l('/' . $page->path, $path);
617 $path = '/' . $page->path;
621 array('class' => array('page-summary-label'), 'data' => t('Path')),
622 array('class' => array('page-summary-data'), 'data' => $path),
623 array('class' => array('page-summary-operation'), 'data' => $link),
626 if (empty($access['plugins'])) {
627 $access['plugins'] = array();
630 $contexts = page_manager_page_get_contexts($task, $subtask);
631 $access = ctools_access_group_summary($page->access, $contexts);
633 $access = t('Accessible only if @conditions.', array('@conditions' => $access));
636 $access = t('This page is publicly accessible.');
639 $link = l(t('Edit'), page_manager_edit_url($task_name, array('settings', 'access')));
642 array('class' => array('page-summary-label'), 'data' => t('Access')),
643 array('class' => array('page-summary-data'), 'data' => $access),
644 array('class' => array('page-summary-operation'), 'data' => $link),
647 $menu_options = array(
648 'none' => t('No menu entry.'),
649 'normal' => t('Normal menu entry.'),
650 'tab' => t('Menu tab.'),
651 'default tab' => t('Default menu tab.'),
652 'action' => t('Local action'),
655 if (!empty($page->menu)) {
656 $menu = $menu_options[$page->menu['type']];
657 if ($page->menu['type'] != 'none') {
658 $menu .= ' ' . t('Title: %title.', array('%title' => $page->menu['title']));
659 switch ($page->menu['type']) {
661 $menu .= ' ' . t('Parent title: %title.', array('%title' => $page->menu['parent']['title']));
664 if (module_exists('menu')) {
665 $menus = menu_get_menus();
666 $menu .= ' ' . t('Menu block: %title.', array('%title' => $menus[$page->menu['name']]));
673 $menu = t('No menu entry');
676 $link = l(t('Edit'), page_manager_edit_url($task_name, array('settings', 'menu')));
678 array('class' => array('page-summary-label'), 'data' => t('Menu')),
679 array('class' => array('page-summary-data'), 'data' => $menu),
680 array('class' => array('page-summary-operation'), 'data' => $link),
683 $output .= theme('table', array('rows' => $rows, 'attributes' => array('id' => 'page-manager-page-summary')));
688 * Callback to enable/disable the page from the UI.
690 function page_manager_page_enable(&$cache, $status) {
691 $page = &$cache->subtask['subtask'];
692 ctools_include('export');
693 ctools_export_set_object_status($page, $status);
695 $page->disabled = FALSE;
699 * Recalculate the arguments when something like the path changes.
701 function page_manager_page_recalculate_arguments(&$page) {
702 // Ensure $page->arguments contains only real arguments:
703 $arguments = page_manager_page_get_named_arguments($page->path);
705 foreach ($arguments as $keyword => $position) {
706 if (isset($page->arguments[$keyword])) {
707 $args[$keyword] = $page->arguments[$keyword];
710 $args[$keyword] = array(
714 'settings' => array(),
718 $page->arguments = $args;
722 * When adding or cloning a new page, this creates a new page cache
723 * and adds our page to it.
725 * This does not check to see if the existing cache is already locked.
726 * This must be done beforehand.
729 * The page to create.
731 * The cache to use. If the cache has any existing task handlers,
732 * they will be marked for deletion. This may be a blank object.
734 function page_manager_page_new_page_cache(&$page, &$cache) {
735 // Does a page already exist? If so, we are overwriting it so
737 if (!empty($cache->subtask) && !empty($cache->subtask['subtask']) && !empty($cache->subtask['subtask']->pid)) {
738 $page->pid = $cache->subtask['subtask']->pid;
744 $cache->task_name = page_manager_make_task_name('page', $page->name);
745 $cache->task_id = 'page';
746 $cache->task = page_manager_get_task('page');
747 $cache->subtask_id = $page->name;
748 $page->export_type = EXPORT_IN_DATABASE;
749 $page->type = t('Normal');
750 $cache->subtask = page_manager_page_build_subtask($cache->task, $page);
752 if (isset($cache->handlers)) {
753 foreach($cache->handlers as $id => $handler) {
754 $cache->handler_info[$id]['changed'] = PAGE_MANAGER_CHANGED_DELETED;
758 $cache->handlers = array();
759 $cache->handler_info = array();
762 if (!empty($page->default_handlers)) {
763 foreach ($page->default_handlers as $id => $handler) {
764 page_manager_handler_add_to_page($cache, $handler);
768 $cache->locked = FALSE;
769 $cache->changed = TRUE;
773 * Callback to determine if a page is accessible.
780 * The contexts loaded for the task.
782 * TRUE if the current user can access the page.
784 function page_manager_page_access_check($task, $subtask_id, $contexts) {
785 $page = page_manager_page_load($subtask_id);
786 return ctools_access($page->access, $contexts);