5 * Copyright 1999-2000, Tim Perdue/Sourceforge
6 * Copyright 2002, Tim Perdue/GForge, LLC
7 * Copyright 2009, Roland Mas
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 require_once $gfcommon.'include/Error.class.php';
26 require_once $gfcommon.'forum/Forum.class.php';
28 class ForumFactory extends Error {
47 * @param object The Group object to which this forum is associated.
49 function ForumFactory(&$Group) {
51 if (!$Group || !is_object($Group)) {
52 $this->setError(_('Forum:: No Valid Group Object'));
55 if ($Group->isError()) {
56 $this->setError(_('Forum').':: '.$Group->getErrorMessage());
59 $this->Group =& $Group;
65 * getGroup - get the Group object this ForumFactory is associated with.
67 * @return object The Group object.
69 function &getGroup() {
73 function &getAllForumIds() {
75 $res = db_query_params('SELECT group_forum_id FROM forum_group_list
76 WHERE group_forum_id NOT IN (
77 SELECT group_forum_id FROM forum_group_list WHERE group_forum_id IN (
78 SELECT forum_id FROM news_bytes))
80 ORDER BY group_forum_id',
81 array ($this->Group->getID()));
85 while ($arr = db_fetch_array($res)) {
86 $result[] = $arr['group_forum_id'] ;
91 function &getAllForumIdsWithNews() {
93 $res = db_query_params('SELECT group_forum_id FROM forum_group_list WHERE group_id=$1 ORDER BY group_forum_id',
94 array ($this->Group->getID()));
98 while ($arr = db_fetch_array($res)) {
99 $result[] = $arr['group_forum_id'] ;
105 * getForums - get an array of Forum objects for this Group.
107 * @return array The array of Forum objects.
109 function &getForums() {
111 return $this->forums;
114 $this->forums = array () ;
115 $ids = $this->getAllForumIds() ;
118 foreach ($ids as $id) {
119 if (forge_check_perm ('forum', $id, 'read')) {
120 $this->forums[] = new Forum($this->Group, $id);
124 return $this->forums;
128 * getForumsAdmin - get an array of all (public, private and suspended) Forum objects for this Group.
130 * @return array The array of Forum objects.
132 function &getForumsAdmin() {
134 return $this->forums;
138 if (session_loggedin()) {
139 if (!forge_check_perm ('forum_admin', $this->Group->getID())) {
140 $this->setError(_("You don't have a permission to access this page"));
141 $this->forums = false;
143 $result = db_query_params ('SELECT * FROM forum_group_list_vw
145 ORDER BY group_forum_id',
146 array ($this->Group->getID())) ;
149 $this->setError(_("You don't have a permission to access this page"));
150 $this->forums = false;
153 $rows = db_numrows($result);
156 $this->setError(_('Forum not found').' : '.db_error());
157 $this->forums = false;
159 while ($arr = db_fetch_array($result)) {
160 $this->forums[] = new Forum($this->Group, $arr['group_forum_id'], $arr);
163 return $this->forums;
167 * moveThread - move thread in another forum
169 * @param string The forum ID
170 * @param int The thread_id of the tread to change.
171 * @param string The old forum ID
174 * old forum ID is useless if forum_agg_msg_count table is no longer used
176 * @return boolean success.
178 function moveThread($group_forum_id,$thread_id,$old_forum_id = false) {
179 $res = db_query_params('UPDATE forum SET group_forum_id=$1 WHERE thread_id=$2',
180 array($group_forum_id, $thread_id));
182 $this->setError(db_error());
185 $msg_count = db_affected_rows($res);
186 if ($msg_count < 1) {
187 $this->setError("Thread not found");
192 if ($old_forum_id !== false)
194 // Update forum_agg_msg_count table
195 // Note: if error(s) are raised it's certainly because forum_agg_msg_count
196 // is no longer used and updated. So, error(s) are not catched
197 // Update row of old forum id
198 $res = db_query_params('SELECT count FROM forum_agg_msg_count WHERE group_forum_id=$1',
199 array($old_forum_id));
200 if ($res && db_numrows($res)) {
202 $count = db_result($res, 0, 'count');
203 $count -= $msg_count;
204 if ($count < 0) $count = 0;
205 $res = db_query_params('UPDATE forum_agg_msg_count SET count=$1 WHERE group_forum_id=$2',
206 array($count, $old_forum_id));
209 // Error because row doesn't exist... insert it
210 $res = db_query_params('SELECT COUNT(*) AS count FROM forum WHERE group_forum_id=$1',
211 array($old_forum_id));
212 if ($res && db_numrows($res)) {
213 $count = db_result($res, 0, 'count');
214 $res = db_query_params('INSERT INTO forum_agg_msg_count (group_forum_id, count) VALUES ($1,$2)',
215 array($old_forum_id, $count));
219 // Update row of new forum id
220 $res = db_query_params('SELECT count FROM forum_agg_msg_count WHERE group_forum_id=$1',
221 array($group_forum_id));
222 if ($res && db_numrows($res)) {
224 $count = db_result($res, 0, 'count');
225 $count += $msg_count;
226 $res = db_query_params('UPDATE forum_agg_msg_count SET count=$1 WHERE group_forum_id=$2',
227 array($count, $group_forum_id));
231 $res = db_query_params('INSERT INTO forum_agg_msg_count (group_forum_id, count) VALUES ($1,$2)',
232 array($group_forum_id, $msg_count));
243 // c-file-style: "bsd"