repository = $user; parent::__construct(); } /** * Display a list of user. * * @return Response */ public function index(UserRequest $request) { if ($request->wantsJson()) { return $this->getJson($request); } $this ->theme->prependTitle(trans('package::user.names').' :: '); return $this->theme->of('package::admin.user.index')->render(); } /** * Display a list of user. * * @return Response */ public function getJson(UserRequest $request) { $pageLimit = $request->input('pageLimit'); $users = $this->repository ->pushCriteria(app('Litepie\Repository\Criteria\RequestCriteria')) ->setPresenter('\\Provider\\Package\\Repositories\\Presenter\\UserListPresenter') ->scopeQuery(function($query){ return $query->orderBy('id','DESC'); })->paginate($pageLimit); $users['recordsTotal'] = $users['meta']['pagination']['total']; $users['recordsFiltered'] = $users['meta']['pagination']['total']; $users['request'] = $request->all(); return response()->json($users, 200); } /** * Display user. * * @param Request $request * @param Model $user * * @return Response */ public function show(UserRequest $request, User $user) { if (!$user->exists) { return response()->view('package::admin.user.new', compact('user')); } Form::populate($user); return response()->view('package::admin.user.show', compact('user')); } /** * Show the form for creating a new user. * * @param Request $request * * @return Response */ public function create(UserRequest $request) { $user = $this->repository->newInstance([]); Form::populate($user); return response()->view('package::admin.user.create', compact('user')); } /** * Create new user. * * @param Request $request * * @return Response */ public function store(UserRequest $request) { try { $attributes = $request->all(); $attributes['user_id'] = user_id('admin.web'); $user = $this->repository->create($attributes); return response()->json([ 'message' => trans('messages.success.updated', ['Module' => trans('package::user.name')]), 'code' => 204, 'redirect' => trans_url('/admin/package/user/'.$user->getRouteKey()) ], 201); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 400, ], 400); } } /** * Show user for editing. * * @param Request $request * @param Model $user * * @return Response */ public function edit(UserRequest $request, User $user) { Form::populate($user); return response()->view('package::admin.user.edit', compact('user')); } /** * Update the user. * * @param Request $request * @param Model $user * * @return Response */ public function update(UserRequest $request, User $user) { try { $attributes = $request->all(); $user->update($attributes); return response()->json([ 'message' => trans('messages.success.updated', ['Module' => trans('package::user.name')]), 'code' => 204, 'redirect' => trans_url('/admin/package/user/'.$user->getRouteKey()) ], 201); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 400, 'redirect' => trans_url('/admin/package/user/'.$user->getRouteKey()), ], 400); } } /** * Remove the user. * * @param Model $user * * @return Response */ public function destroy(UserRequest $request, User $user) { try { $t = $user->delete(); return response()->json([ 'message' => trans('messages.success.deleted', ['Module' => trans('package::user.name')]), 'code' => 202, 'redirect' => trans_url('/admin/package/user/0'), ], 202); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 400, 'redirect' => trans_url('/admin/package/user/'.$user->getRouteKey()), ], 400); } } }