/**
  * Execute the wizard for editing.
  *
  * For complex objects, sometimes a wizard is needed. This is normally
  * activated by setting 'use wizard' => TRUE in the plugin definition
  * and then creating a 'form info' array to feed the wizard the data
  * it needs.
  *
  * When creating this wizard, the plugin is responsible for defining all forms
  * that will be utilized with the wizard.
  *
  * Using 'add order' or 'edit order' can be used to ensure that add/edit order
  * is different.
  */
 function edit_execute_form_wizard(&$form_state)
 {
     $form_info = $this->get_wizard_info($form_state);
     // If there aren't any forms set, fail.
     if (empty($form_info['order'])) {
         return MENU_NOT_FOUND;
     }
     // Figure out if this is a new instance of the wizard
     if (empty($form_state['step'])) {
         $order = array_keys($form_info['order']);
         $form_state['step'] = reset($order);
     }
     if (empty($form_info['order'][$form_state['step']]) && empty($form_info['forms'][$form_state['step']])) {
         return MENU_NOT_FOUND;
     }
     ctools_include('wizard');
     $output = ctools_wizard_multistep_form($form_info, $form_state['step'], $form_state);
     if (!empty($form_state['complete'])) {
         $this->edit_save_form($form_state);
     } else {
         if ($output && !empty($form_state['item']->export_ui_item_is_cached)) {
             // @todo this should be in the plugin strings
             drupal_set_message(t('You have unsaved changes. These changes will not be made permanent until you click <em>Save</em>.'), 'warning');
         }
     }
     // Unset the executed flag if any non-wizard button was pressed. Those
     // buttons require special handling by whatever client is operating them.
     if (!empty($form_state['executed']) && empty($form_state['clicked_button']['#wizard type'])) {
         unset($form_state['executed']);
     }
     return $output;
 }
 /**
  * Page callback to import information for an exportable item.
  */
 function import_page($js, $input, $step = NULL)
 {
     drupal_set_title($this->get_page_title('import'));
     // Import is basically a multi step wizard form, so let's go ahead and
     // use CTools' wizard.inc for it.
     // If a step not set, they are trying to create a new item. If a step
     // is set, they're in the process of creating an item.
     if (!empty($step)) {
         $item = $this->edit_cache_get(NULL, 'import');
     }
     if (empty($item)) {
         $item = ctools_export_crud_new($this->plugin['schema']);
     }
     $form_state = array('plugin' => $this->plugin, 'object' => &$this, 'ajax' => $js, 'item' => $item, 'op' => 'add', 'form type' => 'import', 'rerender' => TRUE, 'no_redirect' => TRUE, 'step' => $step, 'function args' => func_get_args());
     // import always uses the wizard.
     $output = $this->edit_execute_form_wizard($form_state);
     if (!empty($form_state['executed'])) {
         $export_key = $this->plugin['export']['key'];
         drupal_goto(str_replace('%ctools_export_ui', $form_state['item']->{$export_key}, $this->plugin['redirect']['add']));
     }
     return $output;
     return;
     $form_info = array('id' => 'ctools_export_ui_import', 'path' => ctools_export_ui_plugin_menu_path($this->plugin, 'import') . '/%step', 'return path' => $this->plugin['redirect']['import'], 'show trail' => TRUE, 'show back' => TRUE, 'show return' => FALSE, 'show cancel' => TRUE, 'finish callback' => 'ctools_export_ui_import_finish', 'cancel callback' => 'ctools_export_ui_import_cancel', 'order' => array('code' => t('Import code'), 'edit' => t('Edit')), 'forms' => array('code' => array('form id' => 'ctools_export_ui_import_code'), 'edit' => array('form id' => 'ctools_export_ui_import_edit')));
     $form_state = array('plugin' => $this->plugin, 'input' => $input, 'rerender' => TRUE, 'no_redirect' => TRUE, 'object' => &$this, 'export' => '', 'overwrite' => FALSE, 'function args' => func_get_args());
     if ($step == 'code') {
         // This is only used if the BACK button was hit.
         if (!empty($_SESSION['ctools_export_ui_import'][$this->plugin['name']])) {
             $form_state['item'] = $_SESSION['ctools_export_ui_import'][$this->plugin['name']];
             $form_state['export'] = $form_state['item']->export_ui_code;
             $form_state['overwrite'] = $form_state['item']->export_ui_allow_overwrite;
         }
     } else {
         if ($step == 'begin') {
             $step = 'code';
             if (!empty($_SESSION['ctools_export_ui_import'][$this->plugin['name']])) {
                 unset($_SESSION['ctools_export_ui_import'][$this->plugin['name']]);
             }
         } else {
             if ($step != 'code') {
                 $form_state['item'] = $_SESSION['ctools_export_ui_import'][$this->plugin['name']];
                 $form_state['op'] = 'add';
                 if (!empty($form_state['item']->export_ui_allow_overwrite)) {
                     // if allow overwrite was enabled, set this to 'edit' only if the key already existed.
                     $export_key = $this->plugin['export']['key'];
                     if (ctools_export_crud_load($this->plugin['schema'], $form_state['item']->{$export_key})) {
                         $form_state['op'] = 'edit';
                     }
                 }
             }
         }
     }
     ctools_include('wizard');
     return ctools_wizard_multistep_form($form_info, $step, $form_state);
 }