|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Javaabu\Cms\Http\Controllers\Admin; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use Javaabu\Cms\Http\Requests\CategoriesRequest; |
| 7 | +use Javaabu\Cms\Models\Category; |
| 8 | +use Javaabu\Cms\Models\CategoryType; |
| 9 | +use Javaabu\Helpers\Http\Controllers\Controller; |
| 10 | +use Javaabu\Helpers\Traits\HasOrderbys; |
| 11 | +class CategoriesController extends Controller |
| 12 | +{ |
| 13 | + use HasOrderbys; |
| 14 | + |
| 15 | + /** |
| 16 | + * Create a new controller instance. |
| 17 | + */ |
| 18 | + public function __construct() |
| 19 | + { |
| 20 | + //$this->authorizeResource(Category::class); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Initialize orderbys |
| 25 | + */ |
| 26 | + protected static function initOrderbys() |
| 27 | + { |
| 28 | + static::$orderbys = [ |
| 29 | + 'id' => _d('Id'), |
| 30 | + 'created_at' => _d('Created At'), |
| 31 | + 'updated_at' => _d('Updated At'), |
| 32 | + 'name' => _d('Name') |
| 33 | + ]; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Display a listing of the resource. |
| 38 | + */ |
| 39 | + public function index($locale, CategoryType $type, Request $request) |
| 40 | + { |
| 41 | + $this->authorize('viewAny', $type); |
| 42 | + |
| 43 | + $title = _d('All Categories'); |
| 44 | + $orderby = $this->getOrderBy($request, 'created_at'); |
| 45 | + $order = $this->getOrder($request, 'created_at', $orderby); |
| 46 | + $per_page = $this->getPerPage($request); |
| 47 | + |
| 48 | + $categories = $type->categories() |
| 49 | + ->withDepth() |
| 50 | + ->defaultOrder() |
| 51 | + ->orderBy($orderby, $order); |
| 52 | + |
| 53 | + $search = null; |
| 54 | + if ($search = $request->input('search')) { |
| 55 | + $categories->search($search); |
| 56 | + $title = _d('Categories matching \':search\'', ['search' => $search]); |
| 57 | + } |
| 58 | + |
| 59 | + if ($primary_language = $request->input('primary_language')) { |
| 60 | + $categories->whereLang($primary_language); |
| 61 | + } |
| 62 | + |
| 63 | + if (! is_null($request->input('is_translated')) && $request->has('is_translated')) { |
| 64 | + if ($request->boolean('is_translated')) { |
| 65 | + $categories->whereNotNull('translations'); |
| 66 | + } else { |
| 67 | + $categories->whereNull('translations'); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + $categories->with('type.postType') |
| 72 | + ->withCount('posts', 'staffs', 'statistics'); |
| 73 | + |
| 74 | +// if ($request->download) { |
| 75 | +// return (new CategoriesExport($categories))->download('categories.xlsx'); |
| 76 | +// } |
| 77 | + |
| 78 | + $categories = $categories->paginate($per_page) |
| 79 | + ->appends($request->except('page')); |
| 80 | + |
| 81 | + return view('admin.categories.index', compact('categories', 'type', 'title', 'per_page', 'search')); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Show the form for creating a new resource. |
| 86 | + */ |
| 87 | + public function create($locale, CategoryType $type, Request $request) |
| 88 | + { |
| 89 | + $this->authorize('create', $type); |
| 90 | + |
| 91 | + return view('admin.categories.create', compact('type')); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Store a newly created resource in storage. |
| 96 | + */ |
| 97 | + public function store($locale, CategoryType $type, CategoriesRequest $request) |
| 98 | + { |
| 99 | + $this->authorize('create', $type); |
| 100 | + |
| 101 | + $category = new Category($request->validated()); |
| 102 | + |
| 103 | + $category->type()->associate($type); |
| 104 | + |
| 105 | + $category->slug = $request->input('slug'); |
| 106 | + |
| 107 | + $category->lang = $request->input('lang', app()->getLocale()); |
| 108 | + |
| 109 | + if ($request->has('icon')) { |
| 110 | + $category->icon = $request->input('icon'); |
| 111 | + } |
| 112 | + |
| 113 | + if ($request->has('color')) { |
| 114 | + $category->color = $request->input('color'); |
| 115 | + } |
| 116 | + |
| 117 | + $category->save(); |
| 118 | + |
| 119 | + $category->updateSingleAttachment('featured_image', $request); |
| 120 | + |
| 121 | + if ($request->expectsJson()) { |
| 122 | + return response()->json($category); |
| 123 | + } |
| 124 | + |
| 125 | + $this->flashSuccessMessage(); |
| 126 | + |
| 127 | + return redirect()->action([CategoriesController::class, 'edit'], [$locale, $type, $category]); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Display the specified resource. |
| 132 | + */ |
| 133 | + public function show($locale, CategoryType $type, Category $category) |
| 134 | + { |
| 135 | + $this->authorize('view', $category); |
| 136 | + |
| 137 | + return redirect()->action([CategoriesController::class, 'edit'], [$locale, $type, $category]); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Show the form for editing the specified resource. |
| 142 | + */ |
| 143 | + public function edit($locale, CategoryType $type, Category $category) |
| 144 | + { |
| 145 | + $this->authorize('update', $category); |
| 146 | + |
| 147 | + $allowed_categories = Category::categoryList($type->id, $category->id); |
| 148 | + $category->dontShowTranslationFallbacks(); |
| 149 | + return view('admin.categories.edit', compact('category', 'type', 'allowed_categories')); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Update the specified resource in storage. |
| 154 | + */ |
| 155 | + public function update($locale, CategoryType $type, CategoriesRequest $request, Category $category) |
| 156 | + { |
| 157 | + $this->authorize('update', $category); |
| 158 | + |
| 159 | + $category->fill($request->validated()); |
| 160 | + |
| 161 | + if ($slug = $request->input('slug')) { |
| 162 | + $category->slug = $slug; |
| 163 | + } |
| 164 | + |
| 165 | + if ($request->has('parent')) { |
| 166 | + if ($parent = $request->input('parent')) { |
| 167 | + $category->parent()->associate($parent); |
| 168 | + } else { |
| 169 | + $category->makeRoot(); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + if ($request->has('icon')) { |
| 174 | + $category->icon = $request->input('icon'); |
| 175 | + } |
| 176 | + |
| 177 | + if ($request->has('color')) { |
| 178 | + $category->color = $request->input('color'); |
| 179 | + } |
| 180 | + |
| 181 | + $category->hide_translation = $request->input('hide_translation', false); |
| 182 | + |
| 183 | + $category->save(); |
| 184 | + |
| 185 | + $category->updateSingleAttachment('featured_image', $request); |
| 186 | + |
| 187 | + $this->flashSuccessMessage(); |
| 188 | + |
| 189 | + return redirect()->action([CategoriesController::class, 'edit'], [$locale, $type, $category]); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Remove the specified resource from storage. |
| 194 | + */ |
| 195 | + public function destroy($locale, CategoryType $type, Category $category, Request $request) |
| 196 | + { |
| 197 | + $this->authorize('delete', $category); |
| 198 | + |
| 199 | + if (! $category->delete()) { |
| 200 | + if ($request->expectsJson()) { |
| 201 | + return response()->json(false, 500); |
| 202 | + } |
| 203 | + |
| 204 | + abort(500); |
| 205 | + } |
| 206 | + |
| 207 | + if ($request->expectsJson()) { |
| 208 | + return response()->json(true); |
| 209 | + } |
| 210 | + |
| 211 | + return redirect()->action([CategoriesController::class, 'index'], [$locale, $type]); |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Perform bulk action on the resource |
| 216 | + */ |
| 217 | + public function bulk($locale, CategoryType $type, Request $request) |
| 218 | + { |
| 219 | + $this->authorize('viewAny', $type); |
| 220 | + |
| 221 | + $this->validate($request, [ |
| 222 | + 'action' => 'required|in:delete', |
| 223 | + 'categories' => 'required|array', |
| 224 | + 'categories.*' => 'exists:categories,id,type_id,' . $type->id, |
| 225 | + ]); |
| 226 | + |
| 227 | + $action = $request->input('action'); |
| 228 | + $ids = $request->input('categories', []); |
| 229 | + |
| 230 | + switch ($action) { |
| 231 | + case 'delete': |
| 232 | + // make sure allowed to delete |
| 233 | + $this->authorize('delete', $type); |
| 234 | + |
| 235 | + $type->categories()->whereIn('id', $ids) |
| 236 | + ->get() |
| 237 | + ->each(function (Category $category) { |
| 238 | + $category->delete(); |
| 239 | + }); |
| 240 | + break; |
| 241 | + } |
| 242 | + |
| 243 | + $this->flashSuccessMessage(); |
| 244 | + |
| 245 | + return $this->redirect($request, action([CategoriesController::class, 'index'], [$locale, $type])); |
| 246 | + } |
| 247 | +} |
0 commit comments