From 7ec1724308b8cb6bdc8e2f1ad1f7f616e8298e59 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 23 May 2017 02:47:25 -0700 Subject: [PATCH] Create, edit and destroy for depts --- .../Controllers/DepartmentsController.php | 83 ++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/DepartmentsController.php b/app/Http/Controllers/DepartmentsController.php index 15648d82d3..c4993be939 100644 --- a/app/Http/Controllers/DepartmentsController.php +++ b/app/Http/Controllers/DepartmentsController.php @@ -4,6 +4,8 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Department; +use App\Helpers\Helper; +use Auth; class DepartmentsController extends Controller { @@ -19,7 +21,7 @@ class DepartmentsController extends Controller * * @author [A. Gianotto] [] * @see AssetController::getDatatable() method that generates the JSON response - * @since [v1.0] + * @since [v4.0] * @return View */ public function index(Request $request) @@ -48,6 +50,9 @@ class DepartmentsController extends Controller $this->authorize('create', Department::class); $department = new Department; $department->fill($request->all()); + $department->user_id = Auth::user()->id; + $department->manager_id = ($request->has('manager_id' ) ? $request->input('manager_id') : null); + if ($department->save()) { return redirect()->route("departments.index")->with('success', trans('admin/departments/message.create.success')); @@ -74,4 +79,80 @@ class DepartmentsController extends Controller } return redirect()->route('departments.index')->with('error', trans('admin/departments/message.does_not_exist', compact('id'))); } + + + /** + * Returns a form view used to create a new department. + * + * @author [A. Gianotto] [] + * @see DepartmentsController::postCreate() method that validates and stores the data + * @since [v4.0] + * @return \Illuminate\Contracts\View\View + */ + public function create() + { + return view('departments/edit')->with('item', new Department)->with('manager_list', Helper::managerList())->with('location_list', Helper::locationsList()); + } + + + /** + * Validates and deletes selected department. + * + * @author [A. Gianotto] [] + * @param int $locationId + * @since [v4.0] + * @return \Illuminate\Http\RedirectResponse + */ + public function destroy($id) + { + if (is_null($department = Department::find($id))) { + return redirect()->to(route('departments.index'))->with('error', trans('admin/departments/message.not_found')); + } + + if ($department->users->count() > 0) { + return redirect()->to(route('departments.index'))->with('error', trans('admin/departments/message.assoc_users')); + } + + $department->delete(); + return redirect()->back()->with('success', trans('admin/departments/message.delete.success')); + + } + + /** + * Makes a form view to edit location information. + * + * @author [A. Gianotto] [] + * @see LocationsController::postCreate() method that validates and stores + * @param int $locationId + * @since [v1.0] + * @return \Illuminate\Contracts\View\View + */ + public function edit($id = null) + { + if (is_null($item = Department::find($id))) { + return redirect()->back()->with('error', trans('admin/locations/message.does_not_exist')); + } + return view('departments/edit', compact('item'))->with('manager_list', Helper::managerList())->with('location_list', Helper::locationsList()); + } + + + public function update(Request $request, $id) { + + $this->authorize('create', Department::class); + if (is_null($department = Department::find($id))) { + return redirect()->to('admin/settings/departments')->with('error', trans('admin/departments/message.does_not_exist')); + } + + + $department->fill($request->all()); + $department->manager_id = ($request->has('manager_id' ) ? $request->input('manager_id') : null); + + if ($department->save()) { + return redirect()->route("departments.index")->with('success', trans('admin/departments/message.update.success')); + } + + return redirect()->back()->withInput()->withErrors($department->getErrors()); + } + + }