205 lines
7.1 KiB
PHP
205 lines
7.1 KiB
PHP
<?php
|
||
|
||
namespace Modules\ManagementTools\Utils;
|
||
|
||
use App\Utils\ModuleUtil;
|
||
use Illuminate\Support\Collection;
|
||
use Modules\ProfessionalProject\Entities\PpProject;
|
||
use Modules\ProfessionalProject\Entities\PpProjectCategory;
|
||
|
||
class ProfessionalProjectBridge
|
||
{
|
||
public function __construct(protected ModuleUtil $moduleUtil)
|
||
{
|
||
}
|
||
|
||
public function isAvailable(): bool
|
||
{
|
||
return \Module::has('ProfessionalProject')
|
||
&& \Module::isEnabled('ProfessionalProject')
|
||
&& class_exists(PpProject::class);
|
||
}
|
||
|
||
public function isSubscribed(int $business_id): bool
|
||
{
|
||
if (! $this->isAvailable()) {
|
||
return false;
|
||
}
|
||
|
||
return auth()->user()->can('superadmin')
|
||
|| $this->moduleUtil->hasThePermissionInSubscription($business_id, 'professional_project_module');
|
||
}
|
||
|
||
public function getProjectsForUser(int $business_id, int $user_id, $category_id = null): array
|
||
{
|
||
if (! $this->isAvailable()) {
|
||
return [];
|
||
}
|
||
|
||
return PpProject::projectDropdown($business_id, $user_id, $category_id)->toArray();
|
||
}
|
||
|
||
/**
|
||
* Projects grouped by category for optgroup dropdowns.
|
||
*
|
||
* @return array<string, array<int, string>>
|
||
*/
|
||
public function getProjectsGroupedByCategory(int $business_id, int $user_id): array
|
||
{
|
||
if (! $this->isAvailable()) {
|
||
return [];
|
||
}
|
||
|
||
$grouped = [];
|
||
$categories = PpProjectCategory::forBusiness($business_id)
|
||
->withCount(['projects' => function ($q) use ($business_id, $user_id) {
|
||
$q->where('business_id', $business_id)
|
||
->where('is_archived', false)
|
||
->where(function ($inner) use ($user_id) {
|
||
$inner->where('lead_id', $user_id)
|
||
->orWhereHas('members', fn ($m) => $m->where('user_id', $user_id));
|
||
});
|
||
}])
|
||
->orderBy('position')
|
||
->orderBy('name')
|
||
->get();
|
||
|
||
foreach ($categories as $category) {
|
||
if ($category->projects_count === 0) {
|
||
continue;
|
||
}
|
||
$projects = PpProject::projectDropdown($business_id, $user_id, $category->id);
|
||
if ($projects->isNotEmpty()) {
|
||
$label = PpProjectCategory::breadcrumb($business_id, $category->id)->pluck('name')->implode(' › ');
|
||
$grouped[$label ?: $category->name] = $projects->toArray();
|
||
}
|
||
}
|
||
|
||
$uncategorized = PpProject::projectDropdown($business_id, $user_id, 'uncategorized');
|
||
if ($uncategorized->isNotEmpty()) {
|
||
$grouped[__('professionalproject::lang.uncategorized_projects')] = $uncategorized->toArray();
|
||
}
|
||
|
||
return $grouped;
|
||
}
|
||
|
||
public function getCategoriesWithCounts(int $business_id, ?int $user_id = null, bool $is_admin = false): Collection
|
||
{
|
||
if (! $this->isAvailable()) {
|
||
return collect();
|
||
}
|
||
|
||
return PpProjectCategory::forBusiness($business_id)
|
||
->withCount(['children', 'projects' => function ($q) use ($business_id, $user_id, $is_admin) {
|
||
$q->where('business_id', $business_id)->where('is_archived', false);
|
||
if (! $is_admin && $user_id) {
|
||
$q->where(function ($inner) use ($user_id) {
|
||
$inner->where('lead_id', $user_id)
|
||
->orWhereHas('members', fn ($m) => $m->where('user_id', $user_id));
|
||
});
|
||
}
|
||
}])
|
||
->orderBy('position')
|
||
->orderBy('name')
|
||
->get();
|
||
}
|
||
|
||
public function getCategoryTreeWithCounts(int $business_id, ?int $user_id = null, bool $is_admin = false): Collection
|
||
{
|
||
$flat = $this->getCategoriesWithCounts($business_id, $user_id, $is_admin);
|
||
|
||
return PpProjectCategory::buildTree($flat);
|
||
}
|
||
|
||
public function getCategoryBrowseData(int $business_id, int $category_id, ?int $user_id = null, bool $is_admin = false, string $view = 'list'): array
|
||
{
|
||
$category = PpProjectCategory::forBusiness($business_id)->findOrFail($category_id);
|
||
$breadcrumb = PpProjectCategory::breadcrumb($business_id, $category_id);
|
||
$children = PpProjectCategory::forBusiness($business_id)
|
||
->where('parent_id', $category_id)
|
||
->withCount(['children', 'projects' => function ($q) use ($business_id, $user_id, $is_admin) {
|
||
$q->where('business_id', $business_id)->where('is_archived', false);
|
||
if (! $is_admin && $user_id) {
|
||
$q->where(function ($inner) use ($user_id) {
|
||
$inner->where('lead_id', $user_id)
|
||
->orWhereHas('members', fn ($m) => $m->where('user_id', $user_id));
|
||
});
|
||
}
|
||
}])
|
||
->orderBy('position')
|
||
->orderBy('name')
|
||
->get();
|
||
|
||
$direct_projects = $this->getProjectsInCategory($business_id, $category_id, $user_id, $is_admin);
|
||
|
||
return compact('category', 'breadcrumb', 'children', 'direct_projects', 'view');
|
||
}
|
||
|
||
public function categoryHasChildren(int $business_id, int $category_id): bool
|
||
{
|
||
return PpProjectCategory::forBusiness($business_id)
|
||
->where('parent_id', $category_id)
|
||
->exists();
|
||
}
|
||
|
||
public function getProjectsInCategory(int $business_id, $category_id, ?int $user_id = null, bool $is_admin = false)
|
||
{
|
||
if (! $this->isAvailable()) {
|
||
return collect();
|
||
}
|
||
|
||
$query = PpProject::with(['lead', 'members', 'categories'])
|
||
->where('business_id', $business_id)
|
||
->where('is_archived', false);
|
||
|
||
if (! $is_admin && $user_id) {
|
||
$query->where(function ($q) use ($user_id) {
|
||
$q->where('lead_id', $user_id)
|
||
->orWhereHas('members', fn ($m) => $m->where('user_id', $user_id));
|
||
});
|
||
}
|
||
|
||
if ($category_id === 'uncategorized') {
|
||
$query->uncategorized();
|
||
} elseif ($category_id !== 'all') {
|
||
$query->inCategory((int) $category_id);
|
||
}
|
||
|
||
return $query->orderBy('name')->get();
|
||
}
|
||
|
||
public function resolveProjectNames(array $ids): Collection
|
||
{
|
||
if (! $this->isAvailable() || empty($ids)) {
|
||
return collect();
|
||
}
|
||
|
||
return PpProject::whereIn('id', $ids)->pluck('name', 'id');
|
||
}
|
||
|
||
public function validateProjectForUser(int $business_id, int $user_id, int $project_id): bool
|
||
{
|
||
if (! $this->isAvailable()) {
|
||
return false;
|
||
}
|
||
|
||
return PpProject::where('business_id', $business_id)
|
||
->where('is_archived', false)
|
||
->where('id', $project_id)
|
||
->where(function ($q) use ($user_id) {
|
||
$q->where('lead_id', $user_id)
|
||
->orWhereHas('members', fn ($m) => $m->where('user_id', $user_id));
|
||
})
|
||
->exists();
|
||
}
|
||
|
||
public function categoriesDropdown(int $business_id): array
|
||
{
|
||
if (! $this->isAvailable()) {
|
||
return [];
|
||
}
|
||
|
||
return PpProjectCategory::treeDropdown($business_id);
|
||
}
|
||
}
|