164 lines
5.6 KiB
PHP
164 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\ManagementTools\Http\Controllers;
|
|
|
|
use App\Utils\ModuleUtil;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\ManagementTools\Entities\MtSkill;
|
|
use Modules\ManagementTools\Utils\ManagementToolsUtil;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class SkillController extends Controller
|
|
{
|
|
protected $moduleUtil;
|
|
|
|
protected $mtUtil;
|
|
|
|
public function __construct(ModuleUtil $moduleUtil, ManagementToolsUtil $mtUtil)
|
|
{
|
|
$this->moduleUtil = $moduleUtil;
|
|
$this->mtUtil = $mtUtil;
|
|
}
|
|
|
|
protected function authorizeAccess()
|
|
{
|
|
if (! auth()->user()->can('managementtools.access') || ! auth()->user()->can('managementtools.manage_skills')) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = request()->session()->get('user.business_id');
|
|
|
|
if (request()->ajax()) {
|
|
$skills = MtSkill::forBusiness($business_id)->with('parent')->withCount('children')->orderBy('title');
|
|
|
|
return DataTables::of($skills)
|
|
->addColumn('parent_name', function ($row) {
|
|
return $row->parent ? $row->parent->title : '—';
|
|
})
|
|
->addColumn('action', function ($row) {
|
|
$html = '<div class="btn-group">';
|
|
$html .= '<button type="button" data-href="'.action([self::class, 'edit'], [$row->id]).'" class="btn btn-xs btn-primary btn-modal" data-container=".mt_skill_modal"><i class="glyphicon glyphicon-edit"></i> '.__('managementtools::lang.edit').'</button>';
|
|
$html .= ' <button type="button" data-href="'.action([self::class, 'destroy'], [$row->id]).'" class="btn btn-xs btn-danger delete_mt_skill"><i class="glyphicon glyphicon-trash"></i></button>';
|
|
$html .= '</div>';
|
|
|
|
return $html;
|
|
})
|
|
->editColumn('title', function ($row) {
|
|
$indent = str_repeat(' ', (int) request()->get('depth', 0));
|
|
|
|
return $indent.e($row->title);
|
|
})
|
|
->rawColumns(['action', 'title'])
|
|
->make(true);
|
|
}
|
|
|
|
$parents = $this->mtUtil->getSkillsDropdown($business_id);
|
|
|
|
return view('managementtools::skills.index', compact('parents'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = request()->session()->get('user.business_id');
|
|
$parents = $this->mtUtil->getSkillsDropdown($business_id);
|
|
|
|
return view('managementtools::skills.create', compact('parents'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = $request->session()->get('user.business_id');
|
|
|
|
$request->validate([
|
|
'title' => 'required|string|max:150',
|
|
'description' => 'nullable|string|max:500',
|
|
'parent_id' => 'nullable|integer|exists:mt_skills,id',
|
|
]);
|
|
|
|
MtSkill::create([
|
|
'business_id' => $business_id,
|
|
'title' => $request->title,
|
|
'description' => $request->description,
|
|
'parent_id' => $request->parent_id ?: null,
|
|
]);
|
|
|
|
$output = ['success' => true, 'msg' => __('managementtools::lang.skill_added')];
|
|
|
|
return response()->json($output);
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = request()->session()->get('user.business_id');
|
|
$skill = MtSkill::forBusiness($business_id)->findOrFail($id);
|
|
$parents = $this->mtUtil->getSkillsDropdown($business_id, $skill->id);
|
|
|
|
return view('managementtools::skills.edit', compact('skill', 'parents'));
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = $request->session()->get('user.business_id');
|
|
$skill = MtSkill::forBusiness($business_id)->findOrFail($id);
|
|
|
|
$request->validate([
|
|
'title' => 'required|string|max:150',
|
|
'description' => 'nullable|string|max:500',
|
|
'parent_id' => 'nullable|integer|exists:mt_skills,id',
|
|
]);
|
|
|
|
if ($request->parent_id == $skill->id) {
|
|
return response()->json(['success' => false, 'msg' => __('managementtools::lang.something_went_wrong')]);
|
|
}
|
|
|
|
$skill->update([
|
|
'title' => $request->title,
|
|
'description' => $request->description,
|
|
'parent_id' => $request->parent_id ?: null,
|
|
]);
|
|
|
|
$output = ['success' => true, 'msg' => __('managementtools::lang.skill_updated')];
|
|
|
|
return response()->json($output);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = request()->session()->get('user.business_id');
|
|
$skill = MtSkill::forBusiness($business_id)->withCount('children')->findOrFail($id);
|
|
|
|
if ($skill->children_count > 0) {
|
|
return response()->json(['success' => false, 'msg' => __('managementtools::lang.cannot_delete_has_children')]);
|
|
}
|
|
|
|
$skill->delete();
|
|
|
|
return response()->json(['success' => true, 'msg' => __('managementtools::lang.skill_deleted')]);
|
|
}
|
|
|
|
/**
|
|
* Report: how many employees have each skill.
|
|
*/
|
|
public function report()
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = (int) request()->session()->get('user.business_id');
|
|
$report = $this->mtUtil->getSkillsEmployeeReport($business_id);
|
|
|
|
return view('managementtools::skills.report', [
|
|
'rows' => $report['rows'],
|
|
'summary' => $report,
|
|
]);
|
|
}
|
|
}
|