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 $Group The Group object to which this forum is associated.
49 function __construct(&$Group) {
51 if (!$Group || !is_object($Group)) {
52 $this->setError(_('No Valid Group Object'));
55 if ($Group->isError()) {
56 $this->setError(_('Forum').':: '.$Group->getErrorMessage());
59 if (!$Group->usesForum()) {
60 $this->setError(sprintf(_('%s does not use the Forum tool.'),
61 $Group->getPublicName()));
64 $this->Group =& $Group;
68 * getGroup - get the Group object this ForumFactory is associated with.
70 * @return object The Group object.
72 function &getGroup() {
76 function &getAllForumIds() {
78 $res = db_query_params('SELECT group_forum_id FROM forum_group_list
79 WHERE group_forum_id NOT IN (
80 SELECT group_forum_id FROM forum_group_list WHERE group_forum_id IN (
81 SELECT forum_id FROM news_bytes))
83 ORDER BY group_forum_id',
84 array ($this->Group->getID()));
88 while ($arr = db_fetch_array($res)) {
89 $result[] = $arr['group_forum_id'] ;
94 function &getAllForumIdsWithNews() {
96 $res = db_query_params('SELECT group_forum_id FROM forum_group_list WHERE group_id=$1 ORDER BY group_forum_id',
97 array ($this->Group->getID()));
101 while ($arr = db_fetch_array($res)) {
102 $result[] = $arr['group_forum_id'] ;
108 * getForums - get an array of Forum objects for this Group.
110 * @return array The array of Forum objects.
112 function &getForums() {
114 return $this->forums;
117 $this->forums = array () ;
118 $ids = $this->getAllForumIds() ;
121 foreach ($ids as $id) {
122 if (forge_check_perm ('forum', $id, 'read')) {
123 $this->forums[] = new Forum($this->Group, $id);
127 return $this->forums;
131 * getForumsAdmin - get an array of all (public, private and suspended) Forum objects for this Group.
133 * @return array The array of Forum objects.
135 function &getForumsAdmin() {
137 return $this->forums;
140 if (session_loggedin()) {
141 if (!forge_check_perm ('forum_admin', $this->Group->getID())) {
142 $this->setError(_("You don't have a permission to access this page"));
143 $this->forums = false;
145 $result = db_query_params ('SELECT * FROM forum_group_list_vw
147 ORDER BY group_forum_id',
148 array ($this->Group->getID())) ;
151 $this->setError(_("You don't have a permission to access this page"));
152 $this->forums = false;
155 $rows = db_numrows($result);
158 $this->setError(_('Forum not found').' : '.db_error());
159 $this->forums = false;
161 while ($arr = db_fetch_array($result)) {
162 $this->forums[] = new Forum($this->Group, $arr['group_forum_id'], $arr);
165 return $this->forums;
169 * moveThread - move thread in another forum
171 * @param $group_forum_id
173 * @param bool $old_forum_id
174 * @internal param \The $string forum ID
175 * @internal param \The $int thread_id of the tread to change.
176 * @internal param \The $string old forum ID
179 * old forum ID is useless if forum_agg_msg_count table is no longer used
181 * @return boolean success.
183 function moveThread($group_forum_id,$thread_id,$old_forum_id = false) {
184 $res = db_query_params('UPDATE forum SET group_forum_id=$1 WHERE thread_id=$2',
185 array($group_forum_id, $thread_id));
187 $this->setError(db_error());
190 $msg_count = db_affected_rows($res);
191 if ($msg_count < 1) {
192 $this->setError(_("Thread not found"));
197 if ($old_forum_id !== false)
199 // Update forum_agg_msg_count table
200 // Note: if error(s) are raised it's certainly because forum_agg_msg_count
201 // is no longer used and updated. So, error(s) are not catched
202 // Update row of old forum id
203 $res = db_query_params('SELECT count FROM forum_agg_msg_count WHERE group_forum_id=$1',
204 array($old_forum_id));
205 if ($res && db_numrows($res)) {
207 $count = db_result($res, 0, 'count');
208 $count -= $msg_count;
209 if ($count < 0) $count = 0;
210 $res = db_query_params('UPDATE forum_agg_msg_count SET count=$1 WHERE group_forum_id=$2',
211 array($count, $old_forum_id));
214 // Error because row doesn't exist... insert it
215 $res = db_query_params('SELECT COUNT(*) AS count FROM forum WHERE group_forum_id=$1',
216 array($old_forum_id));
217 if ($res && db_numrows($res)) {
218 $count = db_result($res, 0, 'count');
219 $res = db_query_params('INSERT INTO forum_agg_msg_count (group_forum_id, count) VALUES ($1,$2)',
220 array($old_forum_id, $count));
224 // Update row of new forum id
225 $res = db_query_params('SELECT count FROM forum_agg_msg_count WHERE group_forum_id=$1',
226 array($group_forum_id));
227 if ($res && db_numrows($res)) {
229 $count = db_result($res, 0, 'count');
230 $count += $msg_count;
231 $res = db_query_params('UPDATE forum_agg_msg_count SET count=$1 WHERE group_forum_id=$2',
232 array($count, $group_forum_id));
236 $res = db_query_params('INSERT INTO forum_agg_msg_count (group_forum_id, count) VALUES ($1,$2)',
237 array($group_forum_id, $msg_count));
248 // c-file-style: "bsd"