public function onStart() { $slug = $this->param('slug'); $this['category'] = Category::where('slug', '=', $slug)->first(); if ($this['category']) { $post = new Post(); $query = $post->isPublished()->orderBy('published_at', 'desc')->with('categories'); $this['posts'] = $post->scopeFilterCategories($query, [$this['category']->id])->get(); } }
public function run() { // // @todo // // Add a Welcome post or something // Category::create(['name' => trans('rainlab.blog::lang.categories.uncategorized'), 'slug' => 'uncategorized']); }
protected function loadCategory() { if (!($categoryId = $this->property('categoryFilter'))) { return null; } if (!($category = BlogCategory::whereSlug($categoryId)->first())) { return null; } return $category; }
public function run() { // // @todo // // Add a Welcome post or something // Category::create(['name' => trans('rainlab.blog::lang.categories.uncategorized'), 'slug' => 'uncategorized']); IMsg::create(['user_id' => User::orderBy('id', 'desc')->first()->id, 'content' => '初始化信息第一条']); }
public function run() { Post::create(['title' => 'First blog post', 'slug' => 'first-blog-post', 'content' => ' This is your first ever **blog post**! It might be a good idea to update this post with some more relevant content. You can edit this content by selecting **Blog** from the administration back-end menu. *Enjoy the good times!* ', 'excerpt' => 'The first ever blog post is here. It might be a good idea to update this post with some more relevant content.', 'published_at' => Carbon::now(), 'published' => true]); Category::create(['name' => trans('rainlab.blog::lang.categories.uncategorized'), 'slug' => 'uncategorized']); }
public function index_onDelete() { if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) { foreach ($checkedIds as $categoryId) { if (!($category = Category::find($categoryId))) { continue; } $category->delete(); } Flash::success('Successfully deleted those categories.'); } return $this->listRefresh(); }
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->getNested(); /* * Add a "url" helper attribute for linking to each category */ return $this->linkCategories($categories); }
public function up() { if (Schema::hasColumn('rainlab_blog_categories', 'parent_id')) { return; } Schema::table('rainlab_blog_categories', function ($table) { $table->integer('parent_id')->unsigned()->index()->nullable(); $table->integer('nest_left')->nullable(); $table->integer('nest_right')->nullable(); $table->integer('nest_depth')->nullable(); }); foreach (CategoryModel::all() as $category) { $category->setDefaultLeftAndRight(); $category->save(); } }
public function boot() { Event::listen('pages.menuitem.listTypes', function () { return ['blog-category' => 'Blog category', 'all-blog-categories' => 'All blog categories']; }); Event::listen('pages.menuitem.getTypeInfo', function ($type) { if ($type == 'blog-category' || $type == 'all-blog-categories') { return Category::getMenuTypeInfo($type); } }); Event::listen('pages.menuitem.resolveItem', function ($type, $item, $url, $theme) { if ($type == 'blog-category' || $type == 'all-blog-categories') { return Category::resolveMenuItem($item, $url, $theme); } }); }
public function boot() { /* * Register menu items for the RainLab.Pages plugin */ Event::listen('pages.menuitem.listTypes', function () { return ['blog-category' => 'rainlab.blog::lang.menuitem.blog_category', 'all-blog-categories' => 'rainlab.blog::lang.menuitem.all_blog_categories']; }); Event::listen('pages.menuitem.getTypeInfo', function ($type) { if ($type == 'blog-category' || $type == 'all-blog-categories') { return Category::getMenuTypeInfo($type); } }); Event::listen('pages.menuitem.resolveItem', function ($type, $item, $url, $theme) { if ($type == 'blog-category' || $type == 'all-blog-categories') { return Category::resolveMenuItem($item, $url, $theme); } }); }
public function onGetByCategory() { if (Request::has('category')) { $category = Request::input('category'); } else { return; } if (Request::has('numberOfPost')) { $numberOfPost = Request::input('numberOfPost'); } else { $numberOfPost = 4; } $posts = Category::whereSlug($category)->first()->posts()->isPublished()->with('categories', 'tags')->orderBy('published_at', 'desc')->take($numberOfPost)->get(); /* * Add a "url" helper attribute for linking to each post and category */ $posts->each(function ($post) { $post->setUrl($this->property('postPage'), $this->controller); $post->categories->each(function ($category) { $category->setUrl($this->property('categoryPage'), $this->controller); }); }); return $posts; }
protected function getData() { $categories = Category::all(); return $categories; }
/** * Lists posts for the front end * @param array $options Display options * @return self */ public function scopeListFrontEnd($query, $options) { /* * Default options */ extract(array_merge(['page' => 1, 'perPage' => 30, 'sort' => 'created_at', 'categories' => null, 'category' => null, 'search' => '', 'published' => true], $options)); $searchableFields = ['title', 'slug', 'excerpt', 'content']; if ($published) { $query->isPublished(); } /* * Sorting */ if (!is_array($sort)) { $sort = [$sort]; } foreach ($sort as $_sort) { if (in_array($_sort, array_keys(self::$allowedSortingOptions))) { $parts = explode(' ', $_sort); if (count($parts) < 2) { array_push($parts, 'desc'); } list($sortField, $sortDirection) = $parts; if ($sortField == 'random') { $sortField = DB::raw('RAND()'); } $query->orderBy($sortField, $sortDirection); } } /* * Search */ $search = trim($search); if (strlen($search)) { $query->searchWhere($search, $searchableFields); } /* * Categories */ if ($categories !== null) { if (!is_array($categories)) { $categories = [$categories]; } $query->whereHas('categories', function ($q) use($categories) { $q->whereIn('id', $categories); }); } /* * Category, including children */ if ($category !== null) { $category = Category::find($category); $categories = $category->getAllChildrenAndSelf()->lists('id'); $query->whereHas('categories', function ($q) use($categories) { $q->whereIn('id', $categories); }); } return $query->paginate($perPage, $page); }
/** * @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; }
protected function getCategoryIdsForPost($data) { $ids = []; if ($this->auto_create_categories) { $categoryNames = $this->decodeArrayValue(array_get($data, 'categories')); foreach ($categoryNames as $name) { if (!($name = trim($name))) { continue; } if (isset($this->categoryNameCache[$name])) { $ids[] = $this->categoryNameCache[$name]; } else { $newCategory = Category::firstOrCreate(['name' => $name]); $ids[] = $this->categoryNameCache[$name] = $newCategory->id; } } } elseif ($this->categories) { $ids = (array) $this->categories; } return $ids; }
/** * Returns the URL for the master object of given ID * * @param MenuItem $item Master object iD * * @return string */ public function getUrl(MenuItem $item) { $page_url = Settings::get('blog_category_page', 'blog/category'); $category = Category::find($item->master_object_id); if (!$category) { throw new ApplicationException("Category not found."); } return URL::to($page_url . '/' . $category->slug); }
/** * Prepare vars */ public function onRun() { $this->resultPage = $this->page['resultPage'] = $this->property('resultPage'); $this->categories = $this->page['categories'] = BlogCategory::lists('name', 'id'); $this->categoryFilter = $this->page['categoryFilter'] = $this->property('categoryFilter'); }
/** * 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; }