Projet

Général

Profil

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

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

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
// STYLE API ****************************************************************/
10
/**
11
 * Returns the name of the forum style to use.
12
 */
13
function advanced_forum_get_current_style() {
14
  return variable_get('advanced_forum_style', 'silver_bells');
15
}
16

    
17
function advanced_forum_get_style($style) {
18
  ctools_include('plugins');
19
  return ctools_get_plugins('advanced_forum', 'styles', $style);
20
}
21

    
22
function advanced_forum_get_all_styles() {
23
  ctools_include('plugins') ;
24
  return ctools_get_plugins('advanced_forum', 'styles');
25
}
26

    
27
/**
28
 * Returns the path to the advanced forum style
29
 */
30
function advanced_forum_path_to_style($requested_style = NULL) {
31
  // Set a static variable so this is cached after the first call.
32
  static $style_paths = array();
33

    
34
  if (empty($requested_style)) {
35
    // If no style is passed in, assume the current style is wanted.
36
    $requested_style = advanced_forum_get_current_style();
37
  }
38

    
39
  if (empty($style_path)) {
40
    // Get the path information
41
    $styles = advanced_forum_get_all_styles();
42
    foreach ($styles as $key => $style) {
43
      $style_paths[$key] = $style['path'];
44
    }
45
  }
46
  return $style_paths[$requested_style];
47
}
48

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

    
59
  if (!array_key_exists($style, $lineages)) {
60
    $lineage = array();
61

    
62
    // Get an array with information from all styles.
63
    $all_styles = advanced_forum_get_all_styles();
64

    
65
    // Fallback
66
    if (empty($all_styles[$style]))
67
      $style = "silver_bells";
68

    
69
    // Add the current style to the lineage first
70
    $lineage[$style] = $all_styles[$style]['path'];
71

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

    
81
    // Grab the style we are looking at. This variable starts out as the current
82
    // style in use on the page but will change as we move up the chain.
83
    $current_style = $style;
84

    
85
    while (!empty($all_styles[$current_style]['base style'])) {
86
      // There is a parent style, so move up to it.
87
      $current_style = $all_styles[$current_style]['base style'];
88

    
89
      // Make sure the listed parent style actually exists.
90
      if (!empty($all_styles[$current_style]['path'])) {
91
        // Add the style to our list.
92
        $lineage[$current_style] = $all_styles[$current_style]['path'];
93
      }
94
    }
95

    
96
    $lineages[$style] = $lineage;
97
  }
98

    
99
  return $lineages[$style];
100
}
101

    
102
/**
103
 * Starting at a given style, return path of template file
104
 */
105
function advanced_forum_style_template_path($template_name, $style = NULL) {
106
  static $styles_files = array();
107

    
108
  if (empty($styles_files)) {
109
    $styles_files = file_scan_directory(drupal_get_path('module', 'advanced_forum') . '/styles', '/(.*)/', array(), 2);
110
  }
111

    
112
  $lineage = advanced_forum_style_lineage($style);
113

    
114
  $path = '';
115
  foreach ($lineage as $key => $path) {
116
    $template_path ="";
117
  }
118
    return $path;
119
}
120

    
121
function advanced_forum_template_basename(&$file, $key, $extension) {
122
  $file->name = basename($file->uri, $extension);
123
}
124

    
125
function advanced_forum_find_style_templates($style = NULL) {
126
  // Get theme engine extension
127
  global $theme_engine;
128
  $extension = '.tpl.php';
129
  if (isset($theme_engine)) {
130
    $extension_function = $theme_engine . '_extension';
131
    if (function_exists($extension_function)) {
132
      $extension = $extension_function();
133
    }
134
  }
135

    
136
  // Most significant style template comes first in lineage array
137
  $lineage = array_reverse(advanced_forum_style_lineage($style), TRUE);
138

    
139
  $templates = array();
140

    
141
  // Loop through each style folder looking for templates
142
  foreach ($lineage as $key => $path) {
143
    $styles_files = file_scan_directory($path, '/' . str_replace('.', '\.', $extension) . '$/', array('key' => 'filename'));
144
    // Fix name - file_scan_directory returns template.tpl as name
145
    //array_walk($styles_files, 'advanced_forum_template_basename', $extension);
146
    foreach ($styles_files as $file) {
147
      $file->name = basename($file->uri, $extension);
148
      $theme_file = $file->name;
149
      $theme_path = advanced_forum_path_to_theme();
150

    
151
      // Remove style name from file name
152
      $theme_base_file = str_replace(".$key.", "-", $file->name);
153

    
154
      // Transform - in filenames to _ to match function naming scheme
155
      // and create proper hook name
156
      $hook = strtr($theme_base_file, '-', '_');
157

    
158
      // Check for user theme template override
159
      // 1. check for <theme>/<style>/advanced-forum.<style>.<template>.tpl.php
160
      if (file_exists($theme_path . "/$key/" . $file->name . $extension)) {
161
        $theme_path = $theme_path . "/$key";
162
      }
163
      // 2. check for <theme>/advanced-forum-<template>.tpl.php
164
      elseif (file_exists($theme_path . "/" . $theme_base_file . $extension)) {
165
        $theme_file = $theme_base_file;
166
      }
167
      // 3. check for advanced_forum/styles/<style>/... template
168
      else {
169
        $theme_path = dirname($file->uri);
170
      }
171

    
172
      $templates[$hook] = array(
173
        'template' => $theme_file,
174
        'path' => $theme_path,
175
      );
176
    }
177
  }
178
  return $templates;
179
}
180

    
181
/**
182
 * Load any .inc files related to the style so that preprocess
183
 * functions can run as appropriate.
184
 */
185
function advanced_forum_load_style_includes($style = NULL) {
186
  $lineage = advanced_forum_style_lineage($style);
187
  foreach ($lineage as $key => $path) {
188
    if (file_exists("$path/$key.inc")) {
189
      require_once("$path/$key.inc");
190
    }
191
  }
192
}
193

    
194
/**
195
 * Get the info for a style, using an additive notation to
196
 * include all items from the parent lineage.
197
 */
198
function advanced_forum_style_info($style = NULL) {
199
  static $info = array();
200
  if (empty($style)) {
201
    // If no style is passed in, assume the current style is wanted.
202
    $style = advanced_forum_get_current_style();
203
  }
204

    
205
  if (!array_key_exists($style, $info)) {
206
    $info[$style] = array();
207
    $lineage = advanced_forum_style_lineage();
208
    foreach ($lineage as $key => $dir) {
209
      // using the + operator is additive but will not overwrite.
210
      // $lineage comes out as the actual style with each
211
      // successive style after it, so simply adding the arrays
212
      // together means that all info will be combined and keys
213
      // in the parent info will be defaults passed down to children
214
      // only if they do not override them.
215
      $info[$style] += advanced_forum_get_style($key);
216
    }
217
  }
218

    
219
  return $info[$style];
220
}
221

    
222
function advanced_forum_is_styled($content, $teaser = FALSE, $type = 'node') {
223
  // This is the ID of the topic starting node
224
  static $master_topic_id;
225

    
226
  // Give other modules a first crack at making the decision. To keep this
227
  // simple, he last one in the array wins.
228
  $topic_ids = module_invoke_all('advanced_forum_is_styled_alter', $content, $teaser, $type);
229
  $topic_id = end($topic_ids);
230

    
231
  if (!$topic_id) {
232
    // If no other modules made a decision on this, we look at it.
233
    switch ($type) {
234
      case 'node':
235
        // See if the node should be styled.
236
        $topic_id = advanced_forum_node_is_styled($content, $teaser);
237
        break;
238

    
239
      case 'comment':
240
        $topic_id = advanced_forum_comment_is_styled($content, $master_topic_id);
241
        break;
242

    
243
      case 'comment-wrapper':
244
        if ($content->nid == $master_topic_id) {
245
          // Use our comment wrapper only if we are on a styled node.
246
          $topic_id = $master_topic_id;
247
        }
248
        break;
249
      }
250
    }
251

    
252
  if ($topic_id > 0) {
253
    // If we have a positive number for the topic ID, then this item is styled.
254
    // We need to update the master ID, add the CSS/JS files, and tell the caller
255
    // our decision.
256
    $master_topic_id = $topic_id;
257
    _advanced_forum_add_files();
258
    return TRUE;
259
  }
260
  elseif ($topic_id == -42) {
261
    // A -42 means all the tests passed but the node is being previewed so there
262
    // is no node id, yet. Add the files and return true but don't update the
263
    // master topic ID.
264
    _advanced_forum_add_files();
265
    return TRUE;
266
  }
267

    
268
}
269

    
270
function advanced_forum_node_is_styled($node, $teaser = FALSE) {
271
  // Get the list of types that should have the style applied
272
  $styled_node_types = variable_get('advanced_forum_styled_node_types', array('forum'));
273
  // If this type is in the list of types that should be styled...
274
  if (in_array($node->type, $styled_node_types)) {
275
    // ...and if we are styling teasers or this isn't a teaser...
276
    if (variable_get('advanced_forum_style_teasers', FALSE) || !$teaser) {
277
      // ...and if this is not a new node being previewed...
278
      if (!empty($node->nid)) {
279
        // ...and if we are styling non forum tagged nodes or this is forum tagged...
280
        if (!variable_get('advanced_forum_style_only_forum_tagged', TRUE) || advanced_forum_is_forum_tagged($node)) {
281
          // ... then return the node ID.
282
          return $node->nid;
283
        }
284
      }
285
      // ...or if this _is_ a new node being previewed...
286
      else {
287
        // ...and if we are styling non forum tagged nodes or this is forum tagged...
288
        if (!variable_get('advanced_forum_style_only_forum_tagged', TRUE) || advanced_forum_is_forum_tagged($node, TRUE)) {
289
          // ...Send back -42 as a special code to say that this should be
290
          // styled but that it isn't the actual node id.
291
          return -42;
292
        }
293
      }
294
    }
295
  }
296
}
297

    
298
function advanced_forum_comment_is_styled($comment, $master_topic_id) {
299
  if (isset($master_topic_id) && ($comment->nid == $master_topic_id)) {
300
    // This comment is on a node we already know is styled, or a previous
301
    // comment on this node is styled, so the comment is styled.
302
    return $master_topic_id;
303
  }
304

    
305
  if (variable_get("advanced_forum_style_all_comments", 0)) {
306
    // All comments on this site should be styled.
307
    return $comment->nid;
308
  }
309

    
310
  // Load up the node this comment is attached to and see if it is styled.
311
  // This should not happen often, only in a case where the comment is
312
  // displayed seperately from the node (such as a reply preview).
313
  $node = node_load($comment->nid);
314
  if (advanced_forum_node_is_styled($node, FALSE)) {
315
    return $comment->nid;
316
  }
317

    
318
  // Comment doesn't pass any styling test
319
  return -1;
320
}
321

    
322
function advanced_forum_is_forum_tagged($node, $preview = FALSE) {
323
  // Is this a forum node at all?
324
  if (empty($node->taxonomy_forums)) {
325
    return FALSE;
326
  }
327
  $vid = variable_get('forum_nav_vocabulary');
328

    
329
  // Check for language used
330
  if (empty($node->taxonomy_forums[$node->language])) {
331
    $langcode = LANGUAGE_NONE;
332
  }
333
  else {
334
    $langcode = $node->language;
335
  }
336

    
337
  if ($preview) {
338
     foreach ($node->taxonomy_forums[$langcode] as $tforum) {
339
      if ($tforum['taxonomy_term']->vid == $vid) {
340
        return TRUE;
341
      }
342
    }
343
  }
344
  else {
345
    // Get a list of the terms attached to this node
346
    $terms = $node->taxonomy_forums[$langcode];
347
    if (count($terms) > 0) {
348
      return TRUE;
349
    }
350
  }
351
}
352

    
353
/**
354
 * Returns the path to actual site theme in use because path_to_theme is flaky.
355
 */
356
function advanced_forum_path_to_theme() {
357
  global $theme;
358

    
359
  if (!empty($theme)) {
360
    // Normally, the global theme variable is set, so we use that.
361
    return drupal_get_path('theme', $theme);
362
  }
363

    
364
  // For some reason I've never figured out, some users are reporting
365
  // that the global theme variable is not set when this is called.
366
  // As a band-aid solution, this will pull the default theme out of the
367
  // database and use that.
368
  $default_theme = variable_get('theme_default', 'garland');
369
  return drupal_get_path('theme', $default_theme);
370
}
371

    
372
/**
373
 * Manipulate the template suggestions to add in one for each style as well
374
 * as the default.
375
 */
376
function advanced_forum_add_template_suggestions($template, &$variables) {
377
  // adjust template name
378
  $template = strtr($template, '-', '_');
379
  if (strpos($template, 'advanced_forum') === FALSE) {
380
    $template = "advanced_forum_" . $template;
381
  }
382
  // Set AF template as most significant suggestion
383
  $variables['theme_hook_suggestion'] = $template;
384
  $variables['theme_hook_suggestions'][] = $template;
385
  return;
386
}
387

    
388
/**
389
 * Adds extra files needed for styling.
390
 * This is currently just CSS files but was made generic to allow adding
391
 * javascript in the future.
392
 */
393
function _advanced_forum_add_files() {
394
  // This could get called more than once on a page and we only need to do it once.
395
  static $added;
396

    
397
  if (empty($added)) {
398
    $added = TRUE;
399
    $lineage = advanced_forum_style_lineage();
400
    $lineage = array_reverse($lineage, TRUE);
401
    $theme_path = advanced_forum_path_to_theme();
402

    
403
    foreach (array('structure.css', 'style.css', 'images.css') as $css_type) {
404
      $css_file = "$theme_path/advanced-forum.$css_type";
405
      if (file_exists($css_file)) {
406
        // CSS files with no style name in the theme directory trump all
407
        // to provide a theme specific style override.
408
        drupal_add_css($css_file);
409
      }
410
      else {
411
        // For each style from the current style on up through each parent
412
        // style, look for the style specific CSS file first in the active
413
        // theme and then in the style directory.
414
        foreach ($lineage AS $key => $path) {
415
          $css_file = "advanced-forum.$key.$css_type";
416
          if (file_exists("$theme_path/$css_file")) {
417
            // If the style specific file is in the theme, use that.
418
            drupal_add_css("$theme_path/$css_file");
419
          }
420
          elseif (file_exists("$path/$css_file")) {
421
            // Otherwise look in the style for it.
422
            drupal_add_css("$path/$css_file");
423
          }
424
        }
425
      }
426
    }
427

    
428
    advanced_forum_load_style_includes();
429
  }
430
}
431

    
432
// CTOOLS INTEGRATION FOR STYLES ********************************************/
433

    
434
function advanced_forum_ctools_plugin_type() {
435
  return array(
436
    'styles' => array(
437
      'info file' => TRUE,
438
      'load themes' => TRUE,
439
    ),
440
  );
441
}