220 lines
8.4 KiB
PHP
220 lines
8.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\ManagementTools\Http\Controllers;
|
|
|
|
use App\User;
|
|
use App\Utils\Util;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\ManagementTools\Utils\ProfessionalProjectBridge;
|
|
use Modules\ProfessionalProject\Entities\PpProjectCategory;
|
|
|
|
class ProjectCategoryController extends Controller
|
|
{
|
|
public function __construct(
|
|
protected ProfessionalProjectBridge $ppBridge,
|
|
protected Util $commonUtil
|
|
) {
|
|
}
|
|
|
|
protected function authorizeView(): void
|
|
{
|
|
if (! auth()->user()->can('managementtools.access')) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
if (! $this->ppBridge->isAvailable()) {
|
|
abort(404, __('managementtools::lang.professional_project_required'));
|
|
}
|
|
}
|
|
|
|
protected function authorizeManage(): void
|
|
{
|
|
$this->authorizeView();
|
|
|
|
if (! auth()->user()->can('managementtools.manage_project_categories')
|
|
&& ! auth()->user()->can('managementtools.manage_employee_tasks')) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorizeView();
|
|
$business_id = (int) $request->session()->get('user.business_id');
|
|
$is_admin = $this->commonUtil->is_admin(auth()->user(), $business_id);
|
|
$user_id = auth()->id();
|
|
|
|
$categories = $this->ppBridge->getCategoryTreeWithCounts($business_id, $user_id, $is_admin);
|
|
$active_category = $request->query('category', 'all');
|
|
$can_manage = auth()->user()->can('managementtools.manage_project_categories')
|
|
|| auth()->user()->can('managementtools.manage_employee_tasks');
|
|
|
|
return view('managementtools::project_categories.index', compact(
|
|
'categories',
|
|
'active_category',
|
|
'can_manage',
|
|
'is_admin'
|
|
));
|
|
}
|
|
|
|
public function projects(Request $request, $category_id)
|
|
{
|
|
$this->authorizeView();
|
|
$business_id = (int) $request->session()->get('user.business_id');
|
|
$is_admin = $this->commonUtil->is_admin(auth()->user(), $business_id);
|
|
$user_id = auth()->id();
|
|
|
|
if ($category_id === 'all' || $category_id === 'uncategorized') {
|
|
$projects = $this->ppBridge->getProjectsInCategory($business_id, $category_id, $user_id, $is_admin);
|
|
$html = view('managementtools::project_categories.partials.projects_list', [
|
|
'projects' => $projects,
|
|
'category' => null,
|
|
'category_id' => $category_id,
|
|
])->render();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'mode' => 'projects',
|
|
'html' => $html,
|
|
'count' => $projects->count(),
|
|
]);
|
|
}
|
|
|
|
$category = PpProjectCategory::forBusiness($business_id)->findOrFail($category_id);
|
|
|
|
if ($this->ppBridge->categoryHasChildren($business_id, (int) $category_id)) {
|
|
$browse = $this->ppBridge->getCategoryBrowseData($business_id, (int) $category_id, $user_id, $is_admin);
|
|
$browse['link_mode'] = 'mt';
|
|
$browse['root_url'] = action([self::class, 'index']);
|
|
$html = view('professionalproject::category.partials.subcategories_browser', $browse)->render();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'mode' => 'subcategories',
|
|
'html' => $html,
|
|
'count' => $browse['children']->count(),
|
|
]);
|
|
}
|
|
|
|
$projects = $this->ppBridge->getProjectsInCategory($business_id, $category_id, $user_id, $is_admin);
|
|
$breadcrumb = PpProjectCategory::breadcrumb($business_id, (int) $category_id);
|
|
$html = view('managementtools::project_categories.partials.category_content', [
|
|
'projects' => $projects,
|
|
'category' => $category,
|
|
'category_id' => $category_id,
|
|
'breadcrumb' => $breadcrumb,
|
|
'root_url' => action([self::class, 'index']),
|
|
'link_mode' => 'mt',
|
|
])->render();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'mode' => 'projects',
|
|
'html' => $html,
|
|
'count' => $projects->count(),
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->authorizeManage();
|
|
$business_id = (int) request()->session()->get('user.business_id');
|
|
$users = User::forDropdown($business_id, false);
|
|
$parent_categories = PpProjectCategory::treeDropdown($business_id);
|
|
|
|
return view('managementtools::project_categories.create', compact('users', 'parent_categories'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$this->authorizeManage();
|
|
$business_id = (int) $request->session()->get('user.business_id');
|
|
|
|
$request->validate([
|
|
'name' => 'required|string|max:150',
|
|
'description' => 'nullable|string|max:500',
|
|
'icon' => 'nullable|string|max:50',
|
|
'position' => 'nullable|integer|min:0',
|
|
'parent_id' => 'nullable|integer',
|
|
]);
|
|
|
|
$category = PpProjectCategory::create([
|
|
'business_id' => $business_id,
|
|
'parent_id' => $request->input('parent_id') ?: null,
|
|
'name' => $request->name,
|
|
'description' => $request->description,
|
|
'icon' => $request->icon,
|
|
'position' => (int) $request->input('position', 0),
|
|
'created_by' => auth()->id(),
|
|
]);
|
|
|
|
$category->members()->sync($request->input('member_id', []));
|
|
|
|
return response()->json(['success' => true, 'msg' => __('managementtools::lang.category_added')]);
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$this->authorizeManage();
|
|
$business_id = (int) request()->session()->get('user.business_id');
|
|
$category = PpProjectCategory::forBusiness($business_id)->with(['members', 'projects'])->findOrFail($id);
|
|
$users = User::forDropdown($business_id, false);
|
|
$all_projects = \Modules\ProfessionalProject\Entities\PpProject::where('business_id', $business_id)
|
|
->where('is_archived', false)
|
|
->orderBy('name')
|
|
->pluck('name', 'id');
|
|
$parent_categories = PpProjectCategory::treeDropdown($business_id, $category->id);
|
|
|
|
return view('managementtools::project_categories.edit', compact('category', 'users', 'all_projects', 'parent_categories'));
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$this->authorizeManage();
|
|
$business_id = (int) $request->session()->get('user.business_id');
|
|
$category = PpProjectCategory::forBusiness($business_id)->findOrFail($id);
|
|
|
|
$request->validate([
|
|
'name' => 'required|string|max:150',
|
|
'description' => 'nullable|string|max:500',
|
|
'icon' => 'nullable|string|max:50',
|
|
'position' => 'nullable|integer|min:0',
|
|
'parent_id' => 'nullable|integer',
|
|
'project_id' => 'nullable|array',
|
|
'project_id.*' => 'integer',
|
|
]);
|
|
|
|
if ($request->filled('parent_id') && (int) $request->parent_id === (int) $category->id) {
|
|
return response()->json(['success' => false, 'msg' => __('managementtools::lang.something_went_wrong')]);
|
|
}
|
|
|
|
if ($request->filled('parent_id') && in_array((int) $request->parent_id, PpProjectCategory::descendantIds($business_id, $category->id), true)) {
|
|
return response()->json(['success' => false, 'msg' => __('managementtools::lang.invalid_parent_category')]);
|
|
}
|
|
|
|
$category->update([
|
|
'name' => $request->name,
|
|
'description' => $request->description,
|
|
'icon' => $request->icon,
|
|
'position' => (int) $request->input('position', 0),
|
|
'parent_id' => $request->input('parent_id') ?: null,
|
|
]);
|
|
|
|
$category->members()->sync($request->input('member_id', []));
|
|
$category->projects()->sync($request->input('project_id', []));
|
|
|
|
return response()->json(['success' => true, 'msg' => __('managementtools::lang.category_updated')]);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$this->authorizeManage();
|
|
$business_id = (int) request()->session()->get('user.business_id');
|
|
$category = PpProjectCategory::forBusiness($business_id)->findOrFail($id);
|
|
$category->delete();
|
|
|
|
return response()->json(['success' => true, 'msg' => __('managementtools::lang.category_deleted')]);
|
|
}
|
|
}
|