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