Example #1
0
File: topic.php Project: anqh/anqh
 /**
  * Save topic and handle stats updated.
  *
  * @return  boolean
  */
 public function save_post()
 {
     if ($this->unsaved_post) {
         $this->unsaved_post->is_valid();
         $area = $this->area();
         if (!$this->id) {
             // New topic
             $this->save();
             $this->unsaved_post->forum_topic_id = $this->id;
             $this->unsaved_post->save();
             $this->created = $this->unsaved_post->created;
             $this->first_post_id = $this->unsaved_post->id;
             $area->topic_count++;
         } else {
             // Old topic
             $this->unsaved_post->save();
         }
         // Topic stats
         $this->last_post_id = $this->unsaved_post->id;
         $this->last_poster = $this->unsaved_post->author_name;
         $this->last_posted = $this->unsaved_post->created;
         $this->post_count++;
         $this->save();
         // Area stats
         $area->last_topic_id = $this->id;
         $area->post_count++;
         $area->save();
         return true;
     }
     return false;
 }
Example #2
0
File: topic.php Project: anqh/anqh
 /**
  * Find topic posts by page.
  *
  * @param   integer  $offset
  * @param   integer  $limit
  * @return  Model_Forum_Post[]
  */
 public function posts($offset, $limit)
 {
     $post = Model_Forum_Private_Post::factory();
     $query = DB::select_array($post->fields())->where('forum_topic_id', '=', $this->id)->order_by('created', 'ASC');
     if ($offset || $limit) {
         return $post->load($query->offset($offset), $limit);
     } else {
         return $post->load($query, null);
     }
 }
Example #3
0
File: topic.php Project: anqh/forum
 /**
  * Edit forum post
  *
  * @param  integer  $topic_id  When replying to a topic
  * @param  integer  $post_id   When editing a post
  * @param  integer  $quote_id  When quoting a post
  *
  * @throws  Model_Exception  missing topic, missing post
  */
 protected function _edit_post($topic_id, $post_id = null, $quote_id = null)
 {
     $this->history = false;
     // Topic is always loaded, avoid haxing attempts to edit posts from wrong topics
     $topic = $this->private ? Model_Forum_Private_Topic::factory($topic_id) : Model_Forum_Topic::factory($topic_id);
     if (!$topic->loaded()) {
         throw new Model_Exception($topic, $topic_id);
     }
     Permission::required($topic, Model_Forum_Topic::PERMISSION_POST, self::$user);
     if ($post_id) {
         // Editing a post
         $post = $this->private ? Model_Forum_Private_Post::factory($post_id) : Model_Forum_Post::factory($post_id);
         if (!$post->loaded() || $post->forum_topic_id != $topic->id) {
             throw new Model_Exception($post, $post_id);
         }
         Permission::required($post, Model_Forum_Post::PERMISSION_UPDATE, self::$user);
     } else {
         // New reply
         $post = $this->private ? Model_Forum_Private_Post::factory() : Model_Forum_Post::factory();
     }
     // Quoting a post
     if ($quote_id) {
         $quote = $this->private ? Model_Forum_Private_Post::factory() : Model_Forum_Post::factory($quote_id);
         if (!$quote->loaded() || $quote->forum_topic_id != $topic->id) {
             throw new Model_Exception($quote, $quote_id);
         }
         Permission::required($quote, Model_Forum_Post::PERMISSION_READ, self::$user);
         if (!$post->loaded()) {
             $post->post = '[quote author="' . $quote->author_name . '" post="' . $quote->id . '"]' . $quote->post . "[/quote]\n\n";
         }
         $post->parent_id = $quote_id;
     }
     // Handle post
     $errors = array();
     if ($_POST && Security::csrf_valid()) {
         $post->post = Arr::get($_POST, 'post');
         $post->author_ip = Request::$client_ip;
         $post->author_host = Request::host_name();
         if (!$post->loaded()) {
             // New post
             $post->forum_topic_id = $topic->id;
             $post->forum_area_id = $topic->forum_area_id;
             $post->author_id = self::$user->id;
             $post->author_name = self::$user->username;
             $post->created = time();
             $increase = true;
             // Notify recipients
             if ($this->private) {
                 $topic->notify_recipients(self::$user);
             }
         } else {
             // Old post
             $post->modify_count++;
             $post->modified = time();
             $increase = false;
         }
         try {
             $post->save();
             if ($increase) {
                 // Quote, only for public topics
                 if (!$this->private && $quote_id && $quote->author_id) {
                     $quoted = $quote->author_id;
                     $quote = new Model_Forum_Quote();
                     $quote->user_id = $quoted;
                     $quote->author_id = self::$user->id;
                     $quote->forum_topic_id = $topic->id;
                     $quote->forum_post_id = $post->id;
                     $quote->created = time();
                     $quote->save();
                 }
                 // Topic
                 $topic->post_count++;
                 $topic->last_post_id = $post->id;
                 $topic->last_poster = $post->author_name;
                 // If current topic is set to sink, don't update last posted date
                 if ($topic->status != Model_Forum_Topic::STATUS_SINK) {
                     $topic->last_posted = $post->created;
                 }
                 $topic->save();
                 // Area, only for public topics
                 if (!$this->private) {
                     $area = $topic->area();
                     $area->post_count++;
                     $area->last_topic_id = $topic->id;
                     $area->save();
                 }
                 // User
                 self::$user->post_count++;
                 self::$user->save();
                 // News feed
                 if (!$this->private) {
                     NewsfeedItem_Forum::reply(self::$user, $post);
                 }
             }
             if ($this->ajax) {
                 $post_route = Route::url($this->private ? 'forum_private_post' : 'forum_post', array('topic_id' => Route::model_id($topic), 'id' => $post->id));
                 $post_response = Request::factory($post_route)->execute();
                 $this->response->body($post_response->body());
                 return;
             }
             $this->request->redirect(Route::model($topic, '?page=last#last'));
         } catch (Validation_Exception $e) {
             $errors = $e->array->errors('validate');
         }
     }
     // Common attributes
     if ($quote_id) {
         $mode = View_Forum_PostEdit::QUOTE;
     } else {
         if ($post_id) {
             $mode = View_Forum_PostEdit::EDIT_POST;
         } else {
             $mode = View_Forum_PostEdit::REPLY;
         }
     }
     $section = $this->section_post_edit($mode, $post);
     $section->forum_topic = $topic;
     $section->errors = $errors;
     $section->cancel = $this->ajax ? Route::url($this->private ? 'forum_private_post' : 'forum_post', array('topic_id' => Route::model_id($topic), 'id' => $quote_id ? $quote_id : $post->id)) : Request::back(Route::model($topic), true);
     /*		$form = array(
     			'errors'  => $errors,
     			'ajax'    => $this->ajax ? true : null,
     			'topic'   => $topic,
     			'post'    => $post,
     			'user'    => self::$user,
     			'private' => $this->private,
     			'cancel'  => $this->ajax
     				? Route::get($this->private ? 'forum_private_post' : 'forum_post')
     						->uri(array(
     							'topic_id' => Route::model_id($topic),
     							'id'       => $quote_id ? $quote_id : $post->id,
     						))
     				: Request::back(Route::model($topic), true),
     		);*/
     if ($this->ajax) {
         $this->response->body($mode == View_Forum_PostEdit::EDIT_POST ? $section->content() : $section);
         return;
     }
     // Build page
     $this->view = new View_Page();
     $this->view->title_html = Forum::topic($topic);
     $this->view->add(View_Page::COLUMN_MAIN, $section);
     /*
     		Widget::add('main', View_Module::factory('forum/reply', array(
     			'mod_id'  => 'reply',
     		) + $form));*/
 }
Example #4
0
File: area.php Project: anqh/anqh
 /**
  * Refresh area data.
  * Potentially heavy function, use with caution!
  *
  * @param  boolean  $save
  */
 public function refresh($save = true)
 {
     if (!$this->loaded()) {
         return false;
     }
     // Get table names
     if ($this instanceof Anqh_Model_Forum_Area) {
         $topic_table = Model_Forum_Topic::factory()->get_table_name();
         $post_table = Model_Forum_Post::factory()->get_table_name();
     } else {
         $topic_table = Model_Forum_Private_Topic::factory()->get_table_name();
         $post_table = Model_Forum_Private_Post::factory()->get_table_name();
     }
     // Stats
     $this->topic_count = (int) DB::select(array(DB::expr('COUNT(id)'), 'topics'))->from($topic_table)->where('forum_area_id', '=', $this->id)->execute($this->_db)->get('topics');
     $this->post_count = (int) DB::select(array(DB::expr('COUNT(id)'), 'posts'))->from($post_table)->where('forum_area_id', '=', $this->id)->execute($this->_db)->get('posts');
     // Last topic
     $this->last_topic_id = (int) DB::select(array(DB::expr('MAX(id)'), 'topic_id'))->from($topic_table)->where('forum_area_id', '=', $this->id)->execute($this->_db)->get('topic_id');
     if ($save) {
         $this->save();
     }
     return true;
 }