You are here

public function ctools_export_ui::clone_page in Chaos Tool Suite (ctools) 7

Same name and namespace in other branches
  1. 6 plugins/export_ui/ctools_export_ui.class.php \ctools_export_ui::clone_page()

Main entry point to clone an item.

File

plugins/export_ui/ctools_export_ui.class.php, line 721

Class

ctools_export_ui
Base class for export UI.

Code

public function clone_page($js, $input, $original, $step = NULL) {
  $args = func_get_args();
  drupal_set_title($this
    ->get_page_title('clone', $original), PASS_THROUGH);

  // If a step not set, they are trying to create a new clone. If a step
  // is set, they're in the process of cloning an item.
  if (!empty($this->plugin['use wizard']) && !empty($step)) {
    $item = $this
      ->edit_cache_get(NULL, 'clone');
  }
  if (empty($item)) {

    // To make a clone of an item, we first export it and then re-import it.
    // Export the handler, which is a fantastic way to clean database IDs out of it.
    $export = ctools_export_crud_export($this->plugin['schema'], $original);
    $item = ctools_export_crud_import($this->plugin['schema'], $export);
    if (!empty($input[$this->plugin['export']['key']])) {
      $item->{$this->plugin['export']['key']} = $input[$this->plugin['export']['key']];
    }
    else {
      $item->{$this->plugin['export']['key']} = 'clone_of_' . $item->{$this->plugin['export']['key']};
    }
  }

  // Tabs and breadcrumb disappearing, this helps alleviate through cheating.
  // ...not sure this this is the best way.
  $trail = menu_set_active_item(ctools_export_ui_plugin_base_path($this->plugin));
  $name = $original->{$this->plugin['export']['key']};
  $form_state = array(
    'plugin' => $this->plugin,
    'object' => &$this,
    'ajax' => $js,
    'item' => $item,
    'op' => 'add',
    'form type' => 'clone',
    'original name' => $name,
    'rerender' => TRUE,
    'no_redirect' => TRUE,
    'step' => $step,
    // Store these in case additional args are needed.
    'function args' => $args,
  );
  $output = $this
    ->edit_execute_form($form_state);
  if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
    $this
      ->redirect($form_state['op'], $form_state['item']);
  }
  return $output;
}