Projet

Général

Profil

Paste
Télécharger (8,5 ko) Statistiques
| Branche: | Révision:

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

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 (empty($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 ($cached = cache_get('adv_forum_counts' . $tid, 'cache')) {
141
    if ($cached->expire > time()) {
142
      $counts = $cached->data;
143
    }
144
  }
145
  if (count($_forums) && empty($counts)) {
146
    $query = db_select('node', 'n');
147
    $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
148
    $query->join('forum_index', 'f', 'n.nid = f.nid');
149
    $query->addExpression('COUNT(DISTINCT(f.nid))', 'topic_count');
150
    $query->addExpression('SUM(f.comment_count)', 'comment_count');
151

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

    
169
      if (!empty($node_types)) {
170
        $query->condition('type', $node_types, 'IN');
171
      }
172
    }
173

    
174
    $counts = $query
175
        ->fields('f', array('tid'))
176
        ->condition('status', 1)
177
        ->groupBy('tid')
178
        ->addTag('node_access')
179
        ->execute()
180
        ->fetchAllAssoc('tid');
181
    cache_set('adv_forum_counts' . $tid, $counts, 'cache', time() + 60*60); //1 hour
182
  }
183

    
184
  // get last nodes with comments for subquery SELECT optimization
185
  $subquery = db_select('node_comment_statistics', 'lnsc')
186
    ->fields('lnsc', array('nid'))
187
    ->distinct()
188
    ->orderBy('last_comment_timestamp', 'DESC')
189
    ->range(0, 1000); // should be moved to options or additionally calculated elsewhere
190

    
191
  foreach ($_forums as $forum) {
192
    // Determine if the child term is a container.
193
    if (in_array($forum->tid, variable_get('forum_containers', array()))) {
194
      $forum->container = 1;
195
    }
196

    
197
    // Merge in the topic and post counters.
198
    if (!empty($counts[$forum->tid])) {
199
      $forum->num_topics = $counts[$forum->tid]->topic_count;
200
      $forum->num_posts = $counts[$forum->tid]->topic_count + $counts[$forum->tid]->comment_count;
201
    }
202
    else {
203
      $forum->num_topics = 0;
204
      $forum->num_posts = 0;
205
    }
206

    
207
    // Query "Last Post" information for this forum.
208
    $query = db_select('node', 'n');
209
    $query->join($subquery, 'lastnodes', 'n.nid = lastnodes.nid');
210
    $query->join('forum_index', 'f', 'n.nid = f.nid AND f.tid = :tid', array(':tid' => $forum->tid));
211
    $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
212
    $query->join('users', 'u', 'ncs.last_comment_uid = u.uid');
213
    $query->addExpression('CASE ncs.last_comment_uid WHEN 0 THEN ncs.last_comment_name ELSE u.name END', 'last_comment_name');
214

    
215
    $topics = $query
216
        ->fields('ncs', array('last_comment_timestamp', 'last_comment_uid'))
217
        ->fields('n', array('nid', 'title', 'type'))
218
        ->condition('n.status', 1)
219
        ->orderBy('last_comment_timestamp', 'DESC')
220
        ->range(0, $post_count)
221
        ->addTag('node_access')
222
        ->execute();
223

    
224
    while ($topic = $topics->fetchObject()) {
225
      // Merge in the "Last Post" information.
226
      $last_post = new stdClass();
227
      if (!empty($topic->last_comment_timestamp)) {
228
        $last_post->nid = $topic->nid;
229
        $last_post->node_title = $topic->title;
230
        $last_post->type = $topic->type;
231
        $last_post->created = $topic->last_comment_timestamp;
232
        $last_post->name = $topic->last_comment_name;
233
        $last_post->uid = $topic->last_comment_uid;
234
      }
235
      if ($post_count > 1) {
236
        $forum->last_post[] = $last_post;
237
      }
238
      else {
239
        $forum->last_post = $last_post;
240
      }
241
    }
242

    
243
    $forums[$forum->tid] = $forum;
244
  }
245

    
246
  // Cache the result, and return the tree.
247
  $forum_term->forums = $forums;
248
  $cache[$tid] = $forum_term;
249
  return $forum_term;
250
}
251

    
252
function _advanced_forum_update_parent_post_count(&$forums, $forum) {
253
  foreach ($forum->parents as $parent_tid) {
254
    if (!empty($forums[$parent_tid])) {
255
      $forums[$parent_tid]->num_topics += $forum->num_topics;
256
      $forums[$parent_tid]->num_posts += $forum->num_posts;
257

    
258
      // recursive loop to update all parents
259
      if (!empty($forums[$parent_tid]->parents)) {
260
        _advanced_forum_update_parent_post_count($forums, $forums[$parent_tid]);
261
      }
262
    }
263
  }
264
}
265

    
266
/**
267
 * This is copied from the forum module and adapted.
268
 */
269
function advanced_forum_get_topics($tid, $sortby, $forum_per_page, $sort_form = TRUE) {
270
  $term = taxonomy_term_load($tid);
271
  drupal_add_feed('taxonomy/term/' . $tid . '/feed', 'RSS - ' . check_plain($term->name));
272

    
273
  // Views handles this page
274
  $view = views_get_view('advanced_forum_topic_list');
275
  $view->set_items_per_page($forum_per_page);
276
  $view->sort_form = $sort_form;
277

    
278
  return $view->preview('default', array($tid));
279
}
280