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