ultimatepos/Modules/Portal/Support/PortalNavigationBuilder.php

195 lines
9.6 KiB
PHP

<?php
namespace Modules\Portal\Support;
use App\Business;
use App\Contact;
use Modules\Portal\Http\Controllers\Customer;
use Modules\Portal\Http\Controllers\Supplier;
use Modules\Portal\Http\Controllers\Marketer;
class PortalNavigationBuilder
{
public static function forPortalType(string $type): array
{
return match ($type) {
'supplier' => self::supplier(),
'marketer' => self::marketer(),
default => self::customer(),
};
}
public static function customer(): array
{
$business_id = auth()->user()->business_id;
Contact::where('business_id', $business_id)->findOrFail(auth()->user()->crm_contact_id);
$crm_settings = Business::where('id', $business_id)->value('crm_settings');
$crm_settings = ! empty($crm_settings) ? json_decode($crm_settings, true) : [];
$enabled_modules = session('business.enabled_modules', []);
$items = [
self::item(Customer\DashboardController::class, 'index', __('portal::lang.nav_home'), 'fa-home', 'dashboard'),
self::item(Customer\CommunicationController::class, 'index', __('portal::lang.communications'), 'fa-comments', 'communications'),
];
if (self::moduleEnabled($enabled_modules, ['project', 'Project', 'ProfessionalProject'])) {
$items[] = self::item(Customer\ProjectController::class, 'index', __('portal::lang.my_projects'), 'fa-project-diagram', 'projects');
}
if (self::moduleEnabled($enabled_modules, ['maintenance', 'Maintenance'])) {
$items[] = self::item(Customer\MaintenanceHubController::class, 'index', __('portal::lang.maintenance_hub'), 'fa-industry', 'maintenance', __('portal::lang.maintenance_hub_short'));
$items[] = self::item(Customer\ProductionLineController::class, 'index', __('portal::lang.production_lines'), 'fa-stream', 'production-lines');
$items[] = self::item(Customer\EquipmentController::class, 'index', __('portal::lang.my_equipment'), 'fa-cogs', 'equipment');
$items[] = self::item(Customer\PreventiveScheduleController::class, 'index', __('portal::lang.periodic_services'), 'fa-calendar-alt', 'periodic-services');
$items[] = self::item(Customer\WorkOrderController::class, 'index', __('portal::lang.work_orders'), 'fa-wrench', 'work-orders');
$items[] = self::item(Customer\FieldMissionController::class, 'index', __('portal::lang.field_missions'), 'fa-map-marked-alt', 'field-missions');
$items[] = self::item(Customer\ServiceRequestController::class, 'index', __('portal::lang.service_requests'), 'fa-headset', 'service-requests');
}
if (self::moduleEnabled($enabled_modules, ['tms', 'Tms'])) {
$items[] = self::item(Customer\ShipmentController::class, 'index', __('portal::lang.shipment_tracking'), 'fa-truck', 'shipments');
}
if (self::moduleEnabled($enabled_modules, ['repair', 'Repair'])) {
$items[] = self::item(Customer\RepairController::class, 'index', __('repair::lang.repair'), 'fa-tools', 'repairs');
}
if (self::moduleEnabled($enabled_modules, ['qualitymanagement', 'QualityManagement'])
&& \Module::has('QualityManagement') && \Module::isEnabled('QualityManagement')) {
$items[] = [
'url' => route('customer.quality.ncr.index'),
'label' => __('qualitymanagement::lang.portal_ncr'),
'short_label' => __('qualitymanagement::lang.portal_ncr'),
'icon' => 'fa-clipboard-check',
'segment' => 'quality',
'active' => false,
];
}
if (self::moduleEnabled($enabled_modules, ['industrialengineering', 'IndustrialEngineering'])
&& \Module::has('IndustrialEngineering') && \Module::isEnabled('IndustrialEngineering')) {
$items[] = [
'url' => route('customer.cpq.index'),
'label' => __('industrialengineering::lang.portal_cpq'),
'short_label' => __('industrialengineering::lang.portal_cpq'),
'icon' => 'fa-sliders-h',
'segment' => 'cpq',
'active' => false,
];
}
$items[] = self::item(Customer\SellController::class, 'getSellList', __('portal::lang.nav_sales'), 'fa-file-invoice', 'sells');
$items[] = self::item(Customer\LedgerController::class, 'index', __('portal::lang.nav_ledger'), 'fa-scroll', 'ledger', __('portal::lang.nav_ledger_short'));
if (! empty($crm_settings['enable_order_request'])) {
$items[] = self::item(Customer\OrderRequestController::class, 'index', __('crm::lang.order_request'), 'fa-arrow-circle-up', 'order-request');
}
if (in_array('booking', $enabled_modules)) {
$items[] = self::item(Customer\BookingController::class, 'index', __('restaurant.bookings'), 'fa-calendar-check', 'bookings');
}
$items[] = self::item(Customer\ProposalController::class, 'index', __('portal::lang.nav_proposals'), 'fa-file-contract', 'proposals');
$items[] = self::item(Customer\ProfileController::class, 'getProfile', __('portal::lang.nav_profile'), 'fa-user', 'profile');
return self::mergeModuleNavItems(self::markActive($items), 'customer');
}
public static function supplier(): array
{
$enabled_modules = session('business.enabled_modules', []);
$items = [
self::item(Supplier\DashboardController::class, 'index', __('portal::lang.nav_home'), 'fa-home', 'dashboard'),
self::item(Supplier\PurchaseOrderController::class, 'index', __('lang_v1.purchase_order'), 'fa-clipboard-list', 'purchase-orders'),
self::item(Supplier\PurchaseController::class, 'getPurchaseList', __('purchase.list_purchase'), 'fa-list', 'purchases'),
self::item(Supplier\PaymentController::class, 'index', __('purchase.payments'), 'fa-money-bill-wave', 'payments'),
self::item(Supplier\PurchaseReturnController::class, 'index', __('lang_v1.purchase_return'), 'fa-undo', 'purchase-returns'),
self::item(Supplier\DocumentController::class, 'index', __('portal::lang.documents'), 'fa-folder-open', 'documents'),
self::item(Supplier\LedgerController::class, 'index', __('portal::lang.nav_ledger'), 'fa-scroll', 'ledger'),
];
if (self::moduleEnabled($enabled_modules, ['supplychain', 'SupplyChain'])
&& \Module::has('SupplyChain') && \Module::isEnabled('SupplyChain')) {
$items[] = [
'url' => route('supplier.supplychain.rfq.index'),
'label' => __('supplychain::lang.portal_rfq'),
'short_label' => __('supplychain::lang.portal_rfq'),
'icon' => 'fa-file-signature',
'segment' => 'supply-chain',
'active' => false,
];
}
$items[] = self::item(Supplier\ProfileController::class, 'getProfile', __('portal::lang.nav_profile'), 'fa-user', 'profile');
return self::mergeModuleNavItems(self::markActive($items), 'supplier');
}
{
$enabled_modules = session('business.enabled_modules', []);
$items = [
self::item(Marketer\DashboardController::class, 'index', __('portal::lang.nav_home'), 'fa-home', 'dashboard'),
self::item(Marketer\SellController::class, 'index', __('portal::lang.my_sales'), 'fa-shopping-cart', 'sells'),
self::item(Marketer\CommissionController::class, 'index', __('portal::lang.commission_targets'), 'fa-percent', 'commission'),
];
if (in_array('Crm', $enabled_modules) || in_array('crm', $enabled_modules)) {
$items[] = self::item(Marketer\LeadController::class, 'index', __('crm::lang.leads'), 'fa-user-plus', 'leads');
$items[] = self::item(Marketer\FollowUpController::class, 'index', __('crm::lang.follow_ups'), 'fa-calendar-alt', 'follow-ups');
}
$items[] = self::item(Marketer\ExpenseController::class, 'index', __('expense.expenses'), 'fa-receipt', 'expenses');
$items[] = self::item(Marketer\ProfileController::class, 'getProfile', __('portal::lang.nav_profile'), 'fa-user', 'profile');
return self::markActive($items);
}
private static function item(string $controller, string $action, string $label, string $icon, string $segment, ?string $short_label = null): array
{
return [
'url' => action([$controller, $action]),
'label' => $label,
'short_label' => $short_label ?? $label,
'icon' => $icon,
'segment' => $segment,
'active' => false,
];
}
private static function markActive(array $items): array
{
$current = request()->segment(2) ?: 'dashboard';
foreach ($items as &$item) {
$item['active'] = $current === $item['segment'];
}
return $items;
}
private static function moduleEnabled(array $enabled_modules, array $names): bool
{
return (bool) count(array_intersect($enabled_modules, $names));
}
private static function mergeModuleNavItems(array $items, string $portalType): array
{
$moduleUtil = new \App\Utils\ModuleUtil();
$extra = $moduleUtil->getModuleData('get_portal_navigation', ['portal' => $portalType]);
foreach ($extra as $moduleItems) {
if (! is_array($moduleItems)) {
continue;
}
foreach ($moduleItems as $navItem) {
if (is_array($navItem) && ! empty($navItem['url'])) {
$items[] = $navItem;
}
}
}
return self::markActive($items);
}
}