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