ctools_export_ui.class.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  1. <?php
  2. /**
  3. * Base class for export UI.
  4. */
  5. class ctools_export_ui {
  6. var $plugin;
  7. var $name;
  8. var $options = array();
  9. /**
  10. * Fake constructor -- this is easier to deal with than the real
  11. * constructor because we are retaining PHP4 compatibility, which
  12. * would require all child classes to implement their own constructor.
  13. */
  14. function init($plugin) {
  15. ctools_include('export');
  16. $this->plugin = $plugin;
  17. }
  18. /**
  19. * Get a page title for the current page from our plugin strings.
  20. */
  21. function get_page_title($op, $item = NULL) {
  22. if (empty($this->plugin['strings']['title'][$op])) {
  23. return;
  24. }
  25. // Replace %title that might be there with the exportable title.
  26. $title = $this->plugin['strings']['title'][$op];
  27. if (!empty($item)) {
  28. $export_key = $this->plugin['export']['key'];
  29. $title = (str_replace('%title', check_plain($item->{$export_key}), $title));
  30. }
  31. return $title;
  32. }
  33. /**
  34. * Called by ctools_export_ui_load to load the item.
  35. *
  36. * This can be overridden for modules that want to be able to export
  37. * items currently being edited, for example.
  38. */
  39. function load_item($item_name) {
  40. $item = ctools_export_crud_load($this->plugin['schema'], $item_name);
  41. return empty($item) ? FALSE : $item;
  42. }
  43. // ------------------------------------------------------------------------
  44. // Menu item manipulation
  45. /**
  46. * hook_menu() entry point.
  47. *
  48. * Child implementations that need to add or modify menu items should
  49. * probably call parent::hook_menu($items) and then modify as needed.
  50. */
  51. function hook_menu(&$items) {
  52. // During upgrades, the schema can be empty as this is called prior to
  53. // actual update functions being run. Ensure that we can cope with this
  54. // situation.
  55. if (empty($this->plugin['schema'])) {
  56. return;
  57. }
  58. $prefix = ctools_export_ui_plugin_base_path($this->plugin);
  59. if (isset($this->plugin['menu']['items']) && is_array($this->plugin['menu']['items'])) {
  60. $my_items = array();
  61. foreach ($this->plugin['menu']['items'] as $item) {
  62. // Add menu item defaults.
  63. $item += array(
  64. 'file' => 'export-ui.inc',
  65. 'file path' => drupal_get_path('module', 'ctools') . '/includes',
  66. );
  67. $path = !empty($item['path']) ? $prefix . '/' . $item['path'] : $prefix;
  68. unset($item['path']);
  69. $my_items[$path] = $item;
  70. }
  71. $items += $my_items;
  72. }
  73. }
  74. /**
  75. * Menu callback to determine if an operation is accessible.
  76. *
  77. * This function enforces a basic access check on the configured perm
  78. * string, and then additional checks as needed.
  79. *
  80. * @param $op
  81. * The 'op' of the menu item, which is defined by 'allowed operations'
  82. * and embedded into the arguments in the menu item.
  83. * @param $item
  84. * If an op that works on an item, then the item object, otherwise NULL.
  85. *
  86. * @return
  87. * TRUE if the current user has access, FALSE if not.
  88. */
  89. function access($op, $item) {
  90. if (!user_access($this->plugin['access'])) {
  91. return FALSE;
  92. }
  93. // More fine-grained access control:
  94. if ($op == 'add' && !user_access($this->plugin['create access'])) {
  95. return FALSE;
  96. }
  97. // More fine-grained access control:
  98. if (($op == 'revert' || $op == 'delete') && !user_access($this->plugin['delete access'])) {
  99. return FALSE;
  100. }
  101. // If we need to do a token test, do it here.
  102. if (!empty($this->plugin['allowed operations'][$op]['token']) && (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], $op))) {
  103. return FALSE;
  104. }
  105. switch ($op) {
  106. case 'import':
  107. return user_access('use ctools import');
  108. case 'revert':
  109. return ($item->export_type & EXPORT_IN_DATABASE) && ($item->export_type & EXPORT_IN_CODE);
  110. case 'delete':
  111. return ($item->export_type & EXPORT_IN_DATABASE) && !($item->export_type & EXPORT_IN_CODE);
  112. case 'disable':
  113. return empty($item->disabled);
  114. case 'enable':
  115. return !empty($item->disabled);
  116. default:
  117. return TRUE;
  118. }
  119. }
  120. // ------------------------------------------------------------------------
  121. // These methods are the API for generating the list of exportable items.
  122. /**
  123. * Master entry point for handling a list.
  124. *
  125. * It is unlikely that a child object will need to override this method,
  126. * unless the listing mechanism is going to be highly specialized.
  127. */
  128. function list_page($js, $input) {
  129. $this->items = ctools_export_crud_load_all($this->plugin['schema'], $js);
  130. // Respond to a reset command by clearing session and doing a drupal goto
  131. // back to the base URL.
  132. if (isset($input['op']) && $input['op'] == t('Reset')) {
  133. unset($_SESSION['ctools_export_ui'][$this->plugin['name']]);
  134. if (!$js) {
  135. drupal_goto($_GET['q']);
  136. }
  137. // clear everything but form id, form build id and form token:
  138. $keys = array_keys($input);
  139. foreach ($keys as $id) {
  140. if (!in_array($id, array('form_id', 'form_build_id', 'form_token'))) {
  141. unset($input[$id]);
  142. }
  143. }
  144. $replace_form = TRUE;
  145. }
  146. // If there is no input, check to see if we have stored input in the
  147. // session.
  148. if (!isset($input['form_id'])) {
  149. if (isset($_SESSION['ctools_export_ui'][$this->plugin['name']]) && is_array($_SESSION['ctools_export_ui'][$this->plugin['name']])) {
  150. $input = $_SESSION['ctools_export_ui'][$this->plugin['name']];
  151. }
  152. }
  153. else {
  154. $_SESSION['ctools_export_ui'][$this->plugin['name']] = $input;
  155. unset($_SESSION['ctools_export_ui'][$this->plugin['name']]['q']);
  156. }
  157. // This is where the form will put the output.
  158. $this->rows = array();
  159. $this->sorts = array();
  160. $form_state = array(
  161. 'plugin' => $this->plugin,
  162. 'input' => $input,
  163. 'rerender' => TRUE,
  164. 'no_redirect' => TRUE,
  165. 'object' => &$this,
  166. );
  167. if (!isset($form_state['input']['form_id'])) {
  168. $form_state['input']['form_id'] = 'ctools_export_ui_list_form';
  169. }
  170. // If we do any form rendering, it's to completely replace a form on the
  171. // page, so don't let it force our ids to change.
  172. if ($js && isset($_POST['ajax_html_ids'])) {
  173. unset($_POST['ajax_html_ids']);
  174. }
  175. $form = drupal_build_form('ctools_export_ui_list_form', $form_state);
  176. $form = drupal_render($form);
  177. $output = $this->list_header($form_state) . $this->list_render($form_state) . $this->list_footer($form_state);
  178. if (!$js) {
  179. $this->list_css();
  180. return $form . $output;
  181. }
  182. $commands = array();
  183. $commands[] = ajax_command_replace('#ctools-export-ui-list-items', $output);
  184. if (!empty($replace_form)) {
  185. $commands[] = ajax_command_replace('#ctools-export-ui-list-form', $form);
  186. }
  187. print ajax_render($commands);
  188. ajax_footer();
  189. }
  190. /**
  191. * Create the filter/sort form at the top of a list of exports.
  192. *
  193. * This handles the very default conditions, and most lists are expected
  194. * to override this and call through to parent::list_form() in order to
  195. * get the base form and then modify it as necessary to add search
  196. * gadgets for custom fields.
  197. */
  198. function list_form(&$form, &$form_state) {
  199. // This forces the form to *always* treat as submitted which is
  200. // necessary to make it work.
  201. $form['#token'] = FALSE;
  202. if (empty($form_state['input'])) {
  203. $form["#post"] = TRUE;
  204. }
  205. // Add the 'q' in if we are not using clean URLs or it can get lost when
  206. // using this kind of form.
  207. if (!variable_get('clean_url', FALSE)) {
  208. $form['q'] = array(
  209. '#type' => 'hidden',
  210. '#value' => $_GET['q'],
  211. );
  212. }
  213. $all = array('all' => t('- All -'));
  214. $form['top row'] = array(
  215. '#prefix' => '<div class="ctools-export-ui-row ctools-export-ui-top-row clearfix">',
  216. '#suffix' => '</div>',
  217. );
  218. $form['bottom row'] = array(
  219. '#prefix' => '<div class="ctools-export-ui-row ctools-export-ui-bottom-row clearfix">',
  220. '#suffix' => '</div>',
  221. );
  222. $form['top row']['storage'] = array(
  223. '#type' => 'select',
  224. '#title' => t('Storage'),
  225. '#options' => $all + array(
  226. t('Normal') => t('Normal'),
  227. t('Default') => t('Default'),
  228. t('Overridden') => t('Overridden'),
  229. ),
  230. '#default_value' => 'all',
  231. );
  232. $form['top row']['disabled'] = array(
  233. '#type' => 'select',
  234. '#title' => t('Enabled'),
  235. '#options' => $all + array(
  236. '0' => t('Enabled'),
  237. '1' => t('Disabled')
  238. ),
  239. '#default_value' => 'all',
  240. );
  241. $form['top row']['search'] = array(
  242. '#type' => 'textfield',
  243. '#title' => t('Search'),
  244. );
  245. $form['bottom row']['order'] = array(
  246. '#type' => 'select',
  247. '#title' => t('Sort by'),
  248. '#options' => $this->list_sort_options(),
  249. '#default_value' => 'disabled',
  250. );
  251. $form['bottom row']['sort'] = array(
  252. '#type' => 'select',
  253. '#title' => t('Order'),
  254. '#options' => array(
  255. 'asc' => t('Up'),
  256. 'desc' => t('Down'),
  257. ),
  258. '#default_value' => 'asc',
  259. );
  260. $form['bottom row']['submit'] = array(
  261. '#type' => 'submit',
  262. '#id' => 'ctools-export-ui-list-items-apply',
  263. '#value' => t('Apply'),
  264. '#attributes' => array('class' => array('use-ajax-submit ctools-auto-submit-click')),
  265. );
  266. $form['bottom row']['reset'] = array(
  267. '#type' => 'submit',
  268. '#id' => 'ctools-export-ui-list-items-apply',
  269. '#value' => t('Reset'),
  270. '#attributes' => array('class' => array('use-ajax-submit')),
  271. );
  272. $form['#prefix'] = '<div class="clearfix">';
  273. $form['#suffix'] = '</div>';
  274. $form['#attached']['js'] = array(ctools_attach_js('auto-submit'));
  275. $form['#attached']['library'][] = array('system', 'drupal.ajax');
  276. $form['#attached']['library'][] = array('system', 'jquery.form');
  277. $form['#attributes'] = array('class' => array('ctools-auto-submit-full-form'));
  278. }
  279. /**
  280. * Validate the filter/sort form.
  281. *
  282. * It is very rare that a filter form needs validation, but if it is
  283. * needed, override this.
  284. */
  285. function list_form_validate(&$form, &$form_state) { }
  286. /**
  287. * Submit the filter/sort form.
  288. *
  289. * This submit handler is actually responsible for building up all of the
  290. * rows that will later be rendered, since it is doing the filtering and
  291. * sorting.
  292. *
  293. * For the most part, you should not need to override this method, as the
  294. * fiddly bits call through to other functions.
  295. */
  296. function list_form_submit(&$form, &$form_state) {
  297. // Filter and re-sort the pages.
  298. $plugin = $this->plugin;
  299. $prefix = ctools_export_ui_plugin_base_path($plugin);
  300. foreach ($this->items as $name => $item) {
  301. // Call through to the filter and see if we're going to render this
  302. // row. If it returns TRUE, then this row is filtered out.
  303. if ($this->list_filter($form_state, $item)) {
  304. continue;
  305. }
  306. $operations = $this->build_operations($item);
  307. $this->list_build_row($item, $form_state, $operations);
  308. }
  309. // Now actually sort
  310. if ($form_state['values']['sort'] == 'desc') {
  311. arsort($this->sorts);
  312. }
  313. else {
  314. asort($this->sorts);
  315. }
  316. // Nuke the original.
  317. $rows = $this->rows;
  318. $this->rows = array();
  319. // And restore.
  320. foreach ($this->sorts as $name => $title) {
  321. $this->rows[$name] = $rows[$name];
  322. }
  323. }
  324. /**
  325. * Determine if a row should be filtered out.
  326. *
  327. * This handles the default filters for the export UI list form. If you
  328. * added additional filters in list_form() then this is where you should
  329. * handle them.
  330. *
  331. * @return
  332. * TRUE if the item should be excluded.
  333. */
  334. function list_filter($form_state, $item) {
  335. $schema = ctools_export_get_schema($this->plugin['schema']);
  336. if ($form_state['values']['storage'] != 'all' && $form_state['values']['storage'] != $item->{$schema['export']['export type string']}) {
  337. return TRUE;
  338. }
  339. if ($form_state['values']['disabled'] != 'all' && $form_state['values']['disabled'] != !empty($item->disabled)) {
  340. return TRUE;
  341. }
  342. if ($form_state['values']['search']) {
  343. $search = strtolower($form_state['values']['search']);
  344. foreach ($this->list_search_fields() as $field) {
  345. if (strpos(strtolower($item->$field), $search) !== FALSE) {
  346. $hit = TRUE;
  347. break;
  348. }
  349. }
  350. if (empty($hit)) {
  351. return TRUE;
  352. }
  353. }
  354. }
  355. /**
  356. * Provide a list of fields to test against for the default "search" widget.
  357. *
  358. * This widget will search against whatever fields are configured here. By
  359. * default it will attempt to search against the name, title and description fields.
  360. */
  361. function list_search_fields() {
  362. $fields = array(
  363. $this->plugin['export']['key'],
  364. );
  365. if (!empty($this->plugin['export']['admin_title'])) {
  366. $fields[] = $this->plugin['export']['admin_title'];
  367. }
  368. if (!empty($this->plugin['export']['admin_description'])) {
  369. $fields[] = $this->plugin['export']['admin_description'];
  370. }
  371. return $fields;
  372. }
  373. /**
  374. * Provide a list of sort options.
  375. *
  376. * Override this if you wish to provide more or change how these work.
  377. * The actual handling of the sorting will happen in build_row().
  378. */
  379. function list_sort_options() {
  380. if (!empty($this->plugin['export']['admin_title'])) {
  381. $options = array(
  382. 'disabled' => t('Enabled, title'),
  383. $this->plugin['export']['admin_title'] => t('Title'),
  384. );
  385. }
  386. else {
  387. $options = array(
  388. 'disabled' => t('Enabled, name'),
  389. );
  390. }
  391. $options += array(
  392. 'name' => t('Name'),
  393. 'storage' => t('Storage'),
  394. );
  395. return $options;
  396. }
  397. /**
  398. * Add listing CSS to the page.
  399. *
  400. * Override this if you need custom CSS for your list.
  401. */
  402. function list_css() {
  403. ctools_add_css('export-ui-list');
  404. }
  405. /**
  406. * Builds the operation links for a specific exportable item.
  407. */
  408. function build_operations($item) {
  409. $plugin = $this->plugin;
  410. $schema = ctools_export_get_schema($plugin['schema']);
  411. $operations = $plugin['allowed operations'];
  412. $operations['import'] = FALSE;
  413. if ($item->{$schema['export']['export type string']} == t('Normal')) {
  414. $operations['revert'] = FALSE;
  415. }
  416. elseif ($item->{$schema['export']['export type string']} == t('Overridden')) {
  417. $operations['delete'] = FALSE;
  418. }
  419. else {
  420. $operations['revert'] = FALSE;
  421. $operations['delete'] = FALSE;
  422. }
  423. if (empty($item->disabled)) {
  424. $operations['enable'] = FALSE;
  425. }
  426. else {
  427. $operations['disable'] = FALSE;
  428. }
  429. $allowed_operations = array();
  430. foreach ($operations as $op => $info) {
  431. if (!empty($info)) {
  432. $allowed_operations[$op] = array(
  433. 'title' => $info['title'],
  434. 'href' => ctools_export_ui_plugin_menu_path($plugin, $op, $item->{$this->plugin['export']['key']}),
  435. );
  436. if (!empty($info['ajax'])) {
  437. $allowed_operations[$op]['attributes'] = array('class' => array('use-ajax'));
  438. }
  439. if (!empty($info['token'])) {
  440. $allowed_operations[$op]['query'] = array('token' => drupal_get_token($op));
  441. }
  442. }
  443. }
  444. return $allowed_operations;
  445. }
  446. /**
  447. * Build a row based on the item.
  448. *
  449. * By default all of the rows are placed into a table by the render
  450. * method, so this is building up a row suitable for theme('table').
  451. * This doesn't have to be true if you override both.
  452. */
  453. function list_build_row($item, &$form_state, $operations) {
  454. // Set up sorting
  455. $name = $item->{$this->plugin['export']['key']};
  456. $schema = ctools_export_get_schema($this->plugin['schema']);
  457. // Note: $item->{$schema['export']['export type string']} should have already been set up by export.inc so
  458. // we can use it safely.
  459. switch ($form_state['values']['order']) {
  460. case 'disabled':
  461. $this->sorts[$name] = empty($item->disabled) . $name;
  462. break;
  463. case 'title':
  464. $this->sorts[$name] = $item->{$this->plugin['export']['admin_title']};
  465. break;
  466. case 'name':
  467. $this->sorts[$name] = $name;
  468. break;
  469. case 'storage':
  470. $this->sorts[$name] = $item->{$schema['export']['export type string']} . $name;
  471. break;
  472. }
  473. $this->rows[$name]['data'] = array();
  474. $this->rows[$name]['class'] = !empty($item->disabled) ? array('ctools-export-ui-disabled') : array('ctools-export-ui-enabled');
  475. // If we have an admin title, make it the first row.
  476. if (!empty($this->plugin['export']['admin_title'])) {
  477. $this->rows[$name]['data'][] = array('data' => check_plain($item->{$this->plugin['export']['admin_title']}), 'class' => array('ctools-export-ui-title'));
  478. }
  479. $this->rows[$name]['data'][] = array('data' => check_plain($name), 'class' => array('ctools-export-ui-name'));
  480. $this->rows[$name]['data'][] = array('data' => check_plain($item->{$schema['export']['export type string']}), 'class' => array('ctools-export-ui-storage'));
  481. $ops = theme('links__ctools_dropbutton', array('links' => $operations, 'attributes' => array('class' => array('links', 'inline'))));
  482. $this->rows[$name]['data'][] = array('data' => $ops, 'class' => array('ctools-export-ui-operations'));
  483. // Add an automatic mouseover of the description if one exists.
  484. if (!empty($this->plugin['export']['admin_description'])) {
  485. $this->rows[$name]['title'] = $item->{$this->plugin['export']['admin_description']};
  486. }
  487. }
  488. /**
  489. * Provide the table header.
  490. *
  491. * If you've added columns via list_build_row() but are still using a
  492. * table, override this method to set up the table header.
  493. */
  494. function list_table_header() {
  495. $header = array();
  496. if (!empty($this->plugin['export']['admin_title'])) {
  497. $header[] = array('data' => t('Title'), 'class' => array('ctools-export-ui-title'));
  498. }
  499. $header[] = array('data' => t('Name'), 'class' => array('ctools-export-ui-name'));
  500. $header[] = array('data' => t('Storage'), 'class' => array('ctools-export-ui-storage'));
  501. $header[] = array('data' => t('Operations'), 'class' => array('ctools-export-ui-operations'));
  502. return $header;
  503. }
  504. /**
  505. * Render all of the rows together.
  506. *
  507. * By default we place all of the rows in a table, and this should be the
  508. * way most lists will go.
  509. *
  510. * Whatever you do if this method is overridden, the ID is important for AJAX
  511. * so be sure it exists.
  512. */
  513. function list_render(&$form_state) {
  514. $table = array(
  515. 'header' => $this->list_table_header(),
  516. 'rows' => $this->rows,
  517. 'attributes' => array('id' => 'ctools-export-ui-list-items'),
  518. 'empty' => $this->plugin['strings']['message']['no items'],
  519. );
  520. return theme('table', $table);
  521. }
  522. /**
  523. * Render a header to go before the list.
  524. *
  525. * This will appear after the filter/sort widgets.
  526. */
  527. function list_header($form_state) { }
  528. /**
  529. * Render a footer to go after thie list.
  530. *
  531. * This is a good place to add additional links.
  532. */
  533. function list_footer($form_state) { }
  534. // ------------------------------------------------------------------------
  535. // These methods are the API for adding/editing exportable items
  536. /**
  537. * Perform a drupal_goto() to the location provided by the plugin for the
  538. * operation.
  539. *
  540. * @param $op
  541. * The operation to use. A string must exist in $this->plugin['redirect']
  542. * for this operation.
  543. * @param $item
  544. * The item in use; this may be necessary as item IDs are often embedded in
  545. * redirects.
  546. */
  547. function redirect($op, $item = NULL) {
  548. if (isset($this->plugin['redirect'][$op])) {
  549. $destination = (array) $this->plugin['redirect'][$op];
  550. if ($item) {
  551. $export_key = $this->plugin['export']['key'];
  552. $destination[0] = str_replace('%ctools_export_ui', $item->{$export_key}, $destination[0]);
  553. }
  554. call_user_func_array('drupal_goto', $destination);
  555. }
  556. else {
  557. // If the operation isn't set, fall back to the plugin's base path.
  558. drupal_goto(ctools_export_ui_plugin_base_path($this->plugin));
  559. }
  560. }
  561. function add_page($js, $input, $step = NULL) {
  562. drupal_set_title($this->get_page_title('add'), PASS_THROUGH);
  563. // If a step not set, they are trying to create a new item. If a step
  564. // is set, they're in the process of creating an item.
  565. if (!empty($this->plugin['use wizard']) && !empty($step)) {
  566. $item = $this->edit_cache_get(NULL, 'add');
  567. }
  568. if (empty($item)) {
  569. $item = ctools_export_crud_new($this->plugin['schema']);
  570. }
  571. $form_state = array(
  572. 'plugin' => $this->plugin,
  573. 'object' => &$this,
  574. 'ajax' => $js,
  575. 'item' => $item,
  576. 'op' => 'add',
  577. 'form type' => 'add',
  578. 'rerender' => TRUE,
  579. 'no_redirect' => TRUE,
  580. 'step' => $step,
  581. // Store these in case additional args are needed.
  582. 'function args' => func_get_args(),
  583. );
  584. $output = $this->edit_execute_form($form_state);
  585. if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
  586. $this->redirect($form_state['op'], $form_state['item']);
  587. }
  588. return $output;
  589. }
  590. /**
  591. * Main entry point to edit an item.
  592. */
  593. function edit_page($js, $input, $item, $step = NULL) {
  594. drupal_set_title($this->get_page_title('edit', $item), PASS_THROUGH);
  595. // Check to see if there is a cached item to get if we're using the wizard.
  596. if (!empty($this->plugin['use wizard'])) {
  597. $cached = $this->edit_cache_get($item, 'edit');
  598. if (!empty($cached)) {
  599. $item = $cached;
  600. }
  601. }
  602. $form_state = array(
  603. 'plugin' => $this->plugin,
  604. 'object' => &$this,
  605. 'ajax' => $js,
  606. 'item' => $item,
  607. 'op' => 'edit',
  608. 'form type' => 'edit',
  609. 'rerender' => TRUE,
  610. 'no_redirect' => TRUE,
  611. 'step' => $step,
  612. // Store these in case additional args are needed.
  613. 'function args' => func_get_args(),
  614. );
  615. $output = $this->edit_execute_form($form_state);
  616. if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
  617. $this->redirect($form_state['op'], $form_state['item']);
  618. }
  619. return $output;
  620. }
  621. /**
  622. * Main entry point to clone an item.
  623. */
  624. function clone_page($js, $input, $original, $step = NULL) {
  625. drupal_set_title($this->get_page_title('clone', $original), PASS_THROUGH);
  626. // If a step not set, they are trying to create a new clone. If a step
  627. // is set, they're in the process of cloning an item.
  628. if (!empty($this->plugin['use wizard']) && !empty($step)) {
  629. $item = $this->edit_cache_get(NULL, 'clone');
  630. }
  631. if (empty($item)) {
  632. // To make a clone of an item, we first export it and then re-import it.
  633. // Export the handler, which is a fantastic way to clean database IDs out of it.
  634. $export = ctools_export_crud_export($this->plugin['schema'], $original);
  635. $item = ctools_export_crud_import($this->plugin['schema'], $export);
  636. if (!empty($input[$this->plugin['export']['key']])) {
  637. $item->{$this->plugin['export']['key']} = $input[$this->plugin['export']['key']];
  638. }
  639. else {
  640. $item->{$this->plugin['export']['key']} = 'clone_of_' . $item->{$this->plugin['export']['key']};
  641. }
  642. }
  643. // Tabs and breadcrumb disappearing, this helps alleviate through cheating.
  644. // ...not sure this this is the best way.
  645. $trail = menu_set_active_item(ctools_export_ui_plugin_base_path($this->plugin));
  646. $name = $original->{$this->plugin['export']['key']};
  647. $form_state = array(
  648. 'plugin' => $this->plugin,
  649. 'object' => &$this,
  650. 'ajax' => $js,
  651. 'item' => $item,
  652. 'op' => 'add',
  653. 'form type' => 'clone',
  654. 'original name' => $name,
  655. 'rerender' => TRUE,
  656. 'no_redirect' => TRUE,
  657. 'step' => $step,
  658. // Store these in case additional args are needed.
  659. 'function args' => func_get_args(),
  660. );
  661. $output = $this->edit_execute_form($form_state);
  662. if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
  663. $this->redirect($form_state['op'], $form_state['item']);
  664. }
  665. return $output;
  666. }
  667. /**
  668. * Execute the form.
  669. *
  670. * Add and Edit both funnel into this, but they have a few different
  671. * settings.
  672. */
  673. function edit_execute_form(&$form_state) {
  674. if (!empty($this->plugin['use wizard'])) {
  675. return $this->edit_execute_form_wizard($form_state);
  676. }
  677. else {
  678. return $this->edit_execute_form_standard($form_state);
  679. }
  680. }
  681. /**
  682. * Execute the standard form for editing.
  683. *
  684. * By default, export UI will provide a single form for editing an object.
  685. */
  686. function edit_execute_form_standard(&$form_state) {
  687. $output = drupal_build_form('ctools_export_ui_edit_item_form', $form_state);
  688. if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
  689. $this->edit_save_form($form_state);
  690. }
  691. return $output;
  692. }
  693. /**
  694. * Get the form info for the wizard.
  695. *
  696. * This gets the form info out of the plugin, then adds defaults based on
  697. * how we want edit forms to work.
  698. *
  699. * Overriding this can allow child UIs to tweak this info for specialized
  700. * wizards.
  701. *
  702. * @param array $form_state
  703. * The already created form state.
  704. */
  705. function get_wizard_info(&$form_state) {
  706. if (!isset($form_state['step'])) {
  707. $form_state['step'] = NULL;
  708. }
  709. $export_key = $this->plugin['export']['key'];
  710. // When cloning, the name of the item being cloned is referenced in the
  711. // path, not the name of this item.
  712. if ($form_state['form type'] == 'clone') {
  713. $name = $form_state['original name'];
  714. }
  715. else {
  716. $name = $form_state['item']->{$export_key};
  717. }
  718. $form_info = !empty($this->plugin['form info']) ? $this->plugin['form info'] : array();
  719. $form_info += array(
  720. 'id' => 'ctools_export_ui_edit',
  721. 'path' => ctools_export_ui_plugin_menu_path($this->plugin, $form_state['form type'], $name) . '/%step',
  722. 'show trail' => TRUE,
  723. 'free trail' => TRUE,
  724. 'show back' => $form_state['form type'] == 'add',
  725. 'show return' => FALSE,
  726. 'show cancel' => TRUE,
  727. 'finish callback' => 'ctools_export_ui_wizard_finish',
  728. 'next callback' => 'ctools_export_ui_wizard_next',
  729. 'back callback' => 'ctools_export_ui_wizard_back',
  730. 'cancel callback' => 'ctools_export_ui_wizard_cancel',
  731. 'order' => array(),
  732. 'import order' => array(
  733. 'import' => t('Import code'),
  734. 'settings' => t('Settings'),
  735. ),
  736. );
  737. // Set the order of forms based on the op if we have a specific one.
  738. if (isset($form_info[$form_state['form type'] . ' order'])) {
  739. $form_info['order'] = $form_info[$form_state['form type'] . ' order'];
  740. }
  741. // We have generic fallback forms we can use if they are not specified,
  742. // and they automatically delegate back to the UI object. Use these if
  743. // not specified.
  744. foreach ($form_info['order'] as $key => $title) {
  745. if (empty($form_info['forms'][$key])) {
  746. $form_info['forms'][$key] = array(
  747. 'form id' => 'ctools_export_ui_edit_item_wizard_form',
  748. );
  749. }
  750. }
  751. // 'free trail' means the wizard can freely go back and form from item
  752. // via the trail and not with next/back buttons.
  753. if ($form_state['form type'] == 'add' || ($form_state['form type'] == 'import' && empty($form_state['item']->{$export_key}))) {
  754. $form_info['free trail'] = FALSE;
  755. }
  756. return $form_info;
  757. }
  758. /**
  759. * Execute the wizard for editing.
  760. *
  761. * For complex objects, sometimes a wizard is needed. This is normally
  762. * activated by setting 'use wizard' => TRUE in the plugin definition
  763. * and then creating a 'form info' array to feed the wizard the data
  764. * it needs.
  765. *
  766. * When creating this wizard, the plugin is responsible for defining all forms
  767. * that will be utilized with the wizard.
  768. *
  769. * Using 'add order' or 'edit order' can be used to ensure that add/edit order
  770. * is different.
  771. */
  772. function edit_execute_form_wizard(&$form_state) {
  773. $form_info = $this->get_wizard_info($form_state);
  774. // If there aren't any forms set, fail.
  775. if (empty($form_info['order'])) {
  776. return MENU_NOT_FOUND;
  777. }
  778. // Figure out if this is a new instance of the wizard
  779. if (empty($form_state['step'])) {
  780. $order = array_keys($form_info['order']);
  781. $form_state['step'] = reset($order);
  782. }
  783. if (empty($form_info['order'][$form_state['step']]) && empty($form_info['forms'][$form_state['step']])) {
  784. return MENU_NOT_FOUND;
  785. }
  786. ctools_include('wizard');
  787. $output = ctools_wizard_multistep_form($form_info, $form_state['step'], $form_state);
  788. if (!empty($form_state['complete'])) {
  789. $this->edit_save_form($form_state);
  790. }
  791. else if ($output && !empty($form_state['item']->export_ui_item_is_cached)) {
  792. // @todo this should be in the plugin strings
  793. drupal_set_message(t('You have unsaved changes. These changes will not be made permanent until you click <em>Save</em>.'), 'warning');
  794. }
  795. // Unset the executed flag if any non-wizard button was pressed. Those
  796. // buttons require special handling by whatever client is operating them.
  797. if (!empty($form_state['executed']) && empty($form_state['clicked_button']['#wizard type'])) {
  798. unset($form_state['executed']);
  799. }
  800. return $output;
  801. }
  802. /**
  803. * Wizard 'back' callback when using a wizard to edit an item.
  804. *
  805. * The wizard callback delegates this back to the object.
  806. */
  807. function edit_wizard_back(&$form_state) {
  808. // This only exists so child implementations can use it.
  809. }
  810. /**
  811. * Wizard 'next' callback when using a wizard to edit an item.
  812. *
  813. * The wizard callback delegates this back to the object.
  814. */
  815. function edit_wizard_next(&$form_state) {
  816. $this->edit_cache_set($form_state['item'], $form_state['form type']);
  817. }
  818. /**
  819. * Wizard 'cancel' callback when using a wizard to edit an item.
  820. *
  821. * The wizard callback delegates this back to the object.
  822. */
  823. function edit_wizard_cancel(&$form_state) {
  824. $this->edit_cache_clear($form_state['item'], $form_state['form type']);
  825. }
  826. /**
  827. * Wizard 'cancel' callback when using a wizard to edit an item.
  828. *
  829. * The wizard callback delegates this back to the object.
  830. */
  831. function edit_wizard_finish(&$form_state) {
  832. $form_state['complete'] = TRUE;
  833. // If we are importing, and overwrite was selected, delete the original so
  834. // that this one writes properly.
  835. if ($form_state['form type'] == 'import' && !empty($form_state['item']->export_ui_allow_overwrite)) {
  836. ctools_export_crud_delete($this->plugin['schema'], $form_state['item']);
  837. }
  838. $this->edit_cache_clear($form_state['item'], $form_state['form type']);
  839. }
  840. /**
  841. * Retrieve the item currently being edited from the object cache.
  842. */
  843. function edit_cache_get($item, $op = 'edit') {
  844. ctools_include('object-cache');
  845. if (is_string($item)) {
  846. $name = $item;
  847. }
  848. else {
  849. $name = $this->edit_cache_get_key($item, $op);
  850. }
  851. $cache = ctools_object_cache_get('ctui_' . $this->plugin['name'], $name);
  852. if ($cache) {
  853. $cache->export_ui_item_is_cached = TRUE;
  854. return $cache;
  855. }
  856. }
  857. /**
  858. * Cache the item currently currently being edited.
  859. */
  860. function edit_cache_set($item, $op = 'edit') {
  861. ctools_include('object-cache');
  862. $name = $this->edit_cache_get_key($item, $op);
  863. return $this->edit_cache_set_key($item, $name);
  864. }
  865. function edit_cache_set_key($item, $name) {
  866. return ctools_object_cache_set('ctui_' . $this->plugin['name'], $name, $item);
  867. }
  868. /**
  869. * Clear the object cache for the currently edited item.
  870. */
  871. function edit_cache_clear($item, $op = 'edit') {
  872. ctools_include('object-cache');
  873. $name = $this->edit_cache_get_key($item, $op);
  874. return ctools_object_cache_clear('ctui_' . $this->plugin['name'], $name);
  875. }
  876. /**
  877. * Figure out what the cache key is for this object.
  878. */
  879. function edit_cache_get_key($item, $op) {
  880. $export_key = $this->plugin['export']['key'];
  881. return $op == 'edit' ? $item->{$this->plugin['export']['key']} : "::$op";
  882. }
  883. /**
  884. * Called to save the final product from the edit form.
  885. */
  886. function edit_save_form($form_state) {
  887. $item = &$form_state['item'];
  888. $export_key = $this->plugin['export']['key'];
  889. $result = ctools_export_crud_save($this->plugin['schema'], $item);
  890. if ($result) {
  891. $message = str_replace('%title', check_plain($item->{$export_key}), $this->plugin['strings']['confirmation'][$form_state['op']]['success']);
  892. drupal_set_message($message);
  893. }
  894. else {
  895. $message = str_replace('%title', check_plain($item->{$export_key}), $this->plugin['strings']['confirmation'][$form_state['op']]['fail']);
  896. drupal_set_message($message, 'error');
  897. }
  898. }
  899. /**
  900. * Provide the actual editing form.
  901. */
  902. function edit_form(&$form, &$form_state) {
  903. $export_key = $this->plugin['export']['key'];
  904. $item = $form_state['item'];
  905. $schema = ctools_export_get_schema($this->plugin['schema']);
  906. if (!empty($this->plugin['export']['admin_title'])) {
  907. $form['info'][$this->plugin['export']['admin_title']] = array(
  908. '#type' => 'textfield',
  909. '#title' => t('Administrative title'),
  910. '#description' => t('This will appear in the administrative interface to easily identify it.'),
  911. '#default_value' => $item->{$this->plugin['export']['admin_title']},
  912. );
  913. }
  914. $form['info'][$export_key] = array(
  915. '#title' => t($schema['export']['key name']),
  916. '#type' => 'textfield',
  917. '#default_value' => $item->{$export_key},
  918. '#description' => t('The unique ID for this @export.', array('@export' => $this->plugin['title singular'])),
  919. '#required' => TRUE,
  920. '#maxlength' => 255,
  921. );
  922. if (!empty($this->plugin['export']['admin_title'])) {
  923. $form['info'][$export_key]['#type'] = 'machine_name';
  924. $form['info'][$export_key]['#machine_name'] = array(
  925. 'exists' => 'ctools_export_ui_edit_name_exists',
  926. 'source' => array('info', $this->plugin['export']['admin_title']),
  927. );
  928. }
  929. if ($form_state['op'] === 'edit') {
  930. $form['info'][$export_key]['#disabled'] = TRUE;
  931. $form['info'][$export_key]['#value'] = $item->{$export_key};
  932. }
  933. if (!empty($this->plugin['export']['admin_description'])) {
  934. $form['info'][$this->plugin['export']['admin_description']] = array(
  935. '#type' => 'textarea',
  936. '#title' => t('Administrative description'),
  937. '#default_value' => $item->{$this->plugin['export']['admin_description']},
  938. );
  939. }
  940. // Add plugin's form definitions.
  941. if (!empty($this->plugin['form']['settings'])) {
  942. // Pass $form by reference.
  943. $this->plugin['form']['settings']($form, $form_state);
  944. }
  945. // Add the buttons if the wizard is not in use.
  946. if (empty($form_state['form_info'])) {
  947. // Make sure that whatever happens, the buttons go to the bottom.
  948. $form['buttons']['#weight'] = 100;
  949. // Add buttons.
  950. $form['buttons']['submit'] = array(
  951. '#type' => 'submit',
  952. '#value' => t('Save'),
  953. );
  954. $form['buttons']['delete'] = array(
  955. '#type' => 'submit',
  956. '#value' => $item->export_type & EXPORT_IN_CODE ? t('Revert') : t('Delete'),
  957. '#access' => $form_state['op'] === 'edit' && $item->export_type & EXPORT_IN_DATABASE,
  958. '#submit' => array('ctools_export_ui_edit_item_form_delete'),
  959. );
  960. }
  961. }
  962. /**
  963. * Validate callback for the edit form.
  964. */
  965. function edit_form_validate(&$form, &$form_state) {
  966. if (!empty($this->plugin['form']['validate'])) {
  967. // Pass $form by reference.
  968. $this->plugin['form']['validate']($form, $form_state);
  969. }
  970. }
  971. /**
  972. * Perform a final validation check before allowing the form to be
  973. * finished.
  974. */
  975. function edit_finish_validate(&$form, &$form_state) {
  976. if ($form_state['op'] != 'edit') {
  977. // Validate the export key. Fake an element for form_error().
  978. $export_key = $this->plugin['export']['key'];
  979. $element = array(
  980. '#value' => $form_state['item']->{$export_key},
  981. '#parents' => array($export_key),
  982. );
  983. $form_state['plugin'] = $this->plugin;
  984. ctools_export_ui_edit_name_validate($element, $form_state);
  985. }
  986. }
  987. /**
  988. * Handle the submission of the edit form.
  989. *
  990. * At this point, submission is successful. Our only responsibility is
  991. * to copy anything out of values onto the item that we are able to edit.
  992. *
  993. * If the keys all match up to the schema, this method will not need to be
  994. * overridden.
  995. */
  996. function edit_form_submit(&$form, &$form_state) {
  997. if (!empty($this->plugin['form']['submit'])) {
  998. // Pass $form by reference.
  999. $this->plugin['form']['submit']($form, $form_state);
  1000. }
  1001. // Transfer data from the form to the $item based upon schema values.
  1002. $schema = ctools_export_get_schema($this->plugin['schema']);
  1003. foreach (array_keys($schema['fields']) as $key) {
  1004. if(isset($form_state['values'][$key])) {
  1005. $form_state['item']->{$key} = $form_state['values'][$key];
  1006. }
  1007. }
  1008. }
  1009. // ------------------------------------------------------------------------
  1010. // These methods are the API for 'other' stuff with exportables such as
  1011. // enable, disable, import, export, delete
  1012. /**
  1013. * Callback to enable a page.
  1014. */
  1015. function enable_page($js, $input, $item) {
  1016. return $this->set_item_state(FALSE, $js, $input, $item);
  1017. }
  1018. /**
  1019. * Callback to disable a page.
  1020. */
  1021. function disable_page($js, $input, $item) {
  1022. return $this->set_item_state(TRUE, $js, $input, $item);
  1023. }
  1024. /**
  1025. * Set an item's state to enabled or disabled and output to user.
  1026. *
  1027. * If javascript is in use, this will rebuild the list and send that back
  1028. * as though the filter form had been executed.
  1029. */
  1030. function set_item_state($state, $js, $input, $item) {
  1031. ctools_export_crud_set_status($this->plugin['schema'], $item, $state);
  1032. if (!$js) {
  1033. drupal_goto(ctools_export_ui_plugin_base_path($this->plugin));
  1034. }
  1035. else {
  1036. return $this->list_page($js, $input);
  1037. }
  1038. }
  1039. /**
  1040. * Page callback to delete an exportable item.
  1041. */
  1042. function delete_page($js, $input, $item) {
  1043. $form_state = array(
  1044. 'plugin' => $this->plugin,
  1045. 'object' => &$this,
  1046. 'ajax' => $js,
  1047. 'item' => $item,
  1048. 'op' => $item->export_type & EXPORT_IN_CODE ? 'revert' : 'delete',
  1049. 'rerender' => TRUE,
  1050. 'no_redirect' => TRUE,
  1051. );
  1052. $output = drupal_build_form('ctools_export_ui_delete_confirm_form', $form_state);
  1053. if (!empty($form_state['executed'])) {
  1054. $this->delete_form_submit($form_state);
  1055. $this->redirect($form_state['op'], $item);
  1056. }
  1057. return $output;
  1058. }
  1059. /**
  1060. * Deletes exportable items from the database.
  1061. */
  1062. function delete_form_submit(&$form_state) {
  1063. $item = $form_state['item'];
  1064. ctools_export_crud_delete($this->plugin['schema'], $item);
  1065. $export_key = $this->plugin['export']['key'];
  1066. $message = str_replace('%title', check_plain($item->{$export_key}), $this->plugin['strings']['confirmation'][$form_state['op']]['success']);
  1067. drupal_set_message($message);
  1068. }
  1069. /**
  1070. * Page callback to display export information for an exportable item.
  1071. */
  1072. function export_page($js, $input, $item) {
  1073. drupal_set_title($this->get_page_title('export', $item), PASS_THROUGH);
  1074. return drupal_get_form('ctools_export_form', ctools_export_crud_export($this->plugin['schema'], $item), t('Export'));
  1075. }
  1076. /**
  1077. * Page callback to import information for an exportable item.
  1078. */
  1079. function import_page($js, $input, $step = NULL) {
  1080. drupal_set_title($this->get_page_title('import'), PASS_THROUGH);
  1081. // Import is basically a multi step wizard form, so let's go ahead and
  1082. // use CTools' wizard.inc for it.
  1083. // If a step not set, they are trying to create a new item. If a step
  1084. // is set, they're in the process of creating an item.
  1085. if (!empty($step)) {
  1086. $item = $this->edit_cache_get(NULL, 'import');
  1087. }
  1088. if (empty($item)) {
  1089. $item = ctools_export_crud_new($this->plugin['schema']);
  1090. }
  1091. $form_state = array(
  1092. 'plugin' => $this->plugin,
  1093. 'object' => &$this,
  1094. 'ajax' => $js,
  1095. 'item' => $item,
  1096. 'op' => 'add',
  1097. 'form type' => 'import',
  1098. 'rerender' => TRUE,
  1099. 'no_redirect' => TRUE,
  1100. 'step' => $step,
  1101. // Store these in case additional args are needed.
  1102. 'function args' => func_get_args(),
  1103. );
  1104. // import always uses the wizard.
  1105. $output = $this->edit_execute_form_wizard($form_state);
  1106. if (!empty($form_state['executed'])) {
  1107. $this->redirect($form_state['op'], $form_state['item']);
  1108. }
  1109. return $output;
  1110. }
  1111. /**
  1112. * Import form. Provides simple helptext instructions and textarea for
  1113. * pasting a export definition.
  1114. */
  1115. function edit_form_import(&$form, &$form_state) {
  1116. $form['help'] = array(
  1117. '#type' => 'item',
  1118. '#value' => $this->plugin['strings']['help']['import'],
  1119. );
  1120. $form['import'] = array(
  1121. '#title' => t('@plugin code', array('@plugin' => $this->plugin['title singular proper'])),
  1122. '#type' => 'textarea',
  1123. '#rows' => 10,
  1124. '#required' => TRUE,
  1125. '#default_value' => !empty($form_state['item']->export_ui_code) ? $form_state['item']->export_ui_code : '',
  1126. );
  1127. $form['overwrite'] = array(
  1128. '#title' => t('Allow import to overwrite an existing record.'),
  1129. '#type' => 'checkbox',
  1130. '#default_value' => !empty($form_state['item']->export_ui_allow_overwrite) ? $form_state['item']->export_ui_allow_overwrite : FALSE,
  1131. );
  1132. }
  1133. /**
  1134. * Import form validate handler.
  1135. *
  1136. * Evaluates code and make sure it creates an object before we continue.
  1137. */
  1138. function edit_form_import_validate($form, &$form_state) {
  1139. $item = ctools_export_crud_import($this->plugin['schema'], $form_state['values']['import']);
  1140. if (is_string($item)) {
  1141. form_error($form['import'], t('Unable to get an import from the code. Errors reported: @errors', array('@errors' => $item)));
  1142. return;
  1143. }
  1144. $form_state['item'] = $item;
  1145. $form_state['item']->export_ui_allow_overwrite = $form_state['values']['overwrite'];
  1146. $form_state['item']->export_ui_code = $form_state['values']['import'];
  1147. }
  1148. /**
  1149. * Submit callback for import form.
  1150. *
  1151. * Stores the item in the session.
  1152. */
  1153. function edit_form_import_submit($form, &$form_state) {
  1154. // The validate function already imported and stored the item. This
  1155. // function exists simply to prevent it from going to the default
  1156. // edit_form_submit() method.
  1157. }
  1158. }
  1159. // -----------------------------------------------------------------------
  1160. // Forms to be used with this class.
  1161. //
  1162. // Since Drupal's forms are completely procedural, these forms will
  1163. // mostly just be pass-throughs back to the object.
  1164. /**
  1165. * Add all appropriate includes to forms so that caching the form
  1166. * still loads the files that we need.
  1167. */
  1168. function _ctools_export_ui_add_form_files($form, &$form_state) {
  1169. ctools_form_include($form_state, 'export');
  1170. ctools_form_include($form_state, 'export-ui');
  1171. // Also make sure the plugin .inc file is loaded.
  1172. ctools_form_include_file($form_state, $form_state['object']->plugin['path'] . '/' . $form_state['object']->plugin['file']);
  1173. }
  1174. /**
  1175. * Form callback to handle the filter/sort form when listing items.
  1176. *
  1177. * This simply loads the object defined in the plugin and hands it off.
  1178. */
  1179. function ctools_export_ui_list_form($form, &$form_state) {
  1180. $form_state['object']->list_form($form, $form_state);
  1181. return $form;
  1182. }
  1183. /**
  1184. * Validate handler for ctools_export_ui_list_form.
  1185. */
  1186. function ctools_export_ui_list_form_validate(&$form, &$form_state) {
  1187. $form_state['object']->list_form_validate($form, $form_state);
  1188. }
  1189. /**
  1190. * Submit handler for ctools_export_ui_list_form.
  1191. */
  1192. function ctools_export_ui_list_form_submit(&$form, &$form_state) {
  1193. $form_state['object']->list_form_submit($form, $form_state);
  1194. }
  1195. /**
  1196. * Form callback to edit an exportable item.
  1197. *
  1198. * This simply loads the object defined in the plugin and hands it off.
  1199. */
  1200. function ctools_export_ui_edit_item_form($form, &$form_state) {
  1201. // When called using #ajax via ajax_form_callback(), 'export' may
  1202. // not be included so include it here.
  1203. _ctools_export_ui_add_form_files($form, $form_state);
  1204. $form_state['object']->edit_form($form, $form_state);
  1205. return $form;
  1206. }
  1207. /**
  1208. * Validate handler for ctools_export_ui_edit_item_form.
  1209. */
  1210. function ctools_export_ui_edit_item_form_validate(&$form, &$form_state) {
  1211. $form_state['object']->edit_form_validate($form, $form_state);
  1212. }
  1213. /**
  1214. * Submit handler for ctools_export_ui_edit_item_form.
  1215. */
  1216. function ctools_export_ui_edit_item_form_submit(&$form, &$form_state) {
  1217. $form_state['object']->edit_form_submit($form, $form_state);
  1218. }
  1219. /**
  1220. * Submit handler to delete for ctools_export_ui_edit_item_form
  1221. *
  1222. * @todo Put this on a callback in the object.
  1223. */
  1224. function ctools_export_ui_edit_item_form_delete(&$form, &$form_state) {
  1225. _ctools_export_ui_add_form_files($form, $form_state);
  1226. $export_key = $form_state['plugin']['export']['key'];
  1227. $path = $form_state['item']->export_type & EXPORT_IN_CODE ? 'revert' : 'delete';
  1228. drupal_goto(ctools_export_ui_plugin_menu_path($form_state['plugin'], $path, $form_state['item']->{$export_key}), array('cancel_path' => $_GET['q']));
  1229. }
  1230. /**
  1231. * Validate that an export item name is acceptable and unique during add.
  1232. */
  1233. function ctools_export_ui_edit_name_validate($element, &$form_state) {
  1234. $plugin = $form_state['plugin'];
  1235. // Check for string identifier sanity
  1236. if (!preg_match('!^[a-z0-9_]+$!', $element['#value'])) {
  1237. form_error($element, t('The export id can only consist of lowercase letters, underscores, and numbers.'));
  1238. return;
  1239. }
  1240. // Check for name collision
  1241. if (empty($form_state['item']->export_ui_allow_overwrite) && $exists = ctools_export_crud_load($plugin['schema'], $element['#value'])) {
  1242. form_error($element, t('A @plugin with this name already exists. Please choose another name or delete the existing item before creating a new one.', array('@plugin' => $plugin['title singular'])));
  1243. }
  1244. }
  1245. /**
  1246. * Test for #machine_name type to see if an export exists.
  1247. */
  1248. function ctools_export_ui_edit_name_exists($name, $element, &$form_state) {
  1249. $plugin = $form_state['plugin'];
  1250. return (empty($form_state['item']->export_ui_allow_overwrite) && ctools_export_crud_load($plugin['schema'], $name));
  1251. }
  1252. /**
  1253. * Delete/Revert confirm form.
  1254. *
  1255. * @todo -- call back into the object instead.
  1256. */
  1257. function ctools_export_ui_delete_confirm_form($form, &$form_state) {
  1258. _ctools_export_ui_add_form_files($form, $form_state);
  1259. $plugin = $form_state['plugin'];
  1260. $item = $form_state['item'];
  1261. $form = array();
  1262. $export_key = $plugin['export']['key'];
  1263. $question = str_replace('%title', check_plain($item->{$export_key}), $plugin['strings']['confirmation'][$form_state['op']]['question']);
  1264. $path = (!empty($_REQUEST['cancel_path']) && !url_is_external($_REQUEST['cancel_path'])) ? $_REQUEST['cancel_path'] : ctools_export_ui_plugin_base_path($plugin);
  1265. $form = confirm_form($form,
  1266. $question,
  1267. $path,
  1268. $plugin['strings']['confirmation'][$form_state['op']]['information'],
  1269. $plugin['allowed operations'][$form_state['op']]['title'], t('Cancel')
  1270. );
  1271. return $form;
  1272. }
  1273. // --------------------------------------------------------------------------
  1274. // Forms and callbacks for using the edit system with the wizard.
  1275. /**
  1276. * Form callback to edit an exportable item using the wizard
  1277. *
  1278. * This simply loads the object defined in the plugin and hands it off.
  1279. */
  1280. function ctools_export_ui_edit_item_wizard_form($form, &$form_state) {
  1281. _ctools_export_ui_add_form_files($form, $form_state);
  1282. $method = 'edit_form_' . $form_state['step'];
  1283. if (!method_exists($form_state['object'], $method)) {
  1284. $method = 'edit_form';
  1285. }
  1286. $form_state['object']->$method($form, $form_state);
  1287. return $form;
  1288. }
  1289. /**
  1290. * Validate handler for ctools_export_ui_edit_item_wizard_form.
  1291. */
  1292. function ctools_export_ui_edit_item_wizard_form_validate(&$form, &$form_state) {
  1293. $method = 'edit_form_' . $form_state['step'] . '_validate';
  1294. if (!method_exists($form_state['object'], $method)) {
  1295. $method = 'edit_form_validate';
  1296. }
  1297. $form_state['object']->$method($form, $form_state);
  1298. // Additionally, if there were no errors from that, and we're finishing,
  1299. // perform a final validate to make sure everything is ok.
  1300. if (isset($form_state['clicked_button']['#wizard type']) && $form_state['clicked_button']['#wizard type'] == 'finish' && !form_get_errors()) {
  1301. $form_state['object']->edit_finish_validate($form, $form_state);
  1302. }
  1303. }
  1304. /**
  1305. * Submit handler for ctools_export_ui_edit_item_wizard_form.
  1306. */
  1307. function ctools_export_ui_edit_item_wizard_form_submit(&$form, &$form_state) {
  1308. $method = 'edit_form_' . $form_state['step'] . '_submit';
  1309. if (!method_exists($form_state['object'], $method)) {
  1310. $method = 'edit_form_submit';
  1311. }
  1312. $form_state['object']->$method($form, $form_state);
  1313. }
  1314. /**
  1315. * Wizard 'back' callback when using a wizard to edit an item.
  1316. */
  1317. function ctools_export_ui_wizard_back(&$form_state) {
  1318. $form_state['object']->edit_wizard_back($form_state);
  1319. }
  1320. /**
  1321. * Wizard 'next' callback when using a wizard to edit an item.
  1322. */
  1323. function ctools_export_ui_wizard_next(&$form_state) {
  1324. $form_state['object']->edit_wizard_next($form_state);
  1325. }
  1326. /**
  1327. * Wizard 'cancel' callback when using a wizard to edit an item.
  1328. */
  1329. function ctools_export_ui_wizard_cancel(&$form_state) {
  1330. $form_state['object']->edit_wizard_cancel($form_state);
  1331. }
  1332. /**
  1333. * Wizard 'finish' callback when using a wizard to edit an item.
  1334. */
  1335. function ctools_export_ui_wizard_finish(&$form_state) {
  1336. $form_state['object']->edit_wizard_finish($form_state);
  1337. }