Projet

Général

Profil

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

root / drupal7 / sites / all / themes / adaptivetheme / at_core / inc / plugins.inc @ 74f6bef0

1
<?php
2

    
3
/**
4
 * @file
5
 * AT's liteweight plugin sub-system.
6
 */
7

    
8
/**
9
 * Find and return all plugins.
10
 *
11
 * This will search all base themes and the active theme for "plugins" in their
12
 * info files, and return all plugins directories. Info file entries must
13
 * follow the Panels plugin structure i.e.:
14
 * - "plugins[plugin_type][type] = path/to/plugins"
15
 * For example for AT page layouts we use:
16
 * - "plugins[page_layout][layout] = layouts/core"
17
 *
18
 * @param $theme_name, usually the active theme.
19
 */
20
function at_get_plugins($theme_name) {
21
  $plugins = &drupal_static(__FUNCTION__, array());
22
  if (empty($plugins)) {
23
    if ($plugins_cache = cache_get('at_get_plugins_plugins')) {
24
      $plugins = $plugins_cache->data;
25
    }
26
    else {
27
      $plugins_list = array();
28
      $themes_info = at_get_info_trail($theme_name);
29
      // Look for and get all the plugins
30
      if (!empty($themes_info)) {
31
        foreach ($themes_info as $this_theme => $theme_info) {
32
          foreach ($theme_info as $info) {
33
            if (array_key_exists('plugins', $info)) {
34
              foreach ($info['plugins'] as $plugin_type => $types) {
35
                $plugins_list[$this_theme][$plugin_type] = $types;
36
              }
37
            }
38
          }
39
        }
40

    
41
        $plugins = $plugins_list;
42
      }
43
    }
44
    cache_set('at_get_plugins_plugins', $plugins, 'cache');
45
  }
46

    
47
  return $plugins;
48
}
49

    
50
/**
51
 * Return the paths to all plugin providers plugin directories, this usually
52
 * means themes - both base themes and sub-themes that include plugin directory
53
 * declarations in their info files.
54
 *
55
 * @param $theme_name, ususally the active theme.
56
 */
57
function at_get_plugins_paths($theme_name) {
58
  $provider_paths = array();
59
  $plugins_list = at_get_plugins($theme_name);
60

    
61
  foreach ($plugins_list as $plugin_provider => $provider) {
62
    foreach ($provider as $plugin_type => $types) {
63
      foreach ($types as $type => $path) {
64
        $provider_path = drupal_get_path('theme', $plugin_provider) . '/' . $path;
65
        $provider_paths[$plugin_provider][$plugin_type][$type] = $provider_path;
66
      }
67
    }
68
  }
69

    
70
  return $provider_paths;
71
}
72

    
73
/**
74
 * Returns all files for plugins of a particular type.
75
 * This is called from at_load_plugins(), cannot be cached else it will return
76
 * stale data at some point.
77
 *
78
 * @param $theme_name
79
 */
80
function at_get_plugins_files($theme_name) {
81
  $plugins_files = array();
82
  $plugins_list = at_get_plugins($theme_name);
83

    
84
  $extension = 'inc';
85
  foreach ($plugins_list as $plugin_provider => $provider) {
86
    foreach ($provider as $plugin_type => $types) {
87
      foreach ($types as $type => $path) {
88
        $provider_path = drupal_get_path('theme', $plugin_provider) . '/' . $path;
89
        $plugins_files[$plugin_provider][$plugin_type][$type] = file_scan_directory($provider_path, '/\.' . $extension . '$/', array('key' => 'name'));
90
      }
91
    }
92
  }
93

    
94
  return $plugins_files;
95
}
96

    
97
/**
98
 * Extract plugin data structures.
99
 *
100
 * In essence what this does is return the data strutures (arrays) for all
101
 * plugins of a particular type. As of now only two types are used by AT, being
102
 * "panels" and "page_layout". This is hard to cache because it takes the
103
 * $plugin_type parameter, so everything else that calls this is heavily cached
104
 * instead. It does support an "everything else" plugin type, whatever that is.
105
 *
106
 * @param $theme_name, usually the active theme.
107
 * @param $plugin_type, the plugin type you need to return, usually one of
108
 * "panels" or "page_layout".
109
 */
110
function at_load_plugins($theme_name, $plugin_type) {
111
  $plugin_data_structures = array();
112
  $plugins_list = at_get_plugins_files($theme_name);
113
  $plugins_array = array();
114
  foreach ($plugins_list as $plugin_provider => $plugin_types) {
115
    $plugin_providers[] = $plugin_provider;
116
    foreach ($plugin_types as $type => $plugins) {
117
      if ($type === $plugin_type) {
118
        foreach ($plugins as $ptypes => $plugin) {
119
          $plugins_array[$plugin_provider][$type] = $plugin;
120
        }
121
      }
122
    }
123
  }
124
  $plugin_files = array();
125
  foreach ($plugins_array as $provider => $types) {
126
    foreach ($types as $key => $value) {
127
      $plugin_files = array_merge_recursive($plugin_files, $value);
128
    }
129
  }
130

    
131
  foreach ($plugin_files as $file_data) {
132
    include_once(DRUPAL_ROOT . '/' . $file_data->uri);
133

    
134
    // panels
135
    if ($plugin_type === 'panels') {
136
      foreach ($plugin_providers as $provider) {
137
        $identifiers[$provider] = $provider . '_' . $file_data->name . '_panels_layouts';
138
      }
139
      foreach ($identifiers as $identifier) {
140
        $panels_function = $identifier;
141
        if (function_exists($panels_function)) {
142
          $plugin_data_structures[] = $panels_function();
143
        }
144
      }
145
    }
146
    // page_layout
147
    if ($plugin_type === 'page_layout') {
148
      $identifier = $file_data->name;
149
      $page_layout_function = $identifier;
150
      if (function_exists($page_layout_function)) {
151
        $plugin_data_structures[] = $page_layout_function();
152
      }
153
    }
154

    
155
    // everything else
156
    elseif ($plugin_type !== 'panels' && $plugin_type !== 'page_layout') {
157
      $identifier = $file_data->name;
158
      $function = $identifier;
159
      if (function_exists($function)) {
160
        $plugin_data_structures[] = $function();
161
      }
162
      else {
163
        drupal_set_message(t('You defined an existing plugin type but no functions exists that match. If you are using Panels then you must use the "function method" in your plugins, see AT Cores panel layout plugins for examples.'), 'error');
164
      }
165
    }
166
  }
167

    
168
  if (empty($plugin_data_structures)) {
169
    return;
170
  }
171

    
172
  return $plugin_data_structures;
173
}
174

    
175
/**
176
 * Return Page layout data structures.
177
 * This returns the full data structures for all page layout plugins. Because
178
 * this can be a lot of data and appears to be computationally expensive to get
179
 * it is cached in the cache table.
180
 *
181
 * @param $theme_name, the active theme.
182
 */
183
function responsive_page_layouts_data_structure($theme_name = NULL) {
184
  // Use the passed in theme_name, else grab it from the global variable
185
  if ($theme_name == NULL) {
186
    global $theme_key;
187
    $theme_name = $theme_key;
188
  }
189

    
190
  $page_data_structure = &drupal_static(__FUNCTION__, array());
191
  if (empty($page_data_structure)) {
192
    if ($cache = cache_get('at_page_layouts_data_structure') && !empty($cache->data)) {
193
      $page_data_structure = $cache->data;
194
    }
195
    else {
196
      $data_structure = at_load_plugins($theme_name, $plugin_type = 'page_layout');
197
      foreach ($data_structure as $plugin => $datum) {
198
        foreach ($datum as $method => $layout) {
199
          $page_data_structure[$method] = $layout;
200
        }
201
      }
202
      cache_set('at_page_layouts_data_structure', $page_data_structure, 'cache');
203
    }
204
  }
205

    
206
  return $page_data_structure;
207
}
208

    
209
/**
210
 * Return Panels layout data structures.
211
 * This returns the full data structures for all Panels layout plugins. Because
212
 * this can be a lot of data and appears to be computationally expensive to get
213
 * it is cached in the cache table.
214
 */
215
function responsive_panels_data_structure() {
216
  global $theme_key;
217
  $theme_name = $theme_key;
218
  $panels_data_structure = &drupal_static(__FUNCTION__, array());
219
  if (empty($panels_data_structure)) {
220
    if ($cache = cache_get('at_panels_data_structure')) {
221
      $panels_data_structure = $cache->data;
222
    }
223
    else {
224
      $data_structure = at_load_plugins($theme_name, $plugin_type = 'panels');
225
      // If this theme doesn't define any Panels layouts, use those
226
      // provided by at_core.
227
      if (empty($data_structure)) {
228
        $data_structure = at_load_plugins('adaptivetheme', $plugin_type = 'panels');
229
      }
230
      foreach ($data_structure as $plugin => $datum) {
231
        foreach ($datum as $key => $value) {
232
          $type = $value['type'];
233
          $panels_data_structure[$type][] = $datum;
234
        }
235
      }
236

    
237
      cache_set('at_panels_data_structure', $panels_data_structure, 'cache');
238
    }
239
  }
240
  return $panels_data_structure;
241
}
242

    
243
/**
244
 * Return option arrays for forms.
245
 * Returns the options for radio lists in the page layout settings in the
246
 * appearance theme settings.
247
 *
248
 * @param $theme_name
249
 */
250
function responsive_page_layouts_device_group_options($theme_name) {
251
  $device_group_options = &drupal_static(__FUNCTION__, array());
252
  if (empty($device_group_options)) {
253
    $layout_methods = responsive_page_layouts_data_structure();
254
    foreach ($layout_methods as $method => $values) {
255
      foreach ($values as $key => $value) {
256
        if ($key == 'device_groups') {
257
          $method_values[$method] = $value;
258
        }
259
      }
260
    }
261
    foreach ($method_values as $this_method => $these_values) {
262
      foreach ($these_values as $k => $dv) {
263
        $device_group_options[$dv][] = $this_method;
264
      }
265
    }
266
  }
267

    
268
  return $device_group_options;
269
}
270

    
271
/**
272
 * Return all the admin css files for a plugin type.
273
 * Used primarly to theme the icons for each layout method.
274
 *
275
 * @param $theme_name
276
 * @param $plugin_type
277
 * TODO: add options for other plugin types, e.g. an "everything else" option.
278
 */
279
function responsive_plugins_admin_css($theme_name, $plugin_type) {
280
  $admin_css_files = array();
281
  $plugin_paths = at_get_plugins_paths($theme_name);
282

    
283
  foreach ($plugin_paths as $providers => $provided_types) {
284
    foreach ($provided_types as $provider => $type_path) {
285
      if ($provider === $plugin_type) {
286
        $admin_css_paths[$providers] = $type_path;
287
      }
288
    }
289
  }
290

    
291
  if ($plugin_type === 'panels') {
292
    $rpl = responsive_panels_data_structure();
293
    foreach ($rpl as $group => $layouts) {
294
      foreach ($layouts as $layout) {
295
        foreach ($layout as $layout_key => $value) {
296
          if (isset($value['admin css'])) {
297
            $css_files[$layout_key] = $value['admin css'];
298
          }
299
        }
300
      }
301
    }
302
  }
303

    
304
  // I have been lazy here, in fact only the three_col_grail layout sets
305
  // an admin css file and returns all the CSS for all page layouts.
306
  if ($plugin_type === 'page_layout') {
307
    $rpl = responsive_page_layouts_data_structure();
308
    foreach ($rpl as $layout_method => $value) {
309
      if (isset($value['admin css'])) {
310
        $css_files[$layout_method] = $value['admin css'];
311
      }
312
    }
313
  }
314

    
315
  foreach ($admin_css_paths as $admin_css => $paths) {
316
    foreach ($paths as $path) {
317
      foreach ($css_files as $method_key => $file_name) {
318
        if ($method_key !== 'one') {
319
          $admin_css_files[] = $path . '/' . $method_key . '/' . $file_name;
320
        }
321
      }
322
    }
323
  }
324

    
325
  if (!isset($rpl)) {
326
    return; // fail quietly
327
  }
328

    
329
  return $admin_css_files;
330
}
331

    
332
/**
333
 * Base config for page layout builder.
334
 * This is used in at_core.submit.responsive.inc to help retrieve the form
335
 * values for each device groups layout. Not really part of the plugin
336
 * system and can't recall why I put it here :/
337
 */
338
function page_layout() {
339
  $variables_array = array(
340
    'layout',
341
    'media_query',
342
    'page_width',
343
    'page_unit',
344
    'sidebar_first',
345
    'sidebar_second',
346
    'sidebar_unit',
347
  );
348
  return $variables_array;
349
}
350

    
351
/**
352
 * Return an array of standard gpanel regions. Not really part of the plugin
353
 * system and can't recall why I put it here :/
354
 */
355
function gpanel_regions() {
356
  $gpanel_regions_array = &drupal_static(__FUNCTION__, array());
357
  if (empty($gpanel_regions_array)) {
358
    $gpanel_regions_array = array(
359
      'one' => array(
360
        'title' => t('One column'),
361
        'regions' => array(
362
          'one_main',
363
        ),
364
      ),
365
      'two_brick' => array(
366
        'title' => t('AT Two column brick'),
367
        'regions' => array(
368
          'two_brick_top',
369
          'two_brick_left_above',
370
          'two_brick_right_above',
371
          'two_brick_middle',
372
          'two_brick_left_below',
373
          'two_brick_right_below',
374
          'two_brick_bottom',
375
        ),
376
      ),
377
      'two_66_33' => array(
378
        'title' => t('AT Two column 66/33'),
379
        'regions' => array(
380
          'two_66_33_top',
381
          'two_66_33_first',
382
          'two_66_33_second',
383
          'two_66_33_bottom',
384
        ),
385
      ),
386
      'two_33_66' => array(
387
        'title' => t('AT Two column 33/66'),
388
        'regions' => array(
389
          'two_33_66_top',
390
          'two_33_66_first',
391
          'two_33_66_second',
392
          'two_33_66_bottom',
393
        ),
394
      ),
395
      'two_50' => array(
396
        'title' => t('AT Two column 50/50'),
397
        'regions' => array(
398
          'two_50_top',
399
          'two_50_first',
400
          'two_50_second',
401
          'two_50_bottom',
402
        ),
403
      ),
404
      'three_inset_right' => array(
405
        'title' => t('AT Inset right'),
406
        'regions' => array(
407
          'three_inset_right_sidebar',
408
          'three_inset_right_top',
409
          'three_inset_right_middle',
410
          'three_inset_right_inset',
411
          'three_inset_right_bottom',
412
        ),
413
      ),
414
      'three_inset_left' => array(
415
        'title' => t('AT Inset left'),
416
        'regions' => array(
417
          'three_inset_left_sidebar',
418
          'three_inset_left_top',
419
          'three_inset_left_middle',
420
          'three_inset_left_inset',
421
          'three_inset_left_bottom',
422
        ),
423
      ),
424
      'three_50_25_25' => array(
425
        'title' => t('AT Three column 50/25/25'),
426
        'regions' => array(
427
          'three_50_25_25_top',
428
          'three_50_25_25_first',
429
          'three_50_25_25_second',
430
          'three_50_25_25_third',
431
          'three_50_25_25_bottom',
432
        ),
433
      ),
434
      'three_3x33' => array(
435
        'title' => t('AT Three column 3x33'),
436
        'regions' => array(
437
          'three_33_top',
438
          'three_33_first',
439
          'three_33_second',
440
          'three_33_third',
441
          'three_33_bottom',
442
        ),
443
      ),
444
      'three_25_50_25' => array(
445
        'title' => t('AT Three column 25/50/25'),
446
        'regions' => array(
447
          'three_25_50_25_top',
448
          'three_25_50_25_first',
449
          'three_25_50_25_second',
450
          'three_25_50_25_third',
451
          'three_25_50_25_bottom',
452
        ),
453
      ),
454
      'three_25_25_50' => array(
455
        'title' => t('AT Three column 25/25/50'),
456
        'regions' => array(
457
          'three_25_25_50_top',
458
          'three_25_25_50_first',
459
          'three_25_25_50_second',
460
          'three_25_25_50_third',
461
          'three_25_25_50_bottom',
462
        ),
463
      ),
464
      'four_4x25' => array(
465
        'title' => t('AT Four column 4x25'),
466
        'regions' => array(
467
          'four_first',
468
          'four_second',
469
          'four_third',
470
          'four_fourth',
471
        ),
472
      ),
473
      'five_5x20' => array(
474
        'title' => t('AT Five column 5x20'),
475
        'regions' => array(
476
          'five_first',
477
          'five_second',
478
          'five_third',
479
          'five_fourth',
480
          'five_fifth',
481
        ),
482
      ),
483
      'six_6x16' => array(
484
        'title' => t('AT Six column 6x16'),
485
        'regions' => array(
486
          'six_first',
487
          'six_second',
488
          'six_third',
489
          'six_fourth',
490
          'six_fifth',
491
          'six_sixth',
492
        ),
493
      ),
494
    );
495
  }
496

    
497
  return $gpanel_regions_array;
498
}