summaryrefslogtreecommitdiff
path: root/sites/all/modules/media_browser_plus/views/media_browser_plus.views.inc
blob: a4e277cd52cdf332af2830981505ecb3ab2e0f98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php

/**
 * @file
 * Provide Views data and handlers for media.module
 */

/**
 * Implements hook_form_FORM_ID_alter() for views_exposed_form.
 *
 * Convert the media_browser_plus uri filter for filtering by file scheme into
 * a select.
 *
 * @see file_filters()
 */
function media_browser_plus_form_views_exposed_form_alter(&$form, &$form_state) {
  if ($form_state['view']->name == 'media_browser_plus' && isset($form['uri'])) {
    $visible_steam_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE);
    $options = array();
    foreach ($visible_steam_wrappers as $scheme => $information) {
      $options[$scheme] = check_plain($information['name']);
    }
    if (count($visible_steam_wrappers) > 1) {
      $form['uri'] = array(
        '#options' => array('' => '- ' . t('Any') . ' -') + $options,
        '#type' => 'select',
        '#default_value' => $form['uri']['#default_value'],
      );
    }
    else {
      // If there's only one stream wrapper hide the filter.
      unset($form['#info']['filter-uri']);
      $form['uri'] = array(
        '#type' => 'hidden',
        '#default_value' => '',
      );
    }
  }
}

/**
 * Implements hook_views_plugins().
 *
 * Generate a list of which base-tables to enabled the plugins for.
 */
function media_browser_plus_views_plugins() {
  $plugins = array();

  $path = drupal_get_path('module', 'media_browser_plus') . '/views';

  // Always allow the actual file-table
  $base = array('file_managed');
  if (module_exists('search_api')) {
    // If the Search API module exists, also allow indices of the file-entity
    // that has the fid field indexed.
    $indices = search_api_index_load_multiple(NULL);
    foreach ($indices as $machine_name => $index) {
      if ($index->item_type == 'file' && isset($index->options['fields']['fid'])) {
        $base[] = 'search_api_index_' . $machine_name;
      }
    }
  }

  // Style plugin.
  $plugins['style']['media_browser_plus'] = array(
    'title' => t('Media browser plus'),
    'help' => t('Displays rows as an HTML list including the folder management.'),
    'handler' => 'media_browser_plus_views_plugin_style_media_browser',
    'theme' => 'media_browser_plus_views_view_media_browser',
    'theme path' => $path,
    'base' => $base,
    'uses row plugin' => TRUE,
    'uses row class' => FALSE,
    'uses options' => TRUE,
    'uses fields' => TRUE,
    'type' => 'normal',
    'help topic' => 'style-media-browser',
  );
  return $plugins;
}

/**
 * Implements hook_views_data_alter().
 */
function media_browser_plus_views_data_alter(&$data) {
  $data['file_managed']['media_browser_plus_preview'] = array(
    'title' => t('Preview'),
    'group' => t('Media browser plus'),
    'help' => t('Preview of a file item as used in the media browsers.'),
    'real field' => 'fid',
    'field' => array(
      'handler' => 'media_browser_plus_views_handler_field_preview',
      'click sortable' => FALSE,
    ),
  );
  $data['file_managed']['media_browser_plus_preview_vbo'] = array(
    'title' => t('Preview with operations'),
    'group' => t('Media browser plus'),
    'help' => t('Preview of a file item as used in the media browsers.'),
    'real field' => 'fid',
    'field' => array(
      'handler' => 'media_browser_plus_views_handler_field_preview_vbo',
      'click sortable' => FALSE,
    ),
  );

  $data['file_managed']['media_browser_plus_views_handler_area_actions'] = array(
    'title' => t('Actions'),
    'group' => t('Media browser plus'),
    'help' => t('Area with different file related actions.'),
    'area' => array(
      'handler' => 'media_browser_plus_views_handler_area_actions',
    ),
  );
  $data['file_managed']['media_browser_plus_views_handler_area_basket'] = array(
    'title' => t('Basket'),
    'group' => t('Media browser plus'),
    'help' => t('Area to collect and download files.'),
    'area' => array(
      'handler' => 'media_browser_plus_views_handler_area_basket',
    ),
  );
  $data['file_managed']['media_browser_plus_views_handler_area_navigation'] = array(
    'title' => t('Navigation'),
    'group' => t('Media browser plus'),
    'help' => t('Area to navigate using the folder structure.'),
    'table' => 'field_data_field_folder',
    'area' => array(
      'handler' => 'media_browser_plus_views_handler_area_navigation',
    ),
  );
}

/**
 * Display the view as a media browser.
 *
 * @see template_preprocess_media_views_view_media_browser()
 */
function template_preprocess_media_browser_plus_views_view_media_browser(&$vars) {
  module_load_include('inc', 'media', 'includes/media.browser');

  $view = $vars['view'];
  $display_handler = $vars['view']->display_handler;
  $handler = $vars['view']->style_plugin;

  // If this is a media browser display add some more JS.
  // Re-uses vast parts of template_preprocess_media_views_view_media_browser().
  if ($display_handler instanceof media_views_plugin_display_media_browser) {
    $fids = array();
    foreach ($view->result as $index => $result) {
      $fids[$index] = $result->fid;
    }
    $files = file_load_multiple($fids);
    foreach ($files as $file) {
      // Add url/preview to the file object.
      media_browser_build_media_item($file);
    }

    // Add the files to JS so that they are accessible inside the browser.
    drupal_add_js(array('media' => array('files' => array_values($files))), 'setting');

    // Add the browser parameters to the settings and that this display exists.
    drupal_add_js(array(
      'media' => array(
        'browser' => array(
          'params' => media_get_browser_params(),
          'views' => array(
            $vars['view']->name => array(
              $vars['view']->current_display,
            ),
          ),
        ),
      ),
    ), 'setting');

    // Add media browser javascript and CSS.
    drupal_add_js(drupal_get_path('module', 'media') . '/js/plugins/media.views.js');
  }

  // Add classes and wrappers from the style plugin.
  $handler = $vars['view']->style_plugin;

  $class = explode(' ', $handler->options['class']);
  $class = array_map('drupal_clean_css_identifier', $class);

  $wrapper_class = explode(' ', $handler->options['wrapper_class']);
  $wrapper_class = array_map('drupal_clean_css_identifier', $wrapper_class);

  $vars['class'] = implode(' ', $class);
  $vars['wrapper_class'] = implode(' ', $wrapper_class);
  $vars['wrapper_prefix'] = '<div class="' . implode(' ', $wrapper_class) . '">';
  $vars['wrapper_suffix'] = '</div>';
  $vars['list_type_prefix'] = '<' . $handler->options['type'] . ' id="media-browser-library-list" class="' . implode(' ', $class) . '">';
  $vars['list_type_suffix'] = '</' . $handler->options['type'] . '>';

  // Run theming variables through a standard Views preprocess function.
  template_preprocess_views_view_unformatted($vars);

  // Now add our own stuff - based on the set options.
  // Add the js / css library.
  drupal_add_library('media_browser_plus', 'media_browser_plus');

  // Add more information for the JS integration.
  $vars['options']['view_id'] = $view->name;
  $vars['options']['view_display_id'] = $view->current_display;

  // Add settings for the js library.
  drupal_add_js(array(
    'mbp' => array(
      'views' => array(
        $view->name . ':' . $view->current_display => $vars['options'],
      ),
    )),
    'setting'
  );

  // Special handling of the navigation. We abuse a area handler to add and
  // configure the navigation - that way it's very flexible.
  // Check if a navigation handler is configured.
  $vars['folders'] = NULL;
  foreach (array('header', 'footer') as $area) {
    foreach ($display_handler->handlers[$area] as $handler) {
      if ($handler instanceof media_browser_plus_views_handler_area_navigation) {
        $vars['folders'] = $handler->render_custom();
        break(2);
      }
    }
  }
}

/**
 * Implements hook_views_invalidate_cache().
 */
function media_browser_plus_views_invalidate_cache() {
  cache_clear_all('media:browser:plus:plugin', 'cache', TRUE);
  drupal_static_reset('media_browser_plus_get_browser_plugin_info');
}