コード例 #1
0
 /**
  * @see RainLab\Blog\Components\Posts::prepareVars()
  * @return mixed
  */
 protected function listPosts()
 {
     // get posts in excluded category
     $blockedPosts = [];
     $categories = BlogCategory::with(['posts' => function ($q) {
         $q->select('post_id');
     }])->whereIn('id', $this->property('excludeCategories'))->get();
     $categories->each(function ($item) use(&$blockedPosts) {
         $item->posts->each(function ($item) use(&$blockedPosts) {
             $blockedPosts[] = $item->post_id;
         });
     });
     // Filter posts
     $posts = BlogPost::with(['categories' => function ($q) {
         $q->whereNotIn('id', $this->property('excludeCategories'));
     }])->whereNotIn('id', $blockedPosts)->where(function ($q) {
         $q->where('title', 'LIKE', "%{$this->searchTerm}%")->orWhere('content', 'LIKE', "%{$this->searchTerm}%")->orWhere('excerpt', 'LIKE', "%{$this->searchTerm}%");
     });
     // filter categories
     $cat = Input::get('cat');
     if ($cat) {
         $cat = is_array($cat) ? $cat : [$cat];
         $posts->filterCategories($cat);
     }
     // List all the posts that match search terms, eager load their categories
     $posts = $posts->listFrontEnd(['page' => $this->property('pageNumber'), 'sort' => $this->property('sortOrder'), 'perPage' => $this->property('postsPerPage')]);
     /*
      * Add a "url" helper attribute for linking to each post and category
      */
     $posts->each(function ($post) {
         $post->setUrl($this->postPage, $this->controller);
         $post->categories->each(function ($category) {
             $category->setUrl($this->categoryPage, $this->controller);
         });
         // apply highlight of search result
         if ($this->property('hightlight')) {
             $searchTerm = preg_quote($this->searchTerm, '|');
             // apply highlight
             $post->title = preg_replace('|(' . $searchTerm . ')|i', '<mark>$1</mark>', $post->title);
             $post->excerpt = preg_replace('|(' . $searchTerm . ')|i', '<mark>$1</mark>', $post->excerpt);
             $post->content_html = preg_replace('~(?![^<>]*>)(' . $searchTerm . ')~ism', '<mark>$1</mark>', $post->content_html);
         }
     });
     return $posts;
 }
コード例 #2
0
ファイル: SearchResult.php プロジェクト: ardani/stikes-cms
 /**
  * Get the posts ids of posts with belong to specific categories
  * @param mixed $ids
  * @return array
  */
 protected function getPostIdsByCategories($ids = null)
 {
     if (is_null($ids) || !is_array($ids)) {
         return [];
     }
     $posts = [];
     $categories = BlogCategory::with(['posts' => function ($q) {
         $q->select('post_id');
     }])->whereIn('id', $ids)->get();
     $categories->each(function ($item) use(&$posts) {
         $item->posts->each(function ($item) use(&$posts) {
             $posts[] = $item->post_id;
         });
     });
     return $posts;
 }