Projet

Général

Profil

Paste
Télécharger (11,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_slideshow / views_slideshow_plugin_style_slideshow.inc @ 7547bb19

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the list style plugin.
6
 */
7

    
8
/**
9
 * Style plugin to render each item in a slideshow.
10
 *
11
 * @ingroup views_style_plugins
12
 */
13
class views_slideshow_plugin_style_slideshow extends views_plugin_style {
14

    
15
  /**
16
   * Set default options.
17
   */
18
  public function option_definition() {
19
    $options = parent::option_definition();
20

    
21
    // Load all include files from views slideshow addons.
22
    module_load_all_includes('views_slideshow.inc');
23

    
24
    // Call every module using hook_views_slideshow_option_definition and merge
25
    // it's return value with the other default options.
26
    return array_merge($options, module_invoke_all('views_slideshow_option_definition'));
27
  }
28

    
29
  /**
30
   * Build the settings form for the view.
31
   */
32
  public function options_form(&$form, &$form_state) {
33
    // Include ctools dependent support.
34
    ctools_include('dependent');
35

    
36
    // Load up all views slideshow modules functions.
37
    module_load_all_includes('views_slideshow.inc');
38
    parent::options_form($form, $form_state);
39

    
40
    // Wrap all the form elements to help style the form.
41
    $form['views_slideshow_wrapper'] = array(
42
      '#markup' => '<div id="views-slideshow-form-wrapper">',
43
    );
44

    
45
    // Style.
46
    $form['slideshow_skin_header'] = array(
47
      '#markup' => '<h2>' . t('Style') . '</h2>',
48
    );
49

    
50
    // Get a list of all available skins.
51
    $skin_info = $this->views_slideshow_get_skins();
52
    foreach ($skin_info as $skin => $info) {
53
      $skins[$skin] = $info['name'];
54
    }
55
    asort($skins);
56

    
57
    // Create the drop down box so users can choose an available skin.
58
    $form['slideshow_skin'] = array(
59
      '#type' => 'select',
60
      '#title' => t('Skin'),
61
      '#options' => $skins,
62
      '#default_value' => $this->options['slideshow_skin'],
63
      '#description' => t('Select the skin to use for this display.  Skins allow for easily swappable layouts of things like next/prev links and thumbnails.  Note that not all skins support all widgets, so a combination of skins and widgets may lead to unpredictable results in layout.'),
64
    );
65

    
66
    // Slides.
67
    $form['slides_header'] = array(
68
      '#markup' => '<h2>' . t('Slides') . '</h2>',
69
    );
70

    
71
    // Get all slideshow types.
72
    $slideshows = module_invoke_all('views_slideshow_slideshow_info');
73

    
74
    if ($slideshows) {
75

    
76
      // Build our slideshow options for the form.
77
      $slideshow_options = array();
78
      foreach ($slideshows as $slideshow_id => $slideshow_info) {
79
        $slideshow_options[$slideshow_id] = $slideshow_info['name'];
80
      }
81

    
82
      $form['slideshow_type'] = array(
83
        '#type' => 'select',
84
        '#title' => t('Slideshow Type'),
85
        '#options' => $slideshow_options,
86
        '#default_value' => $this->options['slideshow_type'],
87
      );
88

    
89
      $arguments = array(
90
        &$form,
91
        &$form_state,
92
        &$this,
93
      );
94

    
95
      foreach (module_implements('views_slideshow_slideshow_type_form') as $module) {
96
        $form[$module] = array(
97
          '#type' => 'fieldset',
98
          '#title' => t('!module options', array('!module' => $slideshows[$module]['name'])),
99
          '#collapsible' => TRUE,
100
          '#attributes' => array('class' => array($module)),
101
          '#states' => array(
102
            'visible' => array(
103
              ':input[name="style_options[slideshow_type]"]' => array('value' => $module),
104
            ),
105
          ),
106
        );
107

    
108
        $function = $module . '_views_slideshow_slideshow_type_form';
109
        call_user_func_array($function, $arguments);
110
      }
111
    }
112
    else {
113
      $form['enable_module'] = array(
114
        '#markup' => t('There is no Views Slideshow plugin enabled. Go to the !modules and enable a Views Slideshow plugin module. For example Views Slideshow: Cycle.', array('!modules' => l(t('Modules Page'), 'admin/modules'))),
115
      );
116
    }
117

    
118
    // Widgets.
119
    $form['widgets_header'] = array(
120
      '#markup' => '<h2>' . t('Widgets') . '</h2>',
121
    );
122

    
123
    // Loop through all locations so we can add header for each location.
124
    $location = array('top' => t('Top'), 'bottom' => t('Bottom'));
125
    foreach ($location as $location_id => $location_name) {
126
      // Widget Header.
127
      $form['widgets'][$location_id]['header'] = array(
128
        '#markup' => '<h3>' . t('!location Widgets', array('!location' => $location_name)) . '</h3>',
129
      );
130
    }
131

    
132
    // Get all widgets that are registered.
133
    // If we have widgets then build it's form fields.
134
    $widgets = module_invoke_all('views_slideshow_widget_info');
135
    if (!empty($widgets)) {
136

    
137
      // Build our weight values by number of widgets.
138
      $weights = array();
139
      for ($i = 1; $i <= count($widgets); $i++) {
140
        $weights[$i] = $i;
141
      }
142

    
143
      // Loop through our widgets and locations to build our form values for
144
      // each widget.
145
      foreach ($widgets as $widget_id => $widget_info) {
146
        foreach ($location as $location_id => $location_name) {
147
          $widget_dependency = 'style_options[widgets][' . $location_id . '][' . $widget_id . ']';
148

    
149
          // Determine if a widget is compatible with a slideshow.
150
          $compatible_slideshows = array();
151
          foreach ($slideshows as $slideshow_id => $slideshow_info) {
152
            $is_compatible = 1;
153
            // Check if every required accept value in the widget has a
154
            // corresponding calls value in the slideshow.
155
            foreach ($widget_info['accepts'] as $accept_key => $accept_value) {
156
              if (is_array($accept_value) && !empty($accept_value['required']) && !in_array($accept_key, $slideshow_info['calls'])) {
157
                $is_compatible = 0;
158
                break;
159
              }
160
            }
161

    
162
            // No need to go through this if it's not compatible.
163
            if ($is_compatible) {
164
              // Check if every required calls value in the widget has a
165
              // corresponding accepts call.
166
              foreach ($widget_info['calls'] as $calls_key => $calls_value) {
167
                if (is_array($calls_value) && !empty($calls_value['required']) && !in_array($calls_key, $slideshow_info['accepts'])) {
168
                  $is_compatible = 0;
169
                  break;
170
                }
171
              }
172
            }
173

    
174
            // If it passed all those tests then they are compatible.
175
            if ($is_compatible) {
176
              $compatible_slideshows[] = $slideshow_id;
177
            }
178
          }
179

    
180
          // Use Widget Checkbox.
181
          $form['widgets'][$location_id][$widget_id]['enable'] = array(
182
            '#type' => 'checkbox',
183
            '#title' => $widget_info['name'],
184
            '#default_value' => $this->options['widgets'][$location_id][$widget_id]['enable'],
185
            '#description' => t('Should !name be rendered at the !location of the slides.', array('!name' => $widget_info['name'], '!location' => $location_name)),
186
          );
187

    
188
          $form['widgets'][$location_id][$widget_id]['enable']['#dependency']['edit-style-options-slideshow-type'] = !empty($compatible_slideshows) ? $compatible_slideshows : array('none');
189

    
190
          // Need to wrap this so it indents correctly.
191
          $form['widgets'][$location_id][$widget_id]['wrapper'] = array(
192
            '#markup' => '<div class="vs-dependent">',
193
          );
194

    
195
          // Widget weight
196
          // We check to see if the default value is greater than the number of
197
          // widgets just in case a widget has been removed and the form hasn't
198
          // hasn't been saved again.
199
          $form['widgets'][$location_id][$widget_id]['weight'] = array(
200
            '#type' => 'select',
201
            '#title' => t('Weight of the !name', array('!name' => $widget_info['name'])),
202
            '#default_value' => ($this->options['widgets'][$location_id][$widget_id]['weight'] > count($widgets)) ? count($widgets) : $this->options['widgets'][$location_id][$widget_id]['weight'],
203
            '#options' => $weights,
204
            '#description' => t('Determines in what order the !name appears.  A lower weight will cause the !name to be above higher weight items.', array('!name' => $widget_info['name'])),
205
            '#prefix' => '<div class="vs-dependent">',
206
            '#suffix' => '</div>',
207
            '#states' => array(
208
              'visible' => array(
209
                ':input[name="style_options[widgets][' . $location_id . '][' . $widget_id . '][enable]"]' => array('checked' => TRUE),
210
              ),
211
            ),
212
          );
213

    
214
          // Add all the widget settings.
215
          if (function_exists($widget_id . '_views_slideshow_widget_form_options')) {
216
            $arguments = array(
217
              &$form['widgets'][$location_id][$widget_id],
218
              &$form_state,
219
              &$this,
220
              $this->options['widgets'][$location_id][$widget_id],
221
              $widget_dependency,
222
            );
223
            call_user_func_array($widget_id . '_views_slideshow_widget_form_options', $arguments);
224
          }
225

    
226
          $form['widgets'][$location_id][$widget_id]['wrapper_close'] = array(
227
            '#markup' => '</div>',
228
          );
229
        }
230
      }
231
    }
232

    
233
    $form['views_slideshow_wrapper_close'] = array(
234
      '#markup' => '</div>',
235
    );
236
  }
237

    
238
  /**
239
   * Run any validation on the form settings.
240
   */
241
  public function options_validate(&$form, &$form_state) {
242
    module_load_all_includes('views_slideshow.inc');
243

    
244
    $arguments = array(
245
      &$form,
246
      &$form_state,
247
      &$this,
248
    );
249

    
250
    // Call all modules that use hook_views_slideshow_options_form_validate.
251
    foreach (module_implements('views_slideshow_options_form_validate') as $module) {
252
      $function = $module . '_views_slideshow_options_form_validate';
253
      call_user_func_array($function, $arguments);
254
    }
255
  }
256

    
257
  /**
258
   * Run any necessary actions on submit.
259
   */
260
  public function options_submit(&$form, &$form_state) {
261
    module_load_all_includes('views_slideshow.inc');
262

    
263
    $arguments = array(
264
      $form,
265
      &$form_state,
266
    );
267

    
268
    // Call all modules that use hook_views_slideshow_options_form_submit.
269
    foreach (module_implements('views_slideshow_options_form_submit') as $module) {
270
      $function = $module . '_views_slideshow_options_form_submit';
271
      call_user_func_array($function, $arguments);
272
    }
273

    
274
    // In addition to the skin, we also pre-save the definition that
275
    // correspond to it.  That lets us avoid a hook lookup on every page.
276
    $skins = $this->views_slideshow_get_skins();
277
    $form_state['values']['style_options']['skin_info'] = $skins[$form_state['values']['style_options']['slideshow_skin']];
278
  }
279

    
280
  /**
281
   * Retrieve a list of all available skins in the system.
282
   */
283
  public function views_slideshow_get_skins() {
284
    static $skins;
285

    
286
    if (empty($skins)) {
287
      $skins = array();
288

    
289
      // Call all modules that use hook_views_slideshow_skin_info.
290
      foreach (module_implements('views_slideshow_skin_info') as $module) {
291
        $skin_items = call_user_func($module . '_views_slideshow_skin_info');
292
        if (isset($skin_items) && is_array($skin_items)) {
293
          foreach (array_keys($skin_items) as $skin) {
294
            // Ensure that the definition is complete, so we don't need lots
295
            // of error checking later.
296
            $skin_items[$skin] += array(
297
              'class' => 'default',
298
              'name' => t('Untitled skin'),
299
              'module' => $module,
300
              'path' => '',
301
              'stylesheets' => array(),
302
            );
303
          }
304
          $skins = array_merge($skins, $skin_items);
305
        }
306
      }
307
    }
308

    
309
    return $skins;
310
  }
311

    
312
}