Projet

Général

Profil

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

root / drupal7 / sites / all / modules / advanced_forum / includes / style.inc @ 13c3c9b4

1
<?php
2

    
3
/**
4
 * @file
5
 * Functions relating to the style system, not including core hooks and
6
 * preprocess / theme functions.
7
 */
8

    
9
/**
10
 * Returns the name of the forum style to use.
11
 */
12
function advanced_forum_get_current_style() {
13
  return variable_get('advanced_forum_style', 'silver_bells');
14
}
15

    
16
/**
17
 * Returns style.
18
 */
19
function advanced_forum_get_style($style) {
20
  ctools_include('plugins');
21
  return ctools_get_plugins('advanced_forum', 'styles', $style);
22
}
23

    
24
/**
25
 * Returns all styles.
26
 */
27
function advanced_forum_get_all_styles() {
28
  ctools_include('plugins');
29
  return ctools_get_plugins('advanced_forum', 'styles');
30
}
31

    
32
/**
33
 * Returns the path to the advanced forum style.
34
 */
35
function advanced_forum_path_to_style($requested_style = NULL) {
36
  // Set a static variable so this is cached after the first call.
37
  static $style_paths = array();
38

    
39
  if (empty($requested_style)) {
40
    // If no style is passed in, assume the current style is wanted.
41
    $requested_style = advanced_forum_get_current_style();
42
  }
43

    
44
  if (empty($style_path)) {
45
    // Get the path information.
46
    $styles = advanced_forum_get_all_styles();
47
    foreach ($styles as $key => $style) {
48
      $style_paths[$key] = $style['path'];
49
    }
50
  }
51
  return $style_paths[$requested_style];
52
}
53

    
54
/**
55
 * Starting at a given style, return paths of it and all ancestor styles.
56
 */
57
function advanced_forum_style_lineage($style = NULL) {
58
  static $lineages = array();
59
  if (empty($style)) {
60
    // If no style is passed in, assume the current style is wanted.
61
    $style = advanced_forum_get_current_style();
62
  }
63

    
64
  if (!array_key_exists($style, $lineages)) {
65
    $lineage = array();
66

    
67
    // Get an array with information from all styles.
68
    $all_styles = advanced_forum_get_all_styles();
69

    
70
    // Fallback.
71
    if (empty($all_styles[$style])) {
72
      $style = "silver_bells";
73
    }
74

    
75
    // Add the current style to the lineage first.
76
    $lineage[$style] = $all_styles[$style]['path'];
77

    
78
    // Check if there is an "extra style" listed. This allows you to grab the
79
    // CSS of one other style and toss it in as a pseudo parent. We do not
80
    // follow the path up its parent. The primary use is for adding in the
81
    // "stacked" CSS but could be used for other purposes as well.
82
    if (!empty($all_styles[$style]['extra style']) && !empty($all_styles[$all_styles[$style]['extra style']]['path'])) {
83
      $extra_style = $all_styles[$style]['extra style'];
84
      $lineage[$extra_style] = $all_styles[$extra_style]['path'];
85
    }
86

    
87
    // Grab the style we are looking at. This variable starts out as the current
88
    // style in use on the page but will change as we move up the chain.
89
    $current_style = $style;
90

    
91
    while (!empty($all_styles[$current_style]['base style'])) {
92
      // There is a parent style, so move up to it.
93
      $current_style = $all_styles[$current_style]['base style'];
94

    
95
      // Make sure the listed parent style actually exists.
96
      if (!empty($all_styles[$current_style]['path'])) {
97
        // Add the style to our list.
98
        $lineage[$current_style] = $all_styles[$current_style]['path'];
99
      }
100
    }
101

    
102
    $lineages[$style] = $lineage;
103
  }
104

    
105
  return $lineages[$style];
106
}
107

    
108
/**
109
 * Starting at a given style, return path of template file.
110
 */
111
function advanced_forum_style_template_path($template_name, $style = NULL) {
112
  static $styles_files = array();
113

    
114
  if (empty($styles_files)) {
115
    $styles_files = file_scan_directory(drupal_get_path('module', 'advanced_forum') . '/styles', '/(.*)/', array(), 2);
116
  }
117

    
118
  $lineage = advanced_forum_style_lineage($style);
119

    
120
  $path = '';
121
  foreach ($lineage as $key => $path) {
122
    $template_path = "";
123
  }
124

    
125
  return $path;
126
}
127

    
128
/**
129
 * Add basename of file.
130
 */
131
function advanced_forum_template_basename(&$file, $key, $extension) {
132
  $file->name = basename($file->uri, $extension);
133
}
134

    
135
/**
136
 * Find style template.
137
 */
138
function advanced_forum_find_style_templates($style = NULL) {
139
  // Get theme engine extension.
140
  global $theme_engine;
141
  $extension = '.tpl.php';
142
  if (isset($theme_engine)) {
143
    $extension_function = $theme_engine . '_extension';
144
    if (function_exists($extension_function)) {
145
      $extension = $extension_function();
146
    }
147
  }
148

    
149
  // Most significant style template comes first in lineage array.
150
  $lineage = array_reverse(advanced_forum_style_lineage($style), TRUE);
151

    
152
  $templates = array();
153

    
154
  // Loop through each style folder looking for templates.
155
  foreach ($lineage as $key => $path) {
156
    $styles_files = file_scan_directory($path, '/' . str_replace('.', '\.', $extension) . '$/', array('key' => 'filename'));
157
    foreach ($styles_files as $file) {
158
      $file->name = basename($file->uri, $extension);
159
      $theme_file = $file->name;
160
      $theme_path = advanced_forum_path_to_theme();
161

    
162
      // Remove style name from file name.
163
      $theme_base_file = str_replace(".$key.", "-", $file->name);
164

    
165
      // Transform - in filenames to _ to match function naming scheme
166
      // and create proper hook name.
167
      $hook = strtr($theme_base_file, '-', '_');
168

    
169
      // Check for user theme template override
170
      // 1. check for <theme>/<style>/advanced-forum.<style>.<template>.tpl.php
171
      if (file_exists($theme_path . "/$key/" . $file->name . $extension)) {
172
        $theme_path = $theme_path . "/$key";
173
      }
174
      // 2. check for <theme>/advanced-forum-<template>.tpl.php
175
      elseif (file_exists($theme_path . "/" . $theme_base_file . $extension)) {
176
        $theme_file = $theme_base_file;
177
      }
178
      // 3. check for advanced_forum/styles/<style>/... template.
179
      else {
180
        $theme_path = dirname($file->uri);
181
      }
182

    
183
      $templates[$hook] = array(
184
        'template' => $theme_file,
185
        'path' => $theme_path,
186
      );
187
    }
188
  }
189
  return $templates;
190
}
191

    
192
/**
193
 * Load any .inc files related to the style so that preprocess functions can run as appropriate.
194
 */
195
function advanced_forum_load_style_includes($style = NULL) {
196
  $lineage = advanced_forum_style_lineage($style);
197
  foreach ($lineage as $key => $path) {
198
    if (file_exists("$path/$key.inc")) {
199
      require_once "$path/$key.inc";
200
    }
201
  }
202
}
203

    
204
/**
205
 * Get the info for a style, using an additive notation to include all items from the parent lineage.
206
 */
207
function advanced_forum_style_info($style = NULL) {
208
  static $info = array();
209
  if (empty($style)) {
210
    // If no style is passed in, assume the current style is wanted.
211
    $style = advanced_forum_get_current_style();
212
  }
213

    
214
  if (!array_key_exists($style, $info)) {
215
    $info[$style] = array();
216
    $lineage = advanced_forum_style_lineage();
217
    foreach ($lineage as $key => $dir) {
218
      // Using the + operator is additive but will not overwrite.
219
      // $lineage comes out as the actual style with each
220
      // successive style after it, so simply adding the arrays
221
      // together means that all info will be combined and keys
222
      // in the parent info will be defaults passed down to children
223
      // only if they do not override them.
224
      $info[$style] += advanced_forum_get_style($key);
225
    }
226
  }
227

    
228
  return $info[$style];
229
}
230

    
231
/**
232
 * Returns whether forum is styled.
233
 */
234
function advanced_forum_is_styled($content, $teaser = FALSE, $type = 'node') {
235
  // This is the ID of the topic starting node.
236
  static $master_topic_id;
237

    
238
  // Give other modules a first crack at making the decision. To keep this
239
  // simple, he last one in the array wins.
240
  $topic_ids = module_invoke_all('advanced_forum_is_styled_alter', $content, $teaser, $type);
241
  $topic_id = end($topic_ids);
242

    
243
  if (!$topic_id) {
244
    // If no other modules made a decision on this, we look at it.
245
    switch ($type) {
246
      case 'node':
247
        // See if the node should be styled.
248
        $topic_id = advanced_forum_node_is_styled($content, $teaser);
249
        break;
250

    
251
      case 'comment':
252
        $topic_id = advanced_forum_comment_is_styled($content, $master_topic_id);
253
        break;
254

    
255
      case 'comment-wrapper':
256
        if ($content->nid == $master_topic_id) {
257
          // Use our comment wrapper only if we are on a styled node.
258
          $topic_id = $master_topic_id;
259
        }
260
        break;
261
    }
262
  }
263

    
264
  if ($topic_id > 0) {
265
    // If we have a positive number for the topic ID, then this item is styled.
266
    // We need to update the master ID, add the CSS/JS files, and tell the caller
267
    // our decision.
268
    $master_topic_id = $topic_id;
269
    _advanced_forum_add_files();
270
    return TRUE;
271
  }
272
  elseif ($topic_id == -42) {
273
    // A -42 means all the tests passed but the node is being previewed so there
274
    // is no node id, yet. Add the files and return true but don't update the
275
    // master topic ID.
276
    _advanced_forum_add_files();
277
    return TRUE;
278
  }
279

    
280
}
281

    
282
/**
283
 * Returns whether node is styled.
284
 */
285
function advanced_forum_node_is_styled($node, $teaser = FALSE) {
286
  // Get the list of types that should have the style applied.
287
  $styled_node_types = variable_get('advanced_forum_styled_node_types', array('forum'));
288
  // If this type is in the list of types that should be styled...
289
  if (in_array($node->type, $styled_node_types)) {
290
    // ...and if we are styling teasers or this isn't a teaser...
291
    if (variable_get('advanced_forum_style_teasers', FALSE) || !$teaser) {
292
      // ...and if this is not a new node being previewed...
293
      if (!empty($node->nid)) {
294
        // ...and if we are styling non forum tagged nodes or this is forum tagged...
295
        if (!variable_get('advanced_forum_style_only_forum_tagged', TRUE) || advanced_forum_is_forum_tagged($node)) {
296
          // ... then return the node ID.
297
          return $node->nid;
298
        }
299
      }
300
      // ...or if this _is_ a new node being previewed...
301
      else {
302
        // ...and if we are styling non forum tagged nodes or this is forum tagged...
303
        if (!variable_get('advanced_forum_style_only_forum_tagged', TRUE) || advanced_forum_is_forum_tagged($node, TRUE)) {
304
          // ...Send back -42 as a special code to say that this should be
305
          // styled but that it isn't the actual node id.
306
          return -42;
307
        }
308
      }
309
    }
310
  }
311
}
312

    
313
/**
314
 * Returns whether comment is styled.
315
 */
316
function advanced_forum_comment_is_styled($comment, $master_topic_id) {
317
  if (isset($master_topic_id) && ($comment->nid == $master_topic_id)) {
318
    // This comment is on a node we already know is styled, or a previous
319
    // comment on this node is styled, so the comment is styled.
320
    return $master_topic_id;
321
  }
322

    
323
  if (variable_get("advanced_forum_style_all_comments", 0)) {
324
    // All comments on this site should be styled.
325
    return $comment->nid;
326
  }
327

    
328
  // Load up the node this comment is attached to and see if it is styled.
329
  // This should not happen often, only in a case where the comment is
330
  // displayed seperately from the node (such as a reply preview).
331
  $node = node_load($comment->nid);
332
  if (advanced_forum_node_is_styled($node, FALSE)) {
333
    return $comment->nid;
334
  }
335

    
336
  // Comment doesn't pass any styling test.
337
  return -1;
338
}
339

    
340
/**
341
 * Returns whether forum is tagged.
342
 */
343
function advanced_forum_is_forum_tagged($node, $preview = FALSE) {
344
  // Is this a forum node at all?
345
  if (empty($node->taxonomy_forums)) {
346
    return FALSE;
347
  }
348
  $vid = variable_get('forum_nav_vocabulary');
349

    
350
  // Check for language used.
351
  if (empty($node->taxonomy_forums[$node->language])) {
352
    $langcode = LANGUAGE_NONE;
353
  }
354
  else {
355
    $langcode = $node->language;
356
  }
357

    
358
  if ($preview) {
359
    foreach ($node->taxonomy_forums[$langcode] as $tforum) {
360
      if ($tforum['taxonomy_term']->vid == $vid) {
361
        return TRUE;
362
      }
363
    }
364
  }
365
  else {
366
    // Get a list of the terms attached to this node.
367
    $terms = $node->taxonomy_forums[$langcode];
368
    if (count($terms) > 0) {
369
      return TRUE;
370
    }
371
  }
372
}
373

    
374
/**
375
 * Returns the path to actual site theme in use because path_to_theme is flaky.
376
 */
377
function advanced_forum_path_to_theme() {
378
  global $theme;
379

    
380
  if (!empty($theme)) {
381
    // Normally, the global theme variable is set, so we use that.
382
    return drupal_get_path('theme', $theme);
383
  }
384

    
385
  // For some reason I've never figured out, some users are reporting
386
  // that the global theme variable is not set when this is called.
387
  // As a band-aid solution, this will pull the default theme out of the
388
  // database and use that.
389
  $default_theme = variable_get('theme_default', 'garland');
390
  return drupal_get_path('theme', $default_theme);
391
}
392

    
393
/**
394
 * Manipulate the template suggestions to add in one for each style as well as the default.
395
 */
396
function advanced_forum_add_template_suggestions($template, &$variables) {
397
  // Adjust template name.
398
  $template = strtr($template, '-', '_');
399
  if (strpos($template, 'advanced_forum') === FALSE) {
400
    $template = "advanced_forum_" . $template;
401
  }
402
  // Set AF template as most significant suggestion.
403
  $variables['theme_hook_suggestion'] = $template;
404
  $variables['theme_hook_suggestions'][] = $template;
405
  return;
406
}
407

    
408
/**
409
 * Adds extra files needed for styling.
410
 *
411
 * This is currently just CSS files but was made generic to allow adding
412
 * javascript in the future.
413
 */
414
function _advanced_forum_add_files() {
415
  // This could get called more than once on a page and we only need to do it once.
416
  static $added;
417

    
418
  if (empty($added)) {
419
    $added = TRUE;
420
    $lineage = advanced_forum_style_lineage();
421
    $lineage = array_reverse($lineage, TRUE);
422
    $theme_path = advanced_forum_path_to_theme();
423

    
424
    foreach (array('structure.css', 'style.css', 'images.css') as $css_type) {
425
      $css_file = "$theme_path/advanced-forum.$css_type";
426
      if (file_exists($css_file)) {
427
        // CSS files with no style name in the theme directory trump all
428
        // to provide a theme specific style override.
429
        drupal_add_css($css_file);
430
      }
431
      else {
432
        // For each style from the current style on up through each parent
433
        // style, look for the style specific CSS file first in the active
434
        // theme and then in the style directory.
435
        foreach ($lineage as $key => $path) {
436
          $css_file = "advanced-forum.$key.$css_type";
437
          if (file_exists("$theme_path/$css_file")) {
438
            // If the style specific file is in the theme, use that.
439
            drupal_add_css("$theme_path/$css_file");
440
          }
441
          elseif (file_exists("$path/$css_file")) {
442
            // Otherwise look in the style for it.
443
            drupal_add_css("$path/$css_file");
444
          }
445
        }
446
      }
447
    }
448

    
449
    advanced_forum_load_style_includes();
450
  }
451
}
452

    
453
/**
454
 * Ctools integration for styles.
455
 */
456
function advanced_forum_ctools_plugin_type() {
457
  return array(
458
    'styles' => array(
459
      'info file' => TRUE,
460
      'load themes' => TRUE,
461
    ),
462
  );
463
}