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