|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Backend\Admin; |
| 4 | + |
| 5 | +use App\Helper\ResponseHelper; |
| 6 | +use App\Models\Category; |
| 7 | +use Illuminate\Http\Request; |
| 8 | +use App\Http\Controllers\Controller; |
| 9 | +use App\Http\Requests\StoreCategory; |
| 10 | +use App\Http\Requests\UpdateCategory; |
| 11 | +use Illuminate\Support\Str; |
| 12 | +use Yajra\DataTables\DataTables; |
| 13 | + |
| 14 | +class CategoriesController extends Controller |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Display a listing of the resource. |
| 18 | + * |
| 19 | + * @return \Illuminate\Http\Response |
| 20 | + */ |
| 21 | + public function index(Request $request) |
| 22 | + { |
| 23 | + if($request->ajax()) { |
| 24 | + $categories = Category::anyTrash($request->trash)->get(); |
| 25 | + return DataTables::of($categories) |
| 26 | + ->addColumn('name_my', function($category) { |
| 27 | + $name_str = json_decode($category->name); |
| 28 | + return $name_str->my; |
| 29 | + }) |
| 30 | + ->addColumn('name_en', function ($category) { |
| 31 | + $name_str = json_decode($category->name); |
| 32 | + return $name_str->en; |
| 33 | + }) |
| 34 | + ->editColumn('parent_id', function($category) { |
| 35 | + if($category->parent_id) { |
| 36 | + $encoded = json_decode($category->parent->name); |
| 37 | + return $encoded->my . ' (' . $encoded->en . ')'; |
| 38 | + } |
| 39 | + return '-'; |
| 40 | + }) |
| 41 | + ->addColumn('plus-icon', function() { |
| 42 | + return null; |
| 43 | + }) |
| 44 | + ->addColumn('action', function ($category) use ($request) { |
| 45 | + $detail_btn = ''; |
| 46 | + $restore_btn = ''; |
| 47 | + $edit_btn = '<a class="edit text text-primary mr-2" href="' . route('admin.categories.edit', ['category' => $category->id]) . '"><i class="far fa-edit fa-lg"></i></a>'; |
| 48 | + |
| 49 | + if ($request->trash == 1) { |
| 50 | + $restore_btn = '<a class="restore text text-warning mr-2" href="#" data-id="' . $category->id . '"><i class="fa fa-trash-restore fa-lg"></i></a>'; |
| 51 | + $trash_or_delete_btn = '<a class="destroy text text-danger mr-2" href="#" data-id="' . $category->id . '"><i class="fa fa-minus-circle fa-lg"></i></a>'; |
| 52 | + } else { |
| 53 | + $trash_or_delete_btn = '<a class="trash text text-danger mr-2" href="#" data-id="' . $category->id . '"><i class="fas fa-trash fa-lg"></i></a>'; |
| 54 | + } |
| 55 | + |
| 56 | + return "${detail_btn} ${edit_btn} ${restore_btn} ${trash_or_delete_btn}"; |
| 57 | + }) |
| 58 | + ->rawColumns(['plus-icon', 'name_my', 'name_en', 'action']) |
| 59 | + ->make(true); |
| 60 | + } |
| 61 | + return view('backend.admin.categories.index'); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Show the form for creating a new resource. |
| 66 | + * |
| 67 | + * @return \Illuminate\Http\Response |
| 68 | + */ |
| 69 | + public function create() |
| 70 | + { |
| 71 | + $active_categories = Category::noTrash(); |
| 72 | + $main_categories = $active_categories->main()->get(); |
| 73 | + $suggest_rank = $active_categories->max('rank') + 1; |
| 74 | + return view('backend.admin.categories.create', compact('main_categories', 'suggest_rank')); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Store a newly created resource in storage. |
| 79 | + * |
| 80 | + * @param \Illuminate\Http\Request $request |
| 81 | + * @return \Illuminate\Http\Response |
| 82 | + */ |
| 83 | + public function store(StoreCategory $request) |
| 84 | + { |
| 85 | + $name_str = json_encode(['my' => $request->name_my, 'en' => $request->name_en]); |
| 86 | + $slug = Str::slug($request->slug ?? $request->name_en); |
| 87 | + $parent_id = $request->parent_id; |
| 88 | + $found = false; |
| 89 | + |
| 90 | + $check_slug = Category::where('slug', $slug)->first(); |
| 91 | + if($check_slug) { |
| 92 | + return back()->withErrors(['msg' => 'Link is already exist.'])->withInput(); |
| 93 | + } |
| 94 | + |
| 95 | + if(! empty($parent_id)) { |
| 96 | + $found = Category::find($parent_id); |
| 97 | + } |
| 98 | + |
| 99 | + Category::create([ |
| 100 | + 'name' => $name_str, |
| 101 | + 'slug' => $slug, |
| 102 | + 'rank' => abs($request->rank) ?? (Category::maxRank() + 1), |
| 103 | + 'parent_id' => ($found) ? $parent_id : 0 |
| 104 | + ]); |
| 105 | + |
| 106 | + return redirect()->route('admin.categories.index')->with('success', 'New Category Successfully Created.'); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Display the specified resource. |
| 111 | + * |
| 112 | + * @param int $id |
| 113 | + * @return \Illuminate\Http\Response |
| 114 | + */ |
| 115 | + public function show(Category $category) |
| 116 | + { |
| 117 | + return view('backend.admin.categories.show', compact('category')); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Show the form for editing the specified resource. |
| 122 | + * |
| 123 | + * @param int $id |
| 124 | + * @return \Illuminate\Http\Response |
| 125 | + */ |
| 126 | + public function edit(Category $category) |
| 127 | + { |
| 128 | + $main_categories = Category::noTrash()->main()->get(); |
| 129 | + return view('backend.admin.categories.edit', compact('category', 'main_categories')); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Update the specified resource in storage. |
| 134 | + * |
| 135 | + * @param \Illuminate\Http\Request $request |
| 136 | + * @param int $id |
| 137 | + * @return \Illuminate\Http\Response |
| 138 | + */ |
| 139 | + public function update(UpdateCategory $request, Category $category) |
| 140 | + { |
| 141 | + $name_str = json_encode(['my' => $request->name_my, 'en' => $request->name_en]); |
| 142 | + $slug = Str::slug($request->slug ?? $request->name_en); |
| 143 | + $parent_id = $request->parent_id; |
| 144 | + $found = false; |
| 145 | + |
| 146 | + $check_slug = Category::whereNotIn('id', [$category->id])->where('slug', $slug)->first(); |
| 147 | + if ($check_slug) { |
| 148 | + return back()->withErrors(['msg' => 'Link is already exist.'])->withInput(); |
| 149 | + } |
| 150 | + |
| 151 | + if (!empty($parent_id)) { |
| 152 | + $found = Category::find($parent_id); |
| 153 | + } |
| 154 | + |
| 155 | + $category->update([ |
| 156 | + 'name' => $name_str, |
| 157 | + 'slug' => $slug, |
| 158 | + 'rank' => abs($request->rank) ?? (Category::maxRank() + 1), |
| 159 | + 'parent_id' => ($found) ? $parent_id : 0 |
| 160 | + ]); |
| 161 | + |
| 162 | + return redirect()->route('admin.categories.index')->with('success', 'Successfully Updated.'); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Remove the specified resource from storage. |
| 167 | + * |
| 168 | + * @param int $id |
| 169 | + * @return \Illuminate\Http\Response |
| 170 | + */ |
| 171 | + public function destroy(Category $category) |
| 172 | + { |
| 173 | + $category->delete(); |
| 174 | + return ResponseHelper::success(); |
| 175 | + } |
| 176 | + |
| 177 | + public function trash(Category $category) |
| 178 | + { |
| 179 | + $category->trash(); |
| 180 | + return ResponseHelper::success(); |
| 181 | + } |
| 182 | + |
| 183 | + public function restore(Category $category) |
| 184 | + { |
| 185 | + $category->restore(); |
| 186 | + return ResponseHelper::success(); |
| 187 | + } |
| 188 | +} |
0 commit comments