/** * 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; }
/** * Edit forum topic * * @param integer $area_id * @param integer $topic_id * * @throws Model_Exception invalid area, invalid topic * @throws InvalidArgumentException missing area and topic */ protected function _edit_topic($area_id = null, $topic_id = null) { $this->history = false; $this->view = new View_Page(); if ($area_id && !$topic_id) { // Start new topic $mode = View_Forum_PostEdit::NEW_TOPIC; /** @var Model_Forum_Private_Area|Model_Forum_Area $area */ $area = $this->private ? Model_Forum_Private_Area::factory($area_id) : Model_Forum_Area::factory($area_id); if (!$area->loaded()) { throw new Model_Exception($area, $area_id); } Permission::required($area, Model_Forum_Area::PERMISSION_POST, self::$user); $this->view->title = HTML::chars($area->name); if ($this->private) { $topic = new Model_Forum_Private_Topic(); $post = new Model_Forum_Private_Post(); $cancel = Route::url('forum_area', array('id' => 'private', 'action' => '')); $recipients = array(); } else { $topic = new Model_Forum_Topic(); $post = new Model_Forum_Post(); $cancel = Route::model($area); } } else { if ($topic_id) { // Edit old topic $mode = View_Forum_PostEdit::EDIT_TOPIC; /** @var Model_Forum_Private_Topic|Model_Forum_Topic $topic */ $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_UPDATE, self::$user); // Build recipients list if ($this->private) { $recipients = $topic->find_recipient_names(); } $this->view->title_html = Forum::topic($topic); $cancel = Route::model($topic); // Set actions if (Permission::has($topic, Model_Forum_Topic::PERMISSION_DELETE, self::$user)) { $this->view->actions[] = array('link' => Route::model($topic, 'delete') . '?' . Security::csrf_query(), 'text' => '<i class="icon-trash icon-white"></i> ' . __('Delete topic'), 'class' => 'btn btn-danger topic-delete'); } } else { throw new InvalidArgumentException('Topic and area missing'); } } $errors = array(); if ($_POST && Security::csrf_valid()) { // Get recipients if ($this->private) { $post_recipients = array(); foreach (explode(',', Arr::get_once($_POST, 'recipients')) as $recipient) { if ($user = Model_User::find_user_light(trim($recipient))) { $post_recipients[$user['id']] = $user['username']; } } // Make sure author is included $post_recipients[self::$user->id] = self::$user->username; } if (isset($post)) { // New topic $post->post = $_POST['post']; $post->forum_area_id = $area->id; $post->author_id = self::$user->id; $post->author_name = self::$user->username; $post->author_ip = Request::$client_ip; $post->author_host = Request::host_name(); $post->created = time(); try { $post->is_valid(); } catch (Validation_Exception $e) { $errors += $e->array->errors('validate'); } $topic->author_id = self::$user->id; $topic->author_name = self::$user->username; $topic->name = $_POST['name']; $topic->forum_area_id = $area->id; $topic->created = time(); try { $topic->is_valid(); } catch (Validation_Exception $e) { $errors += $e->array->errors('validate'); } // If no errors found, save models if (empty($errors)) { $topic->save(); // Recipients if ($this->private) { $topic->set_recipients($post_recipients); } // Post $post->forum_topic_id = $topic->id; $post->save(); // Topic $topic->first_post_id = $topic->last_post_id = $post->id; $topic->last_poster = self::$user->username; $topic->last_posted = time(); $topic->post_count = 1; $topic->save(); // Area, only public forums if (!$this->private) { $area->last_topic_id = $topic->id; $area->post_count++; $area->topic_count++; $area->save(); } // User self::$user->post_count++; self::$user->save(); // News feed if (!$this->private) { NewsfeedItem_Forum::topic(self::$user, $topic); } $this->request->redirect(Route::model($topic)); } isset($post_recipients) and $recipients = $post_recipients; } else { // Old topic $topic->set_fields(Arr::intersect($_POST, array('name', 'status', 'sticky'))); try { $topic->save(); // Recipients if ($this->private) { $topic->set_recipients($post_recipients); } $this->request->redirect(Route::model($topic)); } catch (Validation_Exception $e) { $errors = $e->array->errors('validate'); } } } $form['errors'] = $errors; $section = $this->section_post_edit($mode, isset($post) ? $post : null); $section->forum_topic = $topic; $section->errors = $errors; $section->cancel = $cancel; $section->recipients = isset($recipients) ? implode(', ', $recipients) : null; $this->view->add(View_Page::COLUMN_MAIN, $section); }
/** * Action: private */ public function action_messages() { Permission::required(new Model_Forum_Private_Area(), Model_Forum_Private_Area::PERMISSION_READ, self::$user); // Build page $this->view = new View_Page(__('Private messages')); $this->view->tab = 'private-messages'; $this->view->subtitle = __('Personal and group messages'); // Set actions if (Permission::has(new Model_Forum_Private_Area(), Model_Forum_Private_Area::PERMISSION_POST, self::$user)) { $this->view->actions[] = array('link' => Route::url('forum_private_topic_add', array('action' => 'post')), 'text' => '<i class="icon-plus-sign icon-white"></i> ' . __('New message'), 'class' => 'btn btn-primary'); } // Pagination $pagination = $this->section_pagination(Model_Forum_Private_Topic::factory()->get_count(self::$user)); $this->view->add(View_Page::COLUMN_MAIN, $pagination); // Posts $this->view->add(View_Page::COLUMN_MAIN, $this->section_topics_private(Model_Forum_Private_Area::factory()->find_topics(self::$user, $pagination->offset, $pagination->items_per_page))); // Pagination $this->view->add(View_Page::COLUMN_MAIN, $pagination); $this->_side_views(); }