page.admin.inc 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  1. <?php
  2. /**
  3. * @file
  4. * Administrative functions for the page subtasks.
  5. *
  6. * These are attached to the menu system in page.inc via the hook_menu
  7. * delegation. They are included here so that this code is loaded
  8. * only when needed.
  9. */
  10. /**
  11. * Delegated implementation of hook_menu().
  12. */
  13. function page_manager_page_menu(&$items, $task) {
  14. // Set up access permissions.
  15. $access_callback = isset($task['admin access callback']) ? $task['admin access callback'] : 'user_access';
  16. $access_arguments = isset($task['admin access arguments']) ? $task['admin access arguments'] : array('administer page manager');
  17. $base = array(
  18. 'access callback' => $access_callback,
  19. 'access arguments' => $access_arguments,
  20. 'file' => 'plugins/tasks/page.admin.inc',
  21. );
  22. $items['admin/structure/pages/add'] = array(
  23. 'title' => 'Add custom page',
  24. 'page callback' => 'page_manager_page_add_subtask',
  25. 'page arguments' => array(),
  26. 'type' => MENU_LOCAL_ACTION,
  27. ) + $base;
  28. $items['admin/structure/pages/import'] = array(
  29. 'title' => 'Import page',
  30. 'page callback' => 'drupal_get_form',
  31. 'page arguments' => array('page_manager_page_import_subtask', 'page'),
  32. 'type' => MENU_LOCAL_ACTION,
  33. ) + $base;
  34. if ($access_callback == 'user_access') {
  35. $items['admin/structure/pages/import']['access callback'] = 'ctools_access_multiperm';
  36. $items['admin/structure/pages/import']['access arguments'][] = 'use ctools import';
  37. }
  38. // AJAX callbacks for argument modal.
  39. $items['admin/structure/pages/argument'] = array(
  40. 'page callback' => 'page_manager_page_subtask_argument_ajax',
  41. 'type' => MENU_CALLBACK,
  42. ) + $base;
  43. // Add menu entries for each subtask
  44. foreach (page_manager_page_load_all() as $subtask_id => $subtask) {
  45. if (!empty($subtask->disabled)) {
  46. continue;
  47. }
  48. if (!isset($subtask->access['type'])) {
  49. $subtask->access['type'] = 'none';
  50. }
  51. if (!isset($subtask->access['settings'])) {
  52. $subtask->access['settings'] = NULL;
  53. }
  54. $path = array();
  55. $page_arguments = array((string) $subtask_id);
  56. $access_arguments = array($subtask->access);
  57. $load_arguments = array($subtask_id, '%index', '%map');
  58. // Replace named placeholders with our own placeholder to load contexts.
  59. $position = 0;
  60. foreach (explode('/', $subtask->path) as $bit) {
  61. // Remove things like double slashes completely.
  62. if (!isset($bit) || $bit === '') {
  63. continue;
  64. }
  65. if ($bit[0] == '%' && $bit != '%') {
  66. $placeholder = '%pm_arg';
  67. // Chop off that %.
  68. $name = substr($bit, 1);
  69. // Check to see if the argument plugin wants to use a different
  70. // placholder. This will allow to_args.
  71. if (!empty($subtask->arguments[$name])) {
  72. ctools_include('context');
  73. if (!empty($subtask->arguments[$name]['name'])) {
  74. $plugin = ctools_get_argument($subtask->arguments[$name]['name']);
  75. if (isset($plugin['path placeholder'])) {
  76. if (function_exists($plugin['path placeholder'])) {
  77. $placeholder = $plugin['path placeholder']($subtask->arguments[$name]);
  78. }
  79. else {
  80. $placeholder = $plugin['path placeholder'];
  81. }
  82. }
  83. }
  84. }
  85. // If an argument, swap it out with our argument loader and make sure
  86. // the argument gets passed through to the page callback.
  87. $path[] = $placeholder;
  88. $page_arguments[] = $position;
  89. $access_arguments[] = $position;
  90. }
  91. else if ($bit[0] != '!') {
  92. $path[] = $bit;
  93. }
  94. // Increment position. We do it like this to skip empty items that
  95. // could happen from erroneous paths like: this///that
  96. $position++;
  97. }
  98. $menu_path = implode('/', $path);
  99. $items[$menu_path] = page_manager_page_menu_item($task, $subtask->menu, $access_arguments, $page_arguments, $load_arguments);
  100. // Add a parent menu item if one is configured.
  101. if (isset($subtask->menu['type']) && $subtask->menu['type'] == 'default tab') {
  102. array_pop($path);
  103. $parent_path = implode('/', $path);
  104. $items[$parent_path] = page_manager_page_menu_item($task, $subtask->menu['parent'], $access_arguments, $page_arguments, $load_arguments);
  105. }
  106. }
  107. }
  108. /**
  109. * Create a menu item for page manager pages.
  110. *
  111. * @param $menu
  112. * The configuration to use. It will contain a type, and depending on the
  113. * type may also contain weight, title and name. These are presumed to have
  114. * been configured from the UI.
  115. * @param $access_arguments
  116. * Arguments that go with ctools_access_menu; it should be loaded with
  117. * the access plugin type, settings, and positions of any arguments that
  118. * may produce contexts.
  119. * @param $page_arguments
  120. * This should be seeded with the subtask name for easy loading and like
  121. * the access arguments above should contain positions of arguments so
  122. * that the menu system passes contexts through.
  123. * @param $load_arguments
  124. * Arguments to send to the arg loader; should be the subtask id and '%index'.
  125. */
  126. function page_manager_page_menu_item($task, $menu, $access_arguments, $page_arguments, $load_arguments) {
  127. $item = array(
  128. 'access callback' => 'ctools_access_menu',
  129. 'access arguments' => $access_arguments,
  130. 'page callback' => 'page_manager_page_execute',
  131. 'page arguments' => $page_arguments,
  132. 'load arguments' => $load_arguments,
  133. 'file' => 'plugins/tasks/page.inc',
  134. );
  135. if (isset($menu['title'])) {
  136. $item['title'] = $menu['title'];
  137. }
  138. if (isset($menu['weight'])) {
  139. $item['weight'] = $menu['weight'];
  140. }
  141. if (empty($menu['type'])) {
  142. $menu['type'] = 'none';
  143. }
  144. switch ($menu['type']) {
  145. case 'none':
  146. default:
  147. $item['type'] = MENU_CALLBACK;
  148. break;
  149. case 'normal':
  150. $item['type'] = MENU_NORMAL_ITEM;
  151. // Insert item into the proper menu
  152. $item['menu_name'] = $menu['name'];
  153. break;
  154. case 'tab':
  155. $item['type'] = MENU_LOCAL_TASK;
  156. break;
  157. case 'action':
  158. $item['type'] = MENU_LOCAL_ACTION;
  159. break;
  160. case 'default tab':
  161. $item['type'] = MENU_DEFAULT_LOCAL_TASK;
  162. break;
  163. }
  164. return $item;
  165. }
  166. /**
  167. * Page callback to add a subtask.
  168. */
  169. function page_manager_page_add_subtask($task_name = NULL, $step = NULL) {
  170. ctools_include('context');
  171. $task = page_manager_get_task('page');
  172. $task_handler_plugins = page_manager_get_task_handler_plugins($task);
  173. if (empty($task_handler_plugins)) {
  174. drupal_set_message(t('There are currently no variants available and a page may not be added. Perhaps you need to install the Panels module to get a variant?'), 'error');
  175. return ' ';
  176. }
  177. $form_info = array(
  178. 'id' => 'page_manager_add_page',
  179. 'show trail' => TRUE,
  180. 'show back' => TRUE,
  181. 'show return' => FALSE,
  182. 'next callback' => 'page_manager_page_add_subtask_next',
  183. 'finish callback' => 'page_manager_page_add_subtask_finish',
  184. 'return callback' => 'page_manager_page_add_subtask_finish',
  185. 'cancel callback' => 'page_manager_page_add_subtask_cancel',
  186. 'add order' => array(
  187. 'basic' => t('Basic settings'),
  188. 'argument' => t('Argument settings'),
  189. 'access' => t('Access control'),
  190. 'menu' => t('Menu settings'),
  191. ),
  192. 'forms' => array(
  193. 'basic' => array(
  194. 'form id' => 'page_manager_page_form_basic',
  195. ),
  196. 'access' => array(
  197. 'form id' => 'page_manager_page_form_access',
  198. ),
  199. 'menu' => array(
  200. 'form id' => 'page_manager_page_form_menu',
  201. ),
  202. 'argument' => array(
  203. 'form id' => 'page_manager_page_form_argument',
  204. ),
  205. ),
  206. );
  207. if ($task_name) {
  208. $page = page_manager_get_page_cache($task_name);
  209. if (empty($page)) {
  210. return MENU_NOT_FOUND;
  211. }
  212. $form_info['path'] = "admin/structure/pages/add/$task_name/%step";
  213. }
  214. else {
  215. $new_page = page_manager_page_new();
  216. $new_page->name = NULL;
  217. $page = new stdClass();
  218. page_manager_page_new_page_cache($new_page, $page);
  219. $form_info['path'] = 'admin/structure/pages/add/%task_name/%step';
  220. }
  221. if ($step && $step != 'basic') {
  222. $handler_plugin = page_manager_get_task_handler($page->handler);
  223. $form_info['forms'] += $handler_plugin['forms'];
  224. if (isset($page->forms)) {
  225. foreach ($page->forms as $id) {
  226. if (isset($form_info['add order'][$id])) {
  227. $form_info['order'][$id] = $form_info['add order'][$id];
  228. }
  229. else if (isset($handler_plugin['add features'][$id])) {
  230. $form_info['order'][$id] = $handler_plugin['add features'][$id];
  231. }
  232. else if (isset($handler_plugin['required forms'][$id])) {
  233. $form_info['order'][$id] = $handler_plugin['required forms'][$id];
  234. }
  235. }
  236. }
  237. else {
  238. $form_info['order'] = $form_info['add order'];
  239. }
  240. // This means we just submitted our form from the default list
  241. // of steps, which we've traded in for a newly generated list of
  242. // steps above. We need to translate this 'next' step into what
  243. // our questions determined would be next.
  244. if ($step == 'next') {
  245. $keys = array_keys($form_info['order']);
  246. // get rid of 'basic' from the list of forms.
  247. array_shift($keys);
  248. $step = array_shift($keys);
  249. // If $step == 'basic' at this point, we were not presented with any
  250. // additional forms at all. Let's just save and go!
  251. if ($step == 'basic') {
  252. page_manager_save_page_cache($page);
  253. // Redirect to the new page's task handler editor.
  254. drupal_goto(page_manager_edit_url($page->task_name));
  255. }
  256. }
  257. }
  258. else {
  259. $form_info['show trail'] = FALSE;
  260. $form_info['order'] = array(
  261. 'basic' => t('Basic settings'),
  262. 'next' => t('A meaningless second page'),
  263. );
  264. }
  265. ctools_include('wizard');
  266. $form_state = array(
  267. 'task' => $task,
  268. 'subtask' => $page->subtask,
  269. 'page' => &$page,
  270. 'type' => 'add',
  271. 'task_id' => 'page',
  272. 'task_name' => $page->task_name,
  273. 'creating' => TRUE,
  274. );
  275. if (!empty($page->handlers)) {
  276. $keys = array_keys($page->handlers);
  277. $key = array_shift($keys);
  278. $form_state['handler'] = &$page->handlers[$key];
  279. $form_state['handler_id'] = $key;
  280. }
  281. $output = ctools_wizard_multistep_form($form_info, $step, $form_state);
  282. if (!$output) {
  283. // redirect.
  284. drupal_redirect_form(array(), $form_state['redirect']);
  285. }
  286. return $output;
  287. }
  288. /**
  289. * Callback generated when the add page process is finished.
  290. */
  291. function page_manager_page_add_subtask_finish(&$form_state) {
  292. $page = &$form_state['page'];
  293. // Update the cache with changes.
  294. page_manager_set_page_cache($page);
  295. $handler = $form_state['handler'];
  296. $handler_plugin = page_manager_get_task_handler($handler->handler);
  297. // Redirect to the new page's task handler editor.
  298. if (isset($handler_plugin['add finish'])) {
  299. $form_state['redirect'] = page_manager_edit_url($page->task_name, array('handlers', $handler->name, $handler_plugin['add finish']));
  300. }
  301. else {
  302. $form_state['redirect'] = page_manager_edit_url($page->task_name);
  303. }
  304. return;
  305. }
  306. /**
  307. * Callback generated when the 'next' button is clicked.
  308. *
  309. * All we do here is store the cache.
  310. */
  311. function page_manager_page_add_subtask_next(&$form_state) {
  312. if (empty($form_state['task_name']) || $form_state['task_name'] == 'page') {
  313. // We may not have known the path to go next, because we didn't yet know the
  314. // task name. This fixes that.
  315. $form_state['form_info']['path'] = str_replace('%task_name', $form_state['page']->task_name, $form_state['form_info']['path']);
  316. $form_state['redirect'] = ctools_wizard_get_path($form_state['form_info'], $form_state['clicked_button']['#next']);
  317. }
  318. // Update the cache with changes.
  319. page_manager_set_page_cache($form_state['page']);
  320. }
  321. /**
  322. * Callback generated when the 'cancel' button is clicked.
  323. *
  324. * All we do here is clear the cache.
  325. */
  326. function page_manager_page_add_subtask_cancel(&$form_state) {
  327. // Wipe all our stored changes.
  328. if (isset($form_state['page']->task_name)) {
  329. page_manager_clear_page_cache($form_state['page']->task_name);
  330. }
  331. }
  332. /**
  333. * Basic settings form for a page manager page.
  334. */
  335. function page_manager_page_form_basic($form, &$form_state) {
  336. $page = &$form_state['page']->subtask['subtask'];
  337. $task = $form_state['task'];
  338. $form['admin_title'] = array(
  339. '#type' => 'textfield',
  340. '#title' => t('Administrative title'),
  341. '#description' => t('The name of this page. This will appear in the administrative interface to easily identify it.'),
  342. '#default_value' => $page->admin_title,
  343. );
  344. $form['name'] = array(
  345. '#type' => 'machine_name',
  346. '#title' => t('Machine name'),
  347. '#machine_name' => array(
  348. 'exists' => 'page_manager_page_load',
  349. 'source' => array('admin_title'),
  350. ),
  351. '#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!'),
  352. '#default_value' => $page->name,
  353. );
  354. if (isset($page->pid) || empty($form_state['creating'])) {
  355. $form['name']['#disabled'] = TRUE;
  356. $form['name']['#value'] = $page->name;
  357. }
  358. $form['admin_description'] = array(
  359. '#type' => 'textarea',
  360. '#title' => t('Administrative description'),
  361. '#description' => t('A description of what this page is, does or is for, for administrative use.'),
  362. '#default_value' => $page->admin_description,
  363. );
  364. // path
  365. $form['path'] = array(
  366. '#type' => 'textfield',
  367. '#title' => t('Path'),
  368. '#description' => t('The URL path to get to this page. You may create named placeholders for variable parts of the path by using %name for required elements and !name for optional elements. For example: "node/%node/foo", "forum/%forum" or "dashboard/!input". These named placeholders can be turned into contexts on the arguments form.'),
  369. '#default_value' => $page->path,
  370. '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
  371. );
  372. $frontpage = variable_get('site_frontpage', 'node');
  373. $path = array();
  374. if ($page->path) {
  375. foreach (explode('/', $page->path) as $bit) {
  376. if ($bit[0] != '!') {
  377. $path[] = $bit;
  378. }
  379. }
  380. }
  381. $path = implode('/', $path);
  382. if (empty($path) || $path != $frontpage) {
  383. $form['frontpage'] = array(
  384. '#type' => 'checkbox',
  385. '#default_value' => !empty($page->make_frontpage),
  386. '#title' => t('Make this your site home page.'),
  387. '#description' => t('To set this panel as your home page you must create a unique path name with no % placeholders in the path. The site home page is currently set to %homepage on the !siteinfo configuration form.', array('!siteinfo' => l(t('Site Information'), 'admin/config/system/site-information'), '%homepage' => '/' . $frontpage)),
  388. );
  389. $form['admin_paths'] = array(
  390. '#type' => 'checkbox',
  391. '#default_value' => !empty($page->conf['admin_paths']),
  392. '#title' => t('Use this page in an admin overlay.'),
  393. '#description' => t('Admin overlays are used in many places in Drupal 7 and administrative custom pages should probably utilize this feature.'),
  394. );
  395. }
  396. else if ($path == $frontpage) {
  397. $form['frontpage_markup'] = array(
  398. '#value' => '<b>' . t('This page is currently set to be your site home page. This can be modified on the !siteinfo configuration form.', array('!siteinfo' => l(t('Site Information'), 'admin/settings/site-information'))) . '</b>',
  399. );
  400. $form['frontpage'] = array(
  401. '#type' => 'value',
  402. '#value' => TRUE,
  403. );
  404. }
  405. if (!isset($page->pid) && !empty($form_state['creating'])) {
  406. $features['default'] = array(
  407. 'access' => t('Access control'),
  408. 'menu' => t('Visible menu item'),
  409. );
  410. module_load_include('inc', 'page_manager', 'page_manager.admin');
  411. $form = page_manager_handler_add_form($form, $form_state, $features);
  412. }
  413. return $form;
  414. }
  415. function page_manager_page_form_basic_validate_filter($value) {
  416. return $value === -1;
  417. }
  418. /**
  419. * Validate the basic form.
  420. */
  421. function page_manager_page_form_basic_validate(&$form, &$form_state) {
  422. // Ensure path is unused by other pages.
  423. $page = $form_state['page']->subtask['subtask'];
  424. $name = !empty($form_state['values']['name']) ? $form_state['values']['name'] : $page->name;
  425. if (empty($name)) {
  426. form_error($form['name'], t('Name is required.'));
  427. }
  428. // If this is new, make sure the name is unique:
  429. if (empty($page->name)) {
  430. $test = page_manager_page_load($name);
  431. if ($test) {
  432. form_error($form['name'], t('That name is used by another page: @page', array('@page' => $test->admin_title)));
  433. }
  434. // Ensure name fits the rules:
  435. if (preg_match('/[^a-zA-Z0-9_]/', $form_state['values']['name'])) {
  436. form_error($form['name'], t('Page name must be alphanumeric or underscores only.'));
  437. }
  438. }
  439. $pages = page_manager_page_load_all();
  440. foreach ($pages as $test) {
  441. if ($test->name != $name && $test->path == $form_state['values']['path'] && empty($test->disabled)) {
  442. form_error($form['path'], t('That path is used by another page: @page', array('@page' => $test->admin_title)));
  443. }
  444. }
  445. // Ensure path is unused by things NOT pages. We do the double check because
  446. // we're checking against our page callback.
  447. $path = array();
  448. if (empty($form_state['values']['path'])) {
  449. form_error($form['path'], t('Path is required.'));
  450. // stop processing here if there is no path.
  451. return;
  452. }
  453. $found = FALSE;
  454. $error = FALSE;
  455. foreach (explode('/', $form_state['values']['path']) as $position => $bit) {
  456. if (!isset($bit) || $bit === '') {
  457. continue;
  458. }
  459. if ($bit == '%' || $bit == '!') {
  460. form_error($form['path'], t('You cannot have an unnamed placeholder (% or ! by itself). Please name your placeholder by adding a short piece of descriptive text to the % or !, such as %user or %node.'));
  461. }
  462. if ($bit[0] == '%') {
  463. if ($found) {
  464. form_error($form['path'], t('You cannot have a dynamic path element after an optional path element.'));
  465. }
  466. if ($position == 0) {
  467. form_error($form['path'], t('The first element in a path may not be dynamic.'));
  468. }
  469. $path[] = '%';
  470. }
  471. else if ($bit[0] == '!') {
  472. $found = TRUE;
  473. }
  474. else {
  475. if ($found) {
  476. form_error($form['path'], t('You cannot have a static path element after an optional path element.'));
  477. }
  478. $path[] = $bit;
  479. }
  480. }
  481. // Check to see if something that isn't a page manager page is using the path.
  482. $path = implode('/', $path);
  483. $result = db_query('SELECT * FROM {menu_router} WHERE path = :path', array(':path' => $path));
  484. foreach ($result as $router) {
  485. if ($router->page_callback != 'page_manager_page_execute') {
  486. form_error($form['path'], t('That path is already in use. This system cannot override existing paths.'));
  487. }
  488. }
  489. // Ensure the path is not already an alias to something else.
  490. if (strpos($path, '%') === FALSE) {
  491. $alias = db_query('SELECT alias, source FROM {url_alias} WHERE alias = :path', array(':path' => $path))->fetchObject();
  492. if ($alias) {
  493. 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->source)));
  494. }
  495. }
  496. else {
  497. if (!empty($form_state['values']['frontpage'])) {
  498. form_error($form['path'], t('You cannot make this page your site home page if it uses % placeholders.'));
  499. }
  500. }
  501. // Ensure path is properly formed.
  502. $args = page_manager_page_get_named_arguments($form_state['values']['path']);
  503. if ($invalid_args = array_filter($args, 'page_manager_page_form_basic_validate_filter')) {
  504. foreach ($invalid_args as $arg => $position) {
  505. form_error($form['path'], t('Duplicated argument %arg', array('%arg' => $arg)));
  506. }
  507. }
  508. if (isset($args['%'])) {
  509. form_error($form['path'], t('Invalid arg <em>%</em>. All arguments must be named with keywords.'));
  510. }
  511. $form_state['arguments'] = $args;
  512. }
  513. /**
  514. * Store the values from the basic settings form.
  515. */
  516. function page_manager_page_form_basic_submit(&$form, &$form_state) {
  517. $page = &$form_state['page']->subtask['subtask'];
  518. $cache = &$form_state['page'];
  519. // If this is a new thing, then we have to do a bunch of setup to create
  520. // the cache record with the right ID and some basic data that we could
  521. // not know until we asked the user some questions.
  522. if (!isset($page->pid) && !empty($form_state['creating'])) {
  523. // Update the data with our new name.
  524. $page->name = $form_state['values']['name'];
  525. $form_state['page']->task_name = page_manager_make_task_name($form_state['task_id'], $page->name);
  526. $cache->handler = $form_state['values']['handler'];
  527. $cache->subtask_id = $page->name;
  528. $plugin = page_manager_get_task_handler($cache->handler);
  529. // If they created and went back, there might be old, dead handlers
  530. // that are not going to be added.
  531. //
  532. // Remove them:
  533. $cache->handlers = array();
  534. $cache->handler_info = array();
  535. // Create a new handler.
  536. $handler = page_manager_new_task_handler($plugin);
  537. $title = !empty($form_state['values']['title']) ? $form_state['values']['title'] : $plugin['title'];
  538. page_manager_handler_add_to_page($cache, $handler, $title);
  539. // Figure out which forms to present them with
  540. $cache->forms = array();
  541. $cache->forms[] = 'basic'; // This one is always there.
  542. if (!empty($form_state['arguments'])) {
  543. $cache->forms[] = 'argument';
  544. }
  545. $features = $form_state['values']['features'];
  546. $cache->forms = array_merge($cache->forms, array_keys(array_filter($features['default'])));
  547. if (isset($features[$form_state['values']['handler']])) {
  548. $cache->forms = array_merge($cache->forms, array_keys(array_filter($features[$form_state['values']['handler']])));
  549. }
  550. if (isset($plugin['required forms'])) {
  551. $cache->forms = array_merge($cache->forms, array_keys($plugin['required forms']));
  552. }
  553. }
  554. $page->admin_title = $form_state['values']['admin_title'];
  555. $cache->subtask['admin title'] = check_plain($form_state['values']['admin_title']);
  556. $page->admin_description = $form_state['values']['admin_description'];
  557. $cache->subtask['admin description'] = filter_xss_admin($form_state['values']['admin_description']);
  558. if ($page->path != $form_state['values']['path']) {
  559. $page->path = $form_state['values']['path'];
  560. page_manager_page_recalculate_arguments($page);
  561. $cache->path_changed = TRUE;
  562. }
  563. $page->make_frontpage = !empty($form_state['values']['frontpage']);
  564. $page->conf['admin_paths'] = !empty($form_state['values']['admin_paths']);
  565. }
  566. /**
  567. * Form to handle menu item controls.
  568. */
  569. function page_manager_page_form_menu($form, &$form_state) {
  570. ctools_include('dependent');
  571. $form['menu'] = array(
  572. '#prefix' => '<div class="clearfix">',
  573. '#suffix' => '</div>',
  574. '#tree' => TRUE,
  575. );
  576. $menu = $form_state['page']->subtask['subtask']->menu;
  577. if (empty($menu)) {
  578. $menu = array(
  579. 'type' => 'none',
  580. 'title' => '',
  581. 'weight' => 0,
  582. 'name' => 'navigation',
  583. 'parent' => array(
  584. 'type' => 'none',
  585. 'title' => '',
  586. 'weight' => 0,
  587. 'name' => 'navigation',
  588. ),
  589. );
  590. }
  591. $form['menu']['type'] = array(
  592. '#title' => t('Type'),
  593. '#type' => 'radios',
  594. '#options' => array(
  595. 'none' => t('No menu entry'),
  596. 'normal' => t('Normal menu entry'),
  597. 'tab' => t('Menu tab'),
  598. 'default tab' => t('Default menu tab'),
  599. 'action' => t('Local action'),
  600. ),
  601. '#default_value' => $menu['type'],
  602. );
  603. $form['menu']['title'] = array(
  604. '#title' => t('Title'),
  605. '#type' => 'textfield',
  606. '#default_value' => $menu['title'],
  607. '#description' => t('If set to normal or tab, enter the text to use for the menu item.'),
  608. '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab', 'action')),
  609. );
  610. list($major, $minor) = explode('.', VERSION, 2);
  611. // Only display the menu selector if menu module is enabled.
  612. if (module_exists('menu')) {
  613. $form['menu']['name'] = array(
  614. '#title' => t('Menu'),
  615. '#type' => 'select',
  616. '#options' => menu_get_menus(),
  617. '#default_value' => $menu['name'],
  618. '#description' => t('Insert item into an available menu.'),
  619. '#dependency' => array('radio:menu[type]' => array('normal')),
  620. );
  621. }
  622. else {
  623. $form['menu']['name'] = array(
  624. '#type' => 'value',
  625. '#value' => $menu['name'],
  626. );
  627. $form['menu']['markup'] = array(
  628. '#value' => t('Menu selection requires the activation of menu module.'),
  629. );
  630. }
  631. $form['menu']['weight'] = array(
  632. '#title' => t('Weight'),
  633. '#type' => 'textfield',
  634. '#default_value' => isset($menu['weight']) ? $menu['weight'] : 0,
  635. '#description' => t('The lower the weight the higher/further left it will appear.'),
  636. '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab', 'action')),
  637. );
  638. $form['menu']['parent']['type'] = array(
  639. '#prefix' => '<div id="edit-menu-parent-type-wrapper">',
  640. '#suffix' => '</div>',
  641. '#title' => t('Parent menu item'),
  642. '#type' => 'radios',
  643. '#options' => array('none' => t('No menu entry'), 'normal' => t('Normal menu item'), 'tab' => t('Menu tab')),
  644. '#default_value' => $menu['parent']['type'],
  645. '#description' => t('When providing a menu item as a default tab, Drupal needs to know what the parent menu item of that tab will be. Sometimes the parent will already exist, but other times you will need to have one created. The path of a parent item will always be the same path with the last part left off. i.e, if the path to this view is <em>foo/bar/baz</em>, the parent path would be <em>foo/bar</em>.'),
  646. '#dependency' => array('radio:menu[type]' => array('default tab')),
  647. );
  648. $form['menu']['parent']['title'] = array(
  649. '#title' => t('Parent item title'),
  650. '#type' => 'textfield',
  651. '#default_value' => $menu['parent']['title'],
  652. '#description' => t('If creating a parent menu item, enter the title of the item.'),
  653. '#dependency' => array('radio:menu[type]' => array('default tab'), 'radio:menu[parent][type]' => array('normal', 'tab')),
  654. '#dependency_count' => 2,
  655. );
  656. // Only display the menu selector if menu module is enabled.
  657. if (module_exists('menu')) {
  658. $form['menu']['parent']['name'] = array(
  659. '#title' => t('Parent item menu'),
  660. '#type' => 'select',
  661. '#options' => menu_get_menus(),
  662. '#default_value' => $menu['parent']['name'],
  663. '#description' => t('Insert item into an available menu.'),
  664. '#dependency' => array('radio:menu[type]' => array('default tab'), 'radio:menu[parent][type]' => array('normal')),
  665. '#dependency_count' => 2,
  666. );
  667. }
  668. else {
  669. $form['menu']['parent']['name'] = array(
  670. '#type' => 'value',
  671. '#value' => $menu['parent']['name'],
  672. );
  673. }
  674. $form['menu']['parent']['weight'] = array(
  675. '#title' => t('Parent weight'),
  676. '#type' => 'textfield',
  677. '#default_value' => $menu['parent']['weight'],
  678. '#size' => 5,
  679. '#description' => t('Enter the weight of the parent item. The lower the number, the more to the left it will be.'),
  680. '#dependency' => array('radio:menu[type]' => array('default tab'), 'radio:menu[parent][type]' => array('tab', 'normal')),
  681. '#dependency_count' => 2,
  682. );
  683. return $form;
  684. }
  685. /**
  686. * Validate handler for the menu form for add/edit page task.
  687. */
  688. function page_manager_page_form_menu_validate(&$form, &$form_state) {
  689. // If setting a 'normal' menu entry, make sure that any placeholders
  690. // support the to_arg stuff.
  691. if ($form_state['values']['menu']['type'] == 'normal') {
  692. $page = $form_state['page']->subtask['subtask'];
  693. foreach (explode('/', $page->path) as $bit) {
  694. if (!isset($bit) || $bit === '') {
  695. continue;
  696. }
  697. if ($bit[0] == '%') {
  698. // Chop off that %.
  699. $name = substr($bit, 1);
  700. // Check to see if the argument plugin allows to arg:
  701. if (!empty($page->arguments[$name])) {
  702. ctools_include('context');
  703. $plugin = ctools_get_argument($page->arguments[$name]['name']);
  704. if (!empty($plugin['path placeholder to_arg'])) {
  705. continue;
  706. }
  707. }
  708. form_error($form['menu']['type'], t('Paths with non optional placeholders cannot be used as normal menu items unless the selected argument handler provides a default argument to use for the menu item.'));
  709. return;
  710. }
  711. }
  712. }
  713. }
  714. /**
  715. * Submit handler for the menu form for add/edit page task.
  716. */
  717. function page_manager_page_form_menu_submit(&$form, &$form_state) {
  718. $form_state['page']->subtask['subtask']->menu = $form_state['values']['menu'];
  719. $form_state['page']->path_changed = TRUE;
  720. }
  721. /**
  722. * Form to handle menu item controls.
  723. */
  724. function page_manager_page_form_access($form, &$form_state) {
  725. ctools_include('context');
  726. $form_state['module'] = 'page_manager_page';
  727. $form_state['callback argument'] = $form_state['page']->task_name;
  728. $form_state['access'] = $form_state['page']->subtask['subtask']->access;
  729. $form_state['no buttons'] = TRUE;
  730. $form_state['contexts'] = array();
  731. // Load contexts based on argument data:
  732. if ($arguments = _page_manager_page_get_arguments($form_state['page']->subtask['subtask'])) {
  733. $form_state['contexts'] = ctools_context_get_placeholders_from_argument($arguments);
  734. }
  735. ctools_include('context-access-admin');
  736. $form = ctools_access_admin_form($form, $form_state);
  737. return $form;
  738. }
  739. /**
  740. * Submit handler to deal with access control changes.
  741. */
  742. function page_manager_page_form_access_submit(&$form, &$form_state) {
  743. $form_state['page']->subtask['subtask']->access['logic'] = $form_state['values']['logic'];
  744. $form_state['page']->path_changed = TRUE;
  745. }
  746. /**
  747. * Form to handle assigning argument handlers to named arguments.
  748. */
  749. function page_manager_page_form_argument($form, &$form_state) {
  750. $page = &$form_state['page']->subtask['subtask'];
  751. $path = $page->path;
  752. $arguments = page_manager_page_get_named_arguments($path);
  753. $form['table'] = array(
  754. '#theme' => 'page_manager_page_form_argument_table',
  755. '#page-manager-path' => $path,
  756. 'argument' => array(),
  757. );
  758. $task_name = $form_state['page']->task_name;
  759. foreach ($arguments as $keyword => $position) {
  760. $conf = array();
  761. if (isset($page->temporary_arguments[$keyword]) && !empty($form_state['allow temp'])) {
  762. $conf = $page->temporary_arguments[$keyword];
  763. }
  764. else if (isset($page->arguments[$keyword])) {
  765. $conf = $page->arguments[$keyword];
  766. }
  767. $context = t('No context assigned');
  768. $plugin = array();
  769. if ($conf && isset($conf['name'])) {
  770. ctools_include('context');
  771. $plugin = ctools_get_argument($conf['name']);
  772. if (isset($plugin['title'])) {
  773. $context = $plugin['title'];
  774. }
  775. }
  776. $form['table']['argument'][$keyword]['#keyword'] = $keyword;
  777. $form['table']['argument'][$keyword]['#position'] = $position;
  778. $form['table']['argument'][$keyword]['#context'] = $context;
  779. // The URL for this ajax button
  780. $form['table']['argument'][$keyword]['change-url'] = array(
  781. '#attributes' => array('class' => array("page-manager-context-$keyword-change-url")),
  782. '#type' => 'hidden',
  783. '#value' => url("admin/structure/pages/argument/change/$task_name/$keyword", array('absolute' => TRUE)),
  784. );
  785. $form['table']['argument'][$keyword]['change'] = array(
  786. '#type' => 'submit',
  787. '#value' => t('Change'),
  788. '#attributes' => array('class' => array('ctools-use-modal')),
  789. '#id' => "page-manager-context-$keyword-change",
  790. );
  791. $form['table']['argument'][$keyword]['settings'] = array();
  792. // Only show the button if this has a settings form available:
  793. if (!empty($plugin)) {
  794. // The URL for this ajax button
  795. $form['table']['argument'][$keyword]['settings-url'] = array(
  796. '#attributes' => array('class' => array("page-manager-context-$keyword-settings-url")),
  797. '#type' => 'hidden',
  798. '#value' => url("admin/structure/pages/argument/settings/$task_name/$keyword", array('absolute' => TRUE)),
  799. );
  800. $form['table']['argument'][$keyword]['settings'] = array(
  801. '#type' => 'submit',
  802. '#value' => t('Settings'),
  803. '#attributes' => array('class' => array('ctools-use-modal')),
  804. '#id' => "page-manager-context-$keyword-settings",
  805. );
  806. }
  807. }
  808. return $form;
  809. }
  810. /**
  811. * Theme the table for this form.
  812. */
  813. function theme_page_manager_page_form_argument_table($vars) {
  814. $form = $vars['form'];
  815. $header = array(
  816. array('data' => t('Argument'), 'class' => array('page-manager-argument')),
  817. array('data' => t('Position in path'), 'class' => array('page-manager-position')),
  818. array('data' => t('Context assigned'), 'class' => array('page-manager-context')),
  819. array('data' => t('Operations'), 'class' => array('page-manager-operations')),
  820. );
  821. $rows = array();
  822. ctools_include('modal');
  823. ctools_modal_add_js();
  824. foreach (element_children($form['argument']) as $key) {
  825. $row = array();
  826. $row[] = '%' . check_plain($form['argument'][$key]['#keyword']);
  827. $row[] = check_plain($form['argument'][$key]['#position']);
  828. $row[] = $form['argument'][$key]['#context'] . ' &nbsp; ' . drupal_render($form['argument'][$key]['change']);;
  829. $row[] = drupal_render($form['argument'][$key]['settings']) . drupal_render($form['argument'][$key]);
  830. $rows[] = array('data' => $row);
  831. }
  832. if (!$rows) {
  833. $rows[] = array(array('data' => t('The path %path has no arguments to configure.', array('%path' => $form['#page-manager-path'])), 'colspan' => 4));
  834. }
  835. $attributes = array(
  836. 'id' => 'page-manager-argument-table',
  837. );
  838. $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => $attributes));
  839. return $output;
  840. }
  841. /**
  842. * Ajax entry point to edit an item
  843. */
  844. function page_manager_page_subtask_argument_ajax($step = NULL, $task_name = NULL, $keyword = NULL) {
  845. ctools_include('ajax');
  846. ctools_include('modal');
  847. ctools_include('context');
  848. ctools_include('wizard');
  849. if (!$step) {
  850. return ctools_ajax_render_error();
  851. }
  852. if (!$cache = page_manager_get_page_cache($task_name)) {
  853. return ctools_ajax_render_error(t('Invalid object name.'));
  854. }
  855. $page = &$cache->subtask['subtask'];
  856. $path = $page->path;
  857. $arguments = page_manager_page_get_named_arguments($path);
  858. // Load stored object from cache.
  859. if (!isset($arguments[$keyword])) {
  860. return ctools_ajax_render_error(t('Invalid keyword.'));
  861. }
  862. // Set up wizard info
  863. $form_info = array(
  864. 'id' => 'page_manager_page_argument',
  865. 'path' => "admin/structure/pages/argument/%step/$task_name/$keyword",
  866. 'show cancel' => TRUE,
  867. 'next callback' => 'page_manager_page_argument_next',
  868. 'finish callback' => 'page_manager_page_argument_finish',
  869. 'cancel callback' => 'page_manager_page_argument_cancel',
  870. 'order' => array(
  871. 'change' => t('Change context type'),
  872. 'settings' => t('Argument settings'),
  873. ),
  874. 'forms' => array(
  875. 'change' => array(
  876. 'title' => t('Change argument'),
  877. 'form id' => 'page_manager_page_argument_form_change',
  878. ),
  879. 'settings' => array(
  880. 'title' => t('Argument settings'),
  881. 'form id' => 'page_manager_page_argument_form_settings',
  882. ),
  883. ),
  884. );
  885. $form_state = array(
  886. 'page' => $cache,
  887. 'keyword' => $keyword,
  888. 'ajax' => TRUE,
  889. 'modal' => TRUE,
  890. 'modal return' => TRUE,
  891. 'commands' => array(),
  892. );
  893. $output = ctools_wizard_multistep_form($form_info, $step, $form_state);
  894. if (!empty($form_state['cancel'])) {
  895. $commands = array(ctools_modal_command_dismiss());
  896. }
  897. else if (!empty($form_state['complete'])) {
  898. if (isset($page->temporary_arguments[$keyword])) {
  899. $page->arguments[$keyword] = $page->temporary_arguments[$keyword];
  900. }
  901. if (isset($page->temporary_arguments)) {
  902. unset($page->temporary_arguments);
  903. }
  904. // Update the cache with changes.
  905. page_manager_set_page_cache($cache);
  906. // Rerender the table so we can ajax it back in.
  907. // Go directly to the form and retrieve it using a blank form and
  908. // a clone of our current form state. This is an abbreviated
  909. // drupal_get_form that is halted prior to render and is never
  910. // fully processed, but is guaranteed to produce the same form we
  911. // started with so we don't have to do crazy stuff to rerender
  912. // just part of it.
  913. // @todo should there be a tool to do this?
  914. $clone_state = $form_state;
  915. $clone_state['allow temp'] = TRUE;
  916. $form = drupal_build_form('page_manager_page_form_argument', $form_state);
  917. // Render just the table portion.
  918. $output = drupal_render($form['table']);
  919. $commands = array(
  920. ajax_command_replace('#page-manager-argument-table', $output),
  921. ctools_modal_command_dismiss(),
  922. );
  923. }
  924. else {
  925. $commands = ctools_modal_form_render($form_state, $output);
  926. }
  927. print ajax_render($commands);
  928. ajax_footer();
  929. exit;
  930. }
  931. /**
  932. * Callback generated when the add page process is finished.
  933. */
  934. function page_manager_page_argument_finish(&$form_state) {
  935. }
  936. /**
  937. * Callback generated when the 'next' button is clicked.
  938. *
  939. * All we do here is store the cache.
  940. */
  941. function page_manager_page_argument_next(&$form_state) {
  942. // Update the cache with changes.
  943. page_manager_set_page_cache($form_state['page']);
  944. }
  945. /**
  946. * Callback generated when the 'cancel' button is clicked.
  947. *
  948. * We might have some temporary data lying around. We must remove it.
  949. */
  950. function page_manager_page_argument_cancel(&$form_state) {
  951. $page = &$form_state['page']->subtask['subtask'];
  952. if (isset($page->temporary_arguments)) {
  953. unset($page->temporary_arguments);
  954. // Update the cache with changes.
  955. page_manager_set_page_cache($page);
  956. }
  957. }
  958. /**
  959. * Basic settings form for a page manager page.
  960. */
  961. function page_manager_page_argument_form_change($form, &$form_state) {
  962. $page = &$form_state['page']->subtask['subtask'];
  963. $keyword = &$form_state['keyword'];
  964. ctools_include('context');
  965. $plugins = ctools_get_arguments();
  966. $options = array();
  967. foreach ($plugins as $id => $plugin) {
  968. if (empty($plugin['no ui'])) {
  969. $options[$id] = $plugin['title'];
  970. }
  971. }
  972. asort($options);
  973. $options = array('' => t('No context selected')) + $options;
  974. $argument = '';
  975. if (isset($page->arguments[$keyword]) && isset($page->arguments[$keyword]['name'])) {
  976. $argument = $page->arguments[$keyword]['name'];
  977. }
  978. $form['argument'] = array(
  979. '#type' => 'radios',
  980. '#options' => $options,
  981. '#default_value' => $argument,
  982. );
  983. return $form;
  984. }
  985. /**
  986. * Submit handler to change an argument.
  987. */
  988. function page_manager_page_argument_form_change_submit(&$form, &$form_state) {
  989. $page = &$form_state['page']->subtask['subtask'];
  990. $keyword = &$form_state['keyword'];
  991. $argument = $form_state['values']['argument'];
  992. // If the argument is not changing, we do not need to do anything.
  993. if (isset($page->arguments[$keyword]['name']) && $page->arguments[$keyword]['name'] == $argument) {
  994. // Set the task to cancel since no change means do nothing:
  995. $form_state['clicked_button']['#wizard type'] = 'cancel';
  996. return;
  997. }
  998. ctools_include('context');
  999. // If switching to the no context, just wipe out the old data.
  1000. if (empty($argument)) {
  1001. $form_state['clicked_button']['#wizard type'] = 'finish';
  1002. $page->temporary_arguments[$keyword] = array(
  1003. 'settings' => array(),
  1004. 'identifier' => t('No context'),
  1005. );
  1006. return;
  1007. }
  1008. $plugin = ctools_get_argument($argument);
  1009. // Acquire defaults.
  1010. $settings = array();
  1011. if (isset($plugin['default'])) {
  1012. if (is_array($plugin['default'])) {
  1013. $settings = $plugin['default'];
  1014. }
  1015. else if (function_exists($plugin['default'])) {
  1016. $settings = $plugin['default']();
  1017. }
  1018. }
  1019. $id = ctools_context_next_id($page->arguments, $argument);
  1020. $title = isset($plugin['title']) ? $plugin['title'] : t('No context');
  1021. // Set the new argument in a temporary location.
  1022. $page->temporary_arguments[$keyword] = array(
  1023. 'id' => $id,
  1024. 'identifier' => $title . ($id > 1 ? ' ' . $id : ''),
  1025. 'name' => $argument,
  1026. 'settings' => $settings,
  1027. );
  1028. }
  1029. /**
  1030. * Basic settings form for a page manager page.
  1031. */
  1032. function page_manager_page_argument_form_settings($form, &$form_state) {
  1033. $page = &$form_state['page']->subtask['subtask'];
  1034. $keyword = &$form_state['keyword'];
  1035. if (isset($page->temporary_arguments[$keyword])) {
  1036. $conf = $page->temporary_arguments[$keyword];
  1037. }
  1038. else if (isset($page->arguments[$keyword])) {
  1039. $conf = $page->temporary_arguments[$keyword] = $page->arguments[$keyword];
  1040. }
  1041. if (!isset($conf)) {
  1042. // This should be impossible and thus never seen.
  1043. $form['error'] = array('#value' => t('Error: missing argument.'));
  1044. return;
  1045. }
  1046. ctools_include('context');
  1047. $plugin = ctools_get_argument($conf['name']);
  1048. $form['settings'] = array(
  1049. '#tree' => TRUE,
  1050. );
  1051. $form['identifier'] = array(
  1052. '#type' => 'textfield',
  1053. '#title' => t('Context identifier'),
  1054. '#description' => t('This is the title of the context used to identify it later in the administrative process. This will never be shown to a user.'),
  1055. '#default_value' => $conf['identifier'],
  1056. );
  1057. if (!$plugin) {
  1058. // This should be impossible and thus never seen.
  1059. $form['error'] = array('#value' => t('Error: missing or invalid argument plugin %argument.', array('%argument', $argument)));
  1060. return;
  1061. }
  1062. if ($function = ctools_plugin_get_function($plugin, 'settings form')) {
  1063. $function($form, $form_state, $conf['settings']);
  1064. }
  1065. $form_state['plugin'] = $plugin;
  1066. return $form;
  1067. }
  1068. /**
  1069. * Validate handler for argument settings.
  1070. */
  1071. function page_manager_page_argument_form_settings_validate(&$form, &$form_state) {
  1072. if ($function = ctools_plugin_get_function($form_state['plugin'], 'settings form validate')) {
  1073. $function($form, $form_state);
  1074. }
  1075. }
  1076. /**
  1077. * Submit handler for argument settings.
  1078. */
  1079. function page_manager_page_argument_form_settings_submit(&$form, &$form_state) {
  1080. if ($function = ctools_plugin_get_function($form_state['plugin'], 'settings form submit')) {
  1081. $function($form, $form_state);
  1082. }
  1083. $page = &$form_state['page']->subtask['subtask'];
  1084. $keyword = &$form_state['keyword'];
  1085. // Copy the form to our temporary location which will get moved again when
  1086. // finished. Yes, finished is always next but finish can happen from other
  1087. // locations so we funnel through that path rather than duplicate.
  1088. $page->temporary_arguments[$keyword]['identifier'] = $form_state['values']['identifier'];
  1089. if (isset($form_state['values']['settings'])) {
  1090. $page->temporary_arguments[$keyword]['settings'] = $form_state['values']['settings'];
  1091. }
  1092. else {
  1093. $page->temporary_arguments[$keyword]['settings'] = array();
  1094. }
  1095. }
  1096. /**
  1097. * Import a task handler from cut & paste
  1098. */
  1099. function page_manager_page_import_subtask($form, &$form_state, $task_name) {
  1100. $form_state['task'] = page_manager_get_task($task_name);
  1101. drupal_set_title(t('Import page'));
  1102. $form['name'] = array(
  1103. '#type' => 'textfield',
  1104. '#title' => t('Page name'),
  1105. '#description' => t('Enter the name to use for this page if it is different from the source page. Leave blank to use the original name of the page.'),
  1106. );
  1107. $form['path'] = array(
  1108. '#type' => 'textfield',
  1109. '#title' => t('Path'),
  1110. '#description' => t('Enter the path to use for this page if it is different from the source page. Leave blank to use the original path of the page.'),
  1111. );
  1112. $form['overwrite'] = array(
  1113. '#type' => 'checkbox',
  1114. '#title' => t('Allow overwrite of an existing page'),
  1115. '#description' => t('If the name you selected already exists in the database, this page will be allowed to overwrite the existing page.'),
  1116. );
  1117. $form['object'] = array(
  1118. '#type' => 'textarea',
  1119. '#title' => t('Paste page code here'),
  1120. '#rows' => 15,
  1121. );
  1122. $form['submit'] = array(
  1123. '#type' => 'submit',
  1124. '#value' => t('Import'),
  1125. );
  1126. return $form;
  1127. }
  1128. /**
  1129. * Ensure we got a valid page.
  1130. */
  1131. function page_manager_page_import_subtask_validate(&$form, &$form_state) {
  1132. ob_start();
  1133. eval($form_state['values']['object']);
  1134. ob_end_clean();
  1135. if (!isset($page) || !is_object($page)) {
  1136. $errors = ob_get_contents();
  1137. if (empty($errors)) {
  1138. $errors = t('No handler found.');
  1139. }
  1140. form_error($form['object'], t('Unable to get a page from the import. Errors reported: @errors', array('@errors' => $errors)));
  1141. }
  1142. if (empty($form_state['values']['name'])) {
  1143. $form_state['values']['name'] = $page->name;
  1144. }
  1145. $task_name = page_manager_make_task_name('page', $form_state['values']['name']);
  1146. $form_state['cache'] = page_manager_get_page_cache($task_name);
  1147. if ($form_state['cache'] && $form_state['cache']->locked) {
  1148. form_error($form['name'], t('That page name is in use and locked by another user. You must <a href="!break">break the lock</a> on that page before proceeding, or choose a different name.', array('!break' => url(page_manager_edit_url($task_name, array('actions', 'break-lock'))))));
  1149. return;
  1150. }
  1151. if (empty($form_state['values']['path'])) {
  1152. $form_state['values']['path'] = $page->path;
  1153. }
  1154. if (empty($form_state['values']['overwrite'])) {
  1155. $page->name = NULL;
  1156. }
  1157. $form_state['page'] = new stdClass();
  1158. $form_state['page']->subtask['subtask'] = $page;
  1159. page_manager_page_form_basic_validate($form, $form_state);
  1160. }
  1161. /**
  1162. * Submit the import page to create the new page and redirect.
  1163. */
  1164. function page_manager_page_import_subtask_submit($form, &$form_state) {
  1165. $page = &$form_state['page']->subtask['subtask'];
  1166. $page->name = $form_state['values']['name'];
  1167. $page->path = $form_state['values']['path'];
  1168. $task_name = page_manager_make_task_name('page', $page->name);
  1169. $cache = page_manager_get_page_cache($task_name);
  1170. if (!$cache) {
  1171. $cache = new stdClass();
  1172. }
  1173. page_manager_page_new_page_cache($page, $cache);
  1174. page_manager_set_page_cache($cache);
  1175. $form_state['redirect'] = page_manager_edit_url($task_name);
  1176. }
  1177. /**
  1178. * Entry point to export a page.
  1179. */
  1180. function page_manager_page_form_export($form, &$form_state) {
  1181. $page = $form_state['page']->subtask['subtask'];
  1182. $export = page_manager_page_export($page, $form_state['page']->handlers);
  1183. $lines = substr_count($export, "\n");
  1184. $form['code'] = array(
  1185. '#type' => 'textarea',
  1186. '#default_value' => $export,
  1187. '#rows' => $lines,
  1188. );
  1189. unset($form['buttons']);
  1190. return $form;
  1191. }
  1192. /**
  1193. * Entry point to clone a page.
  1194. */
  1195. function page_manager_page_form_clone($form, &$form_state) {
  1196. $page = &$form_state['page']->subtask['subtask'];
  1197. // This provides its own button because it does something totally different.
  1198. unset($form['buttons']);
  1199. $form['admin_title'] = array(
  1200. '#type' => 'textfield',
  1201. '#title' => t('Administrative title'),
  1202. '#description' => t('The name of this page. This will appear in the administrative interface to easily identify it.'),
  1203. '#default_value' => $page->admin_title,
  1204. );
  1205. $form['name'] = array(
  1206. '#type' => 'machine_name',
  1207. '#title' => t('Page name'),
  1208. '#machine_name' => array(
  1209. 'exists' => 'page_manager_page_load',
  1210. 'source' => array('admin_title'),
  1211. ),
  1212. '#description' => t('Enter the name to the new page It must be unique and contain only alphanumeric characters and underscores.'),
  1213. );
  1214. // path
  1215. $form['path'] = array(
  1216. '#type' => 'textfield',
  1217. '#title' => t('Path'),
  1218. '#description' => t('The URL path to get to this page. You may create named placeholders for variable parts of the path by using %name for required elements and !name for optional elements. For example: "node/%node/foo", "forum/%forum" or "dashboard/!input". These named placeholders can be turned into contexts on the arguments form. You cannot use the same path as the original page.'),
  1219. '#default_value' => $page->path,
  1220. );
  1221. $form['handlers'] = array(
  1222. '#type' => 'checkbox',
  1223. '#title' => t('Clone variants'),
  1224. '#description' => t('If checked all variants associated with the page will be cloned as well. If not checked the page will be cloned without variants.'),
  1225. '#default_value' => TRUE,
  1226. );
  1227. $form['submit'] = array(
  1228. '#type' => 'submit',
  1229. '#value' => t('Clone'),
  1230. );
  1231. return $form;
  1232. }
  1233. /**
  1234. * Validate clone page form.
  1235. */
  1236. function page_manager_page_form_clone_validate(&$form, &$form_state) {
  1237. $page = &$form_state['page']->subtask['subtask'];
  1238. $page->old_name = $page->name;
  1239. $page->name = NULL;
  1240. page_manager_page_form_basic_validate($form, $form_state);
  1241. }
  1242. /**
  1243. * submit clone page form.
  1244. *
  1245. * Load the page, change the name(s) to protect the innocent, and if
  1246. * requested, load all the task handlers so that they get saved properly too.
  1247. */
  1248. function page_manager_page_form_clone_submit(&$form, &$form_state) {
  1249. $original = $form_state['page']->subtask['subtask'];
  1250. $original->name = $form_state['values']['name'];
  1251. $original->admin_title = $form_state['values']['admin_title'];
  1252. $original->path = $form_state['values']['path'];
  1253. $handlers = !empty($form_state['values']['handlers']) ? $form_state['page']->handlers : FALSE;
  1254. // Export the handler, which is a fantastic way to clean database IDs out of it.
  1255. $export = page_manager_page_export($original, $handlers);
  1256. ob_start();
  1257. eval($export);
  1258. ob_end_clean();
  1259. $task_name = page_manager_make_task_name('page', $page->name);
  1260. $cache = new stdClass();
  1261. page_manager_page_new_page_cache($page, $cache);
  1262. page_manager_set_page_cache($cache);
  1263. $form_state['redirect'] = page_manager_edit_url($task_name);
  1264. }
  1265. /**
  1266. * Entry point to export a page.
  1267. */
  1268. function page_manager_page_form_delete($form, &$form_state) {
  1269. $page = &$form_state['page']->subtask['subtask'];
  1270. if ($page->type == t('Overridden')) {
  1271. $text = t('Reverting the page will delete the page that is in the database, reverting it to the original default page. Any changes you have made will be lost and cannot be recovered.');
  1272. }
  1273. else {
  1274. $text = t('Are you sure you want to delete this page? Deleting a page cannot be undone.');
  1275. }
  1276. $form['markup'] = array(
  1277. '#value' => '<p>' . $text . '</p>',
  1278. );
  1279. if (empty($form_state['page']->locked)) {
  1280. unset($form['buttons']);
  1281. $form['delete'] = array(
  1282. '#type' => 'submit',
  1283. '#value' => $page->type == t('Overridden') ? t('Revert') : t('Delete'),
  1284. );
  1285. }
  1286. return $form;
  1287. }
  1288. /**
  1289. * Submit handler to delete a view.
  1290. */
  1291. function page_manager_page_form_delete_submit(&$form, &$form_state) {
  1292. $page = $form_state['page']->subtask['subtask'];
  1293. page_manager_page_delete($page);
  1294. if ($page->type != t('Overridden')) {
  1295. $form_state['redirect'] = 'admin/structure/pages';
  1296. drupal_set_message(t('The page has been deleted.'));
  1297. }
  1298. else {
  1299. $form_state['redirect'] = page_manager_edit_url($form_state['page']->task_name, array('summary'));
  1300. drupal_set_message(t('The page has been reverted.'));
  1301. }
  1302. }