/** * Display an article. * * @param \Yajra\CMS\Entities\Category $category * @param \Illuminate\Http\Request $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function show(Category $category, Request $request) { $category->increment('hits'); $layout = $request->query('layout', 'blog'); $limit = $request->get('limit', $layout == 'list' ? 10 : 5); $articles = $category->articles()->latest()->simplePaginate($limit); $path = null; if ($layout === 'list') { $path .= '?layout=list'; $articles->setPath($path); } if ($request->has('limit')) { $path .= '&limit=' . $limit; $articles->setPath($path); } event(new CategoryWasViewed($category)); return view("category.{$layout}", compact('category', 'articles', 'limit')); }
/** * Bootstrap the application services. * * @return void */ public function boot() { form()->macro('categories', function ($name = 'category_id', $selected = null, $options = []) { $html = []; Category::lists()->each(function (Category $category) use(&$html, $selected, $name) { $selected = form()->getValueAttribute($name, $selected); if (is_array($selected)) { $selected = in_array($category->id, $selected) ? 'selected' : null; } else { $selected = (string) $category->id == (string) $selected ? 'selected' : null; } $options = ['value' => $category->id, 'selected' => $selected, 'data-alias' => $category->present()->alias]; $html[] = new HtmlString('<option ' . html()->attributes($options) . '>' . e($category->present()->indentedTitle) . '</option>'); }); $list = implode('', $html); $options = html()->attributes($options + ['name' => $name]); return new HtmlString("<select{$options}>{$list}</select>"); }); form()->macro('imageBrowser', function ($name, $selected = null, $options = []) { return view('system.macro.image-browser', ['name' => $name, 'options' => $options, 'selected' => $selected]); }); }
/** * Rebuild category entity nested set tree. */ public function rebuildCategory() { Category::rebuild(true); return $this->notifySuccess(trans('cms::utilities.category.success')); }
/** * Get the query object to be processed by datatables. * * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder */ public function query() { $category = Category::select(['categories.id', 'categories.lft', \DB::raw('categories.published as status'), 'categories.title', 'categories.depth', 'categories.alias', 'categories.hits', 'categories.authenticated', 'categories.published', 'categories.created_at', 'categories.updated_at'])->whereNotNull('parent_id'); return $this->applyScopes($category); }
/** * Button Response from DataTable request to publish status. * * @param \Yajra\CMS\Entities\Category $category * @return \Illuminate\Http\JsonResponse */ public function publish(Category $category) { $category->togglePublishedState(); $category->getDescendants()->each(function (Category $cat) use($category) { $cat->published = $category->published; $cat->save(); }); return $this->notifySuccess(sprintf('Category successfully %s!', $category->published ? 'published' : 'unpublished')); }
/** * Get related category. * * @return \Yajra\CMS\Entities\Article */ public function category() { $category = $this->fluentParameters()->get('category_id', 0); $categoryId = explode(':', $category)[0]; return Category::findOrNew($categoryId); }