repository = $article; $this->repository ->pushCriteria(\Litepie\Repository\Criteria\RequestCriteria::class) ->pushCriteria(\Article\Blog\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(\Article\Blog\Repositories\Presenter\ArticlePresenter::class) ->$function(); } $articles = $this->repository->paginate(); return $this->response->setMetaTitle(trans('blog::article.names')) ->view('blog::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 = 'blog::article.show'; } else { $view = 'blog::article.new'; } return $this->response->setMetaTitle(trans('app.view') . ' ' . trans('blog::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('blog::article.name')) ->view('blog::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('blog::article.name')])) ->code(204) ->status('success') ->url(guard_url('blog/article/' . $article->getRouteKey())) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('/blog/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('blog::article.name')) ->view('blog::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('blog::article.name')])) ->code(204) ->status('success') ->url(guard_url('blog/article/' . $article->getRouteKey())) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('blog/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('blog::article.name')])) ->code(202) ->status('success') ->url(guard_url('blog/article/0')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('blog/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('blog::article.name')])) ->status("success") ->code(202) ->url(guard_url('blog/article')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->status("error") ->code(400) ->url(guard_url('/blog/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('blog::article.name')])) ->status("success") ->code(202) ->url(guard_url('/blog/article')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->status("error") ->code(400) ->url(guard_url('/blog/article/')) ->redirect(); } } }