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