] */ class CategoriesController extends Controller { /** * Returns a view that invokes the ajax tables which actually contains * the content for the categories listing, which is generated in getDatatable. * * @author [A. Gianotto] [] * @see CategoriesController::getDatatable() method that generates the JSON response * @since [v1.0] * @return \Illuminate\Contracts\View\View */ public function index() { // Show the page $this->authorize('view', Category::class); return view('categories/index'); } /** * Returns a form view to create a new category. * * @author [A. Gianotto] [] * @see CategoriesController::store() method that stores the data * @since [v1.0] * @return \Illuminate\Contracts\View\View */ public function create() { // Show the page $this->authorize('create', Category::class); $category_types= Helper::categoryTypeList(); return view('categories/edit')->with('item', new Category) ->with('category_types', $category_types); } /** * Validates and stores the new category data. * * @author [A. Gianotto] [] * @see CategoriesController::create() method that makes the form. * @since [v1.0] * @return \Illuminate\Http\RedirectResponse */ public function store(ImageUploadRequest $request) { $this->authorize('create', Category::class); $category = new Category(); $category->name = $request->input('name'); $category->category_type = $request->input('category_type'); $category->eula_text = $request->input('eula_text'); $category->use_default_eula = $request->input('use_default_eula', '0'); $category->require_acceptance = $request->input('require_acceptance', '0'); $category->checkin_email = $request->input('checkin_email', '0'); $category->user_id = Auth::id(); if ($request->file('image')) { $image = $request->file('image'); $file_name = str_random(25).".".$image->getClientOriginalExtension(); $path = public_path('uploads/categories/'.$file_name); Image::make($image->getRealPath())->resize(200, null, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->save($path); $category->image = $file_name; } if ($category->save()) { return redirect()->route('categories.index')->with('success', trans('admin/categories/message.create.success')); } return redirect()->back()->withInput()->withErrors($category->getErrors()); } /** * Returns a view that makes a form to update a category. * * @author [A. Gianotto] [] * @see CategoriesController::postEdit() method saves the data * @param int $categoryId * @since [v1.0] * @return \Illuminate\Contracts\View\View */ public function edit($categoryId = null) { $this->authorize('edit', Category::class); if (is_null($item = Category::find($categoryId))) { return redirect()->route('categories.index')->with('error', trans('admin/categories/message.does_not_exist')); } $category_types= Helper::categoryTypeList(); return view('categories/edit', compact('item')) ->with('category_types', $category_types); } /** * Validates and stores the updated category data. * * @author [A. Gianotto] [] * @see CategoriesController::getEdit() method that makes the form. * @param Request $request * @param int $categoryId * @return \Illuminate\Http\RedirectResponse * @since [v1.0] */ public function update(ImageUploadRequest $request, $categoryId = null) { $this->authorize('edit', Category::class); if (is_null($category = Category::find($categoryId))) { // Redirect to the categories management page return redirect()->to('admin/categories')->with('error', trans('admin/categories/message.does_not_exist')); } // Update the category data $category->name = $request->input('name'); // If the item count is > 0, we disable the category type in the edit. Disabled items // don't POST, so if the category_type is blank we just set it to the default. $category->category_type = $request->input('category_type', $category->category_type); $category->eula_text = $request->input('eula_text'); $category->use_default_eula = $request->input('use_default_eula', '0'); $category->require_acceptance = $request->input('require_acceptance', '0'); $category->checkin_email = $request->input('checkin_email', '0'); $old_image = $category->image; // Set the model's image property to null if the image is being deleted if ($request->input('image_delete') == 1) { $category->image = null; } if ($request->file('image')) { $image = $request->file('image'); $file_name = $category->id.'-'.str_slug($image->getClientOriginalName()) . "." . $image->getClientOriginalExtension(); if ($image->getClientOriginalExtension()!='svg') { Image::make($image->getRealPath())->resize(500, null, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->save(app('categories_upload_path').$file_name); } else { $image->move(app('categories_upload_path'), $file_name); } $category->image = $file_name; } if ((($request->file('image')) && (isset($old_image)) && ($old_image!='')) || ($request->input('image_delete') == 1)) { try { unlink(app('categories_upload_path').$old_image); } catch (\Exception $e) { \Log::error($e); } } if ($category->save()) { // Redirect to the new category page return redirect()->route('categories.index')->with('success', trans('admin/categories/message.update.success')); } // The given data did not pass validation return redirect()->back()->withInput()->withErrors($category->getErrors()); } /** * Validates and marks a category as deleted. * * @author [A. Gianotto] [] * @since [v1.0] * @param int $categoryId * @return \Illuminate\Http\RedirectResponse */ public function destroy($categoryId) { $this->authorize('delete', Category::class); // Check if the category exists if (is_null($category = Category::find($categoryId))) { return redirect()->route('categories.index')->with('error', trans('admin/categories/message.not_found')); } if ($category->has_models() > 0) { return redirect()->route('categories.index')->with('error', trans('admin/categories/message.assoc_items', ['asset_type'=>'model'])); } elseif ($category->accessories()->count() > 0) { return redirect()->route('categories.index')->with('error', trans('admin/categories/message.assoc_items', ['asset_type'=>'accessory'])); } elseif ($category->consumables()->count() > 0) { return redirect()->route('categories.index')->with('error', trans('admin/categories/message.assoc_items', ['asset_type'=>'consumable'])); } elseif ($category->components()->count() > 0) { return redirect()->route('categories.index')->with('error', trans('admin/categories/message.assoc_items', ['asset_type'=>'component'])); } $category->delete(); // Redirect to the locations management page return redirect()->route('categories.index')->with('success', trans('admin/categories/message.delete.success')); } /** * Returns a view that invokes the ajax tables which actually contains * the content for the categories detail view, which is generated in getDataView. * * @author [A. Gianotto] [] * @see CategoriesController::getDataView() method that generates the JSON response * @param int $categoryId * @since [v1.8] * @return \Illuminate\Contracts\View\View */ public function show($id) { $this->authorize('view', Category::class); if ($category = Category::find($id)) { if ($category->category_type=='asset') { $category_type = 'hardware'; $category_type_route = 'assets'; } elseif ($category->category_type=='accessory') { $category_type = 'accessories'; $category_type_route = 'accessories'; } else { $category_type = $category->category_type; $category_type_route = $category->category_type.'s'; } return view('categories/view', compact('category')) ->with('category_type',$category_type) ->with('category_type_route',$category_type_route); } // Prepare the error message $error = trans('admin/categories/message.does_not_exist', compact('id')); // Redirect to the user management page return redirect()->route('categories.index')->with('error', $error); } }