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