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