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