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