/** * Save the record * * @return boolean False if error, True on success */ public function save() { $section = $this->get('section'); $this->removeAttribute('section'); $category = $this->get('category'); $this->removeAttribute('category'); if (!$this->get('access')) { $this->set('access', (int) \Config::get('access')); } $isNew = $this->isNew(); if ($isNew && !$this->get('parent')) { $this->set('lft', 0); $this->set('rgt', 1); } if ($this->isNew() && $this->get('parent')) { $parent = $this->parent(); if (!$parent) { $this->addError(Lang::txt('Parent node does not exist.')); return false; } // Get the reposition data for shifting the tree and re-inserting the node. if (!($reposition = $this->getTreeRepositionData($parent, 2, 'last-child'))) { // Error message set in getNode method. return false; } // Shift left values. $query = $this->getQuery()->update($this->getTableName())->set(['lft' => new Raw('lft + 2')])->where($reposition->left_where['col'], $reposition->left_where['op'], $reposition->left_where['val'])->whereEquals('scope', $parent->get('scope'))->whereEquals('scope_id', $parent->get('scope_id'))->whereEquals('thread', $parent->get('thread')); if (!$query->execute()) { $this->addError($query->getError()); return false; } // Shift right values. $query = $this->getQuery()->update($this->getTableName())->set(['rgt' => new Raw('rgt + 2')])->where($reposition->right_where['col'], $reposition->right_where['op'], $reposition->right_where['val'])->whereEquals('scope', $parent->get('scope'))->whereEquals('scope_id', $parent->get('scope_id'))->whereEquals('thread', $parent->get('thread')); if (!$query->execute()) { $this->addError($query->getError()); return false; } $this->set('lft', $reposition->new_lft); $this->set('rgt', $reposition->new_rgt); } $result = parent::save(); if ($result) { // Set the thread ID if (!$this->get('parent')) { $this->set('thread', $this->get('id')); $result = parent::save(); } if (!$isNew) { // Make sure state and category changes carry through to replies // If it's marked as deleted, skip it $query = $this->getQuery()->update($this->getTableName())->set(['state' => $this->get('state'), 'category_id' => $this->get('category_id')])->whereEquals('parent', $this->get('id'))->where('state', '!=', self::STATE_DELETED); if (!$query->execute()) { $this->addError($query->getError()); return false; } // Make sure state changes carry through to attachments $query = $this->getQuery()->update(Attachment::blank()->getTableName())->set(['state' => $this->get('state')])->whereEquals('post_id', $this->get('id'))->where('state', '!=', self::STATE_DELETED); if (!$query->execute()) { $this->addError($query->getError()); return false; } } } if ($section) { $this->set('section', $section); } if ($category) { $this->set('category', $category); } return $result; }
/** * Get a filtered list of posts for a thread * * @param object $post \Components\Forum\Models\Post * @param array $filters Filters to apply * @return void */ protected function _posts($post, $filters = array()) { $thread = new stdClass(); $thread->lastchange = '0000-00-00 00:00:00'; $thread->lastid = 0; $thread->posts = null; $thread->html = null; $thread->total = 0; //$results = $post->getTree($post->id, $filters); $entries = Post::all()->whereEquals('thread', $post->id)->whereIn('state', (array) $filters['state'])->whereIn('access', $filters['access']); if (isset($filters['start_at']) && $filters['start_at']) { $entries->where('created', '>', $filters['start_at']); } $results = $entries->order('created', 'asc')->rows(); if ($results->count()) { //$results = $post->toTree($results); $res = array(); $i = 0; foreach ($results as $row) { $thread->lastchange = $row->get('created') > $thread->lastchange ? $row->get('created') : $thread->lastchange; $res[$i] = new stdClass(); $res[$i]->replies = null; $cview = $this->view('comment', 'threads')->set('option', $this->option)->set('comment', $row)->set('post', $post)->set('config', $this->params)->set('depth', Request::getInt('depth', 1, 'post'))->set('cls', 'odd')->set('base', $this->base)->set('attach', Attachment::blank())->set('course', $this->course)->set('search', '')->set('unit', '')->set('lecture', ''); if ($this->_active == 'outline') { $cview->set('unit', $this->unit->get('alias')); $cview->set('lecture', $this->lecture->get('alias')); } foreach ($row->toArray() as $key => $val) { $res[$i]->{$key} = $val; } $res[$i]->html = $cview->loadTemplate(); $i++; } $thread->total = count($res); $thread->posts = $res; } return $thread; }