Exemplo n.º 1
0
 /**
  * Load post and start building query for related posts
  */
 public function onRun()
 {
     // Load the target post
     $post = Post::where('slug', $this->property('slug'))->with('tags')->first();
     // Abort if there is no source, or it has no tags
     if (!$post || !($tagIds = $post->tags->lists('id'))) {
         return;
     }
     // Start building our query for related posts
     $query = Post::isPublished()->where('id', '<>', $post->id)->whereHas('tags', function ($tag) use($tagIds) {
         $tag->whereIn('id', $tagIds);
     })->with('tags');
     // Sort the related posts
     $subQuery = DB::raw('(
         select count(*)
         from `bedard_blogtags_post_tag`
         where `bedard_blogtags_post_tag`.`post_id` = `rainlab_blog_posts`.`id`
         and `bedard_blogtags_post_tag`.`tag_id` in (' . implode(', ', $tagIds) . ')
     )');
     $key = $this->property('orderBy') ?: $subQuery;
     $query->orderBy($key, $this->property('direction'));
     // Limit the number of results
     if ($take = intval($this->property('results'))) {
         $query->take($take);
     }
     // Execute the query
     $this->posts = $query->get();
 }
Exemplo n.º 2
0
 public function onStart()
 {
     $slug = $this->param('slug');
     $this['post'] = Post::where('slug', '=', $slug)->isPublished()->with('categories')->first();
     if (!$this['post']) {
         return Redirect::to('/404');
     }
     // only to change the title to be more pleasant
     $this->page->settings['title'] = "Post - " . $this["post"]->title;
 }
Exemplo n.º 3
0
 /**
  * Load post and start building query for related posts
  */
 public function onRun()
 {
     $post = Post::where('slug', $this->property('slug'))->with('tags')->first();
     if (!$post) {
         return;
     }
     $this->tagIds = [];
     foreach ($post->tags as $tag) {
         $this->tagIds[] = $tag->id;
     }
     if (empty($this->tagIds)) {
         return;
     }
     $this->query = Post::isPublished()->where('id', '<>', $post->id)->whereHas('tags', function ($query) {
         $query->whereIn('id', $this->tagIds);
     })->with('tags');
 }
Exemplo n.º 4
0
 public function boot()
 {
     PostComponent::extend(function ($component) {
         if ($this->app->runningInBackend()) {
             return;
         }
         if (!Session::has('postsviewed')) {
             Session::put('postsviewed', []);
         }
         $post = PostModel::where('slug', $component->getController()->getRouter()->getParameters()['slug'])->first();
         if (!is_null($post) && !in_array($post->getKey(), Session::get('postsviewed'))) {
             $this->setViews($post);
             Session::push('postsviewed', $post->getKey());
         }
         return true;
     });
 }
Exemplo n.º 5
0
 protected function findDuplicatePost($data)
 {
     if ($id = array_get($data, 'id')) {
         return Post::find($id);
     }
     $title = array_get($data, 'title');
     $post = Post::where('title', $title);
     if ($slug = array_get($data, 'slug')) {
         $post->orWhere('slug', $slug);
     }
     return $post->first();
 }
Exemplo n.º 6
0
 /**
  *
  * Returns array of archive posts
  * Uses properties: year, month, day to figure out archive page settings
  *
  * @return array
  */
 public function archivePosts()
 {
     list($start, $end) = $this->getCurrentRange();
     $posts = Post::where('published_at', '>=', $start)->where('published_at', '<', $end)->with('categories')->orderBy('published_at', 'desc')->get();
     return $this->preparePosts($posts);
 }
 private static function findPostTimestamp($searcher)
 {
     $query_builder = Post::where('published', 1)->where('published_at', '<=', Db::raw('NOW()'));
     return $searcher($query_builder, 'published_at');
 }