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