/[drupal]/contributions/modules/forumthread/forumthread.module
ViewVC logotype

Contents of /contributions/modules/forumthread/forumthread.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Thu Mar 27 21:53:05 2008 UTC (20 months ago) by brauerranch
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.4: +3 -3 lines
File MIME type: text/x-php
fixing a bug in db_rewrite_sql implementation
1 <?php
2 // $Id: forumthread.module,v 1.4 2008/03/24 06:12:59 brauerranch Exp $
3
4 /**
5 * @file
6 * Enables an alternate view of the standard Drupal forums with Forum Topics
7 *
8 * The forumthread module provides a list view of forum modes
9 * showing the title of the node and the subject of all replies
10 * in a single threaded view.
11 */
12
13 function forumthread_menu($may_cache) {
14 $items = array();
15
16 if ($may_cache) {
17 $items[] = array('path' => 'forum',
18 'title' => t('Forums'),
19 'callback' => 'forumthread_page',
20 'access' => user_access('access content'),
21 'type' => MENU_NORMAL_ITEM);
22 }
23 return $items;
24 }
25
26 /**
27 * Menu callback; prints a forum listing.
28 */
29 function forumthread_page($tid = 0) {
30 drupal_set_title(t('Forum'));
31 global $user;
32 $forum_per_page = variable_get('forum_per_page', 25);
33
34 drupal_add_css(drupal_get_path('module', 'forumthread') .'/forumthread.css');
35 if ($tid && !in_array($tid, variable_get('forum_containers', array()))) {
36 if (user_access('create forum topics')) {
37 $output .= '<li>'. l(t('Post new forum topic.'), "node/add/forum/$tid") .'</li>';
38 }
39 else if ($user->uid) {
40 $output .= '<li>'. t('You are not allowed to post a new forum topic.') .'</li>';
41 }
42 else {
43 $output .= '<li>'. t('<a href="@login">Login</a> to post a new forum topic.', array('@login' => url('user/login', drupal_get_destination()))) .'</li>';
44 }
45 $output .= '</ul>';
46 $output .= forumthread_render($tid);
47 if (user_access('create forum topics')) {
48 $output .= '<li>'. l(t('Post new forum topic.'), "node/add/forum/$tid") .'</li>';
49 }
50 else if ($user->uid) {
51 $output .= '<li>'. t('You are not allowed to post a new forum topic.') .'</li>';
52 }
53 else {
54 $output .= '<li>'. t('<a href="@login">Login</a> to post a new forum topic.', array('@login' => url('user/login', drupal_get_destination()))) .'</li>';
55 }
56 $output .= '</ul>';
57 }
58 else {
59 if (user_access('create forum topics')) {
60 $output .= '<li>'. l(t('Post new forum topic.'), "node/add/forum/$tid") .'</li>';
61 }
62 else if ($user->uid) {
63 $output .= '<li>'. t('You are not allowed to post a new forum topic.') .'</li>';
64 }
65 else {
66 $output .= '<li>'. t('<a href="@login">Login</a> to post a new forum topic.', array('@login' => url('user/login', drupal_get_destination()))) .'</li>';
67 }
68 $output .= '</ul>';
69
70 $query = "SELECT tid from {forum} f group by tid";
71 $query = db_rewrite_sql($query, 'f', 'nid');
72 $result = pager_query($query, $forum_per_page, 0);
73 while ($fid = db_fetch_object($result)) {
74 $output .= forumthread_render($fid->tid);
75 }
76 if (user_access('create forum topics')) {
77 $output .= '<li>'. l(t('Post new forum topic.'), "node/add/forum/$tid") .'</li>';
78 }
79 else if ($user->uid) {
80 $output .= '<li>'. t('You are not allowed to post a new forum topic.') .'</li>';
81 }
82 else {
83 $output .= '<li>'. t('<a href="@login">Login</a> to post a new forum topic.', array('@login' => url('user/login', drupal_get_destination()))) .'</li>';
84 }
85 $output .= '</ul>';
86
87
88 }
89 return $output;
90 }
91
92 /**
93 * Return a structured object of the comments for a specific node.
94 *
95 * @param $nid
96 * The node id for the node that comments are being loaded for
97 * @param $cid
98 * The comment id of a specific comment -- TODO We probably don't need this here since we're not rendering
99 * nodes.
100 * @return
101 * Returns a structured object with the node and threaded comments.
102 */
103 function get_node_comments($nid, $cid = 0) {
104 //make sure user has rights to access comments or we don't return any
105 if (user_access('access comments')) {
106 // Pre-process variables.
107 $mode = _comment_get_display_setting('mode');
108 $order = _comment_get_display_setting('sort');
109 $comments_per_page = _comment_get_display_setting('comments_per_page');
110
111 if ($cid) {
112 // Single comment view.
113 $query = 'SELECT c.cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name,
114 c.mail, c.homepage, u.uid, u.name AS registered_name, u.picture, u.data, c.score, c.users,
115 c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d';
116 $query_args = array($cid);
117 if (!user_access('administer comments')) {
118 $query .= ' AND c.status = %d';
119 $query_args[] = COMMENT_PUBLISHED;
120 }
121 $result = db_query($query, $query_args);
122 if ($comment = db_fetch_object($result)) {
123 $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
124 $links = module_invoke_all('link', 'comment', $comment, 1);
125 foreach (module_implements('link_alter') as $module) {
126 $function = $module .'_link_alter';
127 $function($node, $links);
128 }
129 $output .= theme('comment_view', $comment, $links);
130 }
131 }
132 else {
133 // Multiple comment view
134 $query_count = 'SELECT COUNT(*) FROM {comments} WHERE nid = %d';
135 $query = 'SELECT c.cid as cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage,
136 u.uid, u.name AS registered_name, u.picture, u.data, c.score, c.users, c.thread, c.status FROM {comments} c INNER JOIN
137 {users} u ON c.uid = u.uid WHERE c.nid = %d';
138 $query_args = array($nid);
139 if (!user_access('administer comments')) {
140 $query .= ' AND c.status = %d';
141 $query_count .= ' AND status = %d';
142 $query_args[] = COMMENT_PUBLISHED;
143 }
144 if ($order == COMMENT_ORDER_NEWEST_FIRST) {
145 if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
146 $query .= ' ORDER BY c.cid DESC';
147 }
148 else {
149 $query .= ' ORDER BY c.thread DESC';
150 }
151 }
152 else if ($order == COMMENT_ORDER_OLDEST_FIRST) {
153 if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
154 $query .= ' ORDER BY c.cid';
155 }
156 else {
157 /*
158 ** See comment above. Analysis learns that this doesn't cost
159 ** too much. It scales much much better than having the whole
160 ** comment structure.
161 */
162 $query .= ' ORDER BY SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))';
163 }
164 }
165 }
166 $result = db_query($query, $nid);
167 }
168
169 while ($commentt = db_fetch_object($result)) {
170 $comment = drupal_unpack($commentt);
171 $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
172 $comment->depth = count(explode('.', $comment->thread)) - 1;
173 $comment_thread[] = $comment;
174 }
175 return $comment_thread;
176 }
177
178 /**
179 *forumthread_render returns an HTML formatted threaded forum
180 */
181 function theme_forumthread($forum) {
182 $breadcrumb[] = array('path' => $_GET['q']);
183 menu_set_location($breadcrumb);
184 foreach ($forum as $topic) {
185 $output .= "<ul class=\"thread\"><li class=\"topic\"><a href=\"/node/" . $topic->nid ."\">" . $topic->title ."</a>";
186 $output .= " <span class=\"author\"> - <a href=\"/user/" . $topic->uid ."\">" . $topic->name ."</a></span>";
187 $output .= "<span class=\"postdate\">, " . format_date($topic->timestamp) ."</span></li>";
188 $divs = 0;
189 $last_depth = 0;
190 if ($topic->comment_array) {
191 foreach ($topic->comment_array as $comment) {
192 $comment->depth ++;
193 if ($comment->depth > $last_depth) {
194 $divs++;
195 $output .= '<ul class="indented">';
196 $last_depth++;
197 }
198 else {
199 while ($comment->depth < $last_depth) {
200 $divs--;
201 $output .= '</ul>';
202 $last_depth--;
203 }
204 }
205 $output .= "<li class=\"response\"><a href=\"/node/" . $comment->nid ."#comment-" . $comment->cid ."\">" . $comment->subject ."</a> - ";
206 if ($comment->name ? $output .= " <span class=\"author\"><a href=\"/user/" . $comment->uid ."\">" . $comment->name ."</a>,</span>" : "");
207 $output .= "<span class=\"postdate\"> " . format_date($comment->timestamp) ."</span></li>";
208 }
209 }
210 //using -1 adds one extra close ul tag for the topic that opened the topic.
211 for ($i = -1; $i < $divs; $i++) {
212 $output .= '</ul>';
213 }
214 }
215
216 return $output;
217 }
218
219 function fourmthread_comment($arg1, $op) {
220 switch ($op) {
221 case 'list':
222 break;
223 default:
224 }
225 }
226
227 /**
228 * Implementation of hook view
229 */
230 function forumthread_render($tid) {
231 $forum_per_page = variable_get('forum_per_page', 25);
232 $sortby = variable_get('forum_order', 1);
233 $forums = forum_get_forums($tid);
234 if ($page) {
235 $vocabulary = taxonomy_get_vocabulary(variable_get('forum_nav_vocabulary', ''));
236 // Breadcrumb navigation
237 $breadcrumb = array();
238 $breadcrumb[] = array('path' => 'forum', 'title' => $vocabulary->name);
239 if ($parents = taxonomy_get_parents_all($node->tid)) {
240 $parents = array_reverse($parents);
241 foreach ($parents as $p) {
242 $breadcrumb[] = array('path' => 'forum/'. $p->tid, 'title' => $p->name);
243 }
244 }
245 $breadcrumb[] = array('path' => 'node/'. $node->nid);
246 menu_set_location($breadcrumb);
247 }
248
249 foreach ($forums as $forum) {
250 if (!array_key_exists('container', $forum)) {
251 $included_topics = forum_get_topics($tid, $sortby, $forum_per_page);
252 }
253 }
254 $parents = taxonomy_get_parents_all($tid);
255 if ($tid && !in_array($tid, variable_get('forum_containers', array()))) {
256 $topics = forum_get_topics($tid, $sortby, $forum_per_page);
257 foreach ($topics as $topic) {
258 if ($topic->num_comments) {
259 $topic->comment_array = get_node_comments($topic->nid);
260 }
261 $topic_thread[] = $topic;
262 }
263 }
264 if(count($topic_thread) > 0) {
265 $output = theme('forumthread', $topic_thread);
266 }
267 $output .= theme('pager', NULL, $forum_per_page, 0);
268
269 return $output;
270 }
271 /**
272 * Implementation of hook_view().
273 */
274 function forumthread_view(&$node, $teaser = FALSE, $page = FALSE) {
275 drupal_add_css(drupal_get_path('module', 'forum') .'/forum.css');
276 if ($page) {
277 $vocabulary = taxonomy_get_vocabulary(variable_get('forum_nav_vocabulary', ''));
278 // Breadcrumb navigation
279 $breadcrumb = array();
280 $breadcrumb[] = array('path' => 'forum', 'title' => $vocabulary->name);
281 if ($parents = taxonomy_get_parents_all($node->tid)) {
282 $parents = array_reverse($parents);
283 foreach ($parents as $p) {
284 $breadcrumb[] = array('path' => 'forum/'. $p->tid, 'title' => $p->name);
285 }
286 }
287 $breadcrumb[] = array('path' => 'node/'. $node->nid);
288 menu_set_location($breadcrumb);
289 }
290
291 $node = node_prepare($node, $teaser);
292 if (!$teaser) {
293 $node->content['forum_navigation'] = array(
294 '#value' => theme('forum_topic_navigation', $node),
295 '#weight' => 100,
296 );
297 }
298 return $node;
299 }

  ViewVC Help
Powered by ViewVC 1.1.2