3 * GForge Forums Facility
5 * Copyright 2002 GForge, LLC
10 * This file is part of GForge.
12 * GForge is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * GForge is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with GForge; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 By Tim Perdue, Sourceforge, 11/99
32 Massive rewrite by Tim Perdue 7/2000 (nested/views/save)
34 Complete OO rewrite by Tim Perdue 12/2002
37 require_once('common/include/Error.class.php');
38 require_once('common/forum/ForumMessage.class.php');
40 class ForumMessageFactory extends Error {
50 * The forum_messages array.
52 * @var array forum_messages.
63 * @param object The Forum object to which this ForumMessageFactory is associated.
64 * @return boolean success.
66 function ForumMessageFactory(&$Forum) {
68 if (!$Forum || !is_object($Forum)) {
69 $this->setError("ForumMessage:: Invalid group_form_id");
72 if ($Forum->isError()) {
73 $this->setError('ForumMessage:: '.$Forum->getErrorMessage());
76 $this->Forum =& $Forum;
82 * setup - call this function before getThreaded/nested/etc to set up the user preferences.
84 * @param int The number of rows to skip.
85 * @param string The style of forum, whether it's nested, ultimate, etc.
86 * @param int The maximum number of rows to return.
87 * @param int Whether to set these prefs into the database - use "custom".
89 function setup($offset,$style,$max_rows,$set) {
90 //echo "<BR>offset: $offset| style: $style|max_rows: $max_rows|set: $set+";
91 if ((!$offset) || ($offset < 0)) {
94 $this->offset=$offset;
97 if (!$style || ($style != 'ultimate' && $style != 'flat' && $style != 'nested' && $style != 'threaded')) {
100 if (!$max_rows || $max_rows < 5) {
103 if (session_loggedin()) {
104 $u =& session_get_user();
105 $_pref=$style.'|'.$max_rows;
106 if ($set=='custom') {
107 if ($u->getPreference('forum_style')) {
108 if ($_pref == $u->getPreference('forum_style')) {
109 //pref already stored
112 $u->setPreference ('forum_style',$_pref);
116 $u->setPreference ('forum_style',$_pref);
119 if ($u->getPreference('forum_style')) {
120 $_pref_arr=explode ('|',$u->getPreference('forum_style'));
121 $style=$_pref_arr[0];
122 $max_rows=$_pref_arr[1];
124 //no saved pref and we're not setting
125 //one because this is all default settings
130 if (!$style || ($style != 'ultimate' && $style != 'flat' && $style != 'nested' && $style != 'threaded')) {
134 if (!$max_rows || $max_rows < 5) {
137 $this->max_rows=$max_rows;
141 * getStyle - the style of forum this is - nested/ultimate/etc.
143 * @return string The style.
145 function getStyle() {
150 * nestArray - take an array of Forum Messages and building a multi-dimensional associative array for followups.
152 * @return array The nested multi-dimensional associative array.
154 function &nestArray(&$row) {
156 for ($i=0; $i<$cnt; $i++) {
158 $msg_arr["".$row[$i]->getParentID().""][] =& $row[$i];
165 * getNested - Return an array of ForumMessage objects arranged for nested forum views.
167 * @return array The array of ForumMessages.
169 function &getNested($thread_id=false) {
170 if ($this->forum_messages) {
171 return $this->forum_messages;
174 $thread_sql=" AND thread_id='$thread_id' ";
177 $sql="SELECT * FROM forum_user_vw
178 WHERE group_forum_id='".$this->Forum->getID()."'
180 ORDER BY most_recent_date DESC";
182 $result=db_query($sql,($this->max_rows+25),$this->offset);
183 $rows = db_numrows($result);
184 $this->fetched_rows=$rows;
185 if (!$result || $rows < 1) {
186 $this->setError('No Messages Found '.db_error());
189 while ($arr = db_fetch_array($result)) {
190 $this->forum_messages[] = new ForumMessage($this->Forum, $arr['msg_id'], $arr);
193 return $this->forum_messages;
197 * getThreaded - Return an array of ForumMessage objects arranged for threaded forum views.
199 * @return array The array of ForumMessages.
201 function &getThreaded($thread_id=false) {
202 if ($this->forum_messages) {
203 return $this->forum_messages;
206 $thread_sql=" AND thread_id='$thread_id' ";
208 $sql="SELECT * FROM forum_user_vw
209 WHERE group_forum_id='".$this->Forum->getID()."'
211 ORDER BY most_recent_date DESC";
213 $result=db_query($sql,($this->max_rows+25),$this->offset);
214 $rows = db_numrows($result);
215 $this->fetched_rows=$rows;
216 if (!$result || $rows < 1) {
217 $this->setError('No Messages Found '.db_error());
220 while ($arr = db_fetch_array($result)) {
221 $this->forum_messages[] = new ForumMessage($this->Forum, $arr['msg_id'], $arr);
224 return $this->forum_messages;
228 * getFlat - Return an array of ForumMessage objects arranged for flat forum views.
230 * @return array The array of ForumMessages.
232 function &getFlat($thread_id=false) {
233 if ($this->forum_messages) {
234 return $this->forum_messages;
237 $thread_sql=" AND thread_id='$thread_id' ";
239 $sql="SELECT * FROM forum_user_vw
240 WHERE group_forum_id='".$this->Forum->getID()."'
242 ORDER BY msg_id DESC";
244 $result=db_query($sql,($this->max_rows+1),$this->offset);
245 $rows = db_numrows($result);
246 $this->fetched_rows=$rows;
247 if (!$result || $rows < 1) {
248 $this->setError('No Messages Found '.db_error());
251 while ($arr = db_fetch_array($result)) {
252 $this->forum_messages[] = new ForumMessage($this->Forum, $arr['msg_id'], $arr);
255 return $this->forum_messages;