CMS  Version 3.9
forum.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 Fakoli::usingFile("framework/join.inc");
40 
41 class Forum extends DataItem
42 {
43  var $primary_key = "forum_id";
44  var $table = "forum";
45  var $default_format = "{title}";
46 
47  // Fields
48 
49  var $fields = array("forum_id" => Number,
50  "identifier" => String,
51  "title" => String,
52  "teaser" => Text,
53  "description" => HTML,
54  "created_date" => Date,
55  "last_modified" => Timestamp,
56  "owner_id" => Number,
57  "published" => Boolean,
58  "sort_order" => Number);
59 
60  // Relations
61 
62  var $relations = array(
63  "Sites" => Site,
64  "Owner" => "",
65  "Messages" => ForumMessage,
66  "Topics" => ForumTopic,
67  "LastPost" => ForumMessage,
68  "Summary" => ForumSummary,
69  "Subscriptions" => ForumSubscription,
70  "UserSubscriptions" => ForumSubscription,
71  );
72 
73  function Sites($constraint = "")
74  {
75  return $this->crossReference(Site, ForumSiteXref, $constraint);
76  }
77 
78  function Owner()
79  {
80  $mgr = new UserManager();
81  return $mgr->getUser($this->owner_id);
82  }
83 
84  function Topics($constraint = "")
85  {
86  return $this->getRelatedList(ForumTopic, "", $constraint);
87  }
88 
89  function Messages($constraint = "")
90  {
91  return $this->getRelatedList(ForumMessage, "", $constraint);
92  }
93 
94  function LastPost()
95  {
96  $messages = $this->Messages("ORDER BY date_posted DESC LIMIT 1");
97  return (count($messages) > 0) ? $messages[0] : null;
98  }
99 
100  function Summary()
101  {
102  return $this->getRelated(ForumSummary);
103  }
104 
105  function Subscriptions($constraint = "")
106  {
107  return $this->getRelatedList(ForumSubscription, "", $constraint);
108  }
109 
116  function UserSubscription()
117  {
118  global $user;
119  $pk = $user->getPrimaryKey();
120 
121  $subscriptions = $this->Subscriptions("WHERE user_id={$user->$pk}");
122  return count($subscriptions) ? $subscriptions[0] : null;
123  }
124 
125  function allowDelete()
126  {
127  $count = queryValue(ForumMessage, "COUNT(1)", "WHERE forum_id={$this->forum_id} AND message != '' AND deleted=0");
128  return ($count > 0) ? false : true;
129  }
130 
131  static function findByIdentifier($identifier)
132  {
133  return Query::create(Forum, "WHERE identifier=:identifier")
134  ->bind(":identifier", $identifier)
135  ->executeSingle();
136  }
137 }
138 
139 
140 class ForumSiteXref extends DataItem
141 {
142  var $primary_key = "forum_site_xref_id";
143  var $table = "forum_site_xref";
144 
145  // Fields
146 
147  var $fields = array("forum_site_xref_id" => Number,
148  "forum_id" => Number,
149  "site_id" => Number);
150 
151  // Relations
152 
153  var $relations = array( "Forum" => Forum,
154  "Site" => Site);
155 
156  function Forum()
157  {
158  return $this->getRelated(Forum);
159  }
160 
161  function Site()
162  {
163  return $this->getRelated(Site);
164  }
165 }
166 
167 class ForumMessage extends DataItem
168 {
169  var $primary_key = "message_id";
170  var $table = "forum_message";
171  var $default_format = "{title}";
172 
173  // Fields
174 
175  var $fields = array("message_id" => Number,
176  "title" => String,
177  "message" => HTML,
178  "parent_id" => Number,
179  "topic_id" => Number,
180  "author_id" => Number,
181  "date_posted" => Date,
182  "last_modified" => Timestamp,
183  "forum_id" => Number,
184  "deleted" => Boolean);
185 
186  // Relations
187 
188  var $relations = array( "Forum" => Forum,
189  "Parent" => ForumMessage,
190  "Topic" => ForumTopic,
191  "Author" => "",
192  "Attachments" => Attachment);
193 
194  function Forum()
195  {
196  return $this->getRelated(Forum);
197  }
198 
199  function Parent()
200  {
201  return $this->getRelated(ForumMessage, "parent_id");
202  }
203 
204  function Topic()
205  {
206  return $this->getRelated(ForumTopic);
207  }
208 
209  function Author()
210  {
211  $mgr = new UserManager();
212  return $mgr->getUser($this->author_id);
213  }
214 
215  function Attachments($constraint = "")
216  {
217  return $this->crossReference(Attachment, ForumMessageAttachmentXref, $constraint);
218  }
219 }
220 
221 
222 class ForumMessageAttachmentXref extends DataItem
223 {
224  var $primary_key = "forum_message_attachment_xref_id";
225  var $table = "forum_message_attachment_xref";
226 
227  // Fields
228  var $fields = array("forum_message_attachment_xref_id" => Number,
229  "message_id" => Number,
230  "attachment_id" => Number);
231 
232  // Relations
233  var $relations = array( "Message" => ForumMessage,
234  "Attachment" => Attachment);
235 
236  function Message()
237  {
238  return $this->getRelated(ForumMessage);
239  }
240 
241  function Attachment()
242  {
243  return $this->getRelated(Attachment);
244  }
245 }
246 
247 
248 class ForumTopic extends DataItem
249 {
250  var $primary_key = "topic_id";
251  var $table = "forum_topic";
252 
253  // Fields
254  var $fields = array("topic_id" => Number,
255  "message_id" => Number,
256  "forum_id" => Number,
257  "views" => Number,
258  "replies" => Number,
259  "date_created" => Date);
260 
261  // Relations
262  var $relations = array( "FirstPost" => ForumMessage,
263  "Forum" => Forum,
264  "LastPost" => ForumMessage,
265  "Messages" => ForumMessage);
266 
267  function FirstPost()
268  {
269  return $this->getRelated(ForumMessage);
270  }
271 
272  function LastPost()
273  {
274  $messages = $this->Messages("WHERE deleted=false ORDER BY date_posted DESC LIMIT 1");
275  return count($messages) > 0 ? $messages[0] : null;
276  }
277 
278  function Forum()
279  {
280  return $this->getRelated(Forum);
281  }
282 
283  function getTitle()
284  {
285  return $this->FirstPost()->title;
286  }
287 
288  function Messages($constraint = "")
289  {
290  return $this->getRelatedList(ForumMessage, "", $constraint);
291  }
292 
298  function countReplies()
299  {
300  return Query::create(ForumMessage, "WHERE topic_id=:topic_id and parent_id > 0 and deleted=0")
301  ->bind(":topic_id", $this->topic_id)
302  ->executeValue("COUNT(1)");
303  }
304 }
305 
306 
307 class ForumSummary extends DataItem
308 {
309  var $primary_key = "forum_id";
310  var $table = "forum_summary";
311 
312  // Fields
313  var $fields = array("forum_id" => Number,
314  "title" => String,
315  "teaser" => String,
316  "topics" => Number,
317  "posts" => Number,
318  "published" => Boolean,
319  "sort_order" => Number);
320 
321  // Relations
322  var $relations = array("Forum" => Forum,
323  "LastPost" => ForumMessage);
324 
325  function Forum()
326  {
327  return $this->getRelated(Forum);
328  }
329 
330  function LastPost()
331  {
332  $messages = $this->getRelatedList(ForumMessage, "", "WHERE deleted!=1 ORDER BY date_posted DESC LIMIT 1");
333  return (count($messages) > 0) ? $messages[0] : null;
334  }
335 }?>
$constraint
$calendar owner_id
return false
static usingFile()
Uses the specified framework file(s) from the framework directory.
Definition: core.inc:369
Definition: forum.inc:42
UserSubscription()
Get the subscriptions to this forum for this user.
Definition: forum.inc:116
LastPost()
Definition: forum.inc:94
Subscriptions($constraint="")
Definition: forum.inc:105
Summary()
Definition: forum.inc:100
$relations
Definition: forum.inc:62
$default_format
Definition: forum.inc:45
allowDelete()
Definition: forum.inc:125
Owner()
Definition: forum.inc:78
Sites($constraint="")
Definition: forum.inc:73
$fields
Definition: forum.inc:49
Topics($constraint="")
Definition: forum.inc:84
Messages($constraint="")
Definition: forum.inc:89
$primary_key
Definition: forum.inc:43
static findByIdentifier($identifier)
Definition: forum.inc:131
$table
Definition: forum.inc:44
Attachments($constraint="")
Definition: forum.inc:215
$primary_key
Definition: forum.inc:250
Messages($constraint="")
Definition: forum.inc:288
LastPost()
Definition: forum.inc:272
countReplies()
When counting forum message replies to a topic, exclude the forum message that has a parent id of 0,...
Definition: forum.inc:298
getTitle()
Definition: forum.inc:283
FirstPost()
Definition: forum.inc:267
Definition: site.inc:40
Provides the interface to the user model for the application.
global $user
$messages
$identifier
Definition: rss.inc:37