Projet

Général

Profil

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

root / drupal7 / sites / all / modules / advanced_forum / includes / advanced_forum_preprocess_forum_list.inc @ 87dbc3bf

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
function _advanced_forum_preprocess_forum_list(&$variables) {
9
  advanced_forum_add_template_suggestions("forum_list", $variables);
10

    
11
  // Added variable for default collapsed configuration
12
  $variables['collapsed_main_list'] = variable_get('advanced_forum_default_collapsed_list', array());
13

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

    
26
    drupal_add_js($settings, array('type' => 'setting'));
27
    $variables['collapsible'] = TRUE;
28
  }
29

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

    
36
  unset($variables['tid']);
37

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

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

    
51
    // Figure out how deep the hierarchy goes for the next step.
52
    if ($forum->depth > $lowest_depth) {
53
      $lowest_depth = $forum->depth;
54
    }
55
  }
56

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

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

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

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

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

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

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

    
148
        // Set the variable to control the row striping.
149
        $item->zebra = advanced_forum_stripe();
150

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

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

    
188
          // Reset the striping.
189
          advanced_forum_stripe(TRUE);
190
          $depth_offset = 1;
191
        }
192

    
193
        // Set the variable to control the row striping.
194
        $item->zebra = advanced_forum_stripe();
195

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

    
205
        $tables[$table_counter]['items'][$id] = $item;
206
      }
207
      elseif ($item_depth == 2) {
208
        // Subforum.
209
        $tables[$table_counter]['items'][$parent->tid]->subforum_list[$id] = $item;
210
      }
211
    }
212
  }
213

    
214
  // Theme subcontainers and subforums.
215
  foreach ($tables as $table_id => $table) {
216
    foreach ($table['items'] as $item_id => $item) {
217
      if (!empty($item->subforum_list)) {
218
        $tables[$table_id]['items'][$item_id]->subforums = theme('advanced_forum_subforum_list', array('subforum_list' => $item->subforum_list));
219
      }
220

    
221
      if (!empty($item->subcontainer_list)) {
222
        $tables[$table_id]['items'][$item_id]->subcontainers = theme('advanced_forum_subcontainer_list', array('subcontainer_list' => $item->subcontainer_list));
223
      }
224
    }
225
  }
226

    
227
  $variables['tables'] = $tables;
228

    
229
  // Remove unneeded variables.
230
  unset($variables['zebra']);
231
}
232

    
233
function advanced_forum_stripe($reset = FALSE) {
234
  static $stripe = 'odd';
235

    
236
  if ($reset) {
237
    $stripe = 'odd';
238
  }
239
  else {
240
    $stripe = ($stripe == 'odd') ? 'even' : 'odd';
241
  }
242

    
243
  return $stripe;
244
}
245

    
246
/**
247
 * Prepare an individual container for display.
248
 */
249
function advanced_forum_process_container($container) {
250
  // Create the link to the container.
251
  $container->link = url("forum/$container->tid");
252

    
253
  // Sanitise the name and description so they can be safely printed.
254
  $container->name = check_plain($container->name);
255
  $container->description = !empty($container->description) ? filter_xss_admin($container->description) : '';
256

    
257
  // Create a variable to check if the item is a container in the template.
258
  $container->is_container = TRUE;
259

    
260
  // @TODO: Make the icon change if subforums have posts.
261
  $container->icon_classes = "forum-list-icon forum-list-icon-default";
262
  $container->icon_text = t("No new");
263

    
264
  // Initialize these variables to avoid notices later.
265
  $container->total_topics = 0;
266
  $container->new_topics = 0;
267
  $container->total_posts = 0;
268
  $container->new_posts = 0;
269
  $container->child_total_topics = 0;
270
  $container->child_new_topics = 0;
271
  $container->child_total_posts = 0;
272
  $container->child_new_posts = 0;
273

    
274
  // Rename these to make them more descriptive.
275
  if (isset($container->num_topics)) {
276
    $container->total_topics = $container->num_topics;
277
    unset($container->num_topics);
278
  }
279

    
280
  if (isset($container->num_posts)) {
281
    $container->total_posts = $container->num_posts;
282
    unset($container->num_posts);
283
  }
284

    
285
  return $container;
286
}
287

    
288
/**
289
 * Prepare an individual forum for display.
290
 */
291
function advanced_forum_process_forum($forum) {
292
  // Create a variable to check if the item is a container in the template.
293
  $forum->is_container = FALSE;
294

    
295
  // Create the link to the forum.
296
  $forum->link = url("forum/$forum->tid");
297

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

    
304
  // Initialize these variables to avoid notices later since not all forums
305
  // have new content or even any content at all.
306
  $forum->total_topics = 0;
307
  $forum->child_total_topics = 0;
308

    
309
  $forum->new_topics = 0;
310
  $forum->new_topics_text = '';
311
  $forum->new_topics_link = '';
312
  $forum->child_new_topics = 0;
313

    
314
  $forum->total_posts = 0;
315
  $forum->child_total_posts = 0;
316

    
317
  $forum->new_posts = 0;
318
  $forum->new_posts_text = '';
319
  $forum->new_posts_link = '';
320
  $forum->child_new_posts = 0;
321

    
322
  // Rename these to make them more descriptive.
323
  if (isset($forum->num_topics)) {
324
    $forum->total_topics = $forum->num_topics;
325
    unset($forum->num_topics);
326
  }
327

    
328
  if (isset($forum->num_posts)) {
329
    $forum->total_posts = $forum->num_posts;
330
    unset($forum->num_posts);
331
  }
332

    
333
  // If the viewer is authenticated, check for new topics and posts.
334
  global $user;
335
  if ($user->uid) {
336
    // New topics.
337
    $forum->new_topics = _forum_topics_unread($forum->tid, $user->uid);
338
    if ($forum->new_topics) {
339
      $forum->new_topics_text = format_plural($forum->new_topics, '1 new', '@count new');
340
      $forum->new_topics_link = url("forum/$forum->tid", array('fragment' => 'new'));
341
    }
342

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

    
347
      if ($forum->new_posts) {
348
        $forum->new_posts_text = format_plural($forum->new_posts, '1 new', '@count new');
349
        $forum->new_posts_path = "forum/$forum->tid";
350
        $forum->new_posts_link = url("forum/$forum->tid", array('fragment' => 'new'));
351
      }
352
    }
353
  }
354

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

    
365
  // Display forum images using Field API
366
  $entity = entity_load('taxonomy_term', array($forum->tid));
367
  $field_name = variable_get('advanced_forum_forum_image_field', '');
368

    
369
  if (!empty($entity[$forum->tid]->$field_name)) {
370
    $field = $entity[$forum->tid]->$field_name;
371
    global $language;
372
    $langcode = isset($field[$language->language]) ? $language->language : LANGUAGE_NONE;
373

    
374
    if (isset($field[$langcode])) {
375
      $display = array(
376
        'label' => 'hidden',
377
        'type' => 'image',
378
        'settings' => array(
379
          'image_style' => variable_get('advanced_forum_forum_image_preset', ''),
380
          'image_link' => '',
381
        ),
382
      );
383
      $output = field_view_value('taxonomy_term', $entity[$forum->tid], $field_name, $field[$langcode][0], $display);
384
      $forum->forum_image = render($output);
385
    }
386
  }
387

    
388
  drupal_alter('advanced_forum', $forum);
389
  return $forum;
390
}