Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views_slideshow / views_slideshow_plugin_style_slideshow.inc @ b433176d

1
<?php
2

    
3
/**
4
 * Style plugin to render each item in a slideshow.
5
 */
6
class views_slideshow_plugin_style_slideshow extends views_plugin_style {
7

    
8
  /**
9
   * Set default options.
10
   */
11
  public function option_definition() {
12
    $options = parent::option_definition();
13

    
14
    // Load all include files from views slideshow addons.
15
    module_load_all_includes('views_slideshow.inc');
16

    
17
    // Call every module using hook_views_slideshow_option_definition and merge
18
    // it's return value with the other default options.
19
    return array_merge($options, module_invoke_all('views_slideshow_option_definition'));
20
  }
21

    
22
  /**
23
   * Build the settings form for the view.
24
   */
25
  public function options_form(&$form, &$form_state) {
26
    // Include ctools dependent support.
27
    ctools_include('dependent');
28

    
29
    // Load up all views slideshow modules functions.
30
    module_load_all_includes('views_slideshow.inc');
31
    parent::options_form($form, $form_state);
32

    
33
    // Wrap all the form elements to help style the form.
34
    $form['views_slideshow_wrapper'] = array(
35
      '#markup' => '<div id="views-slideshow-form-wrapper">',
36
    );
37

    
38
    // Style.
39
    $form['slideshow_skin_header'] = array(
40
      '#markup' => '<h2>' . t('Style') . '</h2>',
41
    );
42

    
43
    // Get a list of all available skins.
44
    $skin_info = $this->views_slideshow_get_skins();
45
    foreach ($skin_info as $skin => $info) {
46
      $skins[$skin] = $info['name'];
47
    }
48
    asort($skins);
49

    
50
    // Create the drop down box so users can choose an available skin.
51
    $form['slideshow_skin'] = array(
52
      '#type' => 'select',
53
      '#title' => t('Skin'),
54
      '#options' => $skins,
55
      '#default_value' => $this->options['slideshow_skin'],
56
      '#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.'),
57
    );
58

    
59
    // Slides.
60
    $form['slides_header'] = array(
61
      '#markup' => '<h2>' . t('Slides') . '</h2>',
62
    );
63

    
64
    // Get all slideshow types.
65
    $slideshows = module_invoke_all('views_slideshow_slideshow_info');
66

    
67
    if ($slideshows) {
68

    
69
      // Build our slideshow options for the form.
70
      $slideshow_options = array();
71
      foreach ($slideshows as $slideshow_id => $slideshow_info) {
72
        $slideshow_options[$slideshow_id] = $slideshow_info['name'];
73
      }
74

    
75
      $form['slideshow_type'] = array(
76
        '#type' => 'select',
77
        '#title' => t('Slideshow Type'),
78
        '#options' => $slideshow_options,
79
        '#default_value' => $this->options['slideshow_type'],
80
      );
81

    
82
      $arguments = array(
83
        &$form,
84
        &$form_state,
85
        &$this,
86
      );
87

    
88
      foreach (module_implements('views_slideshow_slideshow_type_form') as $module) {
89
        $form[$module] = array(
90
          '#type' => 'fieldset',
91
          '#title' => t('!module options', array('!module' => $slideshows[$module]['name'])),
92
          '#collapsible' => TRUE,
93
          '#attributes' => array('class' => array($module)),
94
          '#states' => array(
95
            'visible' => array(
96
              ':input[name="style_options[slideshow_type]"]' => array('value' => $module),
97
            ),
98
          ),
99
        );
100

    
101
        $function = $module . '_views_slideshow_slideshow_type_form';
102
        call_user_func_array($function, $arguments);
103
      }
104
    }
105
    else {
106
      $form['enable_module'] = array(
107
        '#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'))),
108
      );
109
    }
110

    
111
    // Widgets.
112
    $form['widgets_header'] = array(
113
      '#markup' => '<h2>' . t('Widgets') . '</h2>',
114
    );
115

    
116
    // Loop through all locations so we can add header for each location.
117
    $location = array('top' => t('Top'), 'bottom' => t('Bottom'));
118
    foreach ($location as $location_id => $location_name) {
119
      // Widget Header.
120
      $form['widgets'][$location_id]['header'] = array(
121
        '#markup' => '<h3>' . t('!location Widgets', array('!location' => $location_name)) . '</h3>',
122
      );
123
    }
124

    
125
    // Get all widgets that are registered.
126
    // If we have widgets then build it's form fields.
127
    $widgets = module_invoke_all('views_slideshow_widget_info');
128
    if (!empty($widgets)) {
129

    
130
      // Build our weight values by number of widgets.
131
      $weights = array();
132
      for ($i = 1; $i <= count($widgets); $i++) {
133
        $weights[$i] = $i;
134
      }
135

    
136
      // Loop through our widgets and locations to build our form values for
137
      // each widget.
138
      foreach ($widgets as $widget_id => $widget_info) {
139
        foreach ($location as $location_id => $location_name) {
140
          $widget_dependency = 'style_options[widgets][' . $location_id . '][' . $widget_id . ']';
141

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

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

    
167
            // If it passed all those tests then they are compatible.
168
            if ($is_compatible) {
169
              $compatible_slideshows[] = $slideshow_id;
170
            }
171
          }
172

    
173
          // Use Widget Checkbox.
174
          $form['widgets'][$location_id][$widget_id]['enable'] = array(
175
            '#type' => 'checkbox',
176
            '#title' => $widget_info['name'],
177
            '#default_value' => $this->options['widgets'][$location_id][$widget_id]['enable'],
178
            '#description' => t('Should !name be rendered at the !location of the slides.', array('!name' => $widget_info['name'], '!location' => $location_name)),
179
          );
180

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

    
183
          // Need to wrap this so it indents correctly.
184
          $form['widgets'][$location_id][$widget_id]['wrapper'] = array(
185
            '#markup' => '<div class="vs-dependent">',
186
          );
187

    
188
          // Widget weight
189
          // We check to see if the default value is greater than the number of
190
          // widgets just in case a widget has been removed and the form hasn't
191
          // hasn't been saved again.
192
          $form['widgets'][$location_id][$widget_id]['weight'] = array(
193
            '#type' => 'select',
194
            '#title' => t('Weight of the !name', array('!name' => $widget_info['name'])),
195
            '#default_value' => ($this->options['widgets'][$location_id][$widget_id]['weight'] > count($widgets)) ? count($widgets) : $this->options['widgets'][$location_id][$widget_id]['weight'],
196
            '#options' => $weights,
197
            '#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'])),
198
            '#prefix' => '<div class="vs-dependent">',
199
            '#suffix' => '</div>',
200
            '#states' => array(
201
              'visible' => array(
202
                ':input[name="style_options[widgets][' . $location_id . '][' . $widget_id . '][enable]"]' => array('checked' => TRUE),
203
              ),
204
            ),
205
          );
206

    
207
          // Add all the widget settings.
208
          if (function_exists($widget_id . '_views_slideshow_widget_form_options')) {
209
            $arguments = array(
210
              &$form['widgets'][$location_id][$widget_id],
211
              &$form_state,
212
              &$this,
213
              $this->options['widgets'][$location_id][$widget_id],
214
              $widget_dependency,
215
            );
216
            call_user_func_array($widget_id . '_views_slideshow_widget_form_options', $arguments);
217
          }
218

    
219
          $form['widgets'][$location_id][$widget_id]['wrapper_close'] = array(
220
            '#markup' => '</div>',
221
          );
222
        }
223
      }
224
    }
225

    
226
    $form['views_slideshow_wrapper_close'] = array(
227
      '#markup' => '</div>',
228
    );
229
  }
230

    
231
  /**
232
   * Run any validation on the form settings.
233
   */
234
  public function options_validate(&$form, &$form_state) {
235
    module_load_all_includes('views_slideshow.inc');
236

    
237
    $arguments = array(
238
      &$form,
239
      &$form_state,
240
      &$this,
241
    );
242

    
243
    // Call all modules that use hook_views_slideshow_options_form_validate.
244
    foreach (module_implements('views_slideshow_options_form_validate') as $module) {
245
      $function = $module . '_views_slideshow_options_form_validate';
246
      call_user_func_array($function, $arguments);
247
    }
248
  }
249

    
250
  /**
251
   * Run any necessary actions on submit.
252
   */
253
  public function options_submit(&$form, &$form_state) {
254
    module_load_all_includes('views_slideshow.inc');
255

    
256
    $arguments = array(
257
      $form,
258
      &$form_state,
259
    );
260

    
261
    // Call all modules that use hook_views_slideshow_options_form_submit.
262
    foreach (module_implements('views_slideshow_options_form_submit') as $module) {
263
      $function = $module . '_views_slideshow_options_form_submit';
264
      call_user_func_array($function, $arguments);
265
    }
266

    
267
    // In addition to the skin, we also pre-save the definition that
268
    // correspond to it.  That lets us avoid a hook lookup on every page.
269
    $skins = $this->views_slideshow_get_skins();
270
    $form_state['values']['style_options']['skin_info'] = $skins[$form_state['values']['style_options']['slideshow_skin']];
271
  }
272

    
273
  /**
274
   * Retrieve a list of all available skins in the system.
275
   */
276
  public function views_slideshow_get_skins() {
277
    static $skins;
278

    
279
    if (empty($skins)) {
280
      $skins = array();
281

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

    
302
    return $skins;
303
  }
304

    
305
}