예제 #1
0
파일: Post.php 프로젝트: zrosiak/fields
 /**
  * Ładowanie danych posta z bazy
  * @return Post
  */
 protected function loadPost()
 {
     $this->categoryPage = $this->property('categoryPage');
     $this->category = Category::loadCategory($this->categoryPage);
     $post = BlogPost::published()->with('categories')->hasView()->slug($this->property('slug'))->where('category_id', $this->category->id)->first();
     return $post;
 }
예제 #2
0
파일: Posts.php 프로젝트: zrosiak/fields
 public function index()
 {
     $this->vars['postsTotal'] = Post::count();
     $this->vars['postsPublished'] = Post::published()->count();
     $this->vars['postsExpired'] = Post::expired()->count();
     $this->vars['postsDrafts'] = $this->vars['postsTotal'] - $this->vars['postsPublished'] - $this->vars['postsExpired'];
     $this->asExtension('ListController')->index();
 }
예제 #3
0
파일: PostTest.php 프로젝트: zrosiak/fields
 public function testHasPublished()
 {
     $post = Post::first();
     $post->is_published = true;
     $post->published_at = date('Y-m-d');
     $post->save();
     dd(Post::first()->published_at);
     $this->assertEquals(1, Post::published()->get()->count());
 }
예제 #4
0
파일: Post.php 프로젝트: zrosiak/fields
 /**
  * pobieranie listy postów wg parametrów
  * @param array $params parametry
  * @return Collection
  */
 public static function getPosts(array $params, $query = null)
 {
     extract(array_merge(['post_id' => null, 'category' => null, 'categories_id' => [], 'template_id' => null, 'additional' => [], 'limit' => 0, 'tags' => false, 'post_tags' => [], 'promoted' => false, 'random' => false, 'subcategories' => false, 'category' => null, 'order' => 'published_at DESC', 'pagination' => false, 'posts_per_page' => 10, 'page' => 1], $params));
     $query = $query ?: Post::published()->with('template');
     // post_id
     if ($post_id) {
         $query->where('id', '<>', $post_id);
     }
     // categories_id
     // ustawione na sztywno id kategorii; nadpisują kategorię
     if ($categories_id) {
         $categories_id = is_array($categories_id) ? $categories_id : explode(',', $categories_id);
         $category = Category::find($categories_id);
     } elseif (!$category instanceof Category) {
         // category
         $category = Category::find($category);
     }
     // subcategories
     // pobieranie id podkategorii i kategorii, jeśli subcategories = true
     // w przeciwnym wypadku pobieranie id kategorii
     if ($category && !$categories_id) {
         if ($category instanceof Category) {
             $categories_id = $subcategories ? $category->getSubcategoriesId() : [$category->id];
         } else {
             $categories_id = [];
             foreach ($category as $c) {
                 $categories_id = $categories_id + ($subcategories ? $c->getSubcategoriesId() : [$c->id]);
             }
         }
     }
     // template_id
     if ($template_id) {
         $query->whereIn('template_id', is_array($template_id) ? $template_id : explode(',', $template_id));
     }
     // random
     if ($random) {
         $query->orderByRaw("RANDOM()");
     } else {
         $query->orderByRaw($order);
     }
     // additional
     if ($additional) {
         $additional = is_array($additional) ? $additional : [$additional];
         $key = array_search('variable', $additional);
         if ($key && isset(${$key})) {
             $additional[$key] = ${$key};
         }
         $query->additional($additional);
     }
     // promoted
     if ($promoted) {
         $query->additional(['is_promoted' => '1']);
     }
     // limit
     if ($limit) {
         $query->limit($limit);
     }
     $query->where(function ($q) use($tags, $post_tags, $categories_id, $category) {
         // tags
         if ($tags && $post_tags) {
             $q->whereHas('tags', function ($q) use($post_tags) {
                 $q->whereIn('id', array_fetch($post_tags->toArray(), 'id'));
             });
         }
         // categories
         if ($categories_id) {
             $q->whereIn('category_id', $categories_id);
         }
         // collection
         if (isset($category->collection)) {
             $collection = is_array($category->collection) ? $category->collection : [$category->collection];
             $q->orWhereIn(Db::raw("additional->>'collection'"), array_map('strval', $collection));
         }
     });
     $posts = $pagination ? $query->paginate($posts_per_page, $page) : $query->get();
     return $posts;
 }