page-wizard.inc 5.1 KB

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