/** * Render comment list. * * @return string */ public function comments() { $html = ''; $comments = comment::findByPage($this->page)->page(1, 10); if ($comments->count() > 0) { $html .= '<div class="items comments">'; foreach ($comments as $comment) { $html .= tpl::load(__DIR__ . DS . 'template.php', array('field' => $this, 'comment' => $comment)); } $html .= '</div>'; } return $html; }
/** * Retrieve all comments for the current page. * * @param string $page_uri Page uri. * @param array $args Optional arguments. * * @return Comments */ function comments($page_uri = null, $args = array()) { if (is_null($page_uri)) { $page_uri = page()->uri(); } // Customize query to perform $defaults = array('walker' => null, 'echo' => true, 'author' => false, 'user' => false, 'order_by' => 'id', 'order' => 'DESC', 'unapproved' => true, 'page' => 1, 'per_page' => 20); $args = array_merge($defaults, $args); extract($args, EXTR_SKIP); // Prepare query $query = comment::findByPage($page_uri); // Approved comments only if ($unapproved === false) { $query->andWhere('status', '=', Comment::STATUS_APPROVED); } // Specifc author only if (!empty($author)) { $query->andWhere('author', 'LIKE', $author); } else { if (!empty($user)) { $user = $user instanceof User ? $user->username() : $user; $query->andWhere('username', 'LIKE', $user); } } // Order by clause if (in_array($order_by, array('id', 'created_at', 'updated_at'))) { $order = strtoupper($order) === 'DESC' ? $order : 'ASC'; $query->order($order_by . ' ' . $order); } // Perform query $comments = $query->page($page, $per_page); // Render comments if requested if ($echo) { $walker = !is_null($walker) ? new $walker() : new Comments\View\Walker(); $output = $walker->walk($comments); echo $output; } return $comments; }
/** * Get all comments for a given page uri. Respects optional request parameter. * * @param string $uri Page uri * @return Collection */ protected function findComments($uri) { // Take optional url paramaters into account $page = get('page', 1); $perPage = get('per_page', 30); return comment::findByPage($uri)->page($page, $perPage); }