2017-05-22 21:32:33 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Models\Department;
|
2017-05-23 02:47:25 -07:00
|
|
|
use App\Helpers\Helper;
|
|
|
|
use Auth;
|
2017-10-25 22:35:58 -07:00
|
|
|
use Image;
|
|
|
|
use App\Http\Requests\ImageUploadRequest;
|
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]
|
2017-05-22 21:32:33 -07:00
|
|
|
* @return View
|
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
|
|
|
$this->authorize('index', Department::class);
|
2017-10-15 23:32:40 -07:00
|
|
|
$company = null;
|
2017-05-22 21:32:33 -07:00
|
|
|
if ($request->has('company_id')) {
|
|
|
|
$company = Company::find($request->input('company_id'));
|
|
|
|
}
|
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]
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2017-10-25 22:35:58 -07:00
|
|
|
public function store(ImageUploadRequest $request)
|
2017-05-22 21:32:33 -07:00
|
|
|
{
|
|
|
|
$this->authorize('create', Department::class);
|
|
|
|
$department = new Department;
|
|
|
|
$department->fill($request->all());
|
2017-05-23 02:47:25 -07:00
|
|
|
$department->user_id = Auth::user()->id;
|
|
|
|
$department->manager_id = ($request->has('manager_id' ) ? $request->input('manager_id') : null);
|
|
|
|
|
2017-10-25 22:35:58 -07:00
|
|
|
if ($request->file('image')) {
|
|
|
|
$image = $request->file('image');
|
|
|
|
$file_name = str_random(25).".".$image->getClientOriginalExtension();
|
|
|
|
$path = public_path('uploads/departments/'.$file_name);
|
|
|
|
Image::make($image->getRealPath())->resize(200, null, function ($constraint) {
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
$constraint->upsize();
|
|
|
|
})->save($path);
|
|
|
|
$department->image = $file_name;
|
|
|
|
}
|
|
|
|
|
2017-05-22 21:32:33 -07:00
|
|
|
if ($department->save()) {
|
2017-05-23 01:09:13 -07:00
|
|
|
return redirect()->route("departments.index")->with('success', trans('admin/departments/message.create.success'));
|
2017-05-22 21:32:33 -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]
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
$department = Department::find($id);
|
|
|
|
|
|
|
|
if (isset($department->id)) {
|
|
|
|
return view('departments/view', compact('department'));
|
|
|
|
}
|
|
|
|
return redirect()->route('departments.index')->with('error', trans('admin/departments/message.does_not_exist', compact('id')));
|
|
|
|
}
|
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]
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
2017-07-29 16:42:17 -07:00
|
|
|
return view('departments/edit')->with('item', new Department)
|
|
|
|
->with('manager_list', Helper::managerList())
|
|
|
|
->with('location_list', Helper::locationsList())
|
|
|
|
->with('company_list', Helper::companyList());
|
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]
|
|
|
|
* @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] [<snipe@snipe.net>]
|
|
|
|
* @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'));
|
|
|
|
}
|
2017-07-29 16:42:17 -07:00
|
|
|
return view('departments/edit', compact('item'))
|
|
|
|
->with('manager_list', Helper::managerList())
|
|
|
|
->with('location_list', Helper::locationsList())
|
|
|
|
->with('company_list', Helper::companyList());
|
2017-05-23 02:47:25 -07:00
|
|
|
}
|
2017-10-15 23:32:40 -07:00
|
|
|
|
2017-05-23 02:47:25 -07:00
|
|
|
public function update(Request $request, $id) {
|
|
|
|
|
|
|
|
$this->authorize('create', Department::class);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
$department->fill($request->all());
|
2017-10-25 22:35:58 -07:00
|
|
|
|
|
|
|
if ($request->file('image')) {
|
|
|
|
$image = $request->file('image');
|
|
|
|
$file_name = str_random(25).".".$image->getClientOriginalExtension();
|
|
|
|
$path = public_path('uploads/departments/'.$file_name);
|
|
|
|
Image::make($image->getRealPath())->resize(200, null, function ($constraint) {
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
$constraint->upsize();
|
|
|
|
})->save($path);
|
|
|
|
$department->image = $file_name;
|
|
|
|
} elseif ($request->input('image_delete')=='1') {
|
|
|
|
$department->image = null;
|
|
|
|
}
|
|
|
|
|
2017-05-23 02:47:25 -07:00
|
|
|
$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());
|
|
|
|
}
|
2017-05-22 21:32:33 -07:00
|
|
|
}
|