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