Exemplo n.º 1
0
 /**
  * Tworzenie struktury związku
  * @param Post $post
  * @param Category $category
  * @param array $params dodatkowe parametry
  * @return Collection
  */
 public static function getStructure(Post $post, Category $category, array $params = [])
 {
     $structure = null;
     $categories = $category->getSubcategories(false, true);
     if ($categories) {
         $structure = Category::orderByRaw('"order" asc NULLS LAST')->find($categories)->filter(function ($elem) use($post) {
             if (empty($elem->communication) === false) {
                 $post = BlogPost::where('id', $elem->communication)->first();
                 $elem->communication = $post->getPaths();
             }
             $elem->posts = BlogPost::getPosts(['categories_id' => [$elem->id], 'template_id' => 2, 'additional' => ['person_type' => 3], 'order' => "additional->>'order' NULLS LAST"]);
             return $elem->posts->count() > 0;
         });
     }
     return $structure;
 }
Exemplo n.º 2
0
 /**
  * Ł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;
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function onRun()
 {
     $this->category_page = $this->property('categoryPage');
     $this->wide = $this->property('wide') == true;
     $this->type = $this->property('type');
     $this->limit = $this->property('limit');
     $this->subcategories = $this->property('subcategories');
     $this->random = $this->property('random');
     $this->is_promoted = $this->property('is_promoted');
     $this->templates = $this->property('template');
     $this->partial = BoxSetting::find($this->type);
     $this->category = Category::loadCategory($this->category_page);
     $this->posts = BlogPost::getPosts(['post_id' => isset($this->page['post']) ? $this->page['post']->id : null, 'category' => $this->category, 'categories_id' => $this->property('categories_id'), 'limit' => $this->limit, 'template_id' => $this->templates, 'promoted' => $this->is_promoted, 'random' => $this->random, 'subcategories' => $this->subcategories]);
 }
Exemplo n.º 4
0
 protected function loadCategories()
 {
     $categories = BlogCategory::orderBy('name');
     if (!$this->property('displayEmpty')) {
         $categories->whereExists(function ($query) {
             $query->select(Db::raw(1))->from('rainlab_blog_posts_categories')->join('rainlab_blog_posts', 'rainlab_blog_posts.id', '=', 'rainlab_blog_posts_categories.post_id')->whereNotNull('rainlab_blog_posts.published')->where('rainlab_blog_posts.published', '=', 1)->whereRaw('rainlab_blog_categories.id = rainlab_blog_posts_categories.category_id');
         });
     }
     $categories = $categories->get();
     /*
      * Add a "url" helper attribute for linking to each category
      */
     $categories->each(function ($category) {
         $category->setUrl($this->categoryPage, $this->controller);
     });
     return $categories;
 }
Exemplo n.º 5
0
 /**
  * zmiana nadrzędnej kategorii
  */
 public function onMove()
 {
     $result = false;
     $nodeId = post('node_id');
     $parentId = post('parent_id');
     $nodeStauts = post('status');
     $order = post('order', 0);
     if (isset($nodeId, $parentId) && $nodeId !== $parentId) {
         $item = Category::find($nodeId);
         if ($item && $item->parent_id !== (int) $parentId) {
             $item->parent_id = (int) $parentId ?: null;
             $item->order = $order;
             $item->save();
             $result = count($item->getParents());
         }
     }
     return ['result' => $result];
 }
Exemplo n.º 6
0
 /**
  * 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;
 }
Exemplo n.º 7
0
 public function beforeSave()
 {
     $object = isset($this->id) ? $this : Category::first();
     if (isset($object->id) && ($original = $object->getOriginal())) {
         // wydobywanie i zapisywanie dodatkowych parametrów
         $dynamicAttributes = array_except($this->getAttributes(), array_keys($original));
         foreach ($dynamicAttributes as $key => $attribute) {
             unset($this->{$key});
         }
         $this->additional = $dynamicAttributes;
     }
     // generwanie atrybutu url
     $this->getUrl();
 }
Exemplo n.º 8
0
 protected function prepareVars()
 {
     $this->pageParam = $this->paramName('pageNumber');
     $this->noPostsMessage = $this->property('noPostsMessage');
     $this->postPage = $this->property('postPage');
     $this->categoryPage = $this->property('categoryPage');
     $this->categoryParent = $this->property('categoryParent');
     $this->category = BlogCategory::loadCategory($this->categoryPage, $this->categoryParent);
     $this->template = $this->property('template') ?: (empty($this->category->template->partial_category) ? null : $this->category->template->partial_category);
     if (empty($this->category->id) || empty($this->category->template->id) && empty($this->template->id)) {
         return \Response::make($this->controller->run('404'), 404);
     }
     // paginacja domyślnie włączona; 'false' wyłącza
     $this->paginationActive = $this->category->pagination !== 'false' || $this->property('postsPerPage') > 0;
     $this->posts = BlogPost::getPosts(['category' => $this->category, 'categories_id' => $this->property('categories_id'), 'template_id' => $this->property('template_id') ?: $this->category->template_id, 'pagination' => $this->paginationActive, 'posts_per_page' => (int) $this->category->pagination > 0 ? $this->category->pagination : $this->property('postsPerPage'), 'page' => $this->property('pageNumber'), 'subcategories' => $this->property('subcategories'), 'order' => "NULLIF(regexp_replace(additional->>'order', E'\\D', '', 'g'), '')::int NULLS LAST, published_at DESC"]);
     $this->setMeta();
 }