4 * Base class for export UI.
6 class ctools_export_ui
{
9 var $options = array();
12 * Fake constructor -- this is easier to deal with than the real
13 * constructor because we are retaining PHP4 compatibility, which
14 * would require all child classes to implement their own constructor.
16 function init($plugin) {
17 ctools_include('export');
19 $this->plugin
= $plugin;
23 * Get a page title for the current page from our plugin strings.
25 function get_page_title($op, $item = NULL) {
26 if (empty($this->plugin
['strings']['title'][$op])) {
30 // Replace %title that might be there with the exportable title.
31 $title = $this->plugin
['strings']['title'][$op];
33 $export_key = $this->plugin
['export']['key'];
34 $title = (str_replace('%title', check_plain($item->{$export_key}), $title));
41 * Called by ctools_export_ui_load to load the item.
43 * This can be overridden for modules that want to be able to export
44 * items currently being edited, for example.
46 function load_item($item_name) {
47 $item = ctools_export_crud_load($this->plugin
['schema'], $item_name);
48 return empty($item) ?
FALSE : $item;
51 // ------------------------------------------------------------------------
52 // Menu item manipulation
55 * hook_menu() entry point.
57 * Child implementations that need to add or modify menu items should
58 * probably call parent::hook_menu($items) and then modify as needed.
60 function hook_menu(&$items) {
61 // During upgrades, the schema can be empty as this is called prior to
62 // actual update functions being run. Ensure that we can cope with this
64 if (empty($this->plugin
['schema'])) {
68 $prefix = ctools_export_ui_plugin_base_path($this->plugin
);
70 if (isset($this->plugin
['menu']['items']) && is_array($this->plugin
['menu']['items'])) {
72 foreach ($this->plugin
['menu']['items'] as $item) {
73 // Add menu item defaults.
75 'file' => 'export-ui.inc',
76 'file path' => drupal_get_path('module', 'ctools') . '/includes',
79 $path = !empty($item['path']) ?
$prefix . '/' . $item['path'] : $prefix;
81 $my_items[$path] = $item;
88 * Menu callback to determine if an operation is accessible.
90 * This function enforces a basic access check on the configured perm
91 * string, and then additional checks as needed.
94 * The 'op' of the menu item, which is defined by 'allowed operations'
95 * and embedded into the arguments in the menu item.
97 * If an op that works on an item, then the item object, otherwise NULL.
100 * TRUE if the current user has access, FALSE if not.
102 function access($op, $item) {
103 if (!user_access($this->plugin
['access'])) {
107 // More fine-grained access control:
108 if ($op == 'add' && !user_access($this->plugin
['create access'])) {
112 // More fine-grained access control:
113 if (($op == 'revert' ||
$op == 'delete') && !user_access($this->plugin
['delete access'])) {
117 // If we need to do a token test, do it here.
118 if (!empty($this->plugin
['allowed operations'][$op]['token']) && (!isset($_GET['token']) ||
!drupal_valid_token($_GET['token'], $op))) {
124 return user_access('use ctools import');
126 return ($item->export_type
& EXPORT_IN_DATABASE
) && ($item->export_type
& EXPORT_IN_CODE
);
128 return ($item->export_type
& EXPORT_IN_DATABASE
) && !($item->export_type
& EXPORT_IN_CODE
);
130 return empty($item->disabled
);
132 return !empty($item->disabled
);
138 // ------------------------------------------------------------------------
139 // These methods are the API for generating the list of exportable items.
142 * Master entry point for handling a list.
144 * It is unlikely that a child object will need to override this method,
145 * unless the listing mechanism is going to be highly specialized.
147 function list_page($js, $input) {
148 $this->items
= ctools_export_crud_load_all($this->plugin
['schema'], $js);
150 // Respond to a reset command by clearing session and doing a drupal goto
151 // back to the base URL.
152 if (isset($input['op']) && $input['op'] == t('Reset')) {
153 unset($_SESSION['ctools_export_ui'][$this->plugin
['name']]);
155 drupal_goto($_GET['q']);
157 // clear everything but form id, form build id and form token:
158 $keys = array_keys($input);
159 foreach ($keys as $id) {
160 if (!in_array($id, array('form_id', 'form_build_id', 'form_token'))) {
164 $replace_form = TRUE;
167 // If there is no input, check to see if we have stored input in the
169 if (!isset($input['form_id'])) {
170 if (isset($_SESSION['ctools_export_ui'][$this->plugin
['name']]) && is_array($_SESSION['ctools_export_ui'][$this->plugin
['name']])) {
171 $input = $_SESSION['ctools_export_ui'][$this->plugin
['name']];
175 $_SESSION['ctools_export_ui'][$this->plugin
['name']] = $input;
176 unset($_SESSION['ctools_export_ui'][$this->plugin
['name']]['q']);
179 // This is where the form will put the output.
180 $this->rows
= array();
181 $this->sorts
= array();
184 'plugin' => $this->plugin
,
187 'no_redirect' => TRUE,
190 if (!isset($form_state['input']['form_id'])) {
191 $form_state['input']['form_id'] = 'ctools_export_ui_list_form';
194 // If we do any form rendering, it's to completely replace a form on the
195 // page, so don't let it force our ids to change.
196 if ($js && isset($_POST['ajax_html_ids'])) {
197 unset($_POST['ajax_html_ids']);
200 $form = drupal_build_form('ctools_export_ui_list_form', $form_state);
201 $form = drupal_render($form);
203 $output = $this->list_header($form_state) . $this->list_render($form_state) . $this->list_footer($form_state);
207 return $form . $output;
211 $commands[] = ajax_command_replace('#ctools-export-ui-list-items', $output);
212 if (!empty($replace_form)) {
213 $commands[] = ajax_command_replace('#ctools-export-ui-list-form', $form);
215 print ajax_render($commands);
220 * Create the filter/sort form at the top of a list of exports.
222 * This handles the very default conditions, and most lists are expected
223 * to override this and call through to parent::list_form() in order to
224 * get the base form and then modify it as necessary to add search
225 * gadgets for custom fields.
227 function list_form(&$form, &$form_state) {
228 // This forces the form to *always* treat as submitted which is
229 // necessary to make it work.
230 $form['#token'] = FALSE;
231 if (empty($form_state['input'])) {
232 $form["#post"] = TRUE;
235 // Add the 'q' in if we are not using clean URLs or it can get lost when
236 // using this kind of form.
237 if (!variable_get('clean_url', FALSE)) {
240 '#value' => $_GET['q'],
244 $all = array('all' => t('- All -'));
246 $form['top row'] = array(
247 '#prefix' => '<div class="ctools-export-ui-row ctools-export-ui-top-row clearfix">',
248 '#suffix' => '</div>',
251 $form['bottom row'] = array(
252 '#prefix' => '<div class="ctools-export-ui-row ctools-export-ui-bottom-row clearfix">',
253 '#suffix' => '</div>',
256 $form['top row']['storage'] = array(
258 '#title' => t('Storage'),
259 '#options' => $all +
array(
260 t('Normal') => t('Normal'),
261 t('Default') => t('Default'),
262 t('Overridden') => t('Overridden'),
264 '#default_value' => 'all',
267 $form['top row']['disabled'] = array(
269 '#title' => t('Enabled'),
270 '#options' => $all +
array(
274 '#default_value' => 'all',
277 $form['top row']['search'] = array(
278 '#type' => 'textfield',
279 '#title' => t('Search'),
282 $form['bottom row']['order'] = array(
284 '#title' => t('Sort by'),
285 '#options' => $this->list_sort_options(),
286 '#default_value' => 'disabled',
289 $form['bottom row']['sort'] = array(
291 '#title' => t('Order'),
296 '#default_value' => 'asc',
299 $form['bottom row']['submit'] = array(
301 '#id' => 'ctools-export-ui-list-items-apply',
302 '#value' => t('Apply'),
303 '#attributes' => array('class' => array('use-ajax-submit ctools-auto-submit-click')),
306 $form['bottom row']['reset'] = array(
308 '#id' => 'ctools-export-ui-list-items-apply',
309 '#value' => t('Reset'),
310 '#attributes' => array('class' => array('use-ajax-submit')),
313 $form['#prefix'] = '<div class="clearfix">';
314 $form['#suffix'] = '</div>';
315 $form['#attached']['js'] = array(ctools_attach_js('auto-submit'));
316 $form['#attached']['library'][] = array('system', 'drupal.ajax');
317 $form['#attached']['library'][] = array('system', 'jquery.form');
318 $form['#attributes'] = array('class' => array('ctools-auto-submit-full-form'));
322 * Validate the filter/sort form.
324 * It is very rare that a filter form needs validation, but if it is
325 * needed, override this.
327 function list_form_validate(&$form, &$form_state) { }
330 * Submit the filter/sort form.
332 * This submit handler is actually responsible for building up all of the
333 * rows that will later be rendered, since it is doing the filtering and
336 * For the most part, you should not need to override this method, as the
337 * fiddly bits call through to other functions.
339 function list_form_submit(&$form, &$form_state) {
340 // Filter and re-sort the pages.
341 $plugin = $this->plugin
;
343 $prefix = ctools_export_ui_plugin_base_path($plugin);
345 foreach ($this->items
as $name => $item) {
346 // Call through to the filter and see if we're going to render this
347 // row. If it returns TRUE, then this row is filtered out.
348 if ($this->list_filter($form_state, $item)) {
352 $operations = $this->build_operations($item);
354 $this->list_build_row($item, $form_state, $operations);
358 if ($form_state['values']['sort'] == 'desc') {
359 arsort($this->sorts
);
365 // Nuke the original.
367 $this->rows
= array();
369 foreach ($this->sorts
as $name => $title) {
370 $this->rows
[$name] = $rows[$name];
375 * Determine if a row should be filtered out.
377 * This handles the default filters for the export UI list form. If you
378 * added additional filters in list_form() then this is where you should
382 * TRUE if the item should be excluded.
384 function list_filter($form_state, $item) {
385 $schema = ctools_export_get_schema($this->plugin
['schema']);
386 if ($form_state['values']['storage'] != 'all' && $form_state['values']['storage'] != $item->{$schema['export']['export type string']}) {
390 if ($form_state['values']['disabled'] != 'all' && $form_state['values']['disabled'] != !empty($item->disabled
)) {
394 if ($form_state['values']['search']) {
395 $search = strtolower($form_state['values']['search']);
396 foreach ($this->list_search_fields() as $field) {
397 if (strpos(strtolower($item->$field), $search) !== FALSE) {
409 * Provide a list of fields to test against for the default "search" widget.
411 * This widget will search against whatever fields are configured here. By
412 * default it will attempt to search against the name, title and description fields.
414 function list_search_fields() {
416 $this->plugin
['export']['key'],
419 if (!empty($this->plugin
['export']['admin_title'])) {
420 $fields[] = $this->plugin
['export']['admin_title'];
422 if (!empty($this->plugin
['export']['admin_description'])) {
423 $fields[] = $this->plugin
['export']['admin_description'];
430 * Provide a list of sort options.
432 * Override this if you wish to provide more or change how these work.
433 * The actual handling of the sorting will happen in build_row().
435 function list_sort_options() {
436 if (!empty($this->plugin
['export']['admin_title'])) {
438 'disabled' => t('Enabled, title'),
439 $this->plugin
['export']['admin_title'] => t('Title'),
444 'disabled' => t('Enabled, name'),
450 'storage' => t('Storage'),
457 * Add listing CSS to the page.
459 * Override this if you need custom CSS for your list.
461 function list_css() {
462 ctools_add_css('export-ui-list');
466 * Builds the operation links for a specific exportable item.
468 function build_operations($item) {
469 $plugin = $this->plugin
;
470 $schema = ctools_export_get_schema($plugin['schema']);
471 $operations = $plugin['allowed operations'];
472 $operations['import'] = FALSE;
474 if ($item->{$schema['export']['export type string']} == t('Normal')) {
475 $operations['revert'] = FALSE;
477 elseif ($item->{$schema['export']['export type string']} == t('Overridden')) {
478 $operations['delete'] = FALSE;
481 $operations['revert'] = FALSE;
482 $operations['delete'] = FALSE;
484 if (empty($item->disabled
)) {
485 $operations['enable'] = FALSE;
488 $operations['disable'] = FALSE;
491 $allowed_operations = array();
493 foreach ($operations as $op => $info) {
495 $allowed_operations[$op] = array(
496 'title' => $info['title'],
497 'href' => ctools_export_ui_plugin_menu_path($plugin, $op, $item->{$this->plugin
['export']['key']}),
499 if (!empty($info['ajax'])) {
500 $allowed_operations[$op]['attributes'] = array('class' => array('use-ajax'));
502 if (!empty($info['token'])) {
503 $allowed_operations[$op]['query'] = array('token' => drupal_get_token($op));
508 return $allowed_operations;
512 * Build a row based on the item.
514 * By default all of the rows are placed into a table by the render
515 * method, so this is building up a row suitable for theme('table').
516 * This doesn't have to be true if you override both.
518 function list_build_row($item, &$form_state, $operations) {
520 $name = $item->{$this->plugin
['export']['key']};
521 $schema = ctools_export_get_schema($this->plugin
['schema']);
523 // Note: $item->{$schema['export']['export type string']} should have already been set up by export.inc so
524 // we can use it safely.
525 switch ($form_state['values']['order']) {
527 $this->sorts
[$name] = empty($item->disabled
) . $name;
530 $this->sorts
[$name] = $item->{$this->plugin
['export']['admin_title']};
533 $this->sorts
[$name] = $name;
536 $this->sorts
[$name] = $item->{$schema['export']['export type string']} . $name;
540 $this->rows
[$name]['data'] = array();
541 $this->rows
[$name]['class'] = !empty($item->disabled
) ?
array('ctools-export-ui-disabled') : array('ctools-export-ui-enabled');
543 // If we have an admin title, make it the first row.
544 if (!empty($this->plugin
['export']['admin_title'])) {
545 $this->rows
[$name]['data'][] = array('data' => check_plain($item->{$this->plugin
['export']['admin_title']}), 'class' => array('ctools-export-ui-title'));
547 $this->rows
[$name]['data'][] = array('data' => check_plain($name), 'class' => array('ctools-export-ui-name'));
548 $this->rows
[$name]['data'][] = array('data' => check_plain($item->{$schema['export']['export type string']}), 'class' => array('ctools-export-ui-storage'));
550 $ops = theme('links__ctools_dropbutton', array('links' => $operations, 'attributes' => array('class' => array('links', 'inline'))));
552 $this->rows
[$name]['data'][] = array('data' => $ops, 'class' => array('ctools-export-ui-operations'));
554 // Add an automatic mouseover of the description if one exists.
555 if (!empty($this->plugin
['export']['admin_description'])) {
556 $this->rows
[$name]['title'] = $item->{$this->plugin
['export']['admin_description']};
561 * Provide the table header.
563 * If you've added columns via list_build_row() but are still using a
564 * table, override this method to set up the table header.
566 function list_table_header() {
568 if (!empty($this->plugin
['export']['admin_title'])) {
569 $header[] = array('data' => t('Title'), 'class' => array('ctools-export-ui-title'));
572 $header[] = array('data' => t('Name'), 'class' => array('ctools-export-ui-name'));
573 $header[] = array('data' => t('Storage'), 'class' => array('ctools-export-ui-storage'));
574 $header[] = array('data' => t('Operations'), 'class' => array('ctools-export-ui-operations'));
580 * Render all of the rows together.
582 * By default we place all of the rows in a table, and this should be the
583 * way most lists will go.
585 * Whatever you do if this method is overridden, the ID is important for AJAX
586 * so be sure it exists.
588 function list_render(&$form_state) {
590 'header' => $this->list_table_header(),
591 'rows' => $this->rows
,
592 'attributes' => array('id' => 'ctools-export-ui-list-items'),
593 'empty' => $this->plugin
['strings']['message']['no items'],
595 return theme('table', $table);
599 * Render a header to go before the list.
601 * This will appear after the filter/sort widgets.
603 function list_header($form_state) { }
606 * Render a footer to go after thie list.
608 * This is a good place to add additional links.
610 function list_footer($form_state) { }
612 // ------------------------------------------------------------------------
613 // These methods are the API for adding/editing exportable items
616 * Perform a drupal_goto() to the location provided by the plugin for the
620 * The operation to use. A string must exist in $this->plugin['redirect']
621 * for this operation.
623 * The item in use; this may be necessary as item IDs are often embedded in
626 function redirect($op, $item = NULL) {
627 if (isset($this->plugin
['redirect'][$op])) {
628 $destination = (array) $this->plugin
['redirect'][$op];
630 $export_key = $this->plugin
['export']['key'];
631 $destination[0] = str_replace('%ctools_export_ui', $item->{$export_key}, $destination[0]);
633 call_user_func_array('drupal_goto', $destination);
636 // If the operation isn't set, fall back to the plugin's base path.
637 drupal_goto(ctools_export_ui_plugin_base_path($this->plugin
));
641 function add_page($js, $input, $step = NULL) {
642 drupal_set_title($this->get_page_title('add'), PASS_THROUGH
);
644 // If a step not set, they are trying to create a new item. If a step
645 // is set, they're in the process of creating an item.
646 if (!empty($this->plugin
['use wizard']) && !empty($step)) {
647 $item = $this->edit_cache_get(NULL, 'add');
650 $item = ctools_export_crud_new($this->plugin
['schema']);
654 'plugin' => $this->plugin
,
659 'form type' => 'add',
661 'no_redirect' => TRUE,
663 // Store these in case additional args are needed.
664 'function args' => func_get_args(),
667 $output = $this->edit_execute_form($form_state);
668 if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
669 $this->redirect($form_state['op'], $form_state['item']);
676 * Main entry point to edit an item.
678 function edit_page($js, $input, $item, $step = NULL) {
679 drupal_set_title($this->get_page_title('edit', $item), PASS_THROUGH
);
681 // Check to see if there is a cached item to get if we're using the wizard.
682 if (!empty($this->plugin
['use wizard'])) {
683 $cached = $this->edit_cache_get($item, 'edit');
684 if (!empty($cached)) {
690 'plugin' => $this->plugin
,
695 'form type' => 'edit',
697 'no_redirect' => TRUE,
699 // Store these in case additional args are needed.
700 'function args' => func_get_args(),
703 $output = $this->edit_execute_form($form_state);
704 if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
705 $this->redirect($form_state['op'], $form_state['item']);
712 * Main entry point to clone an item.
714 function clone_page($js, $input, $original, $step = NULL) {
715 drupal_set_title($this->get_page_title('clone', $original), PASS_THROUGH
);
717 // If a step not set, they are trying to create a new clone. If a step
718 // is set, they're in the process of cloning an item.
719 if (!empty($this->plugin
['use wizard']) && !empty($step)) {
720 $item = $this->edit_cache_get(NULL, 'clone');
723 // To make a clone of an item, we first export it and then re-import it.
724 // Export the handler, which is a fantastic way to clean database IDs out of it.
725 $export = ctools_export_crud_export($this->plugin
['schema'], $original);
726 $item = ctools_export_crud_import($this->plugin
['schema'], $export);
728 if (!empty($input[$this->plugin
['export']['key']])) {
729 $item->{$this->plugin
['export']['key']} = $input[$this->plugin
['export']['key']];
732 $item->{$this->plugin
['export']['key']} = 'clone_of_' . $item->{$this->plugin
['export']['key']};
736 // Tabs and breadcrumb disappearing, this helps alleviate through cheating.
737 // ...not sure this this is the best way.
738 $trail = menu_set_active_item(ctools_export_ui_plugin_base_path($this->plugin
));
740 $name = $original->{$this->plugin
['export']['key']};
743 'plugin' => $this->plugin
,
748 'form type' => 'clone',
749 'original name' => $name,
751 'no_redirect' => TRUE,
753 // Store these in case additional args are needed.
754 'function args' => func_get_args(),
757 $output = $this->edit_execute_form($form_state);
758 if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
759 $this->redirect($form_state['op'], $form_state['item']);
768 * Add and Edit both funnel into this, but they have a few different
771 function edit_execute_form(&$form_state) {
772 if (!empty($this->plugin
['use wizard'])) {
773 return $this->edit_execute_form_wizard($form_state);
776 return $this->edit_execute_form_standard($form_state);
781 * Execute the standard form for editing.
783 * By default, export UI will provide a single form for editing an object.
785 function edit_execute_form_standard(&$form_state) {
786 $output = drupal_build_form('ctools_export_ui_edit_item_form', $form_state);
787 if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
788 $this->edit_save_form($form_state);
795 * Get the form info for the wizard.
797 * This gets the form info out of the plugin, then adds defaults based on
798 * how we want edit forms to work.
800 * Overriding this can allow child UIs to tweak this info for specialized
803 * @param array $form_state
804 * The already created form state.
806 function get_wizard_info(&$form_state) {
807 if (!isset($form_state['step'])) {
808 $form_state['step'] = NULL;
811 $export_key = $this->plugin
['export']['key'];
813 // When cloning, the name of the item being cloned is referenced in the
814 // path, not the name of this item.
815 if ($form_state['form type'] == 'clone') {
816 $name = $form_state['original name'];
819 $name = $form_state['item']->{$export_key};
822 $form_info = !empty($this->plugin
['form info']) ?
$this->plugin
['form info'] : array();
824 'id' => 'ctools_export_ui_edit',
825 'path' => ctools_export_ui_plugin_menu_path($this->plugin
, $form_state['form type'], $name) . '/%step',
826 'show trail' => TRUE,
827 'free trail' => TRUE,
828 'show back' => $form_state['form type'] == 'add',
829 'show return' => FALSE,
830 'show cancel' => TRUE,
831 'finish callback' => 'ctools_export_ui_wizard_finish',
832 'next callback' => 'ctools_export_ui_wizard_next',
833 'back callback' => 'ctools_export_ui_wizard_back',
834 'cancel callback' => 'ctools_export_ui_wizard_cancel',
836 'import order' => array(
837 'import' => t('Import code'),
838 'settings' => t('Settings'),
842 // Set the order of forms based on the op if we have a specific one.
843 if (isset($form_info[$form_state['form type'] . ' order'])) {
844 $form_info['order'] = $form_info[$form_state['form type'] . ' order'];
847 // We have generic fallback forms we can use if they are not specified,
848 // and they automatically delegate back to the UI object. Use these if
850 foreach ($form_info['order'] as $key => $title) {
851 if (empty($form_info['forms'][$key])) {
852 $form_info['forms'][$key] = array(
853 'form id' => 'ctools_export_ui_edit_item_wizard_form',
858 // 'free trail' means the wizard can freely go back and form from item
859 // via the trail and not with next/back buttons.
860 if ($form_state['form type'] == 'add' ||
($form_state['form type'] == 'import' && empty($form_state['item']->{$export_key}))) {
861 $form_info['free trail'] = FALSE;
868 * Execute the wizard for editing.
870 * For complex objects, sometimes a wizard is needed. This is normally
871 * activated by setting 'use wizard' => TRUE in the plugin definition
872 * and then creating a 'form info' array to feed the wizard the data
875 * When creating this wizard, the plugin is responsible for defining all forms
876 * that will be utilized with the wizard.
878 * Using 'add order' or 'edit order' can be used to ensure that add/edit order
881 function edit_execute_form_wizard(&$form_state) {
882 $form_info = $this->get_wizard_info($form_state);
884 // If there aren't any forms set, fail.
885 if (empty($form_info['order'])) {
886 return MENU_NOT_FOUND
;
889 // Figure out if this is a new instance of the wizard
890 if (empty($form_state['step'])) {
891 $order = array_keys($form_info['order']);
892 $form_state['step'] = reset($order);
895 if (empty($form_info['order'][$form_state['step']]) && empty($form_info['forms'][$form_state['step']])) {
896 return MENU_NOT_FOUND
;
899 ctools_include('wizard');
900 $output = ctools_wizard_multistep_form($form_info, $form_state['step'], $form_state);
901 if (!empty($form_state['complete'])) {
902 $this->edit_save_form($form_state);
904 else if ($output && !empty($form_state['item']->export_ui_item_is_cached
)) {
905 // @todo this should be in the plugin strings
906 drupal_set_message(t('You have unsaved changes. These changes will not be made permanent until you click <em>Save</em>.'), 'warning');
909 // Unset the executed flag if any non-wizard button was pressed. Those
910 // buttons require special handling by whatever client is operating them.
911 if (!empty($form_state['executed']) && empty($form_state['clicked_button']['#wizard type'])) {
912 unset($form_state['executed']);
918 * Wizard 'back' callback when using a wizard to edit an item.
920 * The wizard callback delegates this back to the object.
922 function edit_wizard_back(&$form_state) {
923 // This only exists so child implementations can use it.
927 * Wizard 'next' callback when using a wizard to edit an item.
929 * The wizard callback delegates this back to the object.
931 function edit_wizard_next(&$form_state) {
932 $this->edit_cache_set($form_state['item'], $form_state['form type']);
936 * Wizard 'cancel' callback when using a wizard to edit an item.
938 * The wizard callback delegates this back to the object.
940 function edit_wizard_cancel(&$form_state) {
941 $this->edit_cache_clear($form_state['item'], $form_state['form type']);
945 * Wizard 'cancel' callback when using a wizard to edit an item.
947 * The wizard callback delegates this back to the object.
949 function edit_wizard_finish(&$form_state) {
950 $form_state['complete'] = TRUE;
952 // If we are importing, and overwrite was selected, delete the original so
953 // that this one writes properly.
954 if ($form_state['form type'] == 'import' && !empty($form_state['item']->export_ui_allow_overwrite
)) {
955 ctools_export_crud_delete($this->plugin
['schema'], $form_state['item']);
958 $this->edit_cache_clear($form_state['item'], $form_state['form type']);
962 * Retrieve the item currently being edited from the object cache.
964 function edit_cache_get($item, $op = 'edit') {
965 ctools_include('object-cache');
966 if (is_string($item)) {
970 $name = $this->edit_cache_get_key($item, $op);
973 $cache = ctools_object_cache_get('ctui_' . $this->plugin
['name'], $name);
975 $cache->export_ui_item_is_cached
= TRUE;
981 * Cache the item currently currently being edited.
983 function edit_cache_set($item, $op = 'edit') {
984 ctools_include('object-cache');
985 $name = $this->edit_cache_get_key($item, $op);
986 return $this->edit_cache_set_key($item, $name);
989 function edit_cache_set_key($item, $name) {
990 return ctools_object_cache_set('ctui_' . $this->plugin
['name'], $name, $item);
994 * Clear the object cache for the currently edited item.
996 function edit_cache_clear($item, $op = 'edit') {
997 ctools_include('object-cache');
998 $name = $this->edit_cache_get_key($item, $op);
999 return ctools_object_cache_clear('ctui_' . $this->plugin
['name'], $name);
1003 * Figure out what the cache key is for this object.
1005 function edit_cache_get_key($item, $op) {
1006 $export_key = $this->plugin
['export']['key'];
1007 return $op == 'edit' ?
$item->{$this->plugin
['export']['key']} : "::$op";
1011 * Called to save the final product from the edit form.
1013 function edit_save_form($form_state) {
1014 $item = &$form_state['item'];
1015 $export_key = $this->plugin
['export']['key'];
1017 $result = ctools_export_crud_save($this->plugin
['schema'], $item);
1020 $message = str_replace('%title', check_plain($item->{$export_key}), $this->plugin
['strings']['confirmation'][$form_state['op']]['success']);
1021 drupal_set_message($message);
1024 $message = str_replace('%title', check_plain($item->{$export_key}), $this->plugin
['strings']['confirmation'][$form_state['op']]['fail']);
1025 drupal_set_message($message, 'error');
1030 * Provide the actual editing form.
1032 function edit_form(&$form, &$form_state) {
1033 $export_key = $this->plugin
['export']['key'];
1034 $item = $form_state['item'];
1035 $schema = ctools_export_get_schema($this->plugin
['schema']);
1037 if (!empty($this->plugin
['export']['admin_title'])) {
1038 $form['info'][$this->plugin
['export']['admin_title']] = array(
1039 '#type' => 'textfield',
1040 '#title' => t('Administrative title'),
1041 '#description' => t('This will appear in the administrative interface to easily identify it.'),
1042 '#default_value' => $item->{$this->plugin
['export']['admin_title']},
1046 $form['info'][$export_key] = array(
1047 '#title' => t($schema['export']['key name']),
1048 '#type' => 'textfield',
1049 '#default_value' => $item->{$export_key},
1050 '#description' => t('The unique ID for this @export.', array('@export' => $this->plugin
['title singular'])),
1051 '#required' => TRUE,
1052 '#maxlength' => 255,
1055 if (!empty($this->plugin
['export']['admin_title'])) {
1056 $form['info'][$export_key]['#type'] = 'machine_name';
1057 $form['info'][$export_key]['#machine_name'] = array(
1058 'exists' => 'ctools_export_ui_edit_name_exists',
1059 'source' => array('info', $this->plugin
['export']['admin_title']),
1063 if ($form_state['op'] === 'edit') {
1064 $form['info'][$export_key]['#disabled'] = TRUE;
1065 $form['info'][$export_key]['#value'] = $item->{$export_key};
1068 if (!empty($this->plugin
['export']['admin_description'])) {
1069 $form['info'][$this->plugin
['export']['admin_description']] = array(
1070 '#type' => 'textarea',
1071 '#title' => t('Administrative description'),
1072 '#default_value' => $item->{$this->plugin
['export']['admin_description']},
1076 // Add plugin's form definitions.
1077 if (!empty($this->plugin
['form']['settings'])) {
1078 // Pass $form by reference.
1079 $this->plugin
['form']['settings']($form, $form_state);
1082 // Add the buttons if the wizard is not in use.
1083 if (empty($form_state['form_info'])) {
1084 // Make sure that whatever happens, the buttons go to the bottom.
1085 $form['buttons']['#weight'] = 100;
1088 $form['buttons']['submit'] = array(
1089 '#type' => 'submit',
1090 '#value' => t('Save'),
1093 $form['buttons']['delete'] = array(
1094 '#type' => 'submit',
1095 '#value' => $item->export_type
& EXPORT_IN_CODE ?
t('Revert') : t('Delete'),
1096 '#access' => $form_state['op'] === 'edit' && $item->export_type
& EXPORT_IN_DATABASE
,
1097 '#submit' => array('ctools_export_ui_edit_item_form_delete'),
1103 * Validate callback for the edit form.
1105 function edit_form_validate(&$form, &$form_state) {
1106 if (!empty($this->plugin
['form']['validate'])) {
1107 // Pass $form by reference.
1108 $this->plugin
['form']['validate']($form, $form_state);
1113 * Perform a final validation check before allowing the form to be
1116 function edit_finish_validate(&$form, &$form_state) {
1117 if ($form_state['op'] != 'edit') {
1118 // Validate the export key. Fake an element for form_error().
1119 $export_key = $this->plugin
['export']['key'];
1121 '#value' => $form_state['item']->{$export_key},
1122 '#parents' => array($export_key),
1124 $form_state['plugin'] = $this->plugin
;
1125 ctools_export_ui_edit_name_validate($element, $form_state);
1130 * Handle the submission of the edit form.
1132 * At this point, submission is successful. Our only responsibility is
1133 * to copy anything out of values onto the item that we are able to edit.
1135 * If the keys all match up to the schema, this method will not need to be
1138 function edit_form_submit(&$form, &$form_state) {
1139 if (!empty($this->plugin
['form']['submit'])) {
1140 // Pass $form by reference.
1141 $this->plugin
['form']['submit']($form, $form_state);
1144 // Transfer data from the form to the $item based upon schema values.
1145 $schema = ctools_export_get_schema($this->plugin
['schema']);
1146 foreach (array_keys($schema['fields']) as $key) {
1147 if(isset($form_state['values'][$key])) {
1148 $form_state['item']->{$key} = $form_state['values'][$key];
1153 // ------------------------------------------------------------------------
1154 // These methods are the API for 'other' stuff with exportables such as
1155 // enable, disable, import, export, delete
1158 * Callback to enable a page.
1160 function enable_page($js, $input, $item) {
1161 return $this->set_item_state(FALSE, $js, $input, $item);
1165 * Callback to disable a page.
1167 function disable_page($js, $input, $item) {
1168 return $this->set_item_state(TRUE, $js, $input, $item);
1172 * Set an item's state to enabled or disabled and output to user.
1174 * If javascript is in use, this will rebuild the list and send that back
1175 * as though the filter form had been executed.
1177 function set_item_state($state, $js, $input, $item) {
1178 ctools_export_crud_set_status($this->plugin
['schema'], $item, $state);
1181 drupal_goto(ctools_export_ui_plugin_base_path($this->plugin
));
1184 return $this->list_page($js, $input);
1189 * Page callback to delete an exportable item.
1191 function delete_page($js, $input, $item) {
1192 $form_state = array(
1193 'plugin' => $this->plugin
,
1197 'op' => $item->export_type
& EXPORT_IN_CODE ?
'revert' : 'delete',
1199 'no_redirect' => TRUE,
1202 $output = drupal_build_form('ctools_export_ui_delete_confirm_form', $form_state);
1203 if (!empty($form_state['executed'])) {
1204 $this->delete_form_submit($form_state);
1205 $this->redirect($form_state['op'], $item);
1212 * Deletes exportable items from the database.
1214 function delete_form_submit(&$form_state) {
1215 $item = $form_state['item'];
1217 ctools_export_crud_delete($this->plugin
['schema'], $item);
1218 $export_key = $this->plugin
['export']['key'];
1219 $message = str_replace('%title', check_plain($item->{$export_key}), $this->plugin
['strings']['confirmation'][$form_state['op']]['success']);
1220 drupal_set_message($message);
1224 * Page callback to display export information for an exportable item.
1226 function export_page($js, $input, $item) {
1227 drupal_set_title($this->get_page_title('export', $item), PASS_THROUGH
);
1228 return drupal_get_form('ctools_export_form', ctools_export_crud_export($this->plugin
['schema'], $item), t('Export'));
1232 * Page callback to import information for an exportable item.
1234 function import_page($js, $input, $step = NULL) {
1235 drupal_set_title($this->get_page_title('import'), PASS_THROUGH
);
1236 // Import is basically a multi step wizard form, so let's go ahead and
1237 // use CTools' wizard.inc for it.
1239 // If a step not set, they are trying to create a new item. If a step
1240 // is set, they're in the process of creating an item.
1241 if (!empty($step)) {
1242 $item = $this->edit_cache_get(NULL, 'import');
1245 $item = ctools_export_crud_new($this->plugin
['schema']);
1248 $form_state = array(
1249 'plugin' => $this->plugin
,
1254 'form type' => 'import',
1256 'no_redirect' => TRUE,
1258 // Store these in case additional args are needed.
1259 'function args' => func_get_args(),
1262 // import always uses the wizard.
1263 $output = $this->edit_execute_form_wizard($form_state);
1264 if (!empty($form_state['executed'])) {
1265 $this->redirect($form_state['op'], $form_state['item']);
1272 * Import form. Provides simple helptext instructions and textarea for
1273 * pasting a export definition.
1275 function edit_form_import(&$form, &$form_state) {
1276 $form['help'] = array(
1278 '#value' => $this->plugin
['strings']['help']['import'],
1281 $form['import'] = array(
1282 '#title' => t('@plugin code', array('@plugin' => $this->plugin
['title singular proper'])),
1283 '#type' => 'textarea',
1285 '#required' => TRUE,
1286 '#default_value' => !empty($form_state['item']->export_ui_code
) ?
$form_state['item']->export_ui_code
: '',
1289 $form['overwrite'] = array(
1290 '#title' => t('Allow import to overwrite an existing record.'),
1291 '#type' => 'checkbox',
1292 '#default_value' => !empty($form_state['item']->export_ui_allow_overwrite
) ?
$form_state['item']->export_ui_allow_overwrite
: FALSE,
1297 * Import form validate handler.
1299 * Evaluates code and make sure it creates an object before we continue.
1301 function edit_form_import_validate($form, &$form_state) {
1302 $item = ctools_export_crud_import($this->plugin
['schema'], $form_state['values']['import']);
1303 if (is_string($item)) {
1304 form_error($form['import'], t('Unable to get an import from the code. Errors reported: @errors', array('@errors' => $item)));
1308 $form_state['item'] = $item;
1309 $form_state['item']->export_ui_allow_overwrite
= $form_state['values']['overwrite'];
1310 $form_state['item']->export_ui_code
= $form_state['values']['import'];
1314 * Submit callback for import form.
1316 * Stores the item in the session.
1318 function edit_form_import_submit($form, &$form_state) {
1319 // The validate function already imported and stored the item. This
1320 // function exists simply to prevent it from going to the default
1321 // edit_form_submit() method.
1325 // -----------------------------------------------------------------------
1326 // Forms to be used with this class.
1328 // Since Drupal's forms are completely procedural, these forms will
1329 // mostly just be pass-throughs back to the object.
1332 * Add all appropriate includes to forms so that caching the form
1333 * still loads the files that we need.
1335 function _ctools_export_ui_add_form_files($form, &$form_state) {
1336 ctools_form_include($form_state, 'export');
1337 ctools_form_include($form_state, 'export-ui');
1339 // Also make sure the plugin .inc file is loaded.
1340 ctools_form_include_file($form_state, $form_state['object']->plugin
['path'] . '/' . $form_state['object']->plugin
['file']);
1344 * Form callback to handle the filter/sort form when listing items.
1346 * This simply loads the object defined in the plugin and hands it off.
1348 function ctools_export_ui_list_form($form, &$form_state) {
1349 $form_state['object']->list_form($form, $form_state);
1354 * Validate handler for ctools_export_ui_list_form.
1356 function ctools_export_ui_list_form_validate(&$form, &$form_state) {
1357 $form_state['object']->list_form_validate($form, $form_state);
1361 * Submit handler for ctools_export_ui_list_form.
1363 function ctools_export_ui_list_form_submit(&$form, &$form_state) {
1364 $form_state['object']->list_form_submit($form, $form_state);
1368 * Form callback to edit an exportable item.
1370 * This simply loads the object defined in the plugin and hands it off.
1372 function ctools_export_ui_edit_item_form($form, &$form_state) {
1373 // When called using #ajax via ajax_form_callback(), 'export' may
1374 // not be included so include it here.
1375 _ctools_export_ui_add_form_files($form, $form_state);
1377 $form_state['object']->edit_form($form, $form_state);
1382 * Validate handler for ctools_export_ui_edit_item_form.
1384 function ctools_export_ui_edit_item_form_validate(&$form, &$form_state) {
1385 $form_state['object']->edit_form_validate($form, $form_state);
1389 * Submit handler for ctools_export_ui_edit_item_form.
1391 function ctools_export_ui_edit_item_form_submit(&$form, &$form_state) {
1392 $form_state['object']->edit_form_submit($form, $form_state);
1396 * Submit handler to delete for ctools_export_ui_edit_item_form
1398 * @todo Put this on a callback in the object.
1400 function ctools_export_ui_edit_item_form_delete(&$form, &$form_state) {
1401 _ctools_export_ui_add_form_files($form, $form_state);
1403 $export_key = $form_state['plugin']['export']['key'];
1404 $path = $form_state['item']->export_type
& EXPORT_IN_CODE ?
'revert' : 'delete';
1406 drupal_goto(ctools_export_ui_plugin_menu_path($form_state['plugin'], $path, $form_state['item']->{$export_key}), array('cancel_path' => $_GET['q']));
1410 * Validate that an export item name is acceptable and unique during add.
1412 function ctools_export_ui_edit_name_validate($element, &$form_state) {
1413 $plugin = $form_state['plugin'];
1414 // Check for string identifier sanity
1415 if (!preg_match('!^[a-z0-9_]+$!', $element['#value'])) {
1416 form_error($element, t('The export id can only consist of lowercase letters, underscores, and numbers.'));
1420 // Check for name collision
1421 if (empty($form_state['item']->export_ui_allow_overwrite
) && $exists = ctools_export_crud_load($plugin['schema'], $element['#value'])) {
1422 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'])));
1427 * Test for #machine_name type to see if an export exists.
1429 function ctools_export_ui_edit_name_exists($name, $element, &$form_state) {
1430 $plugin = $form_state['plugin'];
1432 return (empty($form_state['item']->export_ui_allow_overwrite
) && ctools_export_crud_load($plugin['schema'], $name));
1436 * Delete/Revert confirm form.
1438 * @todo -- call back into the object instead.
1440 function ctools_export_ui_delete_confirm_form($form, &$form_state) {
1441 _ctools_export_ui_add_form_files($form, $form_state);
1443 $plugin = $form_state['plugin'];
1444 $item = $form_state['item'];
1448 $export_key = $plugin['export']['key'];
1449 $question = str_replace('%title', check_plain($item->{$export_key}), $plugin['strings']['confirmation'][$form_state['op']]['question']);
1450 $path = (!empty($_REQUEST['cancel_path']) && !url_is_external($_REQUEST['cancel_path'])) ?
$_REQUEST['cancel_path'] : ctools_export_ui_plugin_base_path($plugin);
1452 $form = confirm_form($form,
1455 $plugin['strings']['confirmation'][$form_state['op']]['information'],
1456 $plugin['allowed operations'][$form_state['op']]['title'], t('Cancel')
1461 // --------------------------------------------------------------------------
1462 // Forms and callbacks for using the edit system with the wizard.
1465 * Form callback to edit an exportable item using the wizard
1467 * This simply loads the object defined in the plugin and hands it off.
1469 function ctools_export_ui_edit_item_wizard_form($form, &$form_state) {
1470 _ctools_export_ui_add_form_files($form, $form_state);
1472 $method = 'edit_form_' . $form_state['step'];
1473 if (!method_exists($form_state['object'], $method)) {
1474 $method = 'edit_form';
1477 $form_state['object']->$method($form, $form_state);
1482 * Validate handler for ctools_export_ui_edit_item_wizard_form.
1484 function ctools_export_ui_edit_item_wizard_form_validate(&$form, &$form_state) {
1485 $method = 'edit_form_' . $form_state['step'] . '_validate';
1486 if (!method_exists($form_state['object'], $method)) {
1487 $method = 'edit_form_validate';
1490 $form_state['object']->$method($form, $form_state);
1492 // Additionally, if there were no errors from that, and we're finishing,
1493 // perform a final validate to make sure everything is ok.
1494 if (isset($form_state['clicked_button']['#wizard type']) && $form_state['clicked_button']['#wizard type'] == 'finish' && !form_get_errors()) {
1495 $form_state['object']->edit_finish_validate($form, $form_state);
1500 * Submit handler for ctools_export_ui_edit_item_wizard_form.
1502 function ctools_export_ui_edit_item_wizard_form_submit(&$form, &$form_state) {
1503 $method = 'edit_form_' . $form_state['step'] . '_submit';
1504 if (!method_exists($form_state['object'], $method)) {
1505 $method = 'edit_form_submit';
1508 $form_state['object']->$method($form, $form_state);
1512 * Wizard 'back' callback when using a wizard to edit an item.
1514 function ctools_export_ui_wizard_back(&$form_state) {
1515 $form_state['object']->edit_wizard_back($form_state);
1519 * Wizard 'next' callback when using a wizard to edit an item.
1521 function ctools_export_ui_wizard_next(&$form_state) {
1522 $form_state['object']->edit_wizard_next($form_state);
1526 * Wizard 'cancel' callback when using a wizard to edit an item.
1528 function ctools_export_ui_wizard_cancel(&$form_state) {
1529 $form_state['object']->edit_wizard_cancel($form_state);
1533 * Wizard 'finish' callback when using a wizard to edit an item.
1535 function ctools_export_ui_wizard_finish(&$form_state) {
1536 $form_state['object']->edit_wizard_finish($form_state);