2017-05-22 21:32:33 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2017-10-25 22:35:58 -07:00
|
|
|
use App\Http\Requests\ImageUploadRequest;
|
2017-05-22 21:32:33 -07:00
|
|
|
use App\Models\Department;
|
2024-07-04 12:49:22 -07:00
|
|
|
use App\Models\Company;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Illuminate\Http\Request;
|
2024-07-04 14:37:58 -07:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use \Illuminate\Contracts\View\View;
|
2018-09-29 21:33:52 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2024-05-29 04:38:15 -07:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2017-05-22 21:32:33 -07:00
|
|
|
|
|
|
|
class DepartmentsController extends Controller
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
|
|
|
* the content for the assets listing, which is generated in getDatatable.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see AssetController::getDatatable() method that generates the JSON response
|
2017-05-23 02:47:25 -07:00
|
|
|
* @since [v4.0]
|
2018-07-24 19:35:26 -07:00
|
|
|
* @param Request $request
|
2017-05-22 21:32:33 -07:00
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function index(Request $request) : View
|
2017-05-22 21:32:33 -07:00
|
|
|
{
|
|
|
|
$this->authorize('index', Department::class);
|
2017-10-15 23:32:40 -07:00
|
|
|
$company = null;
|
2019-05-23 17:39:50 -07:00
|
|
|
if ($request->filled('company_id')) {
|
2017-05-22 21:32:33 -07:00
|
|
|
$company = Company::find($request->input('company_id'));
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-10-15 23:32:40 -07:00
|
|
|
return view('departments/index')->with('company', $company);
|
2017-05-22 21:32:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
2018-07-24 19:35:26 -07:00
|
|
|
* @param ImageUploadRequest $request
|
2017-05-22 21:32:33 -07:00
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function store(ImageUploadRequest $request) : RedirectResponse
|
2017-05-22 21:32:33 -07:00
|
|
|
{
|
|
|
|
$this->authorize('create', Department::class);
|
|
|
|
$department = new Department;
|
|
|
|
$department->fill($request->all());
|
2024-07-04 12:49:22 -07:00
|
|
|
$department->user_id = auth()->id();
|
2021-06-10 13:15:52 -07:00
|
|
|
$department->manager_id = ($request->filled('manager_id') ? $request->input('manager_id') : null);
|
2022-08-11 12:47:40 -07:00
|
|
|
$department->location_id = ($request->filled('location_id') ? $request->input('location_id') : null);
|
|
|
|
$department->company_id = ($request->filled('company_id') ? $request->input('company_id') : null);
|
2020-08-24 15:10:26 -07:00
|
|
|
$department = $request->handleImages($department);
|
2017-10-25 22:35:58 -07:00
|
|
|
|
2017-05-22 21:32:33 -07:00
|
|
|
if ($department->save()) {
|
2021-06-10 13:15:52 -07:00
|
|
|
return redirect()->route('departments.index')->with('success', trans('admin/departments/message.create.success'));
|
2017-05-22 21:32:33 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-05-23 01:09:13 -07:00
|
|
|
return redirect()->back()->withInput()->withErrors($department->getErrors());
|
2017-05-22 21:32:33 -07:00
|
|
|
}
|
2017-05-23 01:09:13 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
|
|
|
* the content for the department detail page.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $id
|
|
|
|
* @since [v4.0]
|
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function show($id) : View | RedirectResponse
|
2017-05-23 01:09:13 -07:00
|
|
|
{
|
|
|
|
$department = Department::find($id);
|
|
|
|
|
2018-07-12 18:28:02 -07:00
|
|
|
$this->authorize('view', $department);
|
|
|
|
|
2017-05-23 01:09:13 -07:00
|
|
|
if (isset($department->id)) {
|
|
|
|
return view('departments/view', compact('department'));
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2020-04-07 17:26:56 -07:00
|
|
|
return redirect()->route('departments.index')->with('error', trans('admin/departments/message.does_not_exist'));
|
2017-05-23 01:09:13 -07:00
|
|
|
}
|
2017-05-23 02:47:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a form view used to create a new department.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see DepartmentsController::postCreate() method that validates and stores the data
|
|
|
|
* @since [v4.0]
|
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function create() : View
|
2017-05-23 02:47:25 -07:00
|
|
|
{
|
2018-07-12 18:28:02 -07:00
|
|
|
$this->authorize('create', Department::class);
|
|
|
|
|
2017-10-28 11:17:52 -07:00
|
|
|
return view('departments/edit')->with('item', new Department);
|
2017-05-23 02:47:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates and deletes selected department.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $locationId
|
|
|
|
* @since [v4.0]
|
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function destroy($id) : RedirectResponse
|
2017-05-23 02:47:25 -07:00
|
|
|
{
|
|
|
|
if (is_null($department = Department::find($id))) {
|
|
|
|
return redirect()->to(route('departments.index'))->with('error', trans('admin/departments/message.not_found'));
|
|
|
|
}
|
|
|
|
|
2018-07-12 18:28:02 -07:00
|
|
|
$this->authorize('delete', $department);
|
|
|
|
|
2017-05-23 02:47:25 -07:00
|
|
|
if ($department->users->count() > 0) {
|
|
|
|
return redirect()->to(route('departments.index'))->with('error', trans('admin/departments/message.assoc_users'));
|
|
|
|
}
|
|
|
|
|
2018-09-29 21:33:52 -07:00
|
|
|
if ($department->image) {
|
2021-06-10 13:15:52 -07:00
|
|
|
try {
|
2018-09-29 21:33:52 -07:00
|
|
|
Storage::disk('public')->delete('departments'.'/'.$department->image);
|
|
|
|
} catch (\Exception $e) {
|
2024-05-29 04:38:15 -07:00
|
|
|
Log::debug($e);
|
2018-09-29 21:33:52 -07:00
|
|
|
}
|
|
|
|
}
|
2017-05-23 02:47:25 -07:00
|
|
|
$department->delete();
|
2018-09-29 21:33:52 -07:00
|
|
|
|
2017-05-23 02:47:25 -07:00
|
|
|
return redirect()->back()->with('success', trans('admin/departments/message.delete.success'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Makes a form view to edit Department information.
|
2017-05-23 02:47:25 -07:00
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see LocationsController::postCreate() method that validates and stores
|
2018-07-24 19:35:26 -07:00
|
|
|
* @param int $departmentId
|
2017-05-23 02:47:25 -07:00
|
|
|
* @since [v1.0]
|
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function edit($departmentId = null) : View | RedirectResponse
|
2017-05-23 02:47:25 -07:00
|
|
|
{
|
2018-07-24 19:35:26 -07:00
|
|
|
if (is_null($item = Department::find($departmentId))) {
|
2017-05-23 02:47:25 -07:00
|
|
|
return redirect()->back()->with('error', trans('admin/locations/message.does_not_exist'));
|
|
|
|
}
|
2018-07-12 18:28:02 -07:00
|
|
|
|
|
|
|
$this->authorize('update', $item);
|
|
|
|
|
2017-10-28 11:17:52 -07:00
|
|
|
return view('departments/edit', compact('item'));
|
2017-05-23 02:47:25 -07:00
|
|
|
}
|
2017-10-15 23:32:40 -07:00
|
|
|
|
2024-07-04 12:49:22 -07:00
|
|
|
/**
|
|
|
|
* Save updated Department information.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see LocationsController::postCreate() method that validates and stores
|
|
|
|
* @param int $departmentId
|
|
|
|
* @since [v1.0]
|
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function update(ImageUploadRequest $request, $id) : RedirectResponse
|
2021-06-10 13:15:52 -07:00
|
|
|
{
|
2017-05-23 02:47:25 -07:00
|
|
|
if (is_null($department = Department::find($id))) {
|
2017-10-15 23:32:40 -07:00
|
|
|
return redirect()->route('departments.index')->with('error', trans('admin/departments/message.does_not_exist'));
|
2017-05-23 02:47:25 -07:00
|
|
|
}
|
|
|
|
|
2018-07-12 18:28:02 -07:00
|
|
|
$this->authorize('update', $department);
|
|
|
|
|
2017-05-23 02:47:25 -07:00
|
|
|
$department->fill($request->all());
|
2021-06-10 13:15:52 -07:00
|
|
|
$department->manager_id = ($request->filled('manager_id') ? $request->input('manager_id') : null);
|
2022-08-11 12:47:40 -07:00
|
|
|
$department->location_id = ($request->filled('location_id') ? $request->input('location_id') : null);
|
|
|
|
$department->company_id = ($request->filled('company_id') ? $request->input('company_id') : null);
|
2023-07-10 11:44:21 -07:00
|
|
|
$department->phone = $request->input('phone');
|
|
|
|
$department->fax = $request->input('fax');
|
2017-11-07 11:28:13 -08:00
|
|
|
|
2020-08-24 15:10:26 -07:00
|
|
|
$department = $request->handleImages($department);
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-05-23 02:47:25 -07:00
|
|
|
if ($department->save()) {
|
2021-06-10 13:15:52 -07:00
|
|
|
return redirect()->route('departments.index')->with('success', trans('admin/departments/message.update.success'));
|
2017-05-23 02:47:25 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-05-23 02:47:25 -07:00
|
|
|
return redirect()->back()->withInput()->withErrors($department->getErrors());
|
|
|
|
}
|
2017-05-22 21:32:33 -07:00
|
|
|
}
|