00001 <?
00010 class BaseForum extends MyObject
00011 {
00015 public $ThreadClass = 'Thread';
00016
00020 public $ThreadManagerClass = 'Threads';
00021
00025 public $CommentClass = 'ThreadComment';
00026
00032 public function __construct($id = null, $table = 'forums')
00033 {
00034 $this->hasAuthor = false;
00035
00036 parent::__construct($id, $table);
00037
00038
00039 $this->fullTextFields[] = 'name';
00040 $this->fullTextFields[] = 'description';
00041 }
00042
00048 public function getPagesXml()
00049 {
00050 $xml = parent::getPagesXml();
00051 $xml .= <<<XML
00052 <page name="post">
00053 <param name="forum_id" type="int" />
00054 </page>
00055 XML;
00056
00057 return $xml;
00058 }
00059
00063 public function save()
00064 {
00065 $comment = new $this->ThreadClass();
00066
00067
00068 if ($this->id)
00069 {
00070 $ar = dbFetchAssoc(dbQuery("
00071 SELECT count(*) AS threads
00072 FROM $comment->tableName
00073 WHERE forum_id = $this->id
00074 "));
00075 $this->threads = $ar['threads'];
00076
00077 $ar = dbFetchAssoc(dbQuery("
00078 SELECT SUM(comment_count) AS posts
00079 FROM $comment->tableName
00080 WHERE forum_id = $this->id
00081 GROUP BY forum_id
00082 "));
00083 $this->posts = $ar['posts'];
00084 }
00085
00086 return parent::save();
00087 }
00088
00092 public function canView()
00093 {
00094 return true;
00095 }
00096
00100 public function canEdit()
00101 {
00102 global $me;
00103
00104 return $me->isAdmin();
00105 }
00106
00110 public function canDelete()
00111 {
00112 return $this->canEdit();
00113 }
00114
00120 public function editFormAddFields($form)
00121 {
00122 $form->add('TextField', 'name', array(
00123 'required' => true,
00124 'width' => '100%',
00125 ));
00126 $form->add('TextAreaField', 'description', array(
00127 'required' => true,
00128 'width' => '100%',
00129 'height' => '200px'
00130 ));
00131 $form->add('TextField', 'sequence', array(
00132 'required' => true,
00133 'width' => '25%'
00134 ));
00135
00136 if ($this->id)
00137 $form->addSubmit('Save Forum');
00138 else
00139 $form->addSubmit('Create Forum');
00140 }
00141
00145 public function getName($link = false)
00146 {
00147 return parent::getName($link, $this->name);
00148 }
00149
00153 public function drawRow()
00154 {
00155 ?>
00156 <div class="forumRow">
00157 <h2><?=$this->getName(true)?></h2>
00158 <p>
00159 <?=Linkify::bbcode($this->description)?><br/>
00160 <b><?=$this->threads?> threads and <?=$this->posts?> posts.</b>
00161 </p>
00162 </div>
00163 <?
00164 }
00165
00169 public function initPostPage()
00170 {
00171 $this->assertLogin();
00172
00173 $this->pageTitle = 'Post New Thread';
00174 }
00175
00179 public function drawPostPage()
00180 {
00181 global $me;
00182
00183 $form = $this->createPostForm();
00184
00185 if ($form->isSubmitted())
00186 {
00187 $form->validate();
00188
00189 if (!$form->hasError())
00190 {
00191
00192 $thread = new $this->ThreadClass();
00193 $thread->forum_id = $form->getData('forum_id');
00194 $thread->subject = $form->getData('subject');
00195 $thread->user_id = $me->id;
00196 $thread->add_date = date("Y-m-d h:i:s");
00197 $thread->edit_date = date("Y-m-d h:i:s");
00198 $thread->save();
00199
00200
00201 $post = new $this->CommentClass();
00202 $post->add_date = date("Y-m-d H:i:s");
00203 $post->edit_date = date("Y-m-d H:i:s");
00204 $post->user_id = $me->id;
00205 $post->content_id = $thread->id;
00206 $post->body = $form->getData('post');
00207 $post->save();
00208
00209
00210 Util::redirect($thread->getUrl(".view?id=$thread->id"));
00211 }
00212 }
00213
00214 else if ($this->params('forum_id'))
00215 $form->setData(array(
00216 'forum_id' => $this->params('forum_id')
00217 ));
00218
00219 $form->drawIfNeeded();
00220 }
00221
00227 public function createPostForm()
00228 {
00229 $form = new Form();
00230 $form->action = $this->getUrl(".post");
00231
00232
00233 $man = new Forums();
00234 $rs = $man->search();
00235 foreach ($rs AS $ob)
00236 $forums[$ob->id] = $ob->getName();
00237
00238 $form->add('SelectField', 'forum_id', array(
00239 'required' => true,
00240 'options' => $forums
00241 ));
00242
00243 $form->add('TextField', 'subject', array(
00244 'required' => true,
00245 'width' => '100%',
00246 'title' => 'Title'
00247 ));
00248 $form->add('TextAreaField', 'post', array(
00249 'required' => true,
00250 'width' => '100%',
00251 'height' => '200px'
00252 ));
00253
00254 $form->addSubmit('Create Thread');
00255
00256 return $form;
00257 }
00258
00262 public function drawViewPage()
00263 {
00264
00265 $threads = new $this->ThreadManagerClass();
00266
00267 echo "<p>$this->description</p>";
00268
00269
00270 $links = array(
00271 $this->getLink(".post?forum_id=$this->id", "Post new thread"),
00272 $threads->getLink(".search?output=rss&forum_id=$this->id", "Threads RSS Feed")
00273 );
00274 $this->drawPageNavigation($links);
00275
00276
00277 $rs = $threads->search(array(
00278 'forum_id' => $this->id,
00279 ));
00280 $threads->drawResults($rs);
00281 }
00282
00286 public function getRssItem()
00287 {
00288 $item = parent::getRssItem();
00289
00290 $item->title = $this->getName();
00291 $item->description = Linkify::bbcode($this->description);
00292
00293 return $item;
00294 }
00295
00299 public function getPublicData()
00300 {
00301 $data = parent::getPublicData();
00302
00303 $data['name'] = $this->getName();
00304 $data['description'] = Linkify::bbcode($this->description);
00305 $data['posts'] = $this->posts;
00306 $data['threads'] = $this->threads;
00307 $data['sequence'] = $this->sequence;
00308
00309 return $data;
00310 }
00311
00315 public function getCreateFieldsArray()
00316 {
00317 $arr = parent::getCreateFieldsArray();
00318
00319 $arr['name'] = "name varchar(255) default '' not null";
00320 $arr['description'] = "description text default '' not null";
00321 $arr['sequence'] = "sequence tinyint(4) default 0 not null";
00322 $arr['threads'] = "threads int(11) default 0 not null";
00323 $arr['posts'] = "posts int(11) default 0 not null";
00324
00325 return $arr;
00326 }
00327 }
00328 ?>