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