Projet

Général

Profil

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

root / drupal7 / sites / all / themes / bootstrap / theme / common.inc @ 87dbc3bf

1
<?php
2
/**
3
 * @file
4
 * common.inc
5
 *
6
 * Contains common functionality for the entire base theme.
7
 */
8

    
9
/**
10
 * Auto-rebuild the theme registry during theme development.
11
 */
12
if (theme_get_setting('bootstrap_rebuild_registry') && !defined('MAINTENANCE_MODE')) {
13
  // Rebuild .info data.
14
  system_rebuild_theme_data();
15
  // Rebuild theme registry.
16
  drupal_theme_rebuild();
17
}
18

    
19
/**
20
 * Return information from the .info file of a theme (and possible base themes).
21
 *
22
 * @param string $theme_key
23
 *   The machine name of the theme.
24
 * @param string $key
25
 *   The key name of the item to return from the .info file. This value can
26
 *   include "][" to automatically attempt to traverse any arrays.
27
 * @param bool $base_themes
28
 *   Recursively search base themes, defaults to TRUE.
29
 *
30
 * @return string|array|false
31
 *   A string or array depending on the type of value and if a base theme also
32
 *   contains the same $key, FALSE if no $key is found.
33
 */
34
function bootstrap_get_theme_info($theme_key = NULL, $key = NULL, $base_themes = TRUE) {
35
  // If no $theme_key is given, use the current theme if we can determine it.
36
  if (!isset($theme_key)) {
37
    $theme_key = !empty($GLOBALS['theme_key']) ? $GLOBALS['theme_key'] : FALSE;
38
  }
39
  if ($theme_key) {
40
    $themes = list_themes();
41
    if (!empty($themes[$theme_key])) {
42
      $theme = $themes[$theme_key];
43
      // If a key name was specified, return just that array.
44
      if ($key) {
45
        $value = FALSE;
46
        // Recursively add base theme values.
47
        if ($base_themes && isset($theme->base_themes)) {
48
          foreach (array_keys($theme->base_themes) as $base_theme) {
49
            $value = bootstrap_get_theme_info($base_theme, $key);
50
          }
51
        }
52
        if (!empty($themes[$theme_key])) {
53
          $info = $themes[$theme_key]->info;
54
          // Allow array traversal.
55
          $keys = explode('][', $key);
56
          foreach ($keys as $parent) {
57
            if (isset($info[$parent])) {
58
              $info = $info[$parent];
59
            }
60
            else {
61
              $info = FALSE;
62
            }
63
          }
64
          if (is_array($value)) {
65
            if (!empty($info)) {
66
              if (!is_array($info)) {
67
                $info = array($info);
68
              }
69
              $value = drupal_array_merge_deep($value, $info);
70
            }
71
          }
72
          else {
73
            if (!empty($info)) {
74
              if (empty($value)) {
75
                $value = $info;
76
              }
77
              else {
78
                if (!is_array($value)) {
79
                  $value = array($value);
80
                }
81
                if (!is_array($info)) {
82
                  $info = array($info);
83
                }
84
                $value = drupal_array_merge_deep($value, $info);
85
              }
86
            }
87
          }
88
        }
89
        return $value;
90
      }
91
      // If no info $key was specified, just return the entire info array.
92
      return $theme->info;
93
    }
94
  }
95
  return FALSE;
96
}
97

    
98
/**
99
 * Helper function for including theme files.
100
 *
101
 * @param string $theme
102
 *   Name of the theme to use for base path.
103
 * @param string $path
104
 *   Path relative to $theme.
105
 */
106
function bootstrap_include($theme, $path) {
107
  static $themes = array();
108
  if (!isset($themes[$theme])) {
109
    $themes[$theme] = drupal_get_path('theme', $theme);
110
  }
111
  if ($themes[$theme] && ($file = DRUPAL_ROOT . '/' . $themes[$theme] . '/' . $path) && file_exists($file)) {
112
    include_once $file;
113
  }
114
}
115

    
116
/**
117
 * Theme a Bootstrap Glyphicon.
118
 *
119
 * @param string $name
120
 *   The icon name, minus the "glyphicon-" prefix.
121
 *
122
 * @return string
123
 *   The icon HTML markup.
124
 */
125
function _bootstrap_icon($name) {
126
  $output = '';
127
  // Attempt to use the Icon API module, if enabled and it generates output.
128
  if (module_exists('icon')) {
129
    $output = theme('icon', array('bundle' => 'bootstrap', 'icon' => 'glyphicon-' . $name));
130
  }
131
  if (empty($output)) {
132
    // Mimic the Icon API markup.
133
    $attributes = array(
134
      'class' => array('icon', 'glyphicon', 'glyphicon-' . $name),
135
      'aria-hidden' => 'true',
136
    );
137
    $output = '<i' . drupal_attributes($attributes) . '></i>';
138
  }
139
  return $output;
140
}
141

    
142
/**
143
 * Colorize buttons based on the text value.
144
 *
145
 * @param string $text
146
 *   Button text to search against.
147
 *
148
 * @return string
149
 *   The specific button class to use or FALSE if not matched.
150
 */
151
function _bootstrap_colorize_button($text) {
152
  // Text values containing these specific strings, which are matched first.
153
  $specific_strings = array(
154
    'btn-primary' => array(
155
      t('Download feature'),
156
    ),
157
    'btn-success' => array(
158
      t('Add effect'),
159
      t('Add and configure'),
160
    ),
161
    'btn-info' => array(
162
      t('Save and add'),
163
      t('Add another item'),
164
      t('Update style'),
165
    ),
166
  );
167
  // Text values containing these generic strings, which are matches last.
168
  $generic_strings = array(
169
    'btn-primary' => array(
170
      t('Save'),
171
      t('Confirm'),
172
      t('Submit'),
173
      t('Search'),
174
    ),
175
    'btn-success' => array(
176
      t('Add'),
177
      t('Create'),
178
      t('Write'),
179
    ),
180
    'btn-warning' => array(
181
      t('Export'),
182
      t('Import'),
183
      t('Restore'),
184
      t('Rebuild'),
185
    ),
186
    'btn-info' => array(
187
      t('Apply'),
188
      t('Update'),
189
    ),
190
    'btn-danger' => array(
191
      t('Delete'),
192
      t('Remove'),
193
    ),
194
  );
195
  // Specific matching first.
196
  foreach ($specific_strings as $class => $strings) {
197
    foreach ($strings as $string) {
198
      if (strpos(drupal_strtolower($text), drupal_strtolower($string)) !== FALSE) {
199
        return $class;
200
      }
201
    }
202
  }
203
  // Generic matching last.
204
  foreach ($generic_strings as $class => $strings) {
205
    foreach ($strings as $string) {
206
      if (strpos(drupal_strtolower($text), drupal_strtolower($string)) !== FALSE) {
207
        return $class;
208
      }
209
    }
210
  }
211
  return FALSE;
212
}
213

    
214
/**
215
 * Iconize buttons based on the text value.
216
 *
217
 * @param string $text
218
 *   Button text to search against.
219
 *
220
 * @return string
221
 *   The icon markup or FALSE if not matched.
222
 */
223
function _bootstrap_iconize_button($text) {
224
  // Text values containing these specific strings, which are matched first.
225
  $specific_strings = array();
226
  // Text values containing these generic strings, which are matches last.
227
  $generic_strings = array(
228
    'cog' => array(
229
      t('Manage'),
230
      t('Configure'),
231
    ),
232
    'download' => array(
233
      t('Download'),
234
    ),
235
    'export' => array(
236
      t('Export'),
237
    ),
238
    'import' => array(
239
      t('Import'),
240
    ),
241
    'pencil' => array(
242
      t('Edit'),
243
    ),
244
    'plus' => array(
245
      t('Add'),
246
      t('Write'),
247
    ),
248
    'trash' => array(
249
      t('Delete'),
250
      t('Remove'),
251
    ),
252
    'upload' => array(
253
      t('Upload'),
254
    ),
255
  );
256
  // Specific matching first.
257
  foreach ($specific_strings as $icon => $strings) {
258
    foreach ($strings as $string) {
259
      if (strpos(drupal_strtolower($text), drupal_strtolower($string)) !== FALSE) {
260
        return _bootstrap_icon($icon);
261
      }
262
    }
263
  }
264
  // Generic matching last.
265
  foreach ($generic_strings as $icon => $strings) {
266
    foreach ($strings as $string) {
267
      if (strpos(drupal_strtolower($text), drupal_strtolower($string)) !== FALSE) {
268
        return _bootstrap_icon($icon);
269
      }
270
    }
271
  }
272
  return FALSE;
273
}