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