page-wizard.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * @file
  4. */
  5. /**
  6. * Fetch metadata on a specific page_wizard plugin.
  7. *
  8. * @param $page_wizard
  9. * Name of a panel page_wizard.
  10. *
  11. * @return
  12. * An array with information about the requested panel page_wizard.
  13. */
  14. function page_manager_get_page_wizard($page_wizard) {
  15. ctools_include('plugins');
  16. return ctools_get_plugins('page_manager', 'page_wizards', $page_wizard);
  17. }
  18. /**
  19. * Fetch metadata for all page_wizard plugins.
  20. *
  21. * @return
  22. * An array of arrays with information about all available panel page_wizards.
  23. */
  24. function page_manager_get_page_wizards() {
  25. ctools_include('plugins');
  26. return ctools_get_plugins('page_manager', 'page_wizards');
  27. }
  28. /**
  29. * Get the cached changes to a given wizard.
  30. *
  31. * @return
  32. * A $cache object or a clean cache object if none could be loaded.
  33. */
  34. function page_manager_get_wizard_cache($plugin) {
  35. if (is_string($plugin)) {
  36. $plugin = page_manager_get_page_wizard($plugin);
  37. }
  38. if (empty($plugin)) {
  39. return;
  40. }
  41. ctools_include('object-cache');
  42. // Since contexts might be cache, include this so they load.
  43. ctools_include('context');
  44. $cache = ctools_object_cache_get('page_manager_page_wizard', $plugin['name']);
  45. if (!$cache) {
  46. $cache = page_manager_make_wizard_cache($plugin);
  47. }
  48. return $cache;
  49. }
  50. function page_manager_make_wizard_cache($plugin) {
  51. $cache = new stdClass();
  52. $cache->plugin = $plugin;
  53. if ($function = ctools_plugin_get_function($plugin, 'default cache')) {
  54. $function($cache);
  55. }
  56. return $cache;
  57. }
  58. /**
  59. * Store changes to a task handler in the object cache.
  60. */
  61. function page_manager_set_wizard_cache($cache) {
  62. ctools_include('object-cache');
  63. ctools_object_cache_set('page_manager_page_wizard', $cache->plugin['name'], $cache);
  64. }
  65. /**
  66. * Remove an item from the object cache.
  67. */
  68. function page_manager_clear_wizard_cache($name) {
  69. ctools_include('object-cache');
  70. ctools_object_cache_clear('page_manager_page_wizard', $name);
  71. }
  72. /**
  73. * Menu callback for the page wizard.
  74. */
  75. function page_manager_page_wizard($name, $step = NULL) {
  76. $plugin = page_manager_get_page_wizard($name);
  77. if (!$plugin) {
  78. return MENU_NOT_FOUND;
  79. }
  80. // Check for simple access string on plugin.
  81. if (!empty($plugin['access']) && !user_access($plugin['access'])) {
  82. return MENU_ACCESS_DENIED;
  83. }
  84. // Check for possibly more complex access callback on plugin.
  85. if ($function = ctools_plugin_get_function($plugin, 'access callback') && !$function($plugin)) {
  86. return MENU_ACCESS_DENIED;
  87. }
  88. // Create a basic wizard.in form info array and merge it with the
  89. // plugin's.
  90. $form_info = array(
  91. 'id' => 'page_manager_page_wizard',
  92. 'show trail' => TRUE,
  93. 'show back' => TRUE,
  94. 'show return' => FALSE,
  95. 'show cancel' => FALSE,
  96. 'next callback' => 'page_manager_page_wizard_next',
  97. 'finish callback' => 'page_manager_page_wizard_finish',
  98. 'path' => "admin/structure/pages/wizard/$name/%step",
  99. );
  100. $form_info = array_merge_recursive($form_info, $plugin['form info']);
  101. // If step is unset, go with the basic step.
  102. if (!isset($step)) {
  103. $step = current(array_keys($form_info['order']));
  104. $cache = page_manager_make_wizard_cache($plugin);
  105. }
  106. else {
  107. $cache = page_manager_get_wizard_cache($plugin);
  108. }
  109. ctools_include('wizard');
  110. $form_state = array(
  111. 'plugin' => $plugin,
  112. 'wizard cache' => $cache,
  113. 'type' => 'edit',
  114. 'rerender' => TRUE,
  115. 'step' => $step,
  116. );
  117. if (isset($plugin['page title'])) {
  118. drupal_set_title($plugin['page title']);
  119. }
  120. if ($function = ctools_plugin_get_function($form_state['plugin'], 'start')) {
  121. $function($form_info, $step, $form_state);
  122. }
  123. $output = ctools_wizard_multistep_form($form_info, $step, $form_state);
  124. return $output;
  125. }
  126. /**
  127. * Callback generated when the add page process is finished.
  128. */
  129. function page_manager_page_wizard_finish(&$form_state) {
  130. if ($function = ctools_plugin_get_function($form_state['plugin'], 'finish')) {
  131. $function($form_state);
  132. }
  133. page_manager_clear_wizard_cache($form_state['wizard cache']->plugin['name']);
  134. }
  135. /**
  136. * Callback generated when the 'next' button is clicked.
  137. *
  138. * All we do here is store the cache.
  139. */
  140. function page_manager_page_wizard_next(&$form_state) {
  141. if ($function = ctools_plugin_get_function($form_state['plugin'], 'next')) {
  142. $function($form_state);
  143. }
  144. page_manager_set_wizard_cache($form_state['wizard cache']);
  145. }
  146. /**
  147. * Provide a simple administrative list of all wizards.
  148. *
  149. * This is called as a page callback, but can also be used by any module
  150. * that wants to get a list of wizards for its type.
  151. */
  152. function page_manager_page_wizard_list($type = NULL) {
  153. $plugins = page_manager_get_page_wizards();
  154. if (empty($plugins)) {
  155. return '<p>' . t('There are no wizards available at this time.') . '</p>';
  156. }
  157. uasort($plugins, 'ctools_plugin_sort');
  158. $output = '<dl class="page-manager-wizards">';
  159. foreach ($plugins as $id => $plugin) {
  160. if (!$type || (isset($plugin['type']) && $plugin['type'] == $type)) {
  161. $output .= '<dt>' . l($plugin['title'], 'admin/structure/pages/wizard/' . $id) . '</dt>';
  162. $output .= '<dd class="description">' . $plugin['description'] . '</dd>';
  163. }
  164. }
  165. $output .= '</dl>';
  166. return $output;
  167. }