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