protected static function getApps() { if (SessionUtilities::isAuthenticated()) { $user = SessionUtilities::user(); $defaultAppId = $user->default_app_id; if (SessionUtilities::isSysAdmin()) { $appGroups = AppGroupModel::with(['app_by_app_to_app_group' => function ($q) { $q->whereIsActive(1)->whereNotIn('type', [AppTypes::NONE]); }])->get(); $apps = AppModel::whereIsActive(1)->whereNotIn('type', [AppTypes::NONE])->get(); } else { $userId = $user->id; $userAppRoles = UserAppRole::whereUserId($userId)->whereNotNull('role_id')->get(['app_id']); $appIds = []; foreach ($userAppRoles as $uar) { $appIds[] = $uar->app_id; } $appIdsString = implode(',', $appIds); $appIdsString = empty($appIdsString) ? '-1' : $appIdsString; $typeString = implode(',', [AppTypes::NONE]); $typeString = empty($typeString) ? '-1' : $typeString; $appGroups = AppGroupModel::with(['app_by_app_to_app_group' => function ($q) use($appIdsString, $typeString) { $q->whereRaw("(app.id IN ({$appIdsString}) OR role_id > 0) AND is_active = 1 AND type NOT IN ({$typeString})"); }])->get(); $apps = AppModel::whereRaw("(app.id IN ({$appIdsString}) OR role_id > 0) AND is_active = 1 AND type NOT IN ({$typeString})")->get(); } } else { $appGroups = AppGroupModel::with(['app_by_app_to_app_group' => function ($q) { $q->where('role_id', '>', 0)->whereIsActive(1)->whereNotIn('type', [AppTypes::NONE]); }])->get(); $apps = AppModel::whereIsActive(1)->where('role_id', '>', 0)->whereNotIn('type', [AppTypes::NONE])->get(); } if (empty($defaultAppId)) { $systemConfig = SystemConfig::first(['default_app_id']); $defaultAppId = !empty($systemConfig) ? $systemConfig->default_app_id : null; } $inGroups = []; $groupedApps = []; $noGroupedApps = []; foreach ($appGroups as $appGroup) { $appArray = $appGroup->getRelation('app_by_app_to_app_group')->toArray(); if (!empty($appArray)) { $appInfo = []; foreach ($appArray as $app) { $inGroups[] = $app['id']; $appInfo[] = static::makeAppInfo($app, $defaultAppId); } $groupedApps[] = ['id' => $appGroup->id, 'name' => $appGroup->name, 'description' => $appGroup->description, 'app' => $appInfo]; } } /** @type AppModel $app */ foreach ($apps as $app) { if (!in_array($app->id, $inGroups)) { $noGroupedApps[] = static::makeAppInfo($app->toArray(), $defaultAppId); } } return [$groupedApps, $noGroupedApps]; }