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'] ;
92 * getForums - get an array of Forum objects for this Group.
94 * @return array The array of Forum objects.
96 function &getForums() {
101 $this->forums = array () ;
102 $ids = $this->getAllForumIds() ;
105 foreach ($ids as $id) {
106 if (forge_check_perm ('forum', $id, 'read')) {
107 $this->forums[] = new Forum($this->Group, $id);
111 return $this->forums;
115 * getForumsAdmin - get an array of all (public, private and suspended) Forum objects for this Group.
117 * @return array The array of Forum objects.
119 function &getForumsAdmin() {
121 return $this->forums;
125 if (session_loggedin()) {
126 if (!forge_check_perm ('forum_admin', $this->Group->getID())) {
127 $this->setError(_("You don't have a permission to access this page"));
128 $this->forums = false;
130 $result = db_query_params ('SELECT * FROM forum_group_list_vw
132 ORDER BY group_forum_id',
133 array ($this->Group->getID())) ;
136 $this->setError(_("You don't have a permission to access this page"));
137 $this->forums = false;
140 $rows = db_numrows($result);
143 $this->setError(_('Forum not found').' : '.db_error());
144 $this->forums = false;
146 while ($arr = db_fetch_array($result)) {
147 $this->forums[] = new Forum($this->Group, $arr['group_forum_id'], $arr);
150 return $this->forums;
154 * moveThread - move thread in another forum
156 * @param string The forum ID
157 * @param int The thread_id of the tread to change.
158 * @param string The old forum ID
161 * old forum ID is useless if forum_agg_msg_count table is no longer used
163 * @return boolean success.
165 function moveThread($group_forum_id,$thread_id,$old_forum_id = false) {
166 $res = db_query_params('UPDATE forum SET group_forum_id=$1 WHERE thread_id=$2',
167 array($group_forum_id, $thread_id));
169 $this->setError(db_error());
172 $msg_count = db_affected_rows($res);
173 if ($msg_count < 1) {
174 $this->setError("Thread not found");
179 if ($old_forum_id !== false)
181 // Update forum_agg_msg_count table
182 // Note: if error(s) are raised it's certainly because forum_agg_msg_count
183 // is no longer used and updated. So, error(s) are not catched
184 // Update row of old forum id
185 $res = db_query_params('SELECT count FROM forum_agg_msg_count WHERE group_forum_id=$1',
186 array($old_forum_id));
187 if ($res && db_numrows($res)) {
189 $count = db_result($res, 0, 'count');
190 $count -= $msg_count;
191 if ($count < 0) $count = 0;
192 $res = db_query_params('UPDATE forum_agg_msg_count SET count=$1 WHERE group_forum_id=$2',
193 array($count, $old_forum_id));
196 // Error because row doesn't exist... insert it
197 $res = db_query_params('SELECT COUNT(*) AS count FROM forum WHERE group_forum_id=$1',
198 array($old_forum_id));
199 if ($res && db_numrows($res)) {
200 $count = db_result($res, 0, 'count');
201 $res = db_query_params('INSERT INTO forum_agg_msg_count (group_forum_id, count) VALUES ($1,$2)',
202 array($old_forum_id, $count));
206 // Update row of new forum id
207 $res = db_query_params('SELECT count FROM forum_agg_msg_count WHERE group_forum_id=$1',
208 array($group_forum_id));
209 if ($res && db_numrows($res)) {
211 $count = db_result($res, 0, 'count');
212 $count += $msg_count;
213 $res = db_query_params('UPDATE forum_agg_msg_count SET count=$1 WHERE group_forum_id=$2',
214 array($count, $group_forum_id));
218 $res = db_query_params('INSERT INTO forum_agg_msg_count (group_forum_id, count) VALUES ($1,$2)',
219 array($group_forum_id, $msg_count));
230 // c-file-style: "bsd"