Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Holds the contents of a preprocess function moved into its own file
6
 * to ease memory requirements and having too much code in one file.
7
 */
8

    
9
/**
10
 * Preprocess forum list.
11
 */
12
function _advanced_forum_preprocess_forum_list(&$variables) {
13
  advanced_forum_add_template_suggestions("forum_list", $variables);
14

    
15
  // Added variable for default collapsed configuration.
16
  $variables['collapsed_main_list'] = variable_get('advanced_forum_default_collapsed_list', array());
17

    
18
  // Add needed items for the collapsible containers.
19
  $collapsible = variable_get('advanced_forum_collapsible_containers', 'toggle');
20
  $variables['collapsible'] = FALSE;
21
  if ($collapsible != 'none') {
22
    drupal_add_library('system', 'jquery.cookie');
23
    drupal_add_js(drupal_get_path('module', 'advanced_forum') . '/js/advanced_forum.js');
24
    $settings['advanced_forum'] = array(
25
      'modulePath' => drupal_get_path('module', 'advanced_forum'),
26
      'effect' => $collapsible,
27
      'default_collapsed_list' => $variables['collapsed_main_list'],
28
    );
29

    
30
    drupal_add_js($settings, array('type' => 'setting'));
31
    $variables['collapsible'] = TRUE;
32
  }
33

    
34
  // The tid is the ID of the container or forum we are in. Assign it to
35
  // $parent_id for easy reference, assign it to the forum_id template variable
36
  // to give it a nice name for themers, then get rid of the original variable.
37
  $parent_id = isset($variables['tid']) ? $variables['tid'] : 0;
38
  $variables['forum_id'] = $parent_id;
39

    
40
  unset($variables['tid']);
41

    
42
  $variables['use_taxonomy_image'] = (function_exists('taxonomy_image_display') && variable_get('advanced_forum_use_taxonomy_image', TRUE));
43

    
44
  // Process the containers and forums and move them to a new variable.
45
  $items = array();
46
  $lowest_depth = 0;
47
  foreach ($variables['forums'] as $id => $forum) {
48
    if (empty($forum->container)) {
49
      $items[$id] = advanced_forum_process_forum($forum);
50
    }
51
    else {
52
      $items[$id] = advanced_forum_process_container($forum);
53
    }
54

    
55
    // Figure out how deep the hierarchy goes for the next step.
56
    if ($forum->depth > $lowest_depth) {
57
      $lowest_depth = $forum->depth;
58
    }
59
  }
60

    
61
  // Calculate number of and number of new child posts for each item.
62
  for ($depth = $lowest_depth + 1; $depth > 0; $depth--) {
63
    foreach ($items as $id => $item) {
64
      if ($depth <= $lowest_depth) {
65
        if (empty($item->depth)) {
66
          $item->depth = 0;
67
        }
68
        if ($item->depth == $depth) {
69
          $items[$item->parents[0]]->child_total_topics += $item->total_topics + $item->child_total_topics;
70
          $items[$item->parents[0]]->child_new_topics += $item->new_topics + $item->child_new_topics;
71
          $items[$item->parents[0]]->child_total_posts += $item->total_posts + $item->child_total_posts;
72
          $items[$item->parents[0]]->child_new_posts += $item->new_posts + $item->child_new_posts;
73
        }
74
      }
75

    
76
      if ($item->depth + 1 == $depth) {
77
        $parent = $items[$id]->parents[0];
78
        if (($parent != 0) && (!empty($items[$item->parents[0]]))) {
79
          $items[$item->parents[0]]->total_posts += $item->total_posts;
80
          $items[$item->parents[0]]->total_topics += $item->total_topics;
81
          $items[$item->parents[0]]->new_posts += $item->new_posts;
82
          $items[$item->parents[0]]->new_topics += $item->new_topics;
83
          if (isset($items[$parent]->link)) {
84
            $items[$parent]->new_posts_link = $items[$parent]->link . "#new";
85
            $items[$parent]->new_topics_link = $items[$parent]->link . "#new";
86
          }
87
          $items[$parent]->new_topics_text = $items[$parent]->new_topics . " new";
88
          $items[$parent]->new_posts_text = $items[$parent]->new_posts . " new";
89
        }
90
      }
91
    }
92
  }
93

    
94
  // Loop through all the items and fill the $tables variable that will
95
  // hold all the tables with the containers and forums organized, processed,
96
  // and ready for the template.
97
  if ($parent_id) {
98
    // On a container page. Fake the main table.
99
    $table_counter = 1;
100
    $tables[$table_counter]['table_info'] = advanced_forum_process_container($variables['parents'][0]);
101
  }
102
  else {
103
    $table_counter = 0;
104
  }
105

    
106
  // Workaround for container-less forums on main page.
107
  $depth_offset = 0;
108
  foreach ($items as $id => $item) {
109
    // Get a handle on the parent of this item.
110
    if ($parent_id && $item->depth == 0) {
111
      // If we are on a container page, the parent of root items is the main
112
      // container that is being used for the table info.
113
      $parent = $tables[$table_counter]['table_info'];
114
    }
115
    elseif (!empty($variables['forums'][$item->parents[0]])) {
116
      // For simplicity, we assume forums/containers have only one parent.
117
      $parent = $variables['forums'][$item->parents[0]];
118
    }
119
    else {
120
      $depth_offset = 0;
121
    }
122

    
123
    // If we aren't on the main forum page, we need to bump up the depth.
124
    $item_depth = ($parent_id) ? $item->depth + 1 : $item->depth;
125
    $item_depth += $depth_offset;
126

    
127
    if (!empty($item->container)) {
128
      // CONTAINERS.
129
      if ($item_depth == 0) {
130
        // Top level container always starts a new table.
131
        $table_counter++;
132
        $tables[$table_counter]['table_info'] = $item;
133
        $tables[$table_counter]['items'] = array();
134

    
135
        // Reset the striping.
136
        advanced_forum_stripe(TRUE);
137
      }
138
      elseif ($item_depth == 1) {
139
        // Subcontainer at top level is treated like a forum.
140
        // We set the forum icon here, rather than in the process_forum
141
        // function because we need to take into account new content
142
        // in children.
143
        if ($item->new_topics || $item->new_posts || $item->child_new_topics || $item->child_new_posts) {
144
          $item->icon_classes = "forum-list-icon forum-list-icon-new-posts";
145
          $item->icon_text = t("New posts");
146
        }
147
        else {
148
          $item->icon_classes = "forum-list-icon forum-list-icon-default";
149
          $item->icon_text = t("No new");
150
        }
151

    
152
        // Set the variable to control the row striping.
153
        $item->zebra = advanced_forum_stripe();
154

    
155
        // Add the container info to the table's item list.
156
        $tables[$table_counter]['items'][$id] = $item;
157
      }
158
      elseif ($item_depth == 2) {
159
        // A container elsewhere gets added to the parent's subcontainer list.
160
        $tables[$table_counter]['items'][$parent->tid]->subcontainer_list[$id] = $item;
161
      }
162
    }
163
    else {
164
      // FORUMS.
165
      // We set the forum icon here, rather than in the process_forum
166
      // function because we need to take into account new content
167
      // in children.
168
      if ($item->new_topics || $item->new_posts || $item->child_new_topics || $item->child_new_posts) {
169
        $item->icon_classes = "forum-list-icon forum-list-icon-new-posts";
170
        $item->icon_text = t("New posts");
171
      }
172
      else {
173
        $item->icon_classes = "forum-list-icon forum-list-icon-default";
174
        $item->icon_text = t("No new");
175
      }
176

    
177
      if ($item_depth == 0) {
178
        // This is a forum at the root. If it is the first or the first
179
        // since the last container, make a new table. Otherwise, put it in
180
        // the previous table.
181
        if (empty($table_counter) || !empty($tables[$table_counter]['table_info']->container)) {
182
          // This is the first root item or the current table belongs to a
183
          // container. Start a new generic one. We need a tid so just grab
184
          // the tid of the current forum for it.
185
          $table_counter++;
186
          if (!isset($tables[$table_counter]['table_info'])) {
187
            $tables[$table_counter]['table_info'] = new stdClass();
188
          }
189
          $tables[$table_counter]['table_info']->name = t('Forums');
190
          $tables[$table_counter]['table_info']->description = '';
191
          $tables[$table_counter]['table_info']->tid = $item->tid;
192

    
193
          // Reset the striping.
194
          advanced_forum_stripe(TRUE);
195
        }
196
        $depth_offset = 1;
197

    
198
        // Set the variable to control the row striping.
199
        $item->zebra = advanced_forum_stripe();
200

    
201
        // Add the forum info to the table's item list.
202
        $tables[$table_counter]['items'][$id] = $item;
203
      }
204
      elseif ($item_depth == 1) {
205
        // Main forum. Add it to the item list.
206
        if (empty($item->container)) {
207
          $item->zebra = advanced_forum_stripe();
208
        }
209

    
210
        $tables[$table_counter]['items'][$id] = $item;
211
      }
212
      elseif ($item_depth == 2) {
213
        // Subforum.
214
        $tables[$table_counter]['items'][$parent->tid]->subforum_list[$id] = $item;
215
      }
216
    }
217
  }
218

    
219
  // Theme subcontainers and subforums.
220
  foreach ($tables as $table_id => $table) {
221
    foreach ($table['items'] as $item_id => $item) {
222
      if (!empty($item->subforum_list)) {
223
        $tables[$table_id]['items'][$item_id]->subforums = theme('advanced_forum_subforum_list', array('subforum_list' => $item->subforum_list));
224
      }
225

    
226
      if (!empty($item->subcontainer_list)) {
227
        $tables[$table_id]['items'][$item_id]->subcontainers = theme('advanced_forum_subcontainer_list', array('subcontainer_list' => $item->subcontainer_list));
228
      }
229
    }
230
  }
231

    
232
  $variables['tables'] = $tables;
233

    
234
  // Remove unneeded variables.
235
  unset($variables['zebra']);
236
}
237

    
238
/**
239
 * Forum stripe.
240
 */
241
function advanced_forum_stripe($reset = FALSE) {
242
  static $stripe = 'odd';
243

    
244
  if ($reset) {
245
    $stripe = 'odd';
246
  }
247
  else {
248
    $stripe = ($stripe == 'odd') ? 'even' : 'odd';
249
  }
250

    
251
  return $stripe;
252
}
253

    
254
/**
255
 * Prepare an individual container for display.
256
 */
257
function advanced_forum_process_container($container) {
258
  // Create the link to the container.
259
  $container->link = url("forum/$container->tid");
260

    
261
  // Sanitise the name and description so they can be safely printed.
262
  $container->name = check_plain($container->name);
263
  $container->description = !empty($container->description) ? filter_xss_admin($container->description) : '';
264

    
265
  // Create a variable to check if the item is a container in the template.
266
  $container->is_container = TRUE;
267

    
268
  // @TODO: Make the icon change if subforums have posts.
269
  $container->icon_classes = "forum-list-icon forum-list-icon-default";
270
  $container->icon_text = t("No new");
271

    
272
  // Initialize these variables to avoid notices later.
273
  $container->total_topics = 0;
274
  $container->new_topics = 0;
275
  $container->total_posts = 0;
276
  $container->new_posts = 0;
277
  $container->child_total_topics = 0;
278
  $container->child_new_topics = 0;
279
  $container->child_total_posts = 0;
280
  $container->child_new_posts = 0;
281

    
282
  // Rename these to make them more descriptive.
283
  if (isset($container->num_topics)) {
284
    $container->total_topics = $container->num_topics;
285
    unset($container->num_topics);
286
  }
287

    
288
  if (isset($container->num_posts)) {
289
    $container->total_posts = $container->num_posts;
290
    unset($container->num_posts);
291
  }
292

    
293
  return $container;
294
}
295

    
296
/**
297
 * Prepare an individual forum for display.
298
 */
299
function advanced_forum_process_forum($forum) {
300
  // Create a variable to check if the item is a container in the template.
301
  $forum->is_container = FALSE;
302

    
303
  // Create the link to the forum.
304
  $forum->link = url("forum/$forum->tid");
305

    
306
  // Sanitise the name and description so they can be safely printed.
307
  // We don't do this for subforum names because that is sent through l()
308
  // in the theme function which runs it through check_plain().
309
  $forum->name = empty($forum->parents[0]) ? check_plain($forum->name) : $forum->name;
310
  $forum->description = !empty($forum->description) ? filter_xss_admin($forum->description) : '';
311

    
312
  // Initialize these variables to avoid notices later since not all forums
313
  // have new content or even any content at all.
314
  $forum->total_topics = 0;
315
  $forum->child_total_topics = 0;
316

    
317
  $forum->new_topics = 0;
318
  $forum->new_topics_text = '';
319
  $forum->new_topics_link = '';
320
  $forum->child_new_topics = 0;
321

    
322
  $forum->total_posts = 0;
323
  $forum->child_total_posts = 0;
324

    
325
  $forum->new_posts = 0;
326
  $forum->new_posts_text = '';
327
  $forum->new_posts_link = '';
328
  $forum->child_new_posts = 0;
329

    
330
  // Rename these to make them more descriptive.
331
  if (isset($forum->num_topics)) {
332
    $forum->total_topics = $forum->num_topics;
333
    unset($forum->num_topics);
334
  }
335

    
336
  if (isset($forum->num_posts)) {
337
    $forum->total_posts = $forum->num_posts;
338
    unset($forum->num_posts);
339
  }
340

    
341
  // If the viewer is authenticated, check for new topics and posts.
342
  global $user;
343
  if ($user->uid) {
344
    // New topics.
345
    $forum->new_topics = _forum_topics_unread($forum->tid, $user->uid);
346
    if ($forum->new_topics) {
347
      $forum->new_topics_text = format_plural($forum->new_topics, '1 new', '@count new');
348
      $forum->new_topics_link = url("forum/$forum->tid", array('fragment' => 'new'));
349
    }
350

    
351
    // New posts are optional because the query is slow.
352
    if (variable_get('advanced_forum_get_new_comments', 0)) {
353
      $forum->new_posts = advanced_forum_unread_replies_in_forum($forum->tid, $user->uid) + $forum->new_topics;
354

    
355
      if ($forum->new_posts) {
356
        $forum->new_posts_text = format_plural($forum->new_posts, '1 new', '@count new');
357
        $forum->new_posts_path = "forum/$forum->tid";
358
        $forum->new_posts_link = url("forum/$forum->tid", array('fragment' => 'new'));
359
      }
360
    }
361
  }
362

    
363
  // Process the "last post" object into a printable string.
364
  // Trying to copy the string back into the variable directly caused odd bugs
365
  // so we move it to a temp variable then unset the original.
366
  // Before doing so, however, we make a copy in case another module implementing
367
  // hook_TYPE_alter() needs access to the raw data.
368
  $forum->last_post_obj = empty($forum->last_post) ? new stdClass() : $forum->last_post;
369
  $last_post = (empty($forum->last_post)) ? '' : $forum->last_post;
370
  unset($forum->last_post);
371
  $forum->last_post = theme('forum_submitted', array('topic' => $last_post));
372

    
373
  // Display forum images using Field API.
374
  $entity = entity_load('taxonomy_term', array($forum->tid));
375
  $field_name = variable_get('advanced_forum_forum_image_field', '');
376

    
377
  if (!empty($entity[$forum->tid]->$field_name)) {
378
    $field = $entity[$forum->tid]->$field_name;
379
    global $language;
380
    $langcode = isset($field[$language->language]) ? $language->language : LANGUAGE_NONE;
381

    
382
    if (isset($field[$langcode])) {
383
      $display = array(
384
        'label' => 'hidden',
385
        'type' => 'image',
386
        'settings' => array(
387
          'image_style' => variable_get('advanced_forum_forum_image_preset', ''),
388
          'image_link' => '',
389
        ),
390
      );
391
      $output = field_view_value('taxonomy_term', $entity[$forum->tid], $field_name, $field[$langcode][0], $display);
392
      $forum->forum_image = render($output);
393
    }
394
  }
395

    
396
  drupal_alter('advanced_forum', $forum);
397
  return $forum;
398
}