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