예제 #1
0
 protected function prepareTopics()
 {
     if ($area = $this->getArea()) {
         $currentPage = input('page');
         $search = trim(input('search'));
         /** @var  Collection  $topics */
         $topics = TopicModel::filterAreas($area->id)->recentPosts()->search($search)->paginate(20, $currentPage);
         // Add url
         $postSearchTokens = Search::parseQuery($search, ['post'], ['post' => 'post', 'author' => 'author', 'by' => 'author']);
         $postSearch = Search::buildQuery($postSearchTokens, ['post']);
         $topics->each(function (TopicModel $topic) use($postSearch) {
             $topic->setUrl($this->topicPage, $this->controller, $postSearch ? ['search' => $postSearch] : null);
         });
         $this->page['topics'] = $this->topics = $topics;
         // Paginate
         if ($topics) {
             $query = [];
             $search and $query['search'] = $search;
             $query['page'] = '';
             $paginationUrl = Request::url() . '?' . http_build_query($query);
             if ($currentPage > ($lastPage = $topics->lastPage()) && $currentPage > 1) {
                 return Redirect::to($paginationUrl . $lastPage);
             }
             $this->page['paginationUrl'] = $paginationUrl;
         }
     }
 }