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)) {
54 if ($Group->isError()) {
55 $this->setError(_('Forum').':: '.$Group->getErrorMessage());
58 if (!$Group->usesForum()) {
59 $this->setError(sprintf(_('%s does not use the Forum tool'),
60 $Group->getPublicName()));
63 $this->Group =& $Group;
69 * getGroup - get the Group object this ForumFactory is associated with.
71 * @return object The Group object.
73 function &getGroup() {
77 function &getAllForumIds() {
79 $res = db_query_params('SELECT group_forum_id FROM forum_group_list
80 WHERE group_forum_id NOT IN (
81 SELECT group_forum_id FROM forum_group_list WHERE group_forum_id IN (
82 SELECT forum_id FROM news_bytes))
84 ORDER BY group_forum_id',
85 array ($this->Group->getID()));
89 while ($arr = db_fetch_array($res)) {
90 $result[] = $arr['group_forum_id'] ;
95 function &getAllForumIdsWithNews() {
97 $res = db_query_params('SELECT group_forum_id FROM forum_group_list WHERE group_id=$1 ORDER BY group_forum_id',
98 array ($this->Group->getID()));
102 while ($arr = db_fetch_array($res)) {
103 $result[] = $arr['group_forum_id'] ;
109 * getForums - get an array of Forum objects for this Group.
111 * @return array The array of Forum objects.
113 function &getForums() {
115 return $this->forums;
118 $this->forums = array () ;
119 $ids = $this->getAllForumIds() ;
122 foreach ($ids as $id) {
123 if (forge_check_perm ('forum', $id, 'read')) {
124 $this->forums[] = new Forum($this->Group, $id);
128 return $this->forums;
132 * getForumsAdmin - get an array of all (public, private and suspended) Forum objects for this Group.
134 * @return array The array of Forum objects.
136 function &getForumsAdmin() {
138 return $this->forums;
142 if (session_loggedin()) {
143 if (!forge_check_perm ('forum_admin', $this->Group->getID())) {
144 $this->setError(_("You don't have a permission to access this page"));
145 $this->forums = false;
147 $result = db_query_params ('SELECT * FROM forum_group_list_vw
149 ORDER BY group_forum_id',
150 array ($this->Group->getID())) ;
153 $this->setError(_("You don't have a permission to access this page"));
154 $this->forums = false;
157 $rows = db_numrows($result);
160 $this->setError(_('Forum not found').' : '.db_error());
161 $this->forums = false;
163 while ($arr = db_fetch_array($result)) {
164 $this->forums[] = new Forum($this->Group, $arr['group_forum_id'], $arr);
167 return $this->forums;
171 * moveThread - move thread in another forum
173 * @param string The forum ID
174 * @param int The thread_id of the tread to change.
175 * @param string The old forum ID
178 * old forum ID is useless if forum_agg_msg_count table is no longer used
180 * @return boolean success.
182 function moveThread($group_forum_id,$thread_id,$old_forum_id = false) {
183 $res = db_query_params('UPDATE forum SET group_forum_id=$1 WHERE thread_id=$2',
184 array($group_forum_id, $thread_id));
186 $this->setError(db_error());
189 $msg_count = db_affected_rows($res);
190 if ($msg_count < 1) {
191 $this->setError("Thread not found");
196 if ($old_forum_id !== false)
198 // Update forum_agg_msg_count table
199 // Note: if error(s) are raised it's certainly because forum_agg_msg_count
200 // is no longer used and updated. So, error(s) are not catched
201 // Update row of old forum id
202 $res = db_query_params('SELECT count FROM forum_agg_msg_count WHERE group_forum_id=$1',
203 array($old_forum_id));
204 if ($res && db_numrows($res)) {
206 $count = db_result($res, 0, 'count');
207 $count -= $msg_count;
208 if ($count < 0) $count = 0;
209 $res = db_query_params('UPDATE forum_agg_msg_count SET count=$1 WHERE group_forum_id=$2',
210 array($count, $old_forum_id));
213 // Error because row doesn't exist... insert it
214 $res = db_query_params('SELECT COUNT(*) AS count FROM forum WHERE group_forum_id=$1',
215 array($old_forum_id));
216 if ($res && db_numrows($res)) {
217 $count = db_result($res, 0, 'count');
218 $res = db_query_params('INSERT INTO forum_agg_msg_count (group_forum_id, count) VALUES ($1,$2)',
219 array($old_forum_id, $count));
223 // Update row of new forum id
224 $res = db_query_params('SELECT count FROM forum_agg_msg_count WHERE group_forum_id=$1',
225 array($group_forum_id));
226 if ($res && db_numrows($res)) {
228 $count = db_result($res, 0, 'count');
229 $count += $msg_count;
230 $res = db_query_params('UPDATE forum_agg_msg_count SET count=$1 WHERE group_forum_id=$2',
231 array($count, $group_forum_id));
235 $res = db_query_params('INSERT INTO forum_agg_msg_count (group_forum_id, count) VALUES ($1,$2)',
236 array($group_forum_id, $msg_count));
247 // c-file-style: "bsd"