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