landing_page.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * @file
  4. * A page creation wizard to quickly create simple landing pages.
  5. *
  6. * This wizard strips out a lot of features that are not normally needed for
  7. * simple landing pages, such as access control, tabs, contexts, and the
  8. * selection of a variant. It will just assume you want a panel and let you
  9. * select the layout right away. This is 2 fewer forms than the standard
  10. * add page process and it will point you at the finished page rather than
  11. * sending you directly to the edit UI when finished.
  12. *
  13. * It also will auto-enable IPE if it can.
  14. */
  15. $plugin = array(
  16. 'title' => t('Landing page'),
  17. 'page title' => t('Landing page wizard'),
  18. 'description' => t('Landing pages are simple pages that have a path, possibly a visible menu entry, and a panel layout with simple content.'),
  19. 'type' => 'panels',
  20. 'form info' => array(
  21. 'order' => array(
  22. 'basic' => t('Basic settings'),
  23. 'content' => t('Content'),
  24. ),
  25. 'forms' => array(
  26. 'basic' => array(
  27. 'form id' => 'panels_landing_page_basic',
  28. ),
  29. 'content' => array(
  30. 'form id' => 'panels_landing_page_content',
  31. ),
  32. ),
  33. ),
  34. 'default cache' => 'panels_landing_page_new_page',
  35. 'finish' => 'panels_landing_page_finish',
  36. );
  37. /**
  38. * Provide defaults for a new cache.
  39. *
  40. * The cache will store all our temporary data; it isn't really a page
  41. * in itself, but it does contain everything we need to make one at the end.
  42. */
  43. function panels_landing_page_new_page(&$cache) {
  44. $cache->name = '';
  45. $cache->admin_title = '';
  46. $cache->admin_description = '';
  47. $cache->path = '';
  48. $cache->menu_entry = FALSE;
  49. $cache->menu = array(
  50. 'type' => 'none',
  51. 'title' => '',
  52. 'weight' => 0,
  53. 'name' => 'navigation',
  54. 'parent' => array(
  55. 'type' => 'none',
  56. 'title' => '',
  57. 'weight' => 0,
  58. 'name' => 'navigation',
  59. ),
  60. );
  61. $cache->display = panels_new_display();
  62. $cache->display->layout = 'flexible';
  63. $cache->display->storage_type = 'page_manager';
  64. $cache->display->storage_id = 'new';
  65. }
  66. /**
  67. * First page of our page creator wizard.
  68. */
  69. function panels_landing_page_basic($form, &$form_state) {
  70. $cache = &$form_state['wizard cache'];
  71. ctools_include('dependent');
  72. $form['admin_title'] = array(
  73. '#type' => 'textfield',
  74. '#title' => t('Administrative title'),
  75. '#description' => t('The name of this page. This will appear in the administrative interface to easily identify it.'),
  76. '#default_value' => $cache->admin_title,
  77. '#required' => TRUE,
  78. );
  79. $form['name'] = array(
  80. '#type' => 'machine_name',
  81. '#title' => t('Machine name'),
  82. '#machine_name' => array(
  83. 'exists' => 'page_manager_page_load',
  84. 'source' => array('admin_title'),
  85. ),
  86. '#description' => t('The machine readable name of this page. It must be unique, and it must contain only alphanumeric characters and underscores. Once created, you will not be able to change this value!'),
  87. '#default_value' => $cache->name,
  88. );
  89. $form['admin_description'] = array(
  90. '#type' => 'textarea',
  91. '#title' => t('Administrative description'),
  92. '#description' => t('A description of what this page is, does or is for, for administrative use.'),
  93. '#default_value' => $cache->admin_description,
  94. );
  95. // path
  96. $form['path'] = array(
  97. '#type' => 'textfield',
  98. '#title' => t('Path'),
  99. '#default_value' => $cache->path,
  100. '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
  101. '#required' => TRUE,
  102. );
  103. $form['menu_entry'] = array(
  104. '#type' => 'checkbox',
  105. '#default_value' => $cache->menu_entry,
  106. '#title' => t('Add a visible menu entry for this page'),
  107. );
  108. $form['menu']['#tree'] = TRUE;
  109. $form['menu']['title'] = array(
  110. '#title' => t('Menu title'),
  111. '#type' => 'textfield',
  112. '#default_value' => $cache->menu['title'],
  113. '#process' => array('ctools_dependent_process'),
  114. '#dependency' => array('edit-menu-entry' => array(1)),
  115. );
  116. // Only display the menu selector if menu module is enabled.
  117. if (module_exists('menu')) {
  118. $form['menu']['name'] = array(
  119. '#title' => t('Menu'),
  120. '#type' => 'select',
  121. '#options' => menu_get_menus(),
  122. '#default_value' => $cache->menu['name'],
  123. '#process' => array('ctools_dependent_process'),
  124. '#dependency' => array('edit-menu-entry' => array(1)),
  125. );
  126. }
  127. else {
  128. $form['menu']['name'] = array(
  129. '#type' => 'value',
  130. '#value' => $cache->menu['name'],
  131. );
  132. $form['menu']['markup'] = array(
  133. '#value' => t('Menu selection requires the activation of menu module.'),
  134. );
  135. }
  136. $form['menu']['weight'] = array(
  137. '#title' => t('Weight'),
  138. '#type' => 'textfield',
  139. '#default_value' => isset($cache->menu['weight']) ? $cache->menu['weight'] : 0,
  140. '#description' => t('The lower the weight the higher/further left it will appear.'),
  141. '#process' => array('ctools_dependent_process'),
  142. '#dependency' => array('edit-menu-entry' => array(1)),
  143. );
  144. ctools_include('page-wizard', 'panels');
  145. panels_page_wizard_add_layout($form, $form_state);
  146. // Ensure all 'page' features are loaded.
  147. $page_task = page_manager_get_task('page');
  148. return $form;
  149. }
  150. /**
  151. * Submit function to store the form data in our cache.
  152. */
  153. function panels_landing_page_basic_validate(&$form, &$form_state) {
  154. // Validate that the name is ok.
  155. $test = page_manager_page_load($form_state['values']['name']);
  156. if ($test) {
  157. form_error($form['name'], t('That name is used by another page: @page', array('@page' => $test->admin_title)));
  158. }
  159. // Ensure name fits the rules:
  160. if (preg_match('/[^a-zA-Z0-9_]/', $form_state['values']['name'])) {
  161. form_error($form['name'], t('Page name must be alphanumeric or underscores only.'));
  162. }
  163. // Validate that the path is ok.
  164. if (preg_match('/[%!\?#&]/', $form_state['values']['path'])) {
  165. form_error($form['path'], t('%, !, ?, #, or & cannot appear in the path.'));
  166. }
  167. // Check to see if something is already using the path
  168. $result = db_query("SELECT * FROM {menu_router} WHERE path = :path", array(':path' => $form_state['values']['path']))->fetch();
  169. if ($result) {
  170. form_error($form['path'], t('That path is already in use. This system cannot override existing paths.'));
  171. return;
  172. }
  173. // Ensure the path is not already an alias to something else.
  174. $alias = db_query('SELECT alias, source FROM {url_alias} WHERE alias = :path', array(':path' => $form_state['values']['path']))->fetchObject();
  175. if ($alias) {
  176. form_error($form['path'], t('That path is currently assigned to be an alias for @alias. This system cannot override existing aliases.', array('@alias' => $alias->src)));
  177. }
  178. }
  179. /**
  180. * Submit function to store the form data in our cache.
  181. */
  182. function panels_landing_page_basic_submit(&$form, &$form_state) {
  183. $cache = &$form_state['wizard cache'];
  184. $cache->name = $form_state['values']['name'];
  185. $cache->admin_title = $form_state['values']['admin_title'];
  186. $cache->admin_description = $form_state['values']['admin_description'];
  187. $cache->path = $form_state['values']['path'];
  188. $cache->menu_entry = $form_state['values']['menu_entry'];
  189. $cache->menu['title'] = $form_state['values']['menu']['title'];
  190. $cache->menu['weight'] = $form_state['values']['menu']['weight'];
  191. $cache->menu['name'] = $form_state['values']['menu']['name'];
  192. $cache->menu['type'] = $cache->menu_entry ? 'normal' : 'none';
  193. $cache->display->layout = $form_state['values']['layout'];
  194. $cache->display->title = $form_state['values']['admin_title'];
  195. }
  196. /**
  197. * Second page of our wizard. This one provides a layout and lets the
  198. * user add content.
  199. */
  200. function panels_landing_page_content($form, &$form_state) {
  201. ctools_include('page-wizard', 'panels');
  202. panels_page_wizard_add_content($form, $form_state);
  203. return $form;
  204. }
  205. /**
  206. * Submit function to store the form data in our cache.
  207. */
  208. function panels_landing_page_submit(&$form, &$form_state) {
  209. panels_page_wizard_add_content_submit($form, $form_state);
  210. }
  211. /**
  212. * Finish callback for the wizard.
  213. *
  214. * When the wizard is finished, this callback will create the actual
  215. * page, save it, and redirect the user to view the new work.
  216. */
  217. function panels_landing_page_finish(&$form_state) {
  218. $cache = &$form_state['wizard cache'];
  219. // Ensure all 'page' features are loaded.
  220. $page_task = page_manager_get_task('page');
  221. // Assemble a new page subtask.
  222. $subtask = page_manager_page_new();
  223. $subtask->name = $cache->name;
  224. $subtask->path = $cache->path;
  225. $subtask->admin_title = $cache->admin_title;
  226. $subtask->admin_description = $cache->admin_description;
  227. $subtask->path = $cache->path;
  228. $subtask->menu = $cache->menu;
  229. // Create the the panel context variant configured with our display
  230. $plugin = page_manager_get_task_handler('panel_context');
  231. // Set the storage id.
  232. $cache->display->storage_id = $cache->name;
  233. // Create a new handler.
  234. $handler = page_manager_new_task_handler($plugin);
  235. $handler->conf['title'] = t('Landing page');
  236. $handler->conf['display'] = $cache->display;
  237. $handler->conf['pipeline'] = 'ipe';
  238. // Assemble a new $page cache and assign it our page subtask and task
  239. // handler.
  240. $page = new stdClass();
  241. page_manager_page_new_page_cache($subtask, $page);
  242. page_manager_handler_add_to_page($page, $handler);
  243. // Save it
  244. page_manager_save_page_cache($page);
  245. // Send us to the new page immediately.
  246. $form_state['redirect'] = url($cache->path);
  247. }