5 * Copyright 1999-2000, Tim Perdue/Sourceforge
6 * Copyright 2002, Tim Perdue/GForge, LLC
7 * Copyright 2009, Roland Mas
8 * Copyright (C) 2010 Alain Peyrat - Alcatel-Lucent
10 * This file is part of FusionForge. FusionForge is free software;
11 * you can redistribute it and/or modify it under the terms of the
12 * GNU General Public License as published by the Free Software
13 * Foundation; either version 2 of the Licence, or (at your option)
16 * FusionForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 require_once $gfcommon.'include/Error.class.php';
27 require_once $gfcommon.'forum/ForumMessage.class.php';
29 class ForumMessageFactory extends Error {
39 * The forum_messages array.
41 * @var array forum_messages.
52 * @param object The Forum object to which this ForumMessageFactory is associated.
53 * @return boolean success.
55 function ForumMessageFactory(&$Forum) {
57 if (!$Forum || !is_object($Forum)) {
58 $this->setError("ForumMessage:: Invalid group_form_id");
61 if ($Forum->isError()) {
62 $this->setError('ForumMessage:: '.$Forum->getErrorMessage());
65 $this->Forum =& $Forum;
71 * setup - call this function before getThreaded/nested/etc to set up the user preferences.
73 * @param int $offset The number of rows to skip.
74 * @param string $style The style of forum, whether it's nested, ultimate, etc.
75 * @param int $max_rows The maximum number of rows to return.
76 * @param int $set Whether to set these prefs into the database - use "custom".
78 function setup($offset,$style,$max_rows,$set) {
79 //echo "<br />offset: $offset| style: $style|max_rows: $max_rows|set: $set+";
80 if ((!$offset) || ($offset < 0)) {
83 $this->offset=$offset;
86 if (!$style || ($style != 'ultimate' && $style != 'flat' && $style != 'nested' && $style != 'threaded')) {
89 if (!$max_rows || $max_rows < 5) {
92 if (session_loggedin()) {
93 $u =& session_get_user();
94 $_pref=$style.'|'.$max_rows;
96 if ($u->getPreference('forum_style')) {
97 if ($_pref == $u->getPreference('forum_style')) {
101 $u->setPreference ('forum_style',$_pref);
105 $u->setPreference ('forum_style',$_pref);
108 if ($u->getPreference('forum_style')) {
109 $_pref_arr=explode ('|',$u->getPreference('forum_style'));
110 $style=$_pref_arr[0];
111 $max_rows=$_pref_arr[1];
113 //no saved pref and we're not setting
114 //one because this is all default settings
119 if (!$style || ($style != 'ultimate' && $style != 'flat' && $style != 'nested' && $style != 'threaded')) {
123 if (!$max_rows || $max_rows < 5) {
126 $this->max_rows=$max_rows;
130 * getStyle - the style of forum this is - nested/ultimate/etc.
132 * @return string The style.
134 function getStyle() {
139 * nestArray - take an array of Forum Messages and building a multi-dimensional associative array for followups.
141 * @return array The nested multi-dimensional associative array.
143 function &nestArray($row) {
145 for ($i=0; $i<$cnt; $i++) {
147 $msg_arr["".$row[$i]->getParentID().""][] =& $row[$i];
154 * getNested - Return an array of ForumMessage objects arranged for nested forum views.
156 * @return array The array of ForumMessages.
158 function &getNested($thread_id=false) {
159 if ($this->forum_messages) {
160 return $this->forum_messages;
162 if (isset ($thread_id) && is_numeric($thread_id)) {
163 $result = db_query_params ('SELECT * FROM forum_user_vw
164 WHERE group_forum_id=$1
166 ORDER BY most_recent_date DESC',
167 array ($this->Forum->getID(),
172 $result = db_query_params ('SELECT * FROM forum_user_vw
173 WHERE group_forum_id=$1
174 ORDER BY most_recent_date DESC',
175 array ($this->Forum->getID()),
180 $rows = db_numrows($result);
181 $this->fetched_rows=$rows;
182 $this->forum_messages = array();
184 $this->setError('No Messages Found '.db_error());
185 $this->forum_messages = false;
187 while ($arr = db_fetch_array($result)) {
188 $this->forum_messages[] = new ForumMessage($this->Forum, $arr['msg_id'], $arr);
191 return $this->forum_messages;
195 * getThreaded - Return an array of ForumMessage objects arranged for threaded forum views.
197 * @return array The array of ForumMessages.
199 function &getThreaded($thread_id=false) {
200 if ($this->forum_messages) {
201 return $this->forum_messages;
203 if (isset ($thread_id) && is_numeric($thread_id)) {
204 $result = db_query_params ('SELECT * FROM forum_user_vw
205 WHERE group_forum_id=$1
207 ORDER BY most_recent_date DESC',
208 array ($this->Forum->getID(),
213 $result = db_query_params ('SELECT * FROM forum_user_vw
214 WHERE group_forum_id=$1
215 ORDER BY most_recent_date DESC',
216 array ($this->Forum->getID()),
220 $rows = db_numrows($result);
221 $this->fetched_rows=$rows;
223 $this->setError('Error when fetching messages '.db_error());
225 } elseif ($rows < 1) {
226 $this->forum_messages = array();
228 while ($arr = db_fetch_array($result)) {
229 $this->forum_messages[] = new ForumMessage($this->Forum, $arr['msg_id'], $arr);
232 return $this->forum_messages;
236 * getFlat - Return an array of ForumMessage objects arranged for flat forum views.
238 * @return array The array of ForumMessages.
240 function &getFlat($thread_id=false) {
241 if ($this->forum_messages) {
242 return $this->forum_messages;
244 if (isset ($thread_id) && is_numeric($thread_id)) {
245 $result = db_query_params ('SELECT * FROM forum_user_vw
246 WHERE group_forum_id=$1
248 ORDER BY msg_id DESC',
249 array ($this->Forum->getID(),
254 $result = db_query_params ('SELECT * FROM forum_user_vw
255 WHERE group_forum_id=$1
256 ORDER BY msg_id DESC',
257 array ($this->Forum->getID()),
262 $rows = db_numrows($result);
263 $this->fetched_rows=$rows;
264 if (!$result || $rows < 1) {
265 $this->setError('No Messages Found '.db_error());
268 while ($arr = db_fetch_array($result)) {
269 $this->forum_messages[] = new ForumMessage($this->Forum, $arr['msg_id'], $arr);
272 return $this->forum_messages;
278 // c-file-style: "bsd"