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/FFError.class.php';
26 require_once $gfcommon.'forum/Forum.class.php';
28 class ForumFactory extends FFError {
45 * @param object $Group The Group object to which this forum is associated.
46 * @param bool $skip_check
48 function __construct(&$Group, $skip_check=false) {
49 parent::__construct();
50 if (!$Group || !is_object($Group)) {
51 $this->setError(_('Invalid Project'));
54 if ($Group->isError()) {
55 $this->setError(_('Forums')._(': ').$Group->getErrorMessage());
58 if (!$skip_check && !$Group->usesForum()) {
59 $this->setError(sprintf(_('%s does not use the Forum tool.'),
60 $Group->getPublicName()));
63 $this->Group =& $Group;
67 * getGroup - get the Group object this ForumFactory is associated with.
69 * @return object The Group object.
71 function &getGroup() {
75 function &getAllForumIds() {
77 $res = db_query_params('SELECT group_forum_id FROM forum_group_list
78 WHERE group_forum_id NOT IN (
79 SELECT group_forum_id FROM forum_group_list WHERE group_forum_id IN (
80 SELECT forum_id FROM news_bytes))
82 ORDER BY group_forum_id',
83 array($this->Group->getID()));
87 while ($arr = db_fetch_array($res)) {
88 $result[] = $arr['group_forum_id'] ;
93 function &getAllForumIdsWithNews() {
95 $res = db_query_params('SELECT group_forum_id FROM forum_group_list WHERE group_id=$1 ORDER BY group_forum_id',
96 array($this->Group->getID()));
100 while ($arr = db_fetch_array($res)) {
101 $result[] = $arr['group_forum_id'] ;
107 * getForums - get an array of Forum objects for this Group.
109 * @return array The array of Forum objects.
111 function &getForums() {
113 return $this->forums;
116 $this->forums = array();
117 $ids = $this->getAllForumIds();
120 foreach ($ids as $id) {
121 if (forge_check_perm ('forum', $id, 'read')) {
122 $this->forums[] = new Forum($this->Group, $id);
126 return $this->forums;
130 * getForumsAdmin - get an array of all (public, private and suspended) Forum objects for this Group.
132 * @return array The array of Forum objects.
134 function &getForumsAdmin() {
136 return $this->forums;
139 if (session_loggedin()) {
140 if (!forge_check_perm ('forum_admin', $this->Group->getID())) {
141 $this->setError(_('You are not allowed to access this page'));
142 $this->forums = false;
144 $result = db_query_params('SELECT * FROM forum_group_list_vw
146 ORDER BY group_forum_id',
147 array($this->Group->getID())) ;
150 $this->setError(_('You are not allowed to access this page'));
151 $this->forums = false;
154 $rows = db_numrows($result);
156 if (!$result || ($rows < 1)) {
157 $this->setError(_('Forum not found')._(': ').db_error());
158 $this->forums = false;
160 while ($arr = db_fetch_array($result)) {
161 $this->forums[] = new Forum($this->Group, $arr['group_forum_id'], $arr);
164 return $this->forums;
168 * moveThread - move thread in another forum
170 * @param $group_forum_id
172 * @param bool $old_forum_id
174 * Note: 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) {
193 // Update forum_agg_msg_count table
194 // Note: if error(s) are raised it's certainly because forum_agg_msg_count
195 // is no longer used and updated. So, error(s) are not catched
196 // Update row of old forum id
197 $res = db_query_params('SELECT count FROM forum_agg_msg_count WHERE group_forum_id=$1',
198 array($old_forum_id));
199 if ($res && db_numrows($res)) {
201 $count = db_result($res, 0, 'count');
202 $count -= $msg_count;
203 if ($count < 0) $count = 0;
204 $res = db_query_params('UPDATE forum_agg_msg_count SET count=$1 WHERE group_forum_id=$2',
205 array($count, $old_forum_id));
207 // Error because row doesn't exist... insert it
208 $res = db_query_params('SELECT COUNT(*) AS count FROM forum WHERE group_forum_id=$1',
209 array($old_forum_id));
210 if ($res && db_numrows($res)) {
211 $count = db_result($res, 0, 'count');
212 $res = db_query_params('INSERT INTO forum_agg_msg_count (group_forum_id, count) VALUES ($1,$2)',
213 array($old_forum_id, $count));
217 // Update row of new forum id
218 $res = db_query_params('SELECT count FROM forum_agg_msg_count WHERE group_forum_id=$1',
219 array($group_forum_id));
220 if ($res && db_numrows($res)) {
222 $count = db_result($res, 0, 'count');
223 $count += $msg_count;
224 $res = db_query_params('UPDATE forum_agg_msg_count SET count=$1 WHERE group_forum_id=$2',
225 array($count, $group_forum_id));
228 $res = db_query_params('INSERT INTO forum_agg_msg_count (group_forum_id, count) VALUES ($1,$2)',
229 array($group_forum_id, $msg_count));
238 // c-file-style: "bsd"