Projet

Général

Profil

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

root / drupal7 / sites / all / modules / advanced_forum / includes / core-overrides.inc @ 74f6bef0

1
<?php
2

    
3
/**
4
 * @file
5
 * User page callbacks for the forum module.
6
 */
7

    
8
/**
9
 * Menu callback; prints a forum listing.
10
 */
11
function advanced_forum_page($forum_term = NULL) {
12
  if (!isset($forum_term)) {
13
    // On the main page, display all the top-level forums.
14
    $forum_term = advanced_forum_forum_load(0);
15
  }
16

    
17
  // Set tid for <root> container
18
  if (!isset($forum_term->tid)) {
19
    $forum_term->tid = 0;
20
  }
21
  _advanced_forum_add_files();
22

    
23
  $forum_per_page = variable_get('forum_per_page', 25);
24
  $sortby = variable_get('forum_order', 1);
25

    
26
  if (empty($forum_term->container)) {
27
    $topics = advanced_forum_get_topics($forum_term->tid, $sortby, $forum_per_page);
28
  }
29
  else {
30
    $topics = '';
31
  }
32

    
33
  $vid = variable_get('forum_nav_vocabulary', 0);
34
  $vocabulary = taxonomy_vocabulary_load($vid);
35

    
36
  // Breadcrumb navigation:
37
  $breadcrumb[] = l(t('Home'), NULL);
38
  if ($forum_term->tid) {
39
    $breadcrumb[] = l($vocabulary->name, 'forum');
40
  }
41

    
42
  if ($forum_term->parents) {
43
    foreach (array_reverse($forum_term->parents) as $p) {
44
      if ($p->tid != $forum_term->tid) {
45
        $breadcrumb[] = l($p->name, 'forum/' . $p->tid);
46
      }
47
      else {
48
        $title = $p->name;
49
      }
50
    }
51
  }
52

    
53
  if (empty($title)) {
54
    $title = $vocabulary->name;
55
  }
56

    
57

    
58
  if (!variable_get('advanced_forum_disable_breadcrumbs', FALSE)) {
59
    drupal_set_breadcrumb($breadcrumb);
60
  }
61

    
62
  drupal_set_title($title);
63

    
64
  return theme('forums', array(
65
    'forums' => $forum_term->forums,
66
    'topics' => $topics,
67
    'parents' => $forum_term->parents,
68
    'tid' => $forum_term->tid,
69
    'sortby' => $sortby,
70
    'forums_per_page' => $forum_per_page
71
  ));
72
}
73

    
74
/**
75
 * Returns a tree of all forums for a given taxonomy term ID.
76
 *
77
 * This is copied from the forum module and adapted.
78
 *
79
 * @param $tid
80
 *    (optional) Taxonomy ID of the forum, if not givin all forums will be returned.
81
 * @return
82
 *   A tree of taxonomy objects, with the following additional properties:
83
 *    - 'num_topics': Number of topics in the forum
84
 *    - 'num_posts': Total number of posts in all topics
85
 *    - 'last_post': Most recent post for the forum
86
 *    - 'forums': An array of child forums
87
 */
88
function advanced_forum_forum_load($tid = NULL) {
89
  $cache = &drupal_static(__FUNCTION__, array());
90

    
91

    
92
  // Return a cached forum tree if available.
93
  if (!isset($tid)) {
94
    $tid = 0;
95
  }
96
  if (isset($cache[$tid])) {
97
    return $cache[$tid];
98
  }
99

    
100
  // Find out from the style's .info how many posts per forum to collect.
101
  $info = advanced_forum_style_info();
102
  $post_count = isset($info['forum list post count']) ? intval($info['forum list post count']) : 1;
103

    
104
  $vid = variable_get('forum_nav_vocabulary', 0);
105

    
106
  // Load and validate the parent term.
107
  if ($tid) {
108
    $forum_term = taxonomy_term_load($tid);
109

    
110
    if (module_exists('i18n_taxonomy')) {
111
      $forum_term = i18n_taxonomy_localize_terms($forum_term);
112
    }
113
    if (!$forum_term || ($forum_term->vid != $vid)) {
114
      return $cache[$tid] = FALSE;
115
    }
116
  }
117
  // If $tid is 0, create an empty object to hold the child terms.
118
  elseif ($tid === 0) {
119
    $forum_term = (object) array(
120
          'tid' => 0,
121
    );
122
  }
123

    
124
  // Determine if the requested term is a container.
125
  if (!$forum_term->tid || in_array($forum_term->tid, variable_get('forum_containers', array()))) {
126
    $forum_term->container = 1;
127
  }
128

    
129
  // Load parent terms.
130
  $forum_term->parents = taxonomy_get_parents_all($forum_term->tid);
131

    
132
  // Load the tree below.
133
  $forums = array();
134
  $_forums = taxonomy_get_tree($vid, $tid);
135

    
136
  if (module_exists('i18n_taxonomy')) {
137
    $_forums = i18n_taxonomy_localize_terms($_forums);
138
  }
139

    
140
  if (count($_forums)) {
141
    $query = db_select('node', 'n');
142
    $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
143
    $query->join('forum_index', 'f', 'n.nid = f.nid');
144
    $query->addExpression('COUNT(DISTINCT(f.nid))', 'topic_count');
145
    $query->addExpression('SUM(f.comment_count)', 'comment_count');
146

    
147
    // Limit the query to only node types that can actually be associated with
148
    // the forum vocabulary.
149
    if ($vid) {
150
      $vocabulary = taxonomy_vocabulary_load($vid);
151
      $vocabulary_fields = field_read_fields(array('module' => 'taxonomy'));
152
      $node_types = array();
153
      foreach ($vocabulary_fields as $field_name => $vocabulary_field) {
154
        foreach ($vocabulary_field['settings']['allowed_values'] as $key => $allowed_value) {
155
          if ($allowed_value['vocabulary'] == $vocabulary->machine_name) {
156
            $field_info = field_info_field($field_name);
157
            if (isset($field_info['bundles']['node'])) {
158
              $node_types += $field_info['bundles']['node'];
159
            }
160
          }
161
        }
162
      }
163

    
164
      if (!empty($node_types)) {
165
        $query->condition('type', $node_types, 'IN');
166
      }
167
    }
168

    
169
    $counts = $query
170
        ->fields('f', array('tid'))
171
        ->condition('status', 1)
172
        ->groupBy('tid')
173
        ->addTag('node_access')
174
        ->execute()
175
        ->fetchAllAssoc('tid');
176
  }
177

    
178
  foreach ($_forums as $forum) {
179
    // Determine if the child term is a container.
180
    if (in_array($forum->tid, variable_get('forum_containers', array()))) {
181
      $forum->container = 1;
182
    }
183

    
184
    // Merge in the topic and post counters.
185
    if (!empty($counts[$forum->tid])) {
186
      $forum->num_topics = $counts[$forum->tid]->topic_count;
187
      $forum->num_posts = $counts[$forum->tid]->topic_count + $counts[$forum->tid]->comment_count;
188
    }
189
    else {
190
      $forum->num_topics = 0;
191
      $forum->num_posts = 0;
192
    }
193

    
194
    // Query "Last Post" information for this forum.
195
    $query = db_select('node', 'n');
196
    $query->join('forum_index', 'f', 'n.nid = f.nid AND f.tid = :tid', array(':tid' => $forum->tid));
197
    $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
198
    $query->join('users', 'u', 'ncs.last_comment_uid = u.uid');
199
    $query->addExpression('CASE ncs.last_comment_uid WHEN 0 THEN ncs.last_comment_name ELSE u.name END', 'last_comment_name');
200

    
201
    $topics = $query
202
        ->fields('ncs', array('last_comment_timestamp', 'last_comment_uid'))
203
        ->fields('n', array('nid', 'title', 'type'))
204
        ->condition('n.status', 1)
205
        ->orderBy('last_comment_timestamp', 'DESC')
206
        ->range(0, $post_count)
207
        ->addTag('node_access')
208
        ->execute();
209

    
210
    while ($topic = $topics->fetchObject()) {
211
      // Merge in the "Last Post" information.
212
      $last_post = new stdClass();
213
      if (!empty($topic->last_comment_timestamp)) {
214
        $last_post->nid = $topic->nid;
215
        $last_post->node_title = $topic->title;
216
        $last_post->type = $topic->type;
217
        $last_post->created = $topic->last_comment_timestamp;
218
        $last_post->name = $topic->last_comment_name;
219
        $last_post->uid = $topic->last_comment_uid;
220
      }
221
      if ($post_count > 1) {
222
        $forum->last_post[] = $last_post;
223
      }
224
      else {
225
        $forum->last_post = $last_post;
226
      }
227
    }
228

    
229
    $forums[$forum->tid] = $forum;
230
  }
231

    
232
  // Cache the result, and return the tree.
233
  $forum_term->forums = $forums;
234
  $cache[$tid] = $forum_term;
235
  return $forum_term;
236
}
237

    
238
function _advanced_forum_update_parent_post_count(&$forums, $forum) {
239
  foreach ($forum->parents as $parent_tid) {
240
    if (!empty($forums[$parent_tid])) {
241
      $forums[$parent_tid]->num_topics += $forum->num_topics;
242
      $forums[$parent_tid]->num_posts += $forum->num_posts;
243

    
244
      // recursive loop to update all parents
245
      if (!empty($forums[$parent_tid]->parents)) {
246
        _advanced_forum_update_parent_post_count($forums, $forums[$parent_tid]);
247
      }
248
    }
249
  }
250
}
251

    
252
/**
253
 * This is copied from the forum module and adapted.
254
 */
255
function advanced_forum_get_topics($tid, $sortby, $forum_per_page, $sort_form = TRUE) {
256
  $term = taxonomy_term_load($tid);
257
  drupal_add_feed('taxonomy/term/' . $tid . '/feed', 'RSS - ' . check_plain($term->name));
258

    
259
  // Views handles this page
260
  $view = views_get_view('advanced_forum_topic_list');
261
  $view->set_items_per_page($forum_per_page);
262
  $view->sort_form = $sort_form;
263

    
264
  return $view->preview('default', array($tid));
265
}
266