repository = $category; $this->repository ->pushCriteria(\Litepie\Repository\Criteria\RequestCriteria::class) ->pushCriteria(\Classifieds\Category\Repositories\Criteria\CategoryResourceCriteria::class); } /** * Display a list of category. * * @return Response */ public function index(CategoryRequest $request) { $view = $this->response->theme->listView(); if ($this->response->typeIs('json')) { $function = camel_case('get-' . $view); return $this->repository ->setPresenter(\Classifieds\Category\Repositories\Presenter\CategoryPresenter::class) ->$function(); } $categories = $this->repository->paginate(); return $this->response->setMetaTitle(trans('category::category.names')) ->view('category::category.index', true) ->data(compact('categories', 'view')) ->output(); } /** * Display category. * * @param Request $request * @param Model $category * * @return Response */ public function show(CategoryRequest $request, Category $category) { if ($category->exists) { $view = 'category::category.show'; } else { $view = 'category::category.new'; } return $this->response->setMetaTitle(trans('app.view') . ' ' . trans('category::category.name')) ->data(compact('category')) ->view($view, true) ->output(); } /** * Show the form for creating a new category. * * @param Request $request * * @return Response */ public function create(CategoryRequest $request) { $category = $this->repository->newInstance([]); return $this->response->setMetaTitle(trans('app.new') . ' ' . trans('category::category.name')) ->view('category::category.create', true) ->data(compact('category')) ->output(); } /** * Create new category. * * @param Request $request * * @return Response */ public function store(CategoryRequest $request) { try { $attributes = $request->all(); $attributes['user_id'] = user_id(); $attributes['user_type'] = user_type(); $category = $this->repository->create($attributes); return $this->response->message(trans('messages.success.created', ['Module' => trans('category::category.name')])) ->code(204) ->status('success') ->url(guard_url('category/category/' . $category->getRouteKey())) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('/category/category')) ->redirect(); } } /** * Show category for editing. * * @param Request $request * @param Model $category * * @return Response */ public function edit(CategoryRequest $request, Category $category) { return $this->response->setMetaTitle(trans('app.edit') . ' ' . trans('category::category.name')) ->view('category::category.edit', true) ->data(compact('category')) ->output(); } /** * Update the category. * * @param Request $request * @param Model $category * * @return Response */ public function update(CategoryRequest $request, Category $category) { try { $attributes = $request->all(); $category->update($attributes); return $this->response->message(trans('messages.success.updated', ['Module' => trans('category::category.name')])) ->code(204) ->status('success') ->url(guard_url('category/category/' . $category->getRouteKey())) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('category/category/' . $category->getRouteKey())) ->redirect(); } } /** * Remove the category. * * @param Model $category * * @return Response */ public function destroy(CategoryRequest $request, Category $category) { try { $category->delete(); return $this->response->message(trans('messages.success.deleted', ['Module' => trans('category::category.name')])) ->code(202) ->status('success') ->url(guard_url('category/category/0')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('category/category/' . $category->getRouteKey())) ->redirect(); } } /** * Remove multiple category. * * @param Model $category * * @return Response */ public function delete(CategoryRequest $request, $type) { try { $ids = hashids_decode($request->input('ids')); if ($type == 'purge') { $this->repository->purge($ids); } else { $this->repository->delete($ids); } return $this->response->message(trans('messages.success.deleted', ['Module' => trans('category::category.name')])) ->status("success") ->code(202) ->url(guard_url('category/category')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->status("error") ->code(400) ->url(guard_url('/category/category')) ->redirect(); } } /** * Restore deleted categories. * * @param Model $category * * @return Response */ public function restore(CategoryRequest $request) { try { $ids = hashids_decode($request->input('ids')); $this->repository->restore($ids); return $this->response->message(trans('messages.success.restore', ['Module' => trans('category::category.name')])) ->status("success") ->code(202) ->url(guard_url('/category/category')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->status("error") ->code(400) ->url(guard_url('/category/category/')) ->redirect(); } } }