Projet

Général

Profil

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

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

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

    
126
      case 'revert':
127
        return ($item->export_type & EXPORT_IN_DATABASE) && ($item->export_type & EXPORT_IN_CODE);
128

    
129
      case 'delete':
130
        return ($item->export_type & EXPORT_IN_DATABASE) && !($item->export_type & EXPORT_IN_CODE);
131

    
132
      case 'disable':
133
        return empty($item->disabled);
134

    
135
      case 'enable':
136
        return !empty($item->disabled);
137

    
138
      default:
139
        return TRUE;
140
    }
141
  }
142

    
143
  // ------------------------------------------------------------------------
144
  // These methods are the API for generating the list of exportable items.
145

    
146
  /**
147
   * Master entry point for handling a list.
148
   *
149
   * It is unlikely that a child object will need to override this method,
150
   * unless the listing mechanism is going to be highly specialized.
151
   */
152
  public function list_page($js, $input) {
153
    $this->items = ctools_export_crud_load_all($this->plugin['schema'], $js);
154

    
155
    // Respond to a reset command by clearing session and doing a drupal goto
156
    // back to the base URL.
157
    if (isset($input['op']) && $input['op'] == t('Reset')) {
158
      unset($_SESSION['ctools_export_ui'][$this->plugin['name']]);
159
      if (!$js) {
160
        drupal_goto($_GET['q']);
161
      }
162
      // Clear everything but form id, form build id and form token.
163
      $keys = array_keys($input);
164
      foreach ($keys as $id) {
165
        if (!in_array($id, array('form_id', 'form_build_id', 'form_token'))) {
166
          unset($input[$id]);
167
        }
168
      }
169
      $replace_form = TRUE;
170
    }
171

    
172
    // If there is no input, check to see if we have stored input in the
173
    // session.
174
    if (!isset($input['form_id'])) {
175
      if (isset($_SESSION['ctools_export_ui'][$this->plugin['name']]) && is_array($_SESSION['ctools_export_ui'][$this->plugin['name']])) {
176
        $input  = $_SESSION['ctools_export_ui'][$this->plugin['name']];
177
      }
178
    }
179
    else {
180
      $_SESSION['ctools_export_ui'][$this->plugin['name']] = $input;
181
      unset($_SESSION['ctools_export_ui'][$this->plugin['name']]['q']);
182
    }
183

    
184
    // This is where the form will put the output.
185
    $this->rows = array();
186
    $this->sorts = array();
187

    
188
    $form_state = array(
189
      'plugin' => $this->plugin,
190
      'input' => $input,
191
      'rerender' => TRUE,
192
      'no_redirect' => TRUE,
193
      'object' => &$this,
194
    );
195
    if (!isset($form_state['input']['form_id'])) {
196
      $form_state['input']['form_id'] = 'ctools_export_ui_list_form';
197
    }
198

    
199
    // If we do any form rendering, it's to completely replace a form on the
200
    // page, so don't let it force our ids to change.
201
    if ($js && isset($_POST['ajax_html_ids'])) {
202
      unset($_POST['ajax_html_ids']);
203
    }
204

    
205
    $form = drupal_build_form('ctools_export_ui_list_form', $form_state);
206
    $form = drupal_render($form);
207

    
208
    $output = $this->list_header($form_state) . $this->list_render($form_state) . $this->list_footer($form_state);
209

    
210
    if (!$js) {
211
      $this->list_css();
212
      return $form . $output;
213
    }
214

    
215
    $commands = array();
216
    $commands[] = ajax_command_replace('#ctools-export-ui-list-items', $output);
217
    if (!empty($replace_form)) {
218
      $commands[] = ajax_command_replace('#ctools-export-ui-list-form', $form);
219
    }
220
    print ajax_render($commands);
221
    ajax_footer();
222
  }
223

    
224
  /**
225
   * Create the filter/sort form at the top of a list of exports.
226
   *
227
   * This handles the very default conditions, and most lists are expected
228
   * to override this and call through to parent::list_form() in order to
229
   * get the base form and then modify it as necessary to add search
230
   * gadgets for custom fields.
231
   */
232
  public function list_form(&$form, &$form_state) {
233
    // This forces the form to *always* treat as submitted which is
234
    // necessary to make it work.
235
    $form['#token'] = FALSE;
236
    if (empty($form_state['input'])) {
237
      $form["#post"] = TRUE;
238
    }
239

    
240
    // Add the 'q' in if we are not using clean URLs or it can get lost when
241
    // using this kind of form.
242
    if (!variable_get('clean_url', FALSE)) {
243
      $form['q'] = array(
244
        '#type' => 'hidden',
245
        '#value' => $_GET['q'],
246
      );
247
    }
248

    
249
    $all = array('all' => t('- All -'));
250

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

    
256
    $form['bottom row'] = array(
257
      '#prefix' => '<div class="ctools-export-ui-row ctools-export-ui-bottom-row clearfix">',
258
      '#suffix' => '</div>',
259
    );
260

    
261
    $form['top row']['storage'] = array(
262
      '#type' => 'select',
263
      '#title' => t('Storage'),
264
      '#options' => $all + array(
265
        t('Normal') => t('Normal'),
266
        t('Default') => t('Default'),
267
        t('Overridden') => t('Overridden'),
268
      ),
269
      '#default_value' => 'all',
270
    );
271

    
272
    $form['top row']['disabled'] = array(
273
      '#type' => 'select',
274
      '#title' => t('Enabled'),
275
      '#options' => $all + array(
276
        '0' => t('Enabled'),
277
        '1' => t('Disabled'),
278
      ),
279
      '#default_value' => 'all',
280
    );
281

    
282
    $form['top row']['search'] = array(
283
      '#type' => 'textfield',
284
      '#title' => t('Search'),
285
    );
286

    
287
    $form['bottom row']['order'] = array(
288
      '#type' => 'select',
289
      '#title' => t('Sort by'),
290
      '#options' => $this->list_sort_options(),
291
      '#default_value' => 'disabled',
292
    );
293

    
294
    $form['bottom row']['sort'] = array(
295
      '#type' => 'select',
296
      '#title' => t('Order'),
297
      '#options' => array(
298
        'asc' => t('Up'),
299
        'desc' => t('Down'),
300
      ),
301
      '#default_value' => 'asc',
302
    );
303

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

    
311
    $form['bottom row']['reset'] = array(
312
      '#type' => 'submit',
313
      '#id' => 'ctools-export-ui-list-items-reset',
314
      '#value' => t('Reset'),
315
      '#attributes' => array('class' => array('use-ajax-submit')),
316
    );
317

    
318
    $form['#prefix'] = '<div class="clearfix">';
319
    $form['#suffix'] = '</div>';
320
    $form['#attached']['js'] = array(ctools_attach_js('auto-submit'));
321
    $form['#attached']['library'][] = array('system', 'drupal.ajax');
322
    $form['#attached']['library'][] = array('system', 'jquery.form');
323
    $form['#attributes'] = array('class' => array('ctools-auto-submit-full-form'));
324
  }
325

    
326
  /**
327
   * Validate the filter/sort form.
328
   *
329
   * It is very rare that a filter form needs validation, but if it is
330
   * needed, override this.
331
   */
332
  public function list_form_validate(&$form, &$form_state) { }
333

    
334
  /**
335
   * Submit the filter/sort form.
336
   *
337
   * This submit handler is actually responsible for building up all of the
338
   * rows that will later be rendered, since it is doing the filtering and
339
   * sorting.
340
   *
341
   * For the most part, you should not need to override this method, as the
342
   * fiddly bits call through to other functions.
343
   */
344
  public function list_form_submit(&$form, &$form_state) {
345
    // Filter and re-sort the pages.
346
    $plugin = $this->plugin;
347

    
348
    $prefix = ctools_export_ui_plugin_base_path($plugin);
349

    
350
    foreach ($this->items as $name => $item) {
351
      // Call through to the filter and see if we're going to render this
352
      // row. If it returns TRUE, then this row is filtered out.
353
      if ($this->list_filter($form_state, $item)) {
354
        continue;
355
      }
356

    
357
      $operations = $this->build_operations($item);
358

    
359
      $this->list_build_row($item, $form_state, $operations);
360
    }
361

    
362
    // Now actually sort
363
    if ($form_state['values']['sort'] == 'desc') {
364
      arsort($this->sorts);
365
    }
366
    else {
367
      asort($this->sorts);
368
    }
369

    
370
    // Nuke the original.
371
    $rows = $this->rows;
372
    $this->rows = array();
373
    // And restore.
374
    foreach ($this->sorts as $name => $title) {
375
      $this->rows[$name] = $rows[$name];
376
    }
377
  }
378

    
379
  /**
380
   * Determine if a row should be filtered out.
381
   *
382
   * This handles the default filters for the export UI list form. If you
383
   * added additional filters in list_form() then this is where you should
384
   * handle them.
385
   *
386
   * @return
387
   *   TRUE if the item should be excluded.
388
   */
389
  public function list_filter($form_state, $item) {
390
    $schema = ctools_export_get_schema($this->plugin['schema']);
391
    if ($form_state['values']['storage'] != 'all' && $form_state['values']['storage'] != $item->{$schema['export']['export type string']}) {
392
      return TRUE;
393
    }
394

    
395
    if ($form_state['values']['disabled'] != 'all' && $form_state['values']['disabled'] != !empty($item->disabled)) {
396
      return TRUE;
397
    }
398

    
399
    if ($form_state['values']['search']) {
400
      $search = strtolower($form_state['values']['search']);
401
      foreach ($this->list_search_fields() as $field) {
402
        if (strpos(strtolower($item->$field), $search) !== FALSE) {
403
          $hit = TRUE;
404
          break;
405
        }
406
      }
407
      if (empty($hit)) {
408
        return TRUE;
409
      }
410
    }
411
  }
412

    
413
  /**
414
   * Provide a list of fields to test against for the default "search" widget.
415
   *
416
   * This widget will search against whatever fields are configured here. By
417
   * default it will attempt to search against the name, title and description fields.
418
   */
419
  public function list_search_fields() {
420
    $fields = array(
421
      $this->plugin['export']['key'],
422
    );
423

    
424
    if (!empty($this->plugin['export']['admin_title'])) {
425
      $fields[] = $this->plugin['export']['admin_title'];
426
    }
427
    if (!empty($this->plugin['export']['admin_description'])) {
428
      $fields[] = $this->plugin['export']['admin_description'];
429
    }
430

    
431
    return $fields;
432
  }
433

    
434
  /**
435
   * Provide a list of sort options.
436
   *
437
   * Override this if you wish to provide more or change how these work.
438
   * The actual handling of the sorting will happen in build_row().
439
   */
440
  public function list_sort_options() {
441
    if (!empty($this->plugin['export']['admin_title'])) {
442
      $options = array(
443
        'disabled' => t('Enabled, title'),
444
        $this->plugin['export']['admin_title'] => t('Title'),
445
      );
446
    }
447
    else {
448
      $options = array(
449
        'disabled' => t('Enabled, name'),
450
      );
451
    }
452

    
453
    $options += array(
454
      'name' => t('Name'),
455
      'storage' => t('Storage'),
456
    );
457

    
458
    return $options;
459
  }
460

    
461
  /**
462
   * Add listing CSS to the page.
463
   *
464
   * Override this if you need custom CSS for your list.
465
   */
466
  public function list_css() {
467
    ctools_add_css('export-ui-list');
468
  }
469

    
470
  /**
471
   * Builds the operation links for a specific exportable item.
472
   */
473
  public function build_operations($item) {
474
    $plugin = $this->plugin;
475
    $schema = ctools_export_get_schema($plugin['schema']);
476
    $operations = $plugin['allowed operations'];
477
    $operations['import'] = FALSE;
478

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

    
496
    $allowed_operations = array();
497

    
498
    foreach ($operations as $op => $info) {
499
      if (!empty($info)) {
500
        $allowed_operations[$op] = array(
501
          'title' => $info['title'],
502
          'href' => ctools_export_ui_plugin_menu_path($plugin, $op, $item->{$this->plugin['export']['key']}),
503
        );
504
        if (!empty($info['ajax'])) {
505
          $allowed_operations[$op]['attributes'] = array('class' => array('use-ajax'));
506
        }
507
        if (!empty($info['token'])) {
508
          $allowed_operations[$op]['query'] = array('token' => drupal_get_token($op));
509
        }
510
      }
511
    }
512

    
513
    return $allowed_operations;
514
  }
515

    
516
  /**
517
   * Build a row based on the item.
518
   *
519
   * By default all of the rows are placed into a table by the render
520
   * method, so this is building up a row suitable for theme('table').
521
   * This doesn't have to be true if you override both.
522
   */
523
  public function list_build_row($item, &$form_state, $operations) {
524
    // Set up sorting
525
    $name = $item->{$this->plugin['export']['key']};
526
    $schema = ctools_export_get_schema($this->plugin['schema']);
527

    
528
    // Note: $item->{$schema['export']['export type string']} should have already been set up by export.inc so
529
    // we can use it safely.
530
    switch ($form_state['values']['order']) {
531
      case 'disabled':
532
        $this->sorts[$name] = empty($item->disabled) . $name;
533
        break;
534
      case 'title':
535
        $this->sorts[$name] = $item->{$this->plugin['export']['admin_title']};
536
        break;
537
      case 'name':
538
        $this->sorts[$name] = $name;
539
        break;
540
      case 'storage':
541
        $this->sorts[$name] = $item->{$schema['export']['export type string']} . $name;
542
        break;
543
    }
544

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

    
548
    // If we have an admin title, make it the first row.
549
    if (!empty($this->plugin['export']['admin_title'])) {
550
      $this->rows[$name]['data'][] = array('data' => check_plain($item->{$this->plugin['export']['admin_title']}), 'class' => array('ctools-export-ui-title'));
551
    }
552
    $this->rows[$name]['data'][] = array('data' => check_plain($name), 'class' => array('ctools-export-ui-name'));
553
    $this->rows[$name]['data'][] = array('data' => check_plain($item->{$schema['export']['export type string']}), 'class' => array('ctools-export-ui-storage'));
554

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

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

    
559
    // Add an automatic mouseover of the description if one exists.
560
    if (!empty($this->plugin['export']['admin_description'])) {
561
      $this->rows[$name]['title'] = $item->{$this->plugin['export']['admin_description']};
562
    }
563
  }
564

    
565
  /**
566
   * Provide the table header.
567
   *
568
   * If you've added columns via list_build_row() but are still using a
569
   * table, override this method to set up the table header.
570
   */
571
  public function list_table_header() {
572
    $header = array();
573
    if (!empty($this->plugin['export']['admin_title'])) {
574
      $header[] = array('data' => t('Title'), 'class' => array('ctools-export-ui-title'));
575
    }
576

    
577
    $header[] = array('data' => t('Name'), 'class' => array('ctools-export-ui-name'));
578
    $header[] = array('data' => t('Storage'), 'class' => array('ctools-export-ui-storage'));
579
    $header[] = array('data' => t('Operations'), 'class' => array('ctools-export-ui-operations'));
580

    
581
    return $header;
582
  }
583

    
584
  /**
585
   * Render all of the rows together.
586
   *
587
   * By default we place all of the rows in a table, and this should be the
588
   * way most lists will go.
589
   *
590
   * Whatever you do if this method is overridden, the ID is important for AJAX
591
   * so be sure it exists.
592
   */
593
  public function list_render(&$form_state) {
594
    $table = array(
595
      'header' => $this->list_table_header(),
596
      'rows' => $this->rows,
597
      'attributes' => array('id' => 'ctools-export-ui-list-items'),
598
      'empty' => $this->plugin['strings']['message']['no items'],
599
    );
600
    return theme('table', $table);
601
  }
602

    
603
  /**
604
   * Render a header to go before the list.
605
   *
606
   * This will appear after the filter/sort widgets.
607
   */
608
  public function list_header($form_state) { }
609

    
610
  /**
611
   * Render a footer to go after thie list.
612
   *
613
   * This is a good place to add additional links.
614
   */
615
  public function list_footer($form_state) { }
616

    
617
  // ------------------------------------------------------------------------
618
  // These methods are the API for adding/editing exportable items
619

    
620
  /**
621
   * Perform a drupal_goto() to the location provided by the plugin for the
622
   * operation.
623
   *
624
   * @param $op
625
   *   The operation to use. A string must exist in $this->plugin['redirect']
626
   *   for this operation.
627
   * @param $item
628
   *   The item in use; this may be necessary as item IDs are often embedded in
629
   *   redirects.
630
   */
631
  public function redirect($op, $item = NULL) {
632
    if (isset($this->plugin['redirect'][$op])) {
633
      $destination = (array) $this->plugin['redirect'][$op];
634
      if ($item) {
635
        $export_key = $this->plugin['export']['key'];
636
        $destination[0] = str_replace('%ctools_export_ui', $item->{$export_key}, $destination[0]);
637
      }
638
      call_user_func_array('drupal_goto', $destination);
639
    }
640
    else {
641
      // If the operation isn't set, fall back to the plugin's base path.
642
      drupal_goto(ctools_export_ui_plugin_base_path($this->plugin));
643
    }
644
  }
645

    
646
  public function add_page($js, $input, $step = NULL) {
647
    $args = func_get_args();
648
    drupal_set_title($this->get_page_title('add'), PASS_THROUGH);
649

    
650
    // If a step not set, they are trying to create a new item. If a step
651
    // is set, they're in the process of creating an item.
652
    if (!empty($this->plugin['use wizard']) && !empty($step)) {
653
      $item = $this->edit_cache_get(NULL, 'add');
654
    }
655
    if (empty($item)) {
656
      $item = ctools_export_crud_new($this->plugin['schema']);
657
    }
658

    
659
    $form_state = array(
660
      'plugin' => $this->plugin,
661
      'object' => &$this,
662
      'ajax' => $js,
663
      'item' => $item,
664
      'op' => 'add',
665
      'form type' => 'add',
666
      'rerender' => TRUE,
667
      'no_redirect' => TRUE,
668
      'step' => $step,
669
      // Store these in case additional args are needed.
670
      'function args' => $args,
671
    );
672

    
673
    $output = $this->edit_execute_form($form_state);
674
    if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
675
      $this->redirect($form_state['op'], $form_state['item']);
676
    }
677

    
678
    return $output;
679
  }
680

    
681
  /**
682
   * Main entry point to edit an item.
683
   */
684
  public function edit_page($js, $input, $item, $step = NULL) {
685
    $args = func_get_args();
686
    drupal_set_title($this->get_page_title('edit', $item), PASS_THROUGH);
687

    
688
    // Check to see if there is a cached item to get if we're using the wizard.
689
    if (!empty($this->plugin['use wizard'])) {
690
      $cached = $this->edit_cache_get($item, 'edit');
691
      if (!empty($cached)) {
692
        $item = $cached;
693
      }
694
    }
695

    
696
    $form_state = array(
697
      'plugin' => $this->plugin,
698
      'object' => &$this,
699
      'ajax' => $js,
700
      'item' => $item,
701
      'op' => 'edit',
702
      'form type' => 'edit',
703
      'rerender' => TRUE,
704
      'no_redirect' => TRUE,
705
      'step' => $step,
706
      // Store these in case additional args are needed.
707
      'function args' => $args,
708
    );
709

    
710
    $output = $this->edit_execute_form($form_state);
711
    if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
712
      $this->redirect($form_state['op'], $form_state['item']);
713
    }
714

    
715
    return $output;
716
  }
717

    
718
  /**
719
   * Main entry point to clone an item.
720
   */
721
  public function clone_page($js, $input, $original, $step = NULL) {
722
    $args = func_get_args();
723
    drupal_set_title($this->get_page_title('clone', $original), PASS_THROUGH);
724

    
725
    // If a step not set, they are trying to create a new clone. If a step
726
    // is set, they're in the process of cloning an item.
727
    if (!empty($this->plugin['use wizard']) && !empty($step)) {
728
      $item = $this->edit_cache_get(NULL, 'clone');
729
    }
730
    if (empty($item)) {
731
      // To make a clone of an item, we first export it and then re-import it.
732
      // Export the handler, which is a fantastic way to clean database IDs out of it.
733
      $export = ctools_export_crud_export($this->plugin['schema'], $original);
734
      $item = ctools_export_crud_import($this->plugin['schema'], $export);
735

    
736
      if (!empty($input[$this->plugin['export']['key']])) {
737
        $item->{$this->plugin['export']['key']} = $input[$this->plugin['export']['key']];
738
      }
739
      else {
740
        $item->{$this->plugin['export']['key']} = 'clone_of_' . $item->{$this->plugin['export']['key']};
741
      }
742
    }
743

    
744
    // Tabs and breadcrumb disappearing, this helps alleviate through cheating.
745
    // ...not sure this this is the best way.
746
    $trail = menu_set_active_item(ctools_export_ui_plugin_base_path($this->plugin));
747

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

    
750
    $form_state = array(
751
      'plugin' => $this->plugin,
752
      'object' => &$this,
753
      'ajax' => $js,
754
      'item' => $item,
755
      'op' => 'add',
756
      'form type' => 'clone',
757
      'original name' => $name,
758
      'rerender' => TRUE,
759
      'no_redirect' => TRUE,
760
      'step' => $step,
761
      // Store these in case additional args are needed.
762
      'function args' => $args,
763
    );
764

    
765
    $output = $this->edit_execute_form($form_state);
766
    if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
767
      $this->redirect($form_state['op'], $form_state['item']);
768
    }
769

    
770
    return $output;
771
  }
772

    
773
  /**
774
   * Execute the form.
775
   *
776
   * Add and Edit both funnel into this, but they have a few different
777
   * settings.
778
   */
779
  public function edit_execute_form(&$form_state) {
780
    if (!empty($this->plugin['use wizard'])) {
781
      return $this->edit_execute_form_wizard($form_state);
782
    }
783
    else {
784
      return $this->edit_execute_form_standard($form_state);
785
    }
786
  }
787

    
788
  /**
789
   * Execute the standard form for editing.
790
   *
791
   * By default, export UI will provide a single form for editing an object.
792
   */
793
  public function edit_execute_form_standard(&$form_state) {
794
    $output = drupal_build_form('ctools_export_ui_edit_item_form', $form_state);
795
    if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
796
      $this->edit_save_form($form_state);
797
    }
798

    
799
    return $output;
800
  }
801

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

    
819
    $export_key = $this->plugin['export']['key'];
820

    
821
    // When cloning, the name of the item being cloned is referenced in the
822
    // path, not the name of this item.
823
    if ($form_state['form type'] == 'clone') {
824
      $name = $form_state['original name'];
825
    }
826
    else {
827
      $name = $form_state['item']->{$export_key};
828
    }
829

    
830
    $form_info = !empty($this->plugin['form info']) ? $this->plugin['form info'] : array();
831
    $form_info += array(
832
      'id' => 'ctools_export_ui_edit',
833
      'path' => ctools_export_ui_plugin_menu_path($this->plugin, $form_state['form type'], $name) . '/%step',
834
      'show trail' => TRUE,
835
      'free trail' => TRUE,
836
      'show back' => $form_state['form type'] == 'add',
837
      'show return' => FALSE,
838
      'show cancel' => TRUE,
839
      'finish callback' => 'ctools_export_ui_wizard_finish',
840
      'next callback' => 'ctools_export_ui_wizard_next',
841
      'back callback' => 'ctools_export_ui_wizard_back',
842
      'cancel callback' => 'ctools_export_ui_wizard_cancel',
843
      'order' => array(),
844
      'import order' => array(
845
        'import' => t('Import code'),
846
        'settings' => t('Settings'),
847
      ),
848
    );
849

    
850
    // Set the order of forms based on the op if we have a specific one.
851
    if (isset($form_info[$form_state['form type'] . ' order'])) {
852
      $form_info['order'] = $form_info[$form_state['form type'] . ' order'];
853
    }
854

    
855
    // We have generic fallback forms we can use if they are not specified,
856
    // and they automatically delegate back to the UI object. Use these if
857
    // not specified.
858
    foreach ($form_info['order'] as $key => $title) {
859
      if (empty($form_info['forms'][$key])) {
860
        $form_info['forms'][$key] = array(
861
          'form id' => 'ctools_export_ui_edit_item_wizard_form',
862
        );
863
      }
864
    }
865

    
866
    // 'free trail' means the wizard can freely go back and form from item
867
    // via the trail and not with next/back buttons.
868
    if ($form_state['form type'] == 'add' || ($form_state['form type'] == 'import' && empty($form_state['item']->{$export_key}))) {
869
      $form_info['free trail'] = FALSE;
870
    }
871

    
872
    return $form_info;
873
  }
874

    
875
  /**
876
   * Execute the wizard for editing.
877
   *
878
   * For complex objects, sometimes a wizard is needed. This is normally
879
   * activated by setting 'use wizard' => TRUE in the plugin definition
880
   * and then creating a 'form info' array to feed the wizard the data
881
   * it needs.
882
   *
883
   * When creating this wizard, the plugin is responsible for defining all forms
884
   * that will be utilized with the wizard.
885
   *
886
   * Using 'add order' or 'edit order' can be used to ensure that add/edit order
887
   * is different.
888
   */
889
  public function edit_execute_form_wizard(&$form_state) {
890
    $form_info = $this->get_wizard_info($form_state);
891

    
892
    // If there aren't any forms set, fail.
893
    if (empty($form_info['order'])) {
894
      return MENU_NOT_FOUND;
895
    }
896

    
897
    // Figure out if this is a new instance of the wizard
898
    if (empty($form_state['step'])) {
899
      $order = array_keys($form_info['order']);
900
      $form_state['step'] = reset($order);
901
    }
902

    
903
    if (empty($form_info['order'][$form_state['step']]) && empty($form_info['forms'][$form_state['step']])) {
904
      return MENU_NOT_FOUND;
905
    }
906

    
907
    ctools_include('wizard');
908
    $output = ctools_wizard_multistep_form($form_info, $form_state['step'], $form_state);
909
    if (!empty($form_state['complete'])) {
910
      $this->edit_save_form($form_state);
911
    }
912
    elseif ($output && !empty($form_state['item']->export_ui_item_is_cached)) {
913
      // @todo this should be in the plugin strings
914
      drupal_set_message(t('You have unsaved changes. These changes will not be made permanent until you click <em>Save</em>.'), 'warning');
915
    }
916

    
917
    // Unset the executed flag if any non-wizard button was pressed. Those
918
    // buttons require special handling by whatever client is operating them.
919
    if (!empty($form_state['executed']) && empty($form_state['clicked_button']['#wizard type'])) {
920
      unset($form_state['executed']);
921
    }
922
    return $output;
923
  }
924

    
925
  /**
926
   * Wizard 'back' callback when using a wizard to edit an item.
927
   *
928
   * The wizard callback delegates this back to the object.
929
   */
930
  public function edit_wizard_back(&$form_state) {
931
    // This only exists so child implementations can use it.
932
  }
933

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

    
943
  /**
944
   * Wizard 'cancel' callback when using a wizard to edit an item.
945
   *
946
   * The wizard callback delegates this back to the object.
947
   */
948
  public function edit_wizard_cancel(&$form_state) {
949
    $this->edit_cache_clear($form_state['item'], $form_state['form type']);
950
  }
951

    
952
  /**
953
   * Wizard 'cancel' callback when using a wizard to edit an item.
954
   *
955
   * The wizard callback delegates this back to the object.
956
   */
957
  public function edit_wizard_finish(&$form_state) {
958
    $form_state['complete'] = TRUE;
959

    
960
    // If we are importing, and overwrite was selected, delete the original so
961
    // that this one writes properly.
962
    if ($form_state['form type'] == 'import' && !empty($form_state['item']->export_ui_allow_overwrite)) {
963
      ctools_export_crud_delete($this->plugin['schema'], $form_state['item']);
964
    }
965

    
966
    $this->edit_cache_clear($form_state['item'], $form_state['form type']);
967
  }
968

    
969
  /**
970
   * Retrieve the item currently being edited from the object cache.
971
   */
972
  public function edit_cache_get($item, $op = 'edit') {
973
    ctools_include('object-cache');
974
    if (is_string($item)) {
975
      $name = $item;
976
    }
977
    else {
978
      $name = $this->edit_cache_get_key($item, $op);
979
    }
980

    
981
    $cache = ctools_object_cache_get('ctui_' . $this->plugin['name'], $name);
982
    if ($cache) {
983
      $cache->export_ui_item_is_cached = TRUE;
984
      return $cache;
985
    }
986
  }
987

    
988
  /**
989
   * Cache the item currently currently being edited.
990
   */
991
  public function edit_cache_set($item, $op = 'edit') {
992
    ctools_include('object-cache');
993
    $name = $this->edit_cache_get_key($item, $op);
994
    return $this->edit_cache_set_key($item, $name);
995
  }
996

    
997
  public function edit_cache_set_key($item, $name) {
998
    return ctools_object_cache_set('ctui_' . $this->plugin['name'], $name, $item);
999
  }
1000

    
1001
  /**
1002
   * Clear the object cache for the currently edited item.
1003
   */
1004
  public function edit_cache_clear($item, $op = 'edit') {
1005
    ctools_include('object-cache');
1006
    $name = $this->edit_cache_get_key($item, $op);
1007
    return ctools_object_cache_clear('ctui_' . $this->plugin['name'], $name);
1008
  }
1009

    
1010
  /**
1011
   * Figure out what the cache key is for this object.
1012
   */
1013
  public function edit_cache_get_key($item, $op) {
1014
    $export_key = $this->plugin['export']['key'];
1015
    return $op == 'edit' ? $item->{$this->plugin['export']['key']} : "::$op";
1016
  }
1017

    
1018
  /**
1019
   * Called to save the final product from the edit form.
1020
   */
1021
  public function edit_save_form($form_state) {
1022
    $item = &$form_state['item'];
1023
    $export_key = $this->plugin['export']['key'];
1024

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

    
1027
    if ($result) {
1028
      $message = str_replace('%title', check_plain($item->{$export_key}), $this->plugin['strings']['confirmation'][$form_state['op']]['success']);
1029
      drupal_set_message($message);
1030
    }
1031
    else {
1032
      $message = str_replace('%title', check_plain($item->{$export_key}), $this->plugin['strings']['confirmation'][$form_state['op']]['fail']);
1033
      drupal_set_message($message, 'error');
1034
    }
1035
  }
1036

    
1037
  /**
1038
   * Provide the actual editing form.
1039
   */
1040
  public function edit_form(&$form, &$form_state) {
1041
    $export_key = $this->plugin['export']['key'];
1042
    $item = $form_state['item'];
1043
    $schema = ctools_export_get_schema($this->plugin['schema']);
1044

    
1045
    if (!empty($this->plugin['export']['admin_title'])) {
1046
      $form['info'][$this->plugin['export']['admin_title']] = array(
1047
        '#type' => 'textfield',
1048
        '#title' => t('Administrative title'),
1049
        '#description' => t('This will appear in the administrative interface to easily identify it.'),
1050
        '#default_value' => $item->{$this->plugin['export']['admin_title']},
1051
      );
1052
    }
1053

    
1054
    $form['info'][$export_key] = array(
1055
      '#title' => t($schema['export']['key name']),
1056
      '#type' => 'textfield',
1057
      '#default_value' => $item->{$export_key},
1058
      '#description' => t('The unique ID for this @export.', array('@export' => $this->plugin['title singular'])),
1059
      '#required' => TRUE,
1060
      '#maxlength' => 255,
1061
    );
1062

    
1063
    if (!empty($this->plugin['export']['admin_title'])) {
1064
      $form['info'][$export_key]['#type'] = 'machine_name';
1065
      $form['info'][$export_key]['#machine_name'] = array(
1066
        'exists' => 'ctools_export_ui_edit_name_exists',
1067
        'source' => array('info', $this->plugin['export']['admin_title']),
1068
      );
1069
    }
1070

    
1071
    if ($form_state['op'] === 'edit') {
1072
      $form['info'][$export_key]['#disabled'] = TRUE;
1073
      $form['info'][$export_key]['#value'] = $item->{$export_key};
1074
    }
1075

    
1076
    if (!empty($this->plugin['export']['admin_description'])) {
1077
      $form['info'][$this->plugin['export']['admin_description']] = array(
1078
        '#type' => 'textarea',
1079
        '#title' => t('Administrative description'),
1080
        '#default_value' => $item->{$this->plugin['export']['admin_description']},
1081
      );
1082
    }
1083

    
1084
    // Add plugin's form definitions.
1085
    if (!empty($this->plugin['form']['settings'])) {
1086
      // Pass $form by reference.
1087
      $this->plugin['form']['settings']($form, $form_state);
1088
    }
1089

    
1090
    // Add the buttons if the wizard is not in use.
1091
    if (empty($form_state['form_info'])) {
1092
      // Make sure that whatever happens, the buttons go to the bottom.
1093
      $form['buttons']['#weight'] = 100;
1094

    
1095
      // Add buttons.
1096
      $form['buttons']['submit'] = array(
1097
        '#type' => 'submit',
1098
        '#value' => t('Save'),
1099
      );
1100

    
1101
      $form['buttons']['delete'] = array(
1102
        '#type' => 'submit',
1103
        '#value' => $item->export_type & EXPORT_IN_CODE ? t('Revert') : t('Delete'),
1104
        '#access' => $form_state['op'] === 'edit' && $item->export_type & EXPORT_IN_DATABASE,
1105
        '#submit' => array('ctools_export_ui_edit_item_form_delete'),
1106
      );
1107
    }
1108
  }
1109

    
1110
  /**
1111
   * Validate callback for the edit form.
1112
   */
1113
  public function edit_form_validate(&$form, &$form_state) {
1114
    if (!empty($this->plugin['form']['validate'])) {
1115
      // Pass $form by reference.
1116
      $this->plugin['form']['validate']($form, $form_state);
1117
    }
1118
  }
1119

    
1120
  /**
1121
   * Perform a final validation check before allowing the form to be
1122
   * finished.
1123
   */
1124
  public function edit_finish_validate(&$form, &$form_state) {
1125
    if ($form_state['op'] != 'edit') {
1126
      // Validate the export key. Fake an element for form_error().
1127
      $export_key = $this->plugin['export']['key'];
1128
      $element = array(
1129
        '#value' => $form_state['item']->{$export_key},
1130
        '#parents' => array($export_key),
1131
      );
1132
      $form_state['plugin'] = $this->plugin;
1133
      ctools_export_ui_edit_name_validate($element, $form_state);
1134
    }
1135
  }
1136

    
1137
  /**
1138
   * Handle the submission of the edit form.
1139
   *
1140
   * At this point, submission is successful. Our only responsibility is
1141
   * to copy anything out of values onto the item that we are able to edit.
1142
   *
1143
   * If the keys all match up to the schema, this method will not need to be
1144
   * overridden.
1145
   */
1146
  public function edit_form_submit(&$form, &$form_state) {
1147
    if (!empty($this->plugin['form']['submit'])) {
1148
      // Pass $form by reference.
1149
      $this->plugin['form']['submit']($form, $form_state);
1150
    }
1151

    
1152
    // Transfer data from the form to the $item based upon schema values.
1153
    $schema = ctools_export_get_schema($this->plugin['schema']);
1154
    foreach (array_keys($schema['fields']) as $key) {
1155
      if(isset($form_state['values'][$key])) {
1156
        $form_state['item']->{$key} = $form_state['values'][$key];
1157
      }
1158
    }
1159
  }
1160

    
1161
  // ------------------------------------------------------------------------
1162
  // These methods are the API for 'other' stuff with exportables such as
1163
  // enable, disable, import, export, delete
1164

    
1165
  /**
1166
   * Callback to enable a page.
1167
   */
1168
  public function enable_page($js, $input, $item) {
1169
    return $this->set_item_state(FALSE, $js, $input, $item);
1170
  }
1171

    
1172
  /**
1173
   * Callback to disable a page.
1174
   */
1175
  public function disable_page($js, $input, $item) {
1176
    return $this->set_item_state(TRUE, $js, $input, $item);
1177
  }
1178

    
1179
  /**
1180
   * Set an item's state to enabled or disabled and output to user.
1181
   *
1182
   * If javascript is in use, this will rebuild the list and send that back
1183
   * as though the filter form had been executed.
1184
   */
1185
  public function set_item_state($state, $js, $input, $item) {
1186
    ctools_export_crud_set_status($this->plugin['schema'], $item, $state);
1187

    
1188
    if (!$js) {
1189
      drupal_goto(ctools_export_ui_plugin_base_path($this->plugin));
1190
    }
1191
    else {
1192
      return $this->list_page($js, $input);
1193
    }
1194
  }
1195

    
1196
  /**
1197
   * Page callback to delete an exportable item.
1198
   */
1199
  public function delete_page($js, $input, $item) {
1200
    $form_state = array(
1201
      'plugin' => $this->plugin,
1202
      'object' => &$this,
1203
      'ajax' => $js,
1204
      'item' => $item,
1205
      'op' => $item->export_type & EXPORT_IN_CODE ? 'revert' : 'delete',
1206
      'rerender' => TRUE,
1207
      'no_redirect' => TRUE,
1208
    );
1209

    
1210
    $output = drupal_build_form('ctools_export_ui_delete_confirm_form', $form_state);
1211
    if (!empty($form_state['executed'])) {
1212
      $this->delete_form_submit($form_state);
1213
      $this->redirect($form_state['op'], $item);
1214
    }
1215

    
1216
    return $output;
1217
  }
1218

    
1219
  /**
1220
   * Deletes exportable items from the database.
1221
   */
1222
  public function delete_form_submit(&$form_state) {
1223
    $item = $form_state['item'];
1224

    
1225
    ctools_export_crud_delete($this->plugin['schema'], $item);
1226
    $export_key = $this->plugin['export']['key'];
1227
    $message = str_replace('%title', check_plain($item->{$export_key}), $this->plugin['strings']['confirmation'][$form_state['op']]['success']);
1228
    drupal_set_message($message);
1229
  }
1230

    
1231
  /**
1232
   * Page callback to display export information for an exportable item.
1233
   */
1234
  public function export_page($js, $input, $item) {
1235
    drupal_set_title($this->get_page_title('export', $item), PASS_THROUGH);
1236
    return drupal_get_form('ctools_export_form', ctools_export_crud_export($this->plugin['schema'], $item), t('Export'));
1237
  }
1238

    
1239
  /**
1240
   * Page callback to import information for an exportable item.
1241
   */
1242
  public function import_page($js, $input, $step = NULL) {
1243
    $args = func_get_args();
1244
    drupal_set_title($this->get_page_title('import'), PASS_THROUGH);
1245
    // Import is basically a multi step wizard form, so let's go ahead and
1246
    // use CTools' wizard.inc for it.
1247

    
1248
    // If a step not set, they are trying to create a new item. If a step
1249
    // is set, they're in the process of creating an item.
1250
    if (!empty($step)) {
1251
      $item = $this->edit_cache_get(NULL, 'import');
1252
    }
1253
    if (empty($item)) {
1254
      $item = ctools_export_crud_new($this->plugin['schema']);
1255
    }
1256

    
1257
    $form_state = array(
1258
      'plugin' => $this->plugin,
1259
      'object' => &$this,
1260
      'ajax' => $js,
1261
      'item' => $item,
1262
      'op' => 'add',
1263
      'form type' => 'import',
1264
      'rerender' => TRUE,
1265
      'no_redirect' => TRUE,
1266
      'step' => $step,
1267
      // Store these in case additional args are needed.
1268
      'function args' => $args,
1269
    );
1270

    
1271
    // import always uses the wizard.
1272
    $output = $this->edit_execute_form_wizard($form_state);
1273
    if (!empty($form_state['executed'])) {
1274
      $this->redirect($form_state['op'], $form_state['item']);
1275
    }
1276

    
1277
    return $output;
1278
  }
1279

    
1280
  /**
1281
   * Import form. Provides simple helptext instructions and textarea for
1282
   * pasting a export definition.
1283
   */
1284
  public function edit_form_import(&$form, &$form_state) {
1285
    $form['help'] = array(
1286
      '#type' => 'item',
1287
      '#value' => $this->plugin['strings']['help']['import'],
1288
    );
1289

    
1290
    $form['import'] = array(
1291
      '#title' => t('@plugin code', array('@plugin' => $this->plugin['title singular proper'])),
1292
      '#type' => 'textarea',
1293
      '#rows' => 10,
1294
      '#required' => TRUE,
1295
      '#default_value' => !empty($form_state['item']->export_ui_code) ? $form_state['item']->export_ui_code : '',
1296
    );
1297

    
1298
    $form['overwrite'] = array(
1299
      '#title' => t('Allow import to overwrite an existing record.'),
1300
      '#type' => 'checkbox',
1301
      '#default_value' => !empty($form_state['item']->export_ui_allow_overwrite) ? $form_state['item']->export_ui_allow_overwrite : FALSE,
1302
    );
1303
  }
1304

    
1305
  /**
1306
   * Import form validate handler.
1307
   *
1308
   * Evaluates code and make sure it creates an object before we continue.
1309
   */
1310
  public function edit_form_import_validate($form, &$form_state) {
1311
    $item = ctools_export_crud_import($this->plugin['schema'], $form_state['values']['import']);
1312
    if (is_string($item)) {
1313
      form_error($form['import'], t('Unable to get an import from the code. Errors reported: @errors', array('@errors' => $item)));
1314
      return;
1315
    }
1316

    
1317
    $form_state['item'] = $item;
1318
    $form_state['item']->export_ui_allow_overwrite = $form_state['values']['overwrite'];
1319
    $form_state['item']->export_ui_code = $form_state['values']['import'];
1320
  }
1321

    
1322
  /**
1323
   * Submit callback for import form.
1324
   *
1325
   * Stores the item in the session.
1326
   */
1327
  public function edit_form_import_submit($form, &$form_state) {
1328
    // The validate function already imported and stored the item. This
1329
    // function exists simply to prevent it from going to the default
1330
    // edit_form_submit() method.
1331
  }
1332
}
1333

    
1334
// -----------------------------------------------------------------------
1335
// Forms to be used with this class.
1336
//
1337
// Since Drupal's forms are completely procedural, these forms will
1338
// mostly just be pass-throughs back to the object.
1339

    
1340
/**
1341
 * Add all appropriate includes to forms so that caching the form
1342
 * still loads the files that we need.
1343
 */
1344
function _ctools_export_ui_add_form_files($form, &$form_state) {
1345
  ctools_form_include($form_state, 'export');
1346
  ctools_form_include($form_state, 'export-ui');
1347

    
1348
  // Also make sure the plugin .inc file is loaded.
1349
  ctools_form_include_file($form_state, $form_state['object']->plugin['path'] . '/' . $form_state['object']->plugin['file']);
1350
}
1351

    
1352
/**
1353
 * Form callback to handle the filter/sort form when listing items.
1354
 *
1355
 * This simply loads the object defined in the plugin and hands it off.
1356
 */
1357
function ctools_export_ui_list_form($form, &$form_state) {
1358
  $form_state['object']->list_form($form, $form_state);
1359
  return $form;
1360
}
1361

    
1362
/**
1363
 * Validate handler for ctools_export_ui_list_form.
1364
 */
1365
function ctools_export_ui_list_form_validate(&$form, &$form_state) {
1366
  $form_state['object']->list_form_validate($form, $form_state);
1367
}
1368

    
1369
/**
1370
 * Submit handler for ctools_export_ui_list_form.
1371
 */
1372
function ctools_export_ui_list_form_submit(&$form, &$form_state) {
1373
  $form_state['object']->list_form_submit($form, $form_state);
1374
}
1375

    
1376
/**
1377
 * Form callback to edit an exportable item.
1378
 *
1379
 * This simply loads the object defined in the plugin and hands it off.
1380
 */
1381
function ctools_export_ui_edit_item_form($form, &$form_state) {
1382
  // When called using #ajax via ajax_form_callback(), 'export' may
1383
  // not be included so include it here.
1384
  _ctools_export_ui_add_form_files($form, $form_state);
1385

    
1386
  $form_state['object']->edit_form($form, $form_state);
1387
  return $form;
1388
}
1389

    
1390
/**
1391
 * Validate handler for ctools_export_ui_edit_item_form.
1392
 */
1393
function ctools_export_ui_edit_item_form_validate(&$form, &$form_state) {
1394
  $form_state['object']->edit_form_validate($form, $form_state);
1395
}
1396

    
1397
/**
1398
 * Submit handler for ctools_export_ui_edit_item_form.
1399
 */
1400
function ctools_export_ui_edit_item_form_submit(&$form, &$form_state) {
1401
  $form_state['object']->edit_form_submit($form, $form_state);
1402
}
1403

    
1404
/**
1405
 * Submit handler to delete for ctools_export_ui_edit_item_form
1406
 *
1407
 * @todo Put this on a callback in the object.
1408
 */
1409
function ctools_export_ui_edit_item_form_delete(&$form, &$form_state) {
1410
  _ctools_export_ui_add_form_files($form, $form_state);
1411

    
1412
  $export_key = $form_state['plugin']['export']['key'];
1413
  $path = $form_state['item']->export_type & EXPORT_IN_CODE ? 'revert' : 'delete';
1414

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

    
1418
/**
1419
 * Validate that an export item name is acceptable and unique during add.
1420
 */
1421
function ctools_export_ui_edit_name_validate($element, &$form_state) {
1422
  $plugin = $form_state['plugin'];
1423
  // Check for string identifier sanity
1424
  if (!preg_match('!^[a-z0-9_]+$!', $element['#value'])) {
1425
    form_error($element, t('The export id can only consist of lowercase letters, underscores, and numbers.'));
1426
    return;
1427
  }
1428

    
1429
  // Check for name collision
1430
  if (empty($form_state['item']->export_ui_allow_overwrite) && $exists = ctools_export_crud_load($plugin['schema'], $element['#value'])) {
1431
    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'])));
1432
  }
1433
}
1434

    
1435
/**
1436
 * Test for #machine_name type to see if an export exists.
1437
 */
1438
function ctools_export_ui_edit_name_exists($name, $element, &$form_state) {
1439
  $plugin = $form_state['plugin'];
1440

    
1441
  return (empty($form_state['item']->export_ui_allow_overwrite) && ctools_export_crud_load($plugin['schema'], $name));
1442
}
1443

    
1444
/**
1445
 * Delete/Revert confirm form.
1446
 *
1447
 * @todo -- call back into the object instead.
1448
 */
1449
function ctools_export_ui_delete_confirm_form($form, &$form_state) {
1450
  _ctools_export_ui_add_form_files($form, $form_state);
1451

    
1452
  $plugin = $form_state['plugin'];
1453
  $item = $form_state['item'];
1454

    
1455
  $form = array();
1456

    
1457
  $export_key = $plugin['export']['key'];
1458
  $question = str_replace('%title', check_plain($item->{$export_key}), $plugin['strings']['confirmation'][$form_state['op']]['question']);
1459
  $path = (!empty($_REQUEST['cancel_path']) && !url_is_external($_REQUEST['cancel_path'])) ? $_REQUEST['cancel_path'] : ctools_export_ui_plugin_base_path($plugin);
1460

    
1461
  $form = confirm_form($form,
1462
    $question,
1463
    $path,
1464
    $plugin['strings']['confirmation'][$form_state['op']]['information'],
1465
    $plugin['allowed operations'][$form_state['op']]['title'], t('Cancel')
1466
  );
1467
  return $form;
1468
}
1469

    
1470
// --------------------------------------------------------------------------
1471
// Forms and callbacks for using the edit system with the wizard.
1472

    
1473
/**
1474
 * Form callback to edit an exportable item using the wizard
1475
 *
1476
 * This simply loads the object defined in the plugin and hands it off.
1477
 */
1478
function ctools_export_ui_edit_item_wizard_form($form, &$form_state) {
1479
  _ctools_export_ui_add_form_files($form, $form_state);
1480

    
1481
  $method = 'edit_form_' . $form_state['step'];
1482
  if (!method_exists($form_state['object'], $method)) {
1483
    $method = 'edit_form';
1484
  }
1485

    
1486
  $form_state['object']->$method($form, $form_state);
1487
  return $form;
1488
}
1489

    
1490
/**
1491
 * Validate handler for ctools_export_ui_edit_item_wizard_form.
1492
 */
1493
function ctools_export_ui_edit_item_wizard_form_validate(&$form, &$form_state) {
1494
  $method = 'edit_form_' . $form_state['step'] . '_validate';
1495
  if (!method_exists($form_state['object'], $method)) {
1496
    $method = 'edit_form_validate';
1497
  }
1498

    
1499
  $form_state['object']->$method($form, $form_state);
1500

    
1501
  // Additionally, if there were no errors from that, and we're finishing,
1502
  // perform a final validate to make sure everything is ok.
1503
  if (isset($form_state['clicked_button']['#wizard type']) && $form_state['clicked_button']['#wizard type'] == 'finish' && !form_get_errors()) {
1504
    $form_state['object']->edit_finish_validate($form, $form_state);
1505
  }
1506
}
1507

    
1508
/**
1509
 * Submit handler for ctools_export_ui_edit_item_wizard_form.
1510
 */
1511
function ctools_export_ui_edit_item_wizard_form_submit(&$form, &$form_state) {
1512
  $method = 'edit_form_' . $form_state['step'] . '_submit';
1513
  if (!method_exists($form_state['object'], $method)) {
1514
    $method = 'edit_form_submit';
1515
  }
1516

    
1517
  $form_state['object']->$method($form, $form_state);
1518
}
1519

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

    
1527
/**
1528
 * Wizard 'next' callback when using a wizard to edit an item.
1529
 */
1530
function ctools_export_ui_wizard_next(&$form_state) {
1531
  $form_state['object']->edit_wizard_next($form_state);
1532
}
1533

    
1534
/**
1535
 * Wizard 'cancel' callback when using a wizard to edit an item.
1536
 */
1537
function ctools_export_ui_wizard_cancel(&$form_state) {
1538
  $form_state['object']->edit_wizard_cancel($form_state);
1539
}
1540

    
1541
/**
1542
 * Wizard 'finish' callback when using a wizard to edit an item.
1543
 */
1544
function ctools_export_ui_wizard_finish(&$form_state) {
1545
  $form_state['object']->edit_wizard_finish($form_state);
1546
}