Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / plugins / export_ui / views_ui.class.php @ 5d12d676

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the CTools Export UI integration code.
6
 *
7
 * Note that this is only a partial integration.
8
 */
9

    
10
/**
11
 * CTools Export UI class handler for Views UI.
12
 */
13
class views_ui extends ctools_export_ui {
14

    
15
  /**
16
   *
17
   */
18
  function init($plugin) {
19
    // We modify the plugin info here so that we take the defaults and twiddle,
20
    // rather than completely override them.
21
    // Reset the edit path to match what we're really using.
22
    $plugin['menu']['items']['edit']['path'] = 'view/%ctools_export_ui/edit';
23
    $plugin['menu']['items']['clone']['path'] = 'view/%ctools_export_ui/clone';
24
    $plugin['menu']['items']['clone']['type'] = MENU_VISIBLE_IN_BREADCRUMB;
25
    $plugin['menu']['items']['export']['path'] = 'view/%ctools_export_ui/export';
26
    $plugin['menu']['items']['export']['type'] = MENU_VISIBLE_IN_BREADCRUMB;
27
    $plugin['menu']['items']['enable']['path'] = 'view/%ctools_export_ui/enable';
28
    $plugin['menu']['items']['disable']['path'] = 'view/%ctools_export_ui/disable';
29
    $plugin['menu']['items']['delete']['path'] = 'view/%ctools_export_ui/delete';
30
    $plugin['menu']['items']['delete']['type'] = MENU_VISIBLE_IN_BREADCRUMB;
31
    $plugin['menu']['items']['revert']['path'] = 'view/%ctools_export_ui/revert';
32
    $plugin['menu']['items']['revert']['type'] = MENU_VISIBLE_IN_BREADCRUMB;
33

    
34
    $prefix_count = count(explode('/', $plugin['menu']['menu prefix']));
35
    $plugin['menu']['items']['add-template'] = array(
36
      'path' => 'template/%/add',
37
      'title' => 'Add from template',
38
      'page callback' => 'ctools_export_ui_switcher_page',
39
      'page arguments' => array($plugin['name'], 'add_template', $prefix_count + 2),
40
      'load arguments' => array($plugin['name']),
41
      'access callback' => 'ctools_export_ui_task_access',
42
      'access arguments' => array($plugin['name'], 'add_template', $prefix_count + 2),
43
      'type' => MENU_CALLBACK,
44
    );
45

    
46
    return parent::init($plugin);
47
  }
48

    
49
  /**
50
   *
51
   */
52
  function hook_menu(&$items) {
53
    // We are using our own 'edit' still, rather than having edit on this
54
    // object (maybe in the future) so unset the edit callbacks. Store this so
55
    // we can put them back as sometimes they're needed again laster.
56
    $stored_items = $this->plugin['menu']['items'];
57
    // We leave these to make sure the operations still exist in the plugin so
58
    // that the path finder.
59
    unset($this->plugin['menu']['items']['edit']);
60
    unset($this->plugin['menu']['items']['add']);
61
    unset($this->plugin['menu']['items']['import']);
62
    unset($this->plugin['menu']['items']['edit callback']);
63

    
64
    parent::hook_menu($items);
65

    
66
    $this->plugin['menu']['items'] = $stored_items;
67
  }
68

    
69
  /**
70
   *
71
   */
72
  function load_item($item_name) {
73
    return views_ui_cache_load($item_name);
74
  }
75

    
76
  /**
77
   *
78
   */
79
  function list_form(&$form, &$form_state) {
80
    $row_class = 'container-inline';
81
    if (!variable_get('views_ui_show_listing_filters', FALSE)) {
82
      $row_class .= " element-invisible";
83
    }
84

    
85
    views_include('admin');
86

    
87
    parent::list_form($form, $form_state);
88

    
89
    // CTools only has two rows. We want four. That's why we create our own
90
    // structure.
91
    $form['bottom row']['submit']['#attributes']['class'][] = 'js-hide';
92
    $form['first row'] = array(
93
      '#prefix' => '<div class="' . $row_class . ' ctools-export-ui-row ctools-export-ui-first-row clearfix">',
94
      '#suffix' => '</div>',
95
      'search' => $form['top row']['search'],
96
      'submit' => $form['bottom row']['submit'],
97
      'reset' => $form['bottom row']['reset'],
98
    );
99
    $form['second row'] = array(
100
      '#prefix' => '<div class="' . $row_class . ' ctools-export-ui-row ctools-export-ui-second-row clearfix">',
101
      '#suffix' => '</div>',
102
      'storage' => $form['top row']['storage'],
103
      'disabled' => $form['top row']['disabled'],
104
    );
105
    $form['third row'] = array(
106
      '#prefix' => '<div class="' . $row_class . ' ctools-export-ui-row ctools-export-ui-third-row clearfix element-hidden">',
107
      '#suffix' => '</div>',
108
      'order' => $form['bottom row']['order'],
109
      'sort' => $form['bottom row']['sort'],
110
    );
111
    unset($form['top row']);
112
    unset($form['bottom row']);
113

    
114
    // Modify the look and contents of existing form elements.
115
    $form['second row']['storage']['#title'] = '';
116
    $form['second row']['storage']['#options'] = array(
117
      'all' => t('All storage'),
118
      t('Normal') => t('In database'),
119
      t('Default') => t('In code'),
120
      t('Overridden') => t('Database overriding code'),
121
    );
122
    $form['second row']['disabled']['#title'] = '';
123
    $form['second row']['disabled']['#options']['all'] = t('All status');
124
    $form['third row']['sort']['#title'] = '';
125

    
126
    // And finally, add our own.
127
    $this->bases = array();
128
    foreach (views_fetch_base_tables() as $table => $info) {
129
      $this->bases[$table] = $info['title'];
130
    }
131

    
132
    $form['second row']['base'] = array(
133
      '#type' => 'select',
134
      '#options' => array_merge(array('all' => t('All types')), $this->bases),
135
      '#default_value' => 'all',
136
      '#weight' => -1,
137
    );
138

    
139
    $tags = array();
140
    if (isset($form_state['object']->items)) {
141
      foreach ($form_state['object']->items as $name => $view) {
142
        if (!empty($view->tag)) {
143
          $view_tags = drupal_explode_tags($view->tag);
144
          foreach ($view_tags as $tag) {
145
            $tags[$tag] = $tag;
146
          }
147
        }
148
      }
149
    }
150
    asort($tags);
151

    
152
    $form['second row']['tag'] = array(
153
      '#type' => 'select',
154
      '#title' => t('Filter'),
155
      '#options' => array_merge(array('all' => t('All tags')), array('none' => t('No tags')), $tags),
156
      '#default_value' => 'all',
157
      '#weight' => -9,
158
    );
159

    
160
    $displays = array();
161
    foreach (views_fetch_plugin_data('display') as $id => $info) {
162
      if (!empty($info['admin'])) {
163
        $displays[$id] = $info['admin'];
164
      }
165
    }
166
    asort($displays);
167

    
168
    $form['second row']['display'] = array(
169
      '#type' => 'select',
170
      '#options' => array_merge(array('all' => t('All displays')), $displays),
171
      '#default_value' => 'all',
172
      '#weight' => -1,
173
    );
174
  }
175

    
176
  /**
177
   *
178
   */
179
  function list_filter($form_state, $view) {
180
    // Don't filter by tags if all is set up.
181
    if ($form_state['values']['tag'] != 'all') {
182
      // If none is selected check whether the view has a tag.
183
      if ($form_state['values']['tag'] == 'none') {
184
        return !empty($view->tag);
185
      }
186
      else {
187
        // Check whether the tag can be found in the views tag.
188
        return strpos($view->tag, $form_state['values']['tag']) === FALSE;
189
      }
190
    }
191
    if ($form_state['values']['base'] != 'all' && $form_state['values']['base'] != $view->base_table) {
192
      return TRUE;
193
    }
194

    
195
    return parent::list_filter($form_state, $view);
196
  }
197

    
198
  /**
199
   *
200
   */
201
  function list_sort_options() {
202
    return array(
203
      'disabled' => t('Enabled, name'),
204
      'name' => t('Name'),
205
      'path' => t('Path'),
206
      'tag' => t('Tag'),
207
      'storage' => t('Storage'),
208
    );
209
  }
210

    
211
  /**
212
   *
213
   */
214
  function list_build_row($view, &$form_state, $operations) {
215
    if (!empty($view->human_name)) {
216
      $title = $view->human_name;
217
    }
218
    else {
219
      $title = $view->get_title();
220
      if (empty($title)) {
221
        $title = $view->name;
222
      }
223
    }
224

    
225
    $paths = _views_ui_get_paths($view);
226
    $paths = implode(", ", $paths);
227

    
228
    $base = !empty($this->bases[$view->base_table]) ? $this->bases[$view->base_table] : t('Broken');
229

    
230
    $info = theme('views_ui_view_info', array('view' => $view, 'base' => $base));
231

    
232
    // Reorder the operations so that enable is the default action for a
233
    // templatic views.
234
    if (!empty($operations['enable'])) {
235
      $operations = array('enable' => $operations['enable']) + $operations;
236
    }
237

    
238
    // Set up sorting.
239
    switch ($form_state['values']['order']) {
240
      case 'disabled':
241
        $this->sorts[$view->name] = strtolower(empty($view->disabled) . $title);
242
        break;
243

    
244
      case 'name':
245
        $this->sorts[$view->name] = strtolower($title);
246
        break;
247

    
248
      case 'path':
249
        $this->sorts[$view->name] = strtolower($paths);
250
        break;
251

    
252
      case 'tag':
253
        $this->sorts[$view->name] = strtolower($view->tag);
254
        break;
255

    
256
      case 'storage':
257
        $this->sorts[$view->name] = strtolower($view->type . $title);
258
        break;
259
    }
260

    
261
    $theme_args = array(
262
      'links' => $operations,
263
      'attributes' => array(
264
        'class' => array(
265
          'links',
266
          'inline',
267
        ),
268
      ),
269
    );
270
    $ops = theme('links__ctools_dropbutton', $theme_args);
271

    
272
    $this->rows[$view->name] = array(
273
      'data' => array(
274
        array(
275
          'data' => $info,
276
          'class' => array('views-ui-name'),
277
        ),
278
        array(
279
          'data' => check_plain($view->description),
280
          'class' => array('views-ui-description'),
281
        ),
282
        array(
283
          'data' => check_plain($view->tag),
284
          'class' => array('views-ui-tag'),
285
        ),
286
        array(
287
          'data' => $paths,
288
          'class' => array('views-ui-path'),
289
        ),
290
        array(
291
          'data' => $ops,
292
          'class' => array('views-ui-operations'),
293
        ),
294
      ),
295
      'title' => t('Machine name: ') . check_plain($view->name),
296
      'class' => array(
297
        !empty($view->disabled) ? 'ctools-export-ui-disabled' : 'ctools-export-ui-enabled',
298
      ),
299
    );
300
  }
301

    
302
  /**
303
   *
304
   */
305
  function list_render(&$form_state) {
306
    views_include('admin');
307
    views_ui_add_admin_css();
308
    if (empty($_REQUEST['js'])) {
309
      views_ui_check_advanced_help();
310
    }
311
    drupal_add_library('system', 'jquery.bbq');
312
    views_add_js('views-list');
313

    
314
    $this->active = $form_state['values']['order'];
315
    $this->order = $form_state['values']['sort'];
316

    
317
    $query = tablesort_get_query_parameters();
318

    
319
    $header = array(
320
      $this->tablesort_link(t('View name'), 'name', 'views-ui-name'),
321
      array('data' => t('Description'), 'class' => array('views-ui-description')),
322
      $this->tablesort_link(t('Tag'), 'tag', 'views-ui-tag'),
323
      $this->tablesort_link(t('Path'), 'path', 'views-ui-path'),
324
      array('data' => t('Operations'), 'class' => array('views-ui-operations')),
325
    );
326

    
327
    $table = array(
328
      'header' => $header,
329
      'rows' => $this->rows,
330
      'empty' => t('No views match the search criteria.'),
331
      'attributes' => array('id' => 'ctools-export-ui-list-items'),
332
    );
333
    return theme('table', $table);
334
  }
335

    
336
  /**
337
   *
338
   */
339
  function tablesort_link($label, $field, $class) {
340
    $title = t('sort by @s', array('@s' => $label));
341
    $initial = 'asc';
342

    
343
    if ($this->active == $field) {
344
      $initial = ($this->order == 'asc') ? 'desc' : 'asc';
345
      $label .= theme('tablesort_indicator', array('style' => $initial));
346
    }
347

    
348
    $query['order'] = $field;
349
    $query['sort'] = $initial;
350
    $link_options = array(
351
      'html' => TRUE,
352
      'attributes' => array('title' => $title),
353
      'query' => $query,
354
    );
355
    $link = l($label, $_GET['q'], $link_options);
356
    if ($this->active == $field) {
357
      $class .= ' active';
358
    }
359

    
360
    return array('data' => $link, 'class' => $class);
361
  }
362

    
363
  /**
364
   *
365
   */
366
  function clone_page($js, $input, $item, $step = NULL) {
367
    drupal_set_title($this->get_page_title('clone', $item));
368

    
369
    $name = $item->{$this->plugin['export']['key']};
370

    
371
    $form_state = array(
372
      'plugin' => $this->plugin,
373
      'object' => &$this,
374
      'ajax' => $js,
375
      'item' => $item,
376
      'op' => 'add',
377
      'form type' => 'clone',
378
      'original name' => $name,
379
      'rerender' => TRUE,
380
      'no_redirect' => TRUE,
381
      'step' => $step,
382
      // Store these in case additional args are needed.
383
      'function args' => func_get_args(),
384
    );
385

    
386
    $output = drupal_build_form('views_ui_clone_form', $form_state);
387
    if (!empty($form_state['executed'])) {
388
      $item->name = $form_state['values']['name'];
389
      $item->human_name = $form_state['values']['human_name'];
390
      $item->vid = NULL;
391
      views_ui_cache_set($item);
392

    
393
      drupal_goto(ctools_export_ui_plugin_menu_path($this->plugin, 'edit', $item->name));
394
    }
395

    
396
    return $output;
397
  }
398

    
399
  /**
400
   *
401
   */
402
  function add_template_page($js, $input, $name, $step = NULL) {
403
    $templates = views_get_all_templates();
404

    
405
    if (empty($templates[$name])) {
406
      return MENU_NOT_FOUND;
407
    }
408

    
409
    $template = $templates[$name];
410

    
411
    // The template description probably describes the template, not the view
412
    // that will be created from it, but users aren't that likely to touch it.
413
    if (!empty($template->description)) {
414
      unset($template->description);
415
    }
416

    
417
    $template->is_template = TRUE;
418
    $template->type = t('Default');
419

    
420
    $output = $this->clone_page($js, $input, $template, $step);
421
    drupal_set_title(t('Create view from template @template', array('@template' => $template->get_human_name())));
422
    return $output;
423
  }
424

    
425
  /**
426
   *
427
   */
428
  function set_item_state($state, $js, $input, $item) {
429
    ctools_export_set_object_status($item, $state);
430
    menu_rebuild();
431

    
432
    if (!$js) {
433
      drupal_goto(ctools_export_ui_plugin_base_path($this->plugin));
434
    }
435
    else {
436
      return $this->list_page($js, $input);
437
    }
438
  }
439

    
440
  /**
441
   *
442
   */
443
  function list_page($js, $input) {
444
    // Remove filters values from session if filters are hidden.
445
    if (!variable_get('views_ui_show_listing_filters', FALSE) && isset($_SESSION['ctools_export_ui'][$this->plugin['name']])) {
446
      unset($_SESSION['ctools_export_ui'][$this->plugin['name']]);
447
    }
448

    
449
    // Wrap output in a div for CSS.
450
    $output = parent::list_page($js, $input);
451
    if (is_string($output)) {
452
      $output = '<div id="views-ui-list-page">' . $output . '</div>';
453
    }
454
    return $output;
455
  }
456

    
457
}
458

    
459
/**
460
 * Form callback to edit an exportable item using the wizard.
461
 *
462
 * This simply loads the object defined in the plugin and hands it off.
463
 */
464
function views_ui_clone_form($form, &$form_state) {
465
  $counter = 1;
466

    
467
  if (!isset($form_state['item'])) {
468
    $view = views_get_view($form_state['original name']);
469
  }
470
  else {
471
    $view = $form_state['item'];
472
  }
473
  do {
474
    if (empty($form_state['item']->is_template)) {
475
      $name = format_plural($counter, 'Clone of', 'Clone @count of') . ' ' . $view->get_human_name();
476
    }
477
    else {
478
      $name = $view->get_human_name();
479
      if ($counter > 1) {
480
        $name .= ' ' . $counter;
481
      }
482
    }
483
    $counter++;
484
    $machine_name = preg_replace('/[^a-z0-9_]+/', '_', drupal_strtolower($name));
485
  } while (ctools_export_crud_load($form_state['plugin']['schema'], $machine_name));
486

    
487
  $form['human_name'] = array(
488
    '#type' => 'textfield',
489
    '#title' => t('View name'),
490
    '#default_value' => $name,
491
    '#size' => 32,
492
    '#maxlength' => 255,
493
  );
494

    
495
  $form['name'] = array(
496
    '#title' => t('View name'),
497
    '#type' => 'machine_name',
498
    '#required' => TRUE,
499
    '#maxlength' => 128,
500
    '#size' => 128,
501
    '#machine_name' => array(
502
      'exists' => 'ctools_export_ui_edit_name_exists',
503
      'source' => array('human_name'),
504
    ),
505
  );
506

    
507
  $form['submit'] = array(
508
    '#type' => 'submit',
509
    '#value' => t('Continue'),
510
  );
511

    
512
  return $form;
513
}