Projet

Général

Profil

Paste
Télécharger (49,4 ko) Statistiques
| Branche: | Révision:

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

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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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
  public 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

    
728
      if (!empty($input[$this->plugin['export']['key']])) {
729
        $item->{$this->plugin['export']['key']} = $input[$this->plugin['export']['key']];
730
      }
731
      else {
732
        $item->{$this->plugin['export']['key']} = 'clone_of_' . $item->{$this->plugin['export']['key']};
733
      }
734
    }
735

    
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));
739

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

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

    
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']);
760
    }
761

    
762
    return $output;
763
  }
764

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

    
780
  /**
781
   * Execute the standard form for editing.
782
   *
783
   * By default, export UI will provide a single form for editing an object.
784
   */
785
  public 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);
789
    }
790

    
791
    return $output;
792
  }
793

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

    
811
    $export_key = $this->plugin['export']['key'];
812

    
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'];
817
    }
818
    else {
819
      $name = $form_state['item']->{$export_key};
820
    }
821

    
822
    $form_info = !empty($this->plugin['form info']) ? $this->plugin['form info'] : array();
823
    $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',
835
      'order' => array(),
836
      'import order' => array(
837
        'import' => t('Import code'),
838
        'settings' => t('Settings'),
839
      ),
840
    );
841

    
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'];
845
    }
846

    
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
849
    // not specified.
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',
854
        );
855
      }
856
    }
857

    
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;
862
    }
863

    
864
    return $form_info;
865
  }
866

    
867
  /**
868
   * Execute the wizard for editing.
869
   *
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
873
   * it needs.
874
   *
875
   * When creating this wizard, the plugin is responsible for defining all forms
876
   * that will be utilized with the wizard.
877
   *
878
   * Using 'add order' or 'edit order' can be used to ensure that add/edit order
879
   * is different.
880
   */
881
  public function edit_execute_form_wizard(&$form_state) {
882
    $form_info = $this->get_wizard_info($form_state);
883

    
884
    // If there aren't any forms set, fail.
885
    if (empty($form_info['order'])) {
886
      return MENU_NOT_FOUND;
887
    }
888

    
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);
893
    }
894

    
895
    if (empty($form_info['order'][$form_state['step']]) && empty($form_info['forms'][$form_state['step']])) {
896
      return MENU_NOT_FOUND;
897
    }
898

    
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);
903
    }
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');
907
    }
908

    
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']);
913
    }
914
    return $output;
915
  }
916

    
917
  /**
918
   * Wizard 'back' callback when using a wizard to edit an item.
919
   *
920
   * The wizard callback delegates this back to the object.
921
   */
922
  public function edit_wizard_back(&$form_state) {
923
    // This only exists so child implementations can use it.
924
  }
925

    
926
  /**
927
   * Wizard 'next' callback when using a wizard to edit an item.
928
   *
929
   * The wizard callback delegates this back to the object.
930
   */
931
  public function edit_wizard_next(&$form_state) {
932
    $this->edit_cache_set($form_state['item'], $form_state['form type']);
933
  }
934

    
935
  /**
936
   * Wizard 'cancel' callback when using a wizard to edit an item.
937
   *
938
   * The wizard callback delegates this back to the object.
939
   */
940
  public function edit_wizard_cancel(&$form_state) {
941
    $this->edit_cache_clear($form_state['item'], $form_state['form type']);
942
  }
943

    
944
  /**
945
   * Wizard 'cancel' callback when using a wizard to edit an item.
946
   *
947
   * The wizard callback delegates this back to the object.
948
   */
949
  public function edit_wizard_finish(&$form_state) {
950
    $form_state['complete'] = TRUE;
951

    
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']);
956
    }
957

    
958
    $this->edit_cache_clear($form_state['item'], $form_state['form type']);
959
  }
960

    
961
  /**
962
   * Retrieve the item currently being edited from the object cache.
963
   */
964
  public function edit_cache_get($item, $op = 'edit') {
965
    ctools_include('object-cache');
966
    if (is_string($item)) {
967
      $name = $item;
968
    }
969
    else {
970
      $name = $this->edit_cache_get_key($item, $op);
971
    }
972

    
973
    $cache = ctools_object_cache_get('ctui_' . $this->plugin['name'], $name);
974
    if ($cache) {
975
      $cache->export_ui_item_is_cached = TRUE;
976
      return $cache;
977
    }
978
  }
979

    
980
  /**
981
   * Cache the item currently currently being edited.
982
   */
983
  public 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);
987
  }
988

    
989
  public function edit_cache_set_key($item, $name) {
990
    return ctools_object_cache_set('ctui_' . $this->plugin['name'], $name, $item);
991
  }
992

    
993
  /**
994
   * Clear the object cache for the currently edited item.
995
   */
996
  public 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);
1000
  }
1001

    
1002
  /**
1003
   * Figure out what the cache key is for this object.
1004
   */
1005
  public function edit_cache_get_key($item, $op) {
1006
    $export_key = $this->plugin['export']['key'];
1007
    return $op == 'edit' ? $item->{$this->plugin['export']['key']} : "::$op";
1008
  }
1009

    
1010
  /**
1011
   * Called to save the final product from the edit form.
1012
   */
1013
  public function edit_save_form($form_state) {
1014
    $item = &$form_state['item'];
1015
    $export_key = $this->plugin['export']['key'];
1016

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

    
1019
    if ($result) {
1020
      $message = str_replace('%title', check_plain($item->{$export_key}), $this->plugin['strings']['confirmation'][$form_state['op']]['success']);
1021
      drupal_set_message($message);
1022
    }
1023
    else {
1024
      $message = str_replace('%title', check_plain($item->{$export_key}), $this->plugin['strings']['confirmation'][$form_state['op']]['fail']);
1025
      drupal_set_message($message, 'error');
1026
    }
1027
  }
1028

    
1029
  /**
1030
   * Provide the actual editing form.
1031
   */
1032
  public 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']);
1036

    
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']},
1043
      );
1044
    }
1045

    
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,
1053
    );
1054

    
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']),
1060
      );
1061
    }
1062

    
1063
    if ($form_state['op'] === 'edit') {
1064
      $form['info'][$export_key]['#disabled'] = TRUE;
1065
      $form['info'][$export_key]['#value'] = $item->{$export_key};
1066
    }
1067

    
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']},
1073
      );
1074
    }
1075

    
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);
1080
    }
1081

    
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;
1086

    
1087
      // Add buttons.
1088
      $form['buttons']['submit'] = array(
1089
        '#type' => 'submit',
1090
        '#value' => t('Save'),
1091
      );
1092

    
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'),
1098
      );
1099
    }
1100
  }
1101

    
1102
  /**
1103
   * Validate callback for the edit form.
1104
   */
1105
  public 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);
1109
    }
1110
  }
1111

    
1112
  /**
1113
   * Perform a final validation check before allowing the form to be
1114
   * finished.
1115
   */
1116
  public 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'];
1120
      $element = array(
1121
        '#value' => $form_state['item']->{$export_key},
1122
        '#parents' => array($export_key),
1123
      );
1124
      $form_state['plugin'] = $this->plugin;
1125
      ctools_export_ui_edit_name_validate($element, $form_state);
1126
    }
1127
  }
1128

    
1129
  /**
1130
   * Handle the submission of the edit form.
1131
   *
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.
1134
   *
1135
   * If the keys all match up to the schema, this method will not need to be
1136
   * overridden.
1137
   */
1138
  public 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);
1142
    }
1143

    
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];
1149
      }
1150
    }
1151
  }
1152

    
1153
  // ------------------------------------------------------------------------
1154
  // These methods are the API for 'other' stuff with exportables such as
1155
  // enable, disable, import, export, delete
1156

    
1157
  /**
1158
   * Callback to enable a page.
1159
   */
1160
  public function enable_page($js, $input, $item) {
1161
    return $this->set_item_state(FALSE, $js, $input, $item);
1162
  }
1163

    
1164
  /**
1165
   * Callback to disable a page.
1166
   */
1167
  public function disable_page($js, $input, $item) {
1168
    return $this->set_item_state(TRUE, $js, $input, $item);
1169
  }
1170

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

    
1180
    if (!$js) {
1181
      drupal_goto(ctools_export_ui_plugin_base_path($this->plugin));
1182
    }
1183
    else {
1184
      return $this->list_page($js, $input);
1185
    }
1186
  }
1187

    
1188
  /**
1189
   * Page callback to delete an exportable item.
1190
   */
1191
  public function delete_page($js, $input, $item) {
1192
    $form_state = array(
1193
      'plugin' => $this->plugin,
1194
      'object' => &$this,
1195
      'ajax' => $js,
1196
      'item' => $item,
1197
      'op' => $item->export_type & EXPORT_IN_CODE ? 'revert' : 'delete',
1198
      'rerender' => TRUE,
1199
      'no_redirect' => TRUE,
1200
    );
1201

    
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);
1206
    }
1207

    
1208
    return $output;
1209
  }
1210

    
1211
  /**
1212
   * Deletes exportable items from the database.
1213
   */
1214
  public function delete_form_submit(&$form_state) {
1215
    $item = $form_state['item'];
1216

    
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);
1221
  }
1222

    
1223
  /**
1224
   * Page callback to display export information for an exportable item.
1225
   */
1226
  public 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'));
1229
  }
1230

    
1231
  /**
1232
   * Page callback to import information for an exportable item.
1233
   */
1234
  public 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.
1238

    
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');
1243
    }
1244
    if (empty($item)) {
1245
      $item = ctools_export_crud_new($this->plugin['schema']);
1246
    }
1247

    
1248
    $form_state = array(
1249
      'plugin' => $this->plugin,
1250
      'object' => &$this,
1251
      'ajax' => $js,
1252
      'item' => $item,
1253
      'op' => 'add',
1254
      'form type' => 'import',
1255
      'rerender' => TRUE,
1256
      'no_redirect' => TRUE,
1257
      'step' => $step,
1258
      // Store these in case additional args are needed.
1259
      'function args' => func_get_args(),
1260
    );
1261

    
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']);
1266
    }
1267

    
1268
    return $output;
1269
  }
1270

    
1271
  /**
1272
   * Import form. Provides simple helptext instructions and textarea for
1273
   * pasting a export definition.
1274
   */
1275
  public function edit_form_import(&$form, &$form_state) {
1276
    $form['help'] = array(
1277
      '#type' => 'item',
1278
      '#value' => $this->plugin['strings']['help']['import'],
1279
    );
1280

    
1281
    $form['import'] = array(
1282
      '#title' => t('@plugin code', array('@plugin' => $this->plugin['title singular proper'])),
1283
      '#type' => 'textarea',
1284
      '#rows' => 10,
1285
      '#required' => TRUE,
1286
      '#default_value' => !empty($form_state['item']->export_ui_code) ? $form_state['item']->export_ui_code : '',
1287
    );
1288

    
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,
1293
    );
1294
  }
1295

    
1296
  /**
1297
   * Import form validate handler.
1298
   *
1299
   * Evaluates code and make sure it creates an object before we continue.
1300
   */
1301
  public 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)));
1305
      return;
1306
    }
1307

    
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'];
1311
  }
1312

    
1313
  /**
1314
   * Submit callback for import form.
1315
   *
1316
   * Stores the item in the session.
1317
   */
1318
  public 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.
1322
  }
1323
}
1324

    
1325
// -----------------------------------------------------------------------
1326
// Forms to be used with this class.
1327
//
1328
// Since Drupal's forms are completely procedural, these forms will
1329
// mostly just be pass-throughs back to the object.
1330

    
1331
/**
1332
 * Add all appropriate includes to forms so that caching the form
1333
 * still loads the files that we need.
1334
 */
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');
1338

    
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']);
1341
}
1342

    
1343
/**
1344
 * Form callback to handle the filter/sort form when listing items.
1345
 *
1346
 * This simply loads the object defined in the plugin and hands it off.
1347
 */
1348
function ctools_export_ui_list_form($form, &$form_state) {
1349
  $form_state['object']->list_form($form, $form_state);
1350
  return $form;
1351
}
1352

    
1353
/**
1354
 * Validate handler for ctools_export_ui_list_form.
1355
 */
1356
function ctools_export_ui_list_form_validate(&$form, &$form_state) {
1357
  $form_state['object']->list_form_validate($form, $form_state);
1358
}
1359

    
1360
/**
1361
 * Submit handler for ctools_export_ui_list_form.
1362
 */
1363
function ctools_export_ui_list_form_submit(&$form, &$form_state) {
1364
  $form_state['object']->list_form_submit($form, $form_state);
1365
}
1366

    
1367
/**
1368
 * Form callback to edit an exportable item.
1369
 *
1370
 * This simply loads the object defined in the plugin and hands it off.
1371
 */
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);
1376

    
1377
  $form_state['object']->edit_form($form, $form_state);
1378
  return $form;
1379
}
1380

    
1381
/**
1382
 * Validate handler for ctools_export_ui_edit_item_form.
1383
 */
1384
function ctools_export_ui_edit_item_form_validate(&$form, &$form_state) {
1385
  $form_state['object']->edit_form_validate($form, $form_state);
1386
}
1387

    
1388
/**
1389
 * Submit handler for ctools_export_ui_edit_item_form.
1390
 */
1391
function ctools_export_ui_edit_item_form_submit(&$form, &$form_state) {
1392
  $form_state['object']->edit_form_submit($form, $form_state);
1393
}
1394

    
1395
/**
1396
 * Submit handler to delete for ctools_export_ui_edit_item_form
1397
 *
1398
 * @todo Put this on a callback in the object.
1399
 */
1400
function ctools_export_ui_edit_item_form_delete(&$form, &$form_state) {
1401
  _ctools_export_ui_add_form_files($form, $form_state);
1402

    
1403
  $export_key = $form_state['plugin']['export']['key'];
1404
  $path = $form_state['item']->export_type & EXPORT_IN_CODE ? 'revert' : 'delete';
1405

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

    
1409
/**
1410
 * Validate that an export item name is acceptable and unique during add.
1411
 */
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.'));
1417
    return;
1418
  }
1419

    
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'])));
1423
  }
1424
}
1425

    
1426
/**
1427
 * Test for #machine_name type to see if an export exists.
1428
 */
1429
function ctools_export_ui_edit_name_exists($name, $element, &$form_state) {
1430
  $plugin = $form_state['plugin'];
1431

    
1432
  return (empty($form_state['item']->export_ui_allow_overwrite) && ctools_export_crud_load($plugin['schema'], $name));
1433
}
1434

    
1435
/**
1436
 * Delete/Revert confirm form.
1437
 *
1438
 * @todo -- call back into the object instead.
1439
 */
1440
function ctools_export_ui_delete_confirm_form($form, &$form_state) {
1441
  _ctools_export_ui_add_form_files($form, $form_state);
1442

    
1443
  $plugin = $form_state['plugin'];
1444
  $item = $form_state['item'];
1445

    
1446
  $form = array();
1447

    
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);
1451

    
1452
  $form = confirm_form($form,
1453
    $question,
1454
    $path,
1455
    $plugin['strings']['confirmation'][$form_state['op']]['information'],
1456
    $plugin['allowed operations'][$form_state['op']]['title'], t('Cancel')
1457
  );
1458
  return $form;
1459
}
1460

    
1461
// --------------------------------------------------------------------------
1462
// Forms and callbacks for using the edit system with the wizard.
1463

    
1464
/**
1465
 * Form callback to edit an exportable item using the wizard
1466
 *
1467
 * This simply loads the object defined in the plugin and hands it off.
1468
 */
1469
function ctools_export_ui_edit_item_wizard_form($form, &$form_state) {
1470
  _ctools_export_ui_add_form_files($form, $form_state);
1471

    
1472
  $method = 'edit_form_' . $form_state['step'];
1473
  if (!method_exists($form_state['object'], $method)) {
1474
    $method = 'edit_form';
1475
  }
1476

    
1477
  $form_state['object']->$method($form, $form_state);
1478
  return $form;
1479
}
1480

    
1481
/**
1482
 * Validate handler for ctools_export_ui_edit_item_wizard_form.
1483
 */
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';
1488
  }
1489

    
1490
  $form_state['object']->$method($form, $form_state);
1491

    
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);
1496
  }
1497
}
1498

    
1499
/**
1500
 * Submit handler for ctools_export_ui_edit_item_wizard_form.
1501
 */
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';
1506
  }
1507

    
1508
  $form_state['object']->$method($form, $form_state);
1509
}
1510

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

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

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

    
1532
/**
1533
 * Wizard 'finish' callback when using a wizard to edit an item.
1534
 */
1535
function ctools_export_ui_wizard_finish(&$form_state) {
1536
  $form_state['object']->edit_wizard_finish($form_state);
1537
}