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 if (!$Group->usesForum()) {
60 $this->setError(sprintf(_('%s does not use the Forum tool'),
61 $Group->getPublicName()));
64 $this->Group =& $Group;
70 * getGroup - get the Group object this ForumFactory is associated with.
72 * @return object The Group object.
74 function &getGroup() {
78 function &getAllForumIds() {
80 $res = db_query_params('SELECT group_forum_id FROM forum_group_list
81 WHERE group_forum_id NOT IN (
82 SELECT group_forum_id FROM forum_group_list WHERE group_forum_id IN (
83 SELECT forum_id FROM news_bytes))
85 ORDER BY group_forum_id',
86 array ($this->Group->getID()));
90 while ($arr = db_fetch_array($res)) {
91 $result[] = $arr['group_forum_id'] ;
96 function &getAllForumIdsWithNews() {
98 $res = db_query_params('SELECT group_forum_id FROM forum_group_list WHERE group_id=$1 ORDER BY group_forum_id',
99 array ($this->Group->getID()));
103 while ($arr = db_fetch_array($res)) {
104 $result[] = $arr['group_forum_id'] ;
110 * getForums - get an array of Forum objects for this Group.
112 * @return array The array of Forum objects.
114 function &getForums() {
116 return $this->forums;
119 $this->forums = array () ;
120 $ids = $this->getAllForumIds() ;
123 foreach ($ids as $id) {
124 if (forge_check_perm ('forum', $id, 'read')) {
125 $this->forums[] = new Forum($this->Group, $id);
129 return $this->forums;
133 * getForumsAdmin - get an array of all (public, private and suspended) Forum objects for this Group.
135 * @return array The array of Forum objects.
137 function &getForumsAdmin() {
139 return $this->forums;
143 if (session_loggedin()) {
144 if (!forge_check_perm ('forum_admin', $this->Group->getID())) {
145 $this->setError(_("You don't have a permission to access this page"));
146 $this->forums = false;
148 $result = db_query_params ('SELECT * FROM forum_group_list_vw
150 ORDER BY group_forum_id',
151 array ($this->Group->getID())) ;
154 $this->setError(_("You don't have a permission to access this page"));
155 $this->forums = false;
158 $rows = db_numrows($result);
161 $this->setError(_('Forum not found').' : '.db_error());
162 $this->forums = false;
164 while ($arr = db_fetch_array($result)) {
165 $this->forums[] = new Forum($this->Group, $arr['group_forum_id'], $arr);
168 return $this->forums;
172 * moveThread - move thread in another forum
174 * @param string The forum ID
175 * @param int The thread_id of the tread to change.
176 * @param string The 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"