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