Projet

Général

Profil

Paste
Télécharger (48,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / plugins / export_ui / ctools_export_ui.class.php @ e4c061ad

1
<?php
2

    
3
/**
4
 * Base class for export UI.
5
 */
6
class ctools_export_ui {
7
  var $plugin;
8
  var $name;
9
  var $options = array();
10

    
11
  /**
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.
15
   */
16
  function init($plugin) {
17
    ctools_include('export');
18

    
19
    $this->plugin = $plugin;
20
  }
21

    
22
  /**
23
   * Get a page title for the current page from our plugin strings.
24
   */
25
  function get_page_title($op, $item = NULL) {
26
    if (empty($this->plugin['strings']['title'][$op])) {
27
      return;
28
    }
29

    
30
    // Replace %title that might be there with the exportable title.
31
    $title = $this->plugin['strings']['title'][$op];
32
    if (!empty($item)) {
33
      $export_key = $this->plugin['export']['key'];
34
      $title = (str_replace('%title', check_plain($item->{$export_key}), $title));
35
    }
36

    
37
    return $title;
38
  }
39

    
40
  /**
41
   * Called by ctools_export_ui_load to load the item.
42
   *
43
   * This can be overridden for modules that want to be able to export
44
   * items currently being edited, for example.
45
   */
46
  function load_item($item_name) {
47
    $item = ctools_export_crud_load($this->plugin['schema'], $item_name);
48
    return empty($item) ? FALSE : $item;
49
  }
50

    
51
  // ------------------------------------------------------------------------
52
  // Menu item manipulation
53

    
54
  /**
55
   * hook_menu() entry point.
56
   *
57
   * Child implementations that need to add or modify menu items should
58
   * probably call parent::hook_menu($items) and then modify as needed.
59
   */
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
63
    // situation.
64
    if (empty($this->plugin['schema'])) {
65
      return;
66
    }
67

    
68
    $prefix = ctools_export_ui_plugin_base_path($this->plugin);
69

    
70
    if (isset($this->plugin['menu']['items']) && is_array($this->plugin['menu']['items'])) {
71
      $my_items = array();
72
      foreach ($this->plugin['menu']['items'] as $item) {
73
        // Add menu item defaults.
74
        $item += array(
75
          'file' => 'export-ui.inc',
76
          'file path' => drupal_get_path('module', 'ctools') . '/includes',
77
        );
78

    
79
        $path = !empty($item['path']) ? $prefix . '/' . $item['path'] : $prefix;
80
        unset($item['path']);
81
        $my_items[$path] = $item;
82
      }
83
      $items += $my_items;
84
    }
85
  }
86

    
87
  /**
88
   * Menu callback to determine if an operation is accessible.
89
   *
90
   * This function enforces a basic access check on the configured perm
91
   * string, and then additional checks as needed.
92
   *
93
   * @param $op
94
   *   The 'op' of the menu item, which is defined by 'allowed operations'
95
   *   and embedded into the arguments in the menu item.
96
   * @param $item
97
   *   If an op that works on an item, then the item object, otherwise NULL.
98
   *
99
   * @return
100
   *   TRUE if the current user has access, FALSE if not.
101
   */
102
  function access($op, $item) {
103
    if (!user_access($this->plugin['access'])) {
104
      return FALSE;
105
    }
106

    
107
    // More fine-grained access control:
108
    if ($op == 'add' && !user_access($this->plugin['create access'])) {
109
      return FALSE;
110
    }
111

    
112
    // More fine-grained access control:
113
    if (($op == 'revert' || $op == 'delete') && !user_access($this->plugin['delete access'])) {
114
      return FALSE;
115
    }
116

    
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))) {
119
      return FALSE;
120
    }
121

    
122
    switch ($op) {
123
      case 'import':
124
        return user_access('use ctools import');
125
      case 'revert':
126
        return ($item->export_type & EXPORT_IN_DATABASE) && ($item->export_type & EXPORT_IN_CODE);
127
      case 'delete':
128
        return ($item->export_type & EXPORT_IN_DATABASE) && !($item->export_type & EXPORT_IN_CODE);
129
      case 'disable':
130
        return empty($item->disabled);
131
      case 'enable':
132
        return !empty($item->disabled);
133
      default:
134
        return TRUE;
135
    }
136
  }
137

    
138
  // ------------------------------------------------------------------------
139
  // These methods are the API for generating the list of exportable items.
140

    
141
  /**
142
   * Master entry point for handling a list.
143
   *
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.
146
   */
147
  function list_page($js, $input) {
148
    $this->items = ctools_export_crud_load_all($this->plugin['schema'], $js);
149

    
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']]);
154
      if (!$js) {
155
        drupal_goto($_GET['q']);
156
      }
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'))) {
161
          unset($input[$id]);
162
        }
163
      }
164
      $replace_form = TRUE;
165
    }
166

    
167
    // If there is no input, check to see if we have stored input in the
168
    // session.
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']];
172
      }
173
    }
174
    else {
175
      $_SESSION['ctools_export_ui'][$this->plugin['name']] = $input;
176
      unset($_SESSION['ctools_export_ui'][$this->plugin['name']]['q']);
177
    }
178

    
179
    // This is where the form will put the output.
180
    $this->rows = array();
181
    $this->sorts = array();
182

    
183
    $form_state = array(
184
      'plugin' => $this->plugin,
185
      'input' => $input,
186
      'rerender' => TRUE,
187
      'no_redirect' => TRUE,
188
      'object' => &$this,
189
    );
190
    if (!isset($form_state['input']['form_id'])) {
191
      $form_state['input']['form_id'] = 'ctools_export_ui_list_form';
192
    }
193

    
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']);
198
    }
199

    
200
    $form = drupal_build_form('ctools_export_ui_list_form', $form_state);
201
    $form = drupal_render($form);
202

    
203
    $output = $this->list_header($form_state) . $this->list_render($form_state) . $this->list_footer($form_state);
204

    
205
    if (!$js) {
206
      $this->list_css();
207
      return $form . $output;
208
    }
209

    
210
    $commands = array();
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);
214
    }
215
    print ajax_render($commands);
216
    ajax_footer();
217
  }
218

    
219
  /**
220
   * Create the filter/sort form at the top of a list of exports.
221
   *
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.
226
   */
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;
233
    }
234

    
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)) {
238
      $form['q'] = array(
239
        '#type' => 'hidden',
240
        '#value' => $_GET['q'],
241
      );
242
    }
243

    
244
    $all = array('all' => t('- All -'));
245

    
246
    $form['top row'] = array(
247
      '#prefix' => '<div class="ctools-export-ui-row ctools-export-ui-top-row clearfix">',
248
      '#suffix' => '</div>',
249
    );
250

    
251
    $form['bottom row'] = array(
252
      '#prefix' => '<div class="ctools-export-ui-row ctools-export-ui-bottom-row clearfix">',
253
      '#suffix' => '</div>',
254
    );
255

    
256
    $form['top row']['storage'] = array(
257
      '#type' => 'select',
258
      '#title' => t('Storage'),
259
      '#options' => $all + array(
260
        t('Normal') => t('Normal'),
261
        t('Default') => t('Default'),
262
        t('Overridden') => t('Overridden'),
263
      ),
264
      '#default_value' => 'all',
265
    );
266

    
267
    $form['top row']['disabled'] = array(
268
      '#type' => 'select',
269
      '#title' => t('Enabled'),
270
      '#options' => $all + array(
271
        '0' => t('Enabled'),
272
        '1' => t('Disabled')
273
      ),
274
      '#default_value' => 'all',
275
    );
276

    
277
    $form['top row']['search'] = array(
278
      '#type' => 'textfield',
279
      '#title' => t('Search'),
280
    );
281

    
282
    $form['bottom row']['order'] = array(
283
      '#type' => 'select',
284
      '#title' => t('Sort by'),
285
      '#options' => $this->list_sort_options(),
286
      '#default_value' => 'disabled',
287
    );
288

    
289
    $form['bottom row']['sort'] = array(
290
      '#type' => 'select',
291
      '#title' => t('Order'),
292
      '#options' => array(
293
        'asc' => t('Up'),
294
        'desc' => t('Down'),
295
      ),
296
      '#default_value' => 'asc',
297
    );
298

    
299
    $form['bottom row']['submit'] = array(
300
      '#type' => 'submit',
301
      '#id' => 'ctools-export-ui-list-items-apply',
302
      '#value' => t('Apply'),
303
      '#attributes' => array('class' => array('use-ajax-submit ctools-auto-submit-click')),
304
    );
305

    
306
    $form['bottom row']['reset'] = array(
307
      '#type' => 'submit',
308
      '#id' => 'ctools-export-ui-list-items-apply',
309
      '#value' => t('Reset'),
310
      '#attributes' => array('class' => array('use-ajax-submit')),
311
    );
312

    
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'));
319
  }
320

    
321
  /**
322
   * Validate the filter/sort form.
323
   *
324
   * It is very rare that a filter form needs validation, but if it is
325
   * needed, override this.
326
   */
327
  function list_form_validate(&$form, &$form_state) { }
328

    
329
  /**
330
   * Submit the filter/sort form.
331
   *
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
334
   * sorting.
335
   *
336
   * For the most part, you should not need to override this method, as the
337
   * fiddly bits call through to other functions.
338
   */
339
  function list_form_submit(&$form, &$form_state) {
340
    // Filter and re-sort the pages.
341
    $plugin = $this->plugin;
342

    
343
    $prefix = ctools_export_ui_plugin_base_path($plugin);
344

    
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)) {
349
        continue;
350
      }
351

    
352
      $operations = $this->build_operations($item);
353

    
354
      $this->list_build_row($item, $form_state, $operations);
355
    }
356

    
357
    // Now actually sort
358
    if ($form_state['values']['sort'] == 'desc') {
359
      arsort($this->sorts);
360
    }
361
    else {
362
      asort($this->sorts);
363
    }
364

    
365
    // Nuke the original.
366
    $rows = $this->rows;
367
    $this->rows = array();
368
    // And restore.
369
    foreach ($this->sorts as $name => $title) {
370
      $this->rows[$name] = $rows[$name];
371
    }
372
  }
373

    
374
  /**
375
   * Determine if a row should be filtered out.
376
   *
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
379
   * handle them.
380
   *
381
   * @return
382
   *   TRUE if the item should be excluded.
383
   */
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']}) {
387
      return TRUE;
388
    }
389

    
390
    if ($form_state['values']['disabled'] != 'all' && $form_state['values']['disabled'] != !empty($item->disabled)) {
391
      return TRUE;
392
    }
393

    
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) {
398
          $hit = TRUE;
399
          break;
400
        }
401
      }
402
      if (empty($hit)) {
403
        return TRUE;
404
      }
405
    }
406
  }
407

    
408
  /**
409
   * Provide a list of fields to test against for the default "search" widget.
410
   *
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.
413
   */
414
  function list_search_fields() {
415
    $fields = array(
416
      $this->plugin['export']['key'],
417
    );
418

    
419
    if (!empty($this->plugin['export']['admin_title'])) {
420
      $fields[] = $this->plugin['export']['admin_title'];
421
    }
422
    if (!empty($this->plugin['export']['admin_description'])) {
423
      $fields[] = $this->plugin['export']['admin_description'];
424
    }
425

    
426
    return $fields;
427
  }
428

    
429
  /**
430
   * Provide a list of sort options.
431
   *
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().
434
   */
435
  function list_sort_options() {
436
    if (!empty($this->plugin['export']['admin_title'])) {
437
      $options = array(
438
        'disabled' => t('Enabled, title'),
439
        $this->plugin['export']['admin_title'] => t('Title'),
440
      );
441
    }
442
    else {
443
      $options = array(
444
        'disabled' => t('Enabled, name'),
445
      );
446
    }
447

    
448
    $options += array(
449
      'name' => t('Name'),
450
      'storage' => t('Storage'),
451
    );
452

    
453
    return $options;
454
  }
455

    
456
  /**
457
   * Add listing CSS to the page.
458
   *
459
   * Override this if you need custom CSS for your list.
460
   */
461
  function list_css() {
462
    ctools_add_css('export-ui-list');
463
  }
464

    
465
  /**
466
   * Builds the operation links for a specific exportable item.
467
   */
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;
473

    
474
    if ($item->{$schema['export']['export type string']} == t('Normal')) {
475
      $operations['revert'] = FALSE;
476
    }
477
    elseif ($item->{$schema['export']['export type string']} == t('Overridden')) {
478
      $operations['delete'] = FALSE;
479
    }
480
    else {
481
      $operations['revert'] = FALSE;
482
      $operations['delete'] = FALSE;
483
    }
484
    if (empty($item->disabled)) {
485
      $operations['enable'] = FALSE;
486
    }
487
    else {
488
      $operations['disable'] = FALSE;
489
    }
490

    
491
    $allowed_operations = array();
492

    
493
    foreach ($operations as $op => $info) {
494
      if (!empty($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']}),
498
        );
499
        if (!empty($info['ajax'])) {
500
          $allowed_operations[$op]['attributes'] = array('class' => array('use-ajax'));
501
        }
502
        if (!empty($info['token'])) {
503
          $allowed_operations[$op]['query'] = array('token' => drupal_get_token($op));
504
        }
505
      }
506
    }
507

    
508
    return $allowed_operations;
509
  }
510

    
511
  /**
512
   * Build a row based on the item.
513
   *
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.
517
   */
518
  function list_build_row($item, &$form_state, $operations) {
519
    // Set up sorting
520
    $name = $item->{$this->plugin['export']['key']};
521
    $schema = ctools_export_get_schema($this->plugin['schema']);
522

    
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']) {
526
      case 'disabled':
527
        $this->sorts[$name] = empty($item->disabled) . $name;
528
        break;
529
      case 'title':
530
        $this->sorts[$name] = $item->{$this->plugin['export']['admin_title']};
531
        break;
532
      case 'name':
533
        $this->sorts[$name] = $name;
534
        break;
535
      case 'storage':
536
        $this->sorts[$name] = $item->{$schema['export']['export type string']} . $name;
537
        break;
538
    }
539

    
540
    $this->rows[$name]['data'] = array();
541
    $this->rows[$name]['class'] = !empty($item->disabled) ? array('ctools-export-ui-disabled') : array('ctools-export-ui-enabled');
542

    
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'));
546
    }
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'));
549

    
550
    $ops = theme('links__ctools_dropbutton', array('links' => $operations, 'attributes' => array('class' => array('links', 'inline'))));
551

    
552
    $this->rows[$name]['data'][] = array('data' => $ops, 'class' => array('ctools-export-ui-operations'));
553

    
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']};
557
    }
558
  }
559

    
560
  /**
561
   * Provide the table header.
562
   *
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.
565
   */
566
  function list_table_header() {
567
    $header = array();
568
    if (!empty($this->plugin['export']['admin_title'])) {
569
      $header[] = array('data' => t('Title'), 'class' => array('ctools-export-ui-title'));
570
    }
571

    
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'));
575

    
576
    return $header;
577
  }
578

    
579
  /**
580
   * Render all of the rows together.
581
   *
582
   * By default we place all of the rows in a table, and this should be the
583
   * way most lists will go.
584
   *
585
   * Whatever you do if this method is overridden, the ID is important for AJAX
586
   * so be sure it exists.
587
   */
588
  function list_render(&$form_state) {
589
    $table = array(
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'],
594
    );
595
    return theme('table', $table);
596
  }
597

    
598
  /**
599
   * Render a header to go before the list.
600
   *
601
   * This will appear after the filter/sort widgets.
602
   */
603
  function list_header($form_state) { }
604

    
605
  /**
606
   * Render a footer to go after thie list.
607
   *
608
   * This is a good place to add additional links.
609
   */
610
  function list_footer($form_state) { }
611

    
612
  // ------------------------------------------------------------------------
613
  // These methods are the API for adding/editing exportable items
614

    
615
  /**
616
   * Perform a drupal_goto() to the location provided by the plugin for the
617
   * operation.
618
   *
619
   * @param $op
620
   *   The operation to use. A string must exist in $this->plugin['redirect']
621
   *   for this operation.
622
   * @param $item
623
   *   The item in use; this may be necessary as item IDs are often embedded in
624
   *   redirects.
625
   */
626
  function redirect($op, $item = NULL) {
627
    if (isset($this->plugin['redirect'][$op])) {
628
      $destination = (array) $this->plugin['redirect'][$op];
629
      if ($item) {
630
        $export_key = $this->plugin['export']['key'];
631
        $destination[0] = str_replace('%ctools_export_ui', $item->{$export_key}, $destination[0]);
632
      }
633
      call_user_func_array('drupal_goto', $destination);
634
    }
635
    else {
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));
638
    }
639
  }
640

    
641
  function add_page($js, $input, $step = NULL) {
642
    drupal_set_title($this->get_page_title('add'), PASS_THROUGH);
643

    
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');
648
    }
649
    if (empty($item)) {
650
      $item = ctools_export_crud_new($this->plugin['schema']);
651
    }
652

    
653
    $form_state = array(
654
      'plugin' => $this->plugin,
655
      'object' => &$this,
656
      'ajax' => $js,
657
      'item' => $item,
658
      'op' => 'add',
659
      'form type' => 'add',
660
      'rerender' => TRUE,
661
      'no_redirect' => TRUE,
662
      'step' => $step,
663
      // Store these in case additional args are needed.
664
      'function args' => func_get_args(),
665
    );
666

    
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']);
670
    }
671

    
672
    return $output;
673
  }
674

    
675
  /**
676
   * Main entry point to edit an item.
677
   */
678
  function edit_page($js, $input, $item, $step = NULL) {
679
    drupal_set_title($this->get_page_title('edit', $item), PASS_THROUGH);
680

    
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)) {
685
        $item = $cached;
686
      }
687
    }
688

    
689
    $form_state = array(
690
      'plugin' => $this->plugin,
691
      'object' => &$this,
692
      'ajax' => $js,
693
      'item' => $item,
694
      'op' => 'edit',
695
      'form type' => 'edit',
696
      'rerender' => TRUE,
697
      'no_redirect' => TRUE,
698
      'step' => $step,
699
      // Store these in case additional args are needed.
700
      'function args' => func_get_args(),
701
    );
702

    
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']);
706
    }
707

    
708
    return $output;
709
  }
710

    
711
  /**
712
   * Main entry point to clone an item.
713
   */
714
  function clone_page($js, $input, $original, $step = NULL) {
715
    drupal_set_title($this->get_page_title('clone', $original), PASS_THROUGH);
716

    
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');
721
    }
722
    if (empty($item)) {
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);
727
      $item->{$this->plugin['export']['key']} = 'clone_of_' . $item->{$this->plugin['export']['key']};
728
    }
729

    
730
    // Tabs and breadcrumb disappearing, this helps alleviate through cheating.
731
    // ...not sure this this is the best way.
732
    $trail = menu_set_active_item(ctools_export_ui_plugin_base_path($this->plugin));
733

    
734
    $name = $original->{$this->plugin['export']['key']};
735

    
736
    $form_state = array(
737
      'plugin' => $this->plugin,
738
      'object' => &$this,
739
      'ajax' => $js,
740
      'item' => $item,
741
      'op' => 'add',
742
      'form type' => 'clone',
743
      'original name' => $name,
744
      'rerender' => TRUE,
745
      'no_redirect' => TRUE,
746
      'step' => $step,
747
      // Store these in case additional args are needed.
748
      'function args' => func_get_args(),
749
    );
750

    
751
    $output = $this->edit_execute_form($form_state);
752
    if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
753
      $this->redirect($form_state['op'], $form_state['item']);
754
    }
755

    
756
    return $output;
757
  }
758

    
759
  /**
760
   * Execute the form.
761
   *
762
   * Add and Edit both funnel into this, but they have a few different
763
   * settings.
764
   */
765
  function edit_execute_form(&$form_state) {
766
    if (!empty($this->plugin['use wizard'])) {
767
      return $this->edit_execute_form_wizard($form_state);
768
    }
769
    else {
770
      return $this->edit_execute_form_standard($form_state);
771
    }
772
  }
773

    
774
  /**
775
   * Execute the standard form for editing.
776
   *
777
   * By default, export UI will provide a single form for editing an object.
778
   */
779
  function edit_execute_form_standard(&$form_state) {
780
    $output = drupal_build_form('ctools_export_ui_edit_item_form', $form_state);
781
    if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
782
      $this->edit_save_form($form_state);
783
    }
784

    
785
    return $output;
786
  }
787

    
788
  /**
789
   * Get the form info for the wizard.
790
   *
791
   * This gets the form info out of the plugin, then adds defaults based on
792
   * how we want edit forms to work.
793
   *
794
   * Overriding this can allow child UIs to tweak this info for specialized
795
   * wizards.
796
   *
797
   * @param array $form_state
798
   *   The already created form state.
799
   */
800
  function get_wizard_info(&$form_state) {
801
    if (!isset($form_state['step'])) {
802
      $form_state['step'] = NULL;
803
    }
804

    
805
    $export_key = $this->plugin['export']['key'];
806

    
807
    // When cloning, the name of the item being cloned is referenced in the
808
    // path, not the name of this item.
809
    if ($form_state['form type'] == 'clone') {
810
      $name = $form_state['original name'];
811
    }
812
    else {
813
      $name = $form_state['item']->{$export_key};
814
    }
815

    
816
    $form_info = !empty($this->plugin['form info']) ? $this->plugin['form info'] : array();
817
    $form_info += array(
818
      'id' => 'ctools_export_ui_edit',
819
      'path' => ctools_export_ui_plugin_menu_path($this->plugin, $form_state['form type'], $name) . '/%step',
820
      'show trail' => TRUE,
821
      'free trail' => TRUE,
822
      'show back' => $form_state['form type'] == 'add',
823
      'show return' => FALSE,
824
      'show cancel' => TRUE,
825
      'finish callback' => 'ctools_export_ui_wizard_finish',
826
      'next callback' => 'ctools_export_ui_wizard_next',
827
      'back callback' => 'ctools_export_ui_wizard_back',
828
      'cancel callback' => 'ctools_export_ui_wizard_cancel',
829
      'order' => array(),
830
      'import order' => array(
831
        'import' => t('Import code'),
832
        'settings' => t('Settings'),
833
      ),
834
    );
835

    
836
    // Set the order of forms based on the op if we have a specific one.
837
    if (isset($form_info[$form_state['form type'] . ' order'])) {
838
      $form_info['order'] = $form_info[$form_state['form type'] . ' order'];
839
    }
840

    
841
    // We have generic fallback forms we can use if they are not specified,
842
    // and they automatically delegate back to the UI object. Use these if
843
    // not specified.
844
    foreach ($form_info['order'] as $key => $title) {
845
      if (empty($form_info['forms'][$key])) {
846
        $form_info['forms'][$key] = array(
847
          'form id' => 'ctools_export_ui_edit_item_wizard_form',
848
        );
849
      }
850
    }
851

    
852
    // 'free trail' means the wizard can freely go back and form from item
853
    // via the trail and not with next/back buttons.
854
    if ($form_state['form type'] == 'add' || ($form_state['form type'] == 'import' && empty($form_state['item']->{$export_key}))) {
855
      $form_info['free trail'] = FALSE;
856
    }
857

    
858
    return $form_info;
859
  }
860

    
861
  /**
862
   * Execute the wizard for editing.
863
   *
864
   * For complex objects, sometimes a wizard is needed. This is normally
865
   * activated by setting 'use wizard' => TRUE in the plugin definition
866
   * and then creating a 'form info' array to feed the wizard the data
867
   * it needs.
868
   *
869
   * When creating this wizard, the plugin is responsible for defining all forms
870
   * that will be utilized with the wizard.
871
   *
872
   * Using 'add order' or 'edit order' can be used to ensure that add/edit order
873
   * is different.
874
   */
875
  function edit_execute_form_wizard(&$form_state) {
876
    $form_info = $this->get_wizard_info($form_state);
877

    
878
    // If there aren't any forms set, fail.
879
    if (empty($form_info['order'])) {
880
      return MENU_NOT_FOUND;
881
    }
882

    
883
    // Figure out if this is a new instance of the wizard
884
    if (empty($form_state['step'])) {
885
      $order = array_keys($form_info['order']);
886
      $form_state['step'] = reset($order);
887
    }
888

    
889
    if (empty($form_info['order'][$form_state['step']]) && empty($form_info['forms'][$form_state['step']])) {
890
      return MENU_NOT_FOUND;
891
    }
892

    
893
    ctools_include('wizard');
894
    $output = ctools_wizard_multistep_form($form_info, $form_state['step'], $form_state);
895
    if (!empty($form_state['complete'])) {
896
      $this->edit_save_form($form_state);
897
    }
898
    else if ($output && !empty($form_state['item']->export_ui_item_is_cached)) {
899
      // @todo this should be in the plugin strings
900
      drupal_set_message(t('You have unsaved changes. These changes will not be made permanent until you click <em>Save</em>.'), 'warning');
901
    }
902

    
903
    // Unset the executed flag if any non-wizard button was pressed. Those
904
    // buttons require special handling by whatever client is operating them.
905
    if (!empty($form_state['executed']) && empty($form_state['clicked_button']['#wizard type'])) {
906
      unset($form_state['executed']);
907
    }
908
    return $output;
909
  }
910

    
911
  /**
912
   * Wizard 'back' callback when using a wizard to edit an item.
913
   *
914
   * The wizard callback delegates this back to the object.
915
   */
916
  function edit_wizard_back(&$form_state) {
917
    // This only exists so child implementations can use it.
918
  }
919

    
920
  /**
921
   * Wizard 'next' callback when using a wizard to edit an item.
922
   *
923
   * The wizard callback delegates this back to the object.
924
   */
925
  function edit_wizard_next(&$form_state) {
926
    $this->edit_cache_set($form_state['item'], $form_state['form type']);
927
  }
928

    
929
  /**
930
   * Wizard 'cancel' callback when using a wizard to edit an item.
931
   *
932
   * The wizard callback delegates this back to the object.
933
   */
934
  function edit_wizard_cancel(&$form_state) {
935
    $this->edit_cache_clear($form_state['item'], $form_state['form type']);
936
  }
937

    
938
  /**
939
   * Wizard 'cancel' callback when using a wizard to edit an item.
940
   *
941
   * The wizard callback delegates this back to the object.
942
   */
943
  function edit_wizard_finish(&$form_state) {
944
    $form_state['complete'] = TRUE;
945

    
946
    // If we are importing, and overwrite was selected, delete the original so
947
    // that this one writes properly.
948
    if ($form_state['form type'] == 'import' && !empty($form_state['item']->export_ui_allow_overwrite)) {
949
      ctools_export_crud_delete($this->plugin['schema'], $form_state['item']);
950
    }
951

    
952
    $this->edit_cache_clear($form_state['item'], $form_state['form type']);
953
  }
954

    
955
  /**
956
   * Retrieve the item currently being edited from the object cache.
957
   */
958
  function edit_cache_get($item, $op = 'edit') {
959
    ctools_include('object-cache');
960
    if (is_string($item)) {
961
      $name = $item;
962
    }
963
    else {
964
      $name = $this->edit_cache_get_key($item, $op);
965
    }
966

    
967
    $cache = ctools_object_cache_get('ctui_' . $this->plugin['name'], $name);
968
    if ($cache) {
969
      $cache->export_ui_item_is_cached = TRUE;
970
      return $cache;
971
    }
972
  }
973

    
974
  /**
975
   * Cache the item currently currently being edited.
976
   */
977
  function edit_cache_set($item, $op = 'edit') {
978
    ctools_include('object-cache');
979
    $name = $this->edit_cache_get_key($item, $op);
980
    return $this->edit_cache_set_key($item, $name);
981
  }
982

    
983
  function edit_cache_set_key($item, $name) {
984
    return ctools_object_cache_set('ctui_' . $this->plugin['name'], $name, $item);
985
  }
986

    
987
  /**
988
   * Clear the object cache for the currently edited item.
989
   */
990
  function edit_cache_clear($item, $op = 'edit') {
991
    ctools_include('object-cache');
992
    $name = $this->edit_cache_get_key($item, $op);
993
    return ctools_object_cache_clear('ctui_' . $this->plugin['name'], $name);
994
  }
995

    
996
  /**
997
   * Figure out what the cache key is for this object.
998
   */
999
  function edit_cache_get_key($item, $op) {
1000
    $export_key = $this->plugin['export']['key'];
1001
    return $op == 'edit' ? $item->{$this->plugin['export']['key']} : "::$op";
1002
  }
1003

    
1004
  /**
1005
   * Called to save the final product from the edit form.
1006
   */
1007
  function edit_save_form($form_state) {
1008
    $item = &$form_state['item'];
1009
    $export_key = $this->plugin['export']['key'];
1010

    
1011
    $result = ctools_export_crud_save($this->plugin['schema'], $item);
1012

    
1013
    if ($result) {
1014
      $message = str_replace('%title', check_plain($item->{$export_key}), $this->plugin['strings']['confirmation'][$form_state['op']]['success']);
1015
      drupal_set_message($message);
1016
    }
1017
    else {
1018
      $message = str_replace('%title', check_plain($item->{$export_key}), $this->plugin['strings']['confirmation'][$form_state['op']]['fail']);
1019
      drupal_set_message($message, 'error');
1020
    }
1021
  }
1022

    
1023
  /**
1024
   * Provide the actual editing form.
1025
   */
1026
  function edit_form(&$form, &$form_state) {
1027
    $export_key = $this->plugin['export']['key'];
1028
    $item = $form_state['item'];
1029
    $schema = ctools_export_get_schema($this->plugin['schema']);
1030

    
1031
    if (!empty($this->plugin['export']['admin_title'])) {
1032
      $form['info'][$this->plugin['export']['admin_title']] = array(
1033
        '#type' => 'textfield',
1034
        '#title' => t('Administrative title'),
1035
        '#description' => t('This will appear in the administrative interface to easily identify it.'),
1036
        '#default_value' => $item->{$this->plugin['export']['admin_title']},
1037
      );
1038
    }
1039

    
1040
    $form['info'][$export_key] = array(
1041
      '#title' => t($schema['export']['key name']),
1042
      '#type' => 'textfield',
1043
      '#default_value' => $item->{$export_key},
1044
      '#description' => t('The unique ID for this @export.', array('@export' => $this->plugin['title singular'])),
1045
      '#required' => TRUE,
1046
      '#maxlength' => 255,
1047
    );
1048

    
1049
    if (!empty($this->plugin['export']['admin_title'])) {
1050
      $form['info'][$export_key]['#type'] = 'machine_name';
1051
      $form['info'][$export_key]['#machine_name'] = array(
1052
        'exists' => 'ctools_export_ui_edit_name_exists',
1053
        'source' => array('info', $this->plugin['export']['admin_title']),
1054
      );
1055
    }
1056

    
1057
    if ($form_state['op'] === 'edit') {
1058
      $form['info'][$export_key]['#disabled'] = TRUE;
1059
      $form['info'][$export_key]['#value'] = $item->{$export_key};
1060
    }
1061

    
1062
    if (!empty($this->plugin['export']['admin_description'])) {
1063
      $form['info'][$this->plugin['export']['admin_description']] = array(
1064
        '#type' => 'textarea',
1065
        '#title' => t('Administrative description'),
1066
        '#default_value' => $item->{$this->plugin['export']['admin_description']},
1067
      );
1068
    }
1069

    
1070
    // Add plugin's form definitions.
1071
    if (!empty($this->plugin['form']['settings'])) {
1072
      // Pass $form by reference.
1073
      $this->plugin['form']['settings']($form, $form_state);
1074
    }
1075

    
1076
    // Add the buttons if the wizard is not in use.
1077
    if (empty($form_state['form_info'])) {
1078
      // Make sure that whatever happens, the buttons go to the bottom.
1079
      $form['buttons']['#weight'] = 100;
1080

    
1081
      // Add buttons.
1082
      $form['buttons']['submit'] = array(
1083
        '#type' => 'submit',
1084
        '#value' => t('Save'),
1085
      );
1086

    
1087
      $form['buttons']['delete'] = array(
1088
        '#type' => 'submit',
1089
        '#value' => $item->export_type & EXPORT_IN_CODE ? t('Revert') : t('Delete'),
1090
        '#access' => $form_state['op'] === 'edit' && $item->export_type & EXPORT_IN_DATABASE,
1091
        '#submit' => array('ctools_export_ui_edit_item_form_delete'),
1092
      );
1093
    }
1094
  }
1095

    
1096
  /**
1097
   * Validate callback for the edit form.
1098
   */
1099
  function edit_form_validate(&$form, &$form_state) {
1100
    if (!empty($this->plugin['form']['validate'])) {
1101
      // Pass $form by reference.
1102
      $this->plugin['form']['validate']($form, $form_state);
1103
    }
1104
  }
1105

    
1106
  /**
1107
   * Perform a final validation check before allowing the form to be
1108
   * finished.
1109
   */
1110
  function edit_finish_validate(&$form, &$form_state) {
1111
    if ($form_state['op'] != 'edit') {
1112
      // Validate the export key. Fake an element for form_error().
1113
      $export_key = $this->plugin['export']['key'];
1114
      $element = array(
1115
        '#value' => $form_state['item']->{$export_key},
1116
        '#parents' => array($export_key),
1117
      );
1118
      $form_state['plugin'] = $this->plugin;
1119
      ctools_export_ui_edit_name_validate($element, $form_state);
1120
    }
1121
  }
1122

    
1123
  /**
1124
   * Handle the submission of the edit form.
1125
   *
1126
   * At this point, submission is successful. Our only responsibility is
1127
   * to copy anything out of values onto the item that we are able to edit.
1128
   *
1129
   * If the keys all match up to the schema, this method will not need to be
1130
   * overridden.
1131
   */
1132
  function edit_form_submit(&$form, &$form_state) {
1133
    if (!empty($this->plugin['form']['submit'])) {
1134
      // Pass $form by reference.
1135
      $this->plugin['form']['submit']($form, $form_state);
1136
    }
1137

    
1138
    // Transfer data from the form to the $item based upon schema values.
1139
    $schema = ctools_export_get_schema($this->plugin['schema']);
1140
    foreach (array_keys($schema['fields']) as $key) {
1141
      if(isset($form_state['values'][$key])) {
1142
        $form_state['item']->{$key} = $form_state['values'][$key];
1143
      }
1144
    }
1145
  }
1146

    
1147
  // ------------------------------------------------------------------------
1148
  // These methods are the API for 'other' stuff with exportables such as
1149
  // enable, disable, import, export, delete
1150

    
1151
  /**
1152
   * Callback to enable a page.
1153
   */
1154
  function enable_page($js, $input, $item) {
1155
    return $this->set_item_state(FALSE, $js, $input, $item);
1156
  }
1157

    
1158
  /**
1159
   * Callback to disable a page.
1160
   */
1161
  function disable_page($js, $input, $item) {
1162
    return $this->set_item_state(TRUE, $js, $input, $item);
1163
  }
1164

    
1165
  /**
1166
   * Set an item's state to enabled or disabled and output to user.
1167
   *
1168
   * If javascript is in use, this will rebuild the list and send that back
1169
   * as though the filter form had been executed.
1170
   */
1171
  function set_item_state($state, $js, $input, $item) {
1172
    ctools_export_crud_set_status($this->plugin['schema'], $item, $state);
1173

    
1174
    if (!$js) {
1175
      drupal_goto(ctools_export_ui_plugin_base_path($this->plugin));
1176
    }
1177
    else {
1178
      return $this->list_page($js, $input);
1179
    }
1180
  }
1181

    
1182
  /**
1183
   * Page callback to delete an exportable item.
1184
   */
1185
  function delete_page($js, $input, $item) {
1186
    $form_state = array(
1187
      'plugin' => $this->plugin,
1188
      'object' => &$this,
1189
      'ajax' => $js,
1190
      'item' => $item,
1191
      'op' => $item->export_type & EXPORT_IN_CODE ? 'revert' : 'delete',
1192
      'rerender' => TRUE,
1193
      'no_redirect' => TRUE,
1194
    );
1195

    
1196
    $output = drupal_build_form('ctools_export_ui_delete_confirm_form', $form_state);
1197
    if (!empty($form_state['executed'])) {
1198
      $this->delete_form_submit($form_state);
1199
      $this->redirect($form_state['op'], $item);
1200
    }
1201

    
1202
    return $output;
1203
  }
1204

    
1205
  /**
1206
   * Deletes exportable items from the database.
1207
   */
1208
  function delete_form_submit(&$form_state) {
1209
    $item = $form_state['item'];
1210

    
1211
    ctools_export_crud_delete($this->plugin['schema'], $item);
1212
    $export_key = $this->plugin['export']['key'];
1213
    $message = str_replace('%title', check_plain($item->{$export_key}), $this->plugin['strings']['confirmation'][$form_state['op']]['success']);
1214
    drupal_set_message($message);
1215
  }
1216

    
1217
  /**
1218
   * Page callback to display export information for an exportable item.
1219
   */
1220
  function export_page($js, $input, $item) {
1221
    drupal_set_title($this->get_page_title('export', $item), PASS_THROUGH);
1222
    return drupal_get_form('ctools_export_form', ctools_export_crud_export($this->plugin['schema'], $item), t('Export'));
1223
  }
1224

    
1225
  /**
1226
   * Page callback to import information for an exportable item.
1227
   */
1228
  function import_page($js, $input, $step = NULL) {
1229
    drupal_set_title($this->get_page_title('import'), PASS_THROUGH);
1230
    // Import is basically a multi step wizard form, so let's go ahead and
1231
    // use CTools' wizard.inc for it.
1232

    
1233
    // If a step not set, they are trying to create a new item. If a step
1234
    // is set, they're in the process of creating an item.
1235
    if (!empty($step)) {
1236
      $item = $this->edit_cache_get(NULL, 'import');
1237
    }
1238
    if (empty($item)) {
1239
      $item = ctools_export_crud_new($this->plugin['schema']);
1240
    }
1241

    
1242
    $form_state = array(
1243
      'plugin' => $this->plugin,
1244
      'object' => &$this,
1245
      'ajax' => $js,
1246
      'item' => $item,
1247
      'op' => 'add',
1248
      'form type' => 'import',
1249
      'rerender' => TRUE,
1250
      'no_redirect' => TRUE,
1251
      'step' => $step,
1252
      // Store these in case additional args are needed.
1253
      'function args' => func_get_args(),
1254
    );
1255

    
1256
    // import always uses the wizard.
1257
    $output = $this->edit_execute_form_wizard($form_state);
1258
    if (!empty($form_state['executed'])) {
1259
      $this->redirect($form_state['op'], $form_state['item']);
1260
    }
1261

    
1262
    return $output;
1263
  }
1264

    
1265
  /**
1266
   * Import form. Provides simple helptext instructions and textarea for
1267
   * pasting a export definition.
1268
   */
1269
  function edit_form_import(&$form, &$form_state) {
1270
    $form['help'] = array(
1271
      '#type' => 'item',
1272
      '#value' => $this->plugin['strings']['help']['import'],
1273
    );
1274

    
1275
    $form['import'] = array(
1276
      '#title' => t('@plugin code', array('@plugin' => $this->plugin['title singular proper'])),
1277
      '#type' => 'textarea',
1278
      '#rows' => 10,
1279
      '#required' => TRUE,
1280
      '#default_value' => !empty($form_state['item']->export_ui_code) ? $form_state['item']->export_ui_code : '',
1281
    );
1282

    
1283
    $form['overwrite'] = array(
1284
      '#title' => t('Allow import to overwrite an existing record.'),
1285
      '#type' => 'checkbox',
1286
      '#default_value' => !empty($form_state['item']->export_ui_allow_overwrite) ? $form_state['item']->export_ui_allow_overwrite : FALSE,
1287
    );
1288
  }
1289

    
1290
  /**
1291
   * Import form validate handler.
1292
   *
1293
   * Evaluates code and make sure it creates an object before we continue.
1294
   */
1295
  function edit_form_import_validate($form, &$form_state) {
1296
    $item = ctools_export_crud_import($this->plugin['schema'], $form_state['values']['import']);
1297
    if (is_string($item)) {
1298
      form_error($form['import'], t('Unable to get an import from the code. Errors reported: @errors', array('@errors' => $item)));
1299
      return;
1300
    }
1301

    
1302
    $form_state['item'] = $item;
1303
    $form_state['item']->export_ui_allow_overwrite = $form_state['values']['overwrite'];
1304
    $form_state['item']->export_ui_code = $form_state['values']['import'];
1305
  }
1306

    
1307
  /**
1308
   * Submit callback for import form.
1309
   *
1310
   * Stores the item in the session.
1311
   */
1312
  function edit_form_import_submit($form, &$form_state) {
1313
    // The validate function already imported and stored the item. This
1314
    // function exists simply to prevent it from going to the default
1315
    // edit_form_submit() method.
1316
  }
1317
}
1318

    
1319
// -----------------------------------------------------------------------
1320
// Forms to be used with this class.
1321
//
1322
// Since Drupal's forms are completely procedural, these forms will
1323
// mostly just be pass-throughs back to the object.
1324

    
1325
/**
1326
 * Add all appropriate includes to forms so that caching the form
1327
 * still loads the files that we need.
1328
 */
1329
function _ctools_export_ui_add_form_files($form, &$form_state) {
1330
  ctools_form_include($form_state, 'export');
1331
  ctools_form_include($form_state, 'export-ui');
1332

    
1333
  // Also make sure the plugin .inc file is loaded.
1334
  ctools_form_include_file($form_state, $form_state['object']->plugin['path'] . '/' . $form_state['object']->plugin['file']);
1335
}
1336

    
1337
/**
1338
 * Form callback to handle the filter/sort form when listing items.
1339
 *
1340
 * This simply loads the object defined in the plugin and hands it off.
1341
 */
1342
function ctools_export_ui_list_form($form, &$form_state) {
1343
  $form_state['object']->list_form($form, $form_state);
1344
  return $form;
1345
}
1346

    
1347
/**
1348
 * Validate handler for ctools_export_ui_list_form.
1349
 */
1350
function ctools_export_ui_list_form_validate(&$form, &$form_state) {
1351
  $form_state['object']->list_form_validate($form, $form_state);
1352
}
1353

    
1354
/**
1355
 * Submit handler for ctools_export_ui_list_form.
1356
 */
1357
function ctools_export_ui_list_form_submit(&$form, &$form_state) {
1358
  $form_state['object']->list_form_submit($form, $form_state);
1359
}
1360

    
1361
/**
1362
 * Form callback to edit an exportable item.
1363
 *
1364
 * This simply loads the object defined in the plugin and hands it off.
1365
 */
1366
function ctools_export_ui_edit_item_form($form, &$form_state) {
1367
  // When called using #ajax via ajax_form_callback(), 'export' may
1368
  // not be included so include it here.
1369
  _ctools_export_ui_add_form_files($form, $form_state);
1370

    
1371
  $form_state['object']->edit_form($form, $form_state);
1372
  return $form;
1373
}
1374

    
1375
/**
1376
 * Validate handler for ctools_export_ui_edit_item_form.
1377
 */
1378
function ctools_export_ui_edit_item_form_validate(&$form, &$form_state) {
1379
  $form_state['object']->edit_form_validate($form, $form_state);
1380
}
1381

    
1382
/**
1383
 * Submit handler for ctools_export_ui_edit_item_form.
1384
 */
1385
function ctools_export_ui_edit_item_form_submit(&$form, &$form_state) {
1386
  $form_state['object']->edit_form_submit($form, $form_state);
1387
}
1388

    
1389
/**
1390
 * Submit handler to delete for ctools_export_ui_edit_item_form
1391
 *
1392
 * @todo Put this on a callback in the object.
1393
 */
1394
function ctools_export_ui_edit_item_form_delete(&$form, &$form_state) {
1395
  _ctools_export_ui_add_form_files($form, $form_state);
1396

    
1397
  $export_key = $form_state['plugin']['export']['key'];
1398
  $path = $form_state['item']->export_type & EXPORT_IN_CODE ? 'revert' : 'delete';
1399

    
1400
  drupal_goto(ctools_export_ui_plugin_menu_path($form_state['plugin'], $path, $form_state['item']->{$export_key}), array('cancel_path' => $_GET['q']));
1401
}
1402

    
1403
/**
1404
 * Validate that an export item name is acceptable and unique during add.
1405
 */
1406
function ctools_export_ui_edit_name_validate($element, &$form_state) {
1407
  $plugin = $form_state['plugin'];
1408
  // Check for string identifier sanity
1409
  if (!preg_match('!^[a-z0-9_]+$!', $element['#value'])) {
1410
    form_error($element, t('The export id can only consist of lowercase letters, underscores, and numbers.'));
1411
    return;
1412
  }
1413

    
1414
  // Check for name collision
1415
  if (empty($form_state['item']->export_ui_allow_overwrite) && $exists = ctools_export_crud_load($plugin['schema'], $element['#value'])) {
1416
    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'])));
1417
  }
1418
}
1419

    
1420
/**
1421
 * Test for #machine_name type to see if an export exists.
1422
 */
1423
function ctools_export_ui_edit_name_exists($name, $element, &$form_state) {
1424
  $plugin = $form_state['plugin'];
1425

    
1426
  return (empty($form_state['item']->export_ui_allow_overwrite) && ctools_export_crud_load($plugin['schema'], $name));
1427
}
1428

    
1429
/**
1430
 * Delete/Revert confirm form.
1431
 *
1432
 * @todo -- call back into the object instead.
1433
 */
1434
function ctools_export_ui_delete_confirm_form($form, &$form_state) {
1435
  _ctools_export_ui_add_form_files($form, $form_state);
1436

    
1437
  $plugin = $form_state['plugin'];
1438
  $item = $form_state['item'];
1439

    
1440
  $form = array();
1441

    
1442
  $export_key = $plugin['export']['key'];
1443
  $question = str_replace('%title', check_plain($item->{$export_key}), $plugin['strings']['confirmation'][$form_state['op']]['question']);
1444
  $path = empty($_REQUEST['cancel_path']) ? ctools_export_ui_plugin_base_path($plugin) : $_REQUEST['cancel_path'];
1445

    
1446
  $form = confirm_form($form,
1447
    $question,
1448
    $path,
1449
    $plugin['strings']['confirmation'][$form_state['op']]['information'],
1450
    $plugin['allowed operations'][$form_state['op']]['title'], t('Cancel')
1451
  );
1452
  return $form;
1453
}
1454

    
1455
// --------------------------------------------------------------------------
1456
// Forms and callbacks for using the edit system with the wizard.
1457

    
1458
/**
1459
 * Form callback to edit an exportable item using the wizard
1460
 *
1461
 * This simply loads the object defined in the plugin and hands it off.
1462
 */
1463
function ctools_export_ui_edit_item_wizard_form($form, &$form_state) {
1464
  _ctools_export_ui_add_form_files($form, $form_state);
1465

    
1466
  $method = 'edit_form_' . $form_state['step'];
1467
  if (!method_exists($form_state['object'], $method)) {
1468
    $method = 'edit_form';
1469
  }
1470

    
1471
  $form_state['object']->$method($form, $form_state);
1472
  return $form;
1473
}
1474

    
1475
/**
1476
 * Validate handler for ctools_export_ui_edit_item_wizard_form.
1477
 */
1478
function ctools_export_ui_edit_item_wizard_form_validate(&$form, &$form_state) {
1479
  $method = 'edit_form_' . $form_state['step'] . '_validate';
1480
  if (!method_exists($form_state['object'], $method)) {
1481
    $method = 'edit_form_validate';
1482
  }
1483

    
1484
  $form_state['object']->$method($form, $form_state);
1485

    
1486
  // Additionally, if there were no errors from that, and we're finishing,
1487
  // perform a final validate to make sure everything is ok.
1488
  if (isset($form_state['clicked_button']['#wizard type']) && $form_state['clicked_button']['#wizard type'] == 'finish' && !form_get_errors()) {
1489
    $form_state['object']->edit_finish_validate($form, $form_state);
1490
  }
1491
}
1492

    
1493
/**
1494
 * Submit handler for ctools_export_ui_edit_item_wizard_form.
1495
 */
1496
function ctools_export_ui_edit_item_wizard_form_submit(&$form, &$form_state) {
1497
  $method = 'edit_form_' . $form_state['step'] . '_submit';
1498
  if (!method_exists($form_state['object'], $method)) {
1499
    $method = 'edit_form_submit';
1500
  }
1501

    
1502
  $form_state['object']->$method($form, $form_state);
1503
}
1504

    
1505
/**
1506
 * Wizard 'back' callback when using a wizard to edit an item.
1507
 */
1508
function ctools_export_ui_wizard_back(&$form_state) {
1509
  $form_state['object']->edit_wizard_back($form_state);
1510
}
1511

    
1512
/**
1513
 * Wizard 'next' callback when using a wizard to edit an item.
1514
 */
1515
function ctools_export_ui_wizard_next(&$form_state) {
1516
  $form_state['object']->edit_wizard_next($form_state);
1517
}
1518

    
1519
/**
1520
 * Wizard 'cancel' callback when using a wizard to edit an item.
1521
 */
1522
function ctools_export_ui_wizard_cancel(&$form_state) {
1523
  $form_state['object']->edit_wizard_cancel($form_state);
1524
}
1525

    
1526
/**
1527
 * Wizard 'finish' callback when using a wizard to edit an item.
1528
 */
1529
function ctools_export_ui_wizard_finish(&$form_state) {
1530
  $form_state['object']->edit_wizard_finish($form_state);
1531
}