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(); }
/** * 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); }
/** * 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); }