side_menu/lib/Controller/NavController.php

217 lines
6.3 KiB
PHP
Raw Normal View History

2020-08-06 15:00:59 +02:00
<?php
/**
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OCA\SideMenu\Controller;
2020-08-13 11:21:59 +02:00
use OC;
2020-08-11 10:40:31 +02:00
use OC\App\AppStore\Fetcher\CategoryFetcher;
2020-08-13 11:21:59 +02:00
use OC\URLGenerator;
2020-08-06 15:00:59 +02:00
use OCA\SideMenu\Service\AppRepository;
2021-02-03 10:14:03 +01:00
use OCA\SideMenu\Service\CategoryRepository;
2020-09-22 14:24:53 +02:00
use OCA\SideMenu\Service\ConfigProxy;
2020-08-06 15:00:59 +02:00
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use OCP\IUserSession;
2020-08-13 11:21:59 +02:00
use OCP\L10N\IFactory;
2020-08-06 15:00:59 +02:00
class NavController extends Controller
{
/**
2020-09-22 14:24:53 +02:00
* @var ConfigProxy
2020-08-06 15:00:59 +02:00
*/
protected $config;
/**
* @var AppRepository
*/
protected $appRepository;
/**
* @var IFactory
*/
protected $l10nFactory;
/**
* @var CategoryFetcher
*/
protected $categoryFetcher;
2020-08-11 10:40:31 +02:00
/**
* @var URLGenerator
*/
protected $router;
2020-08-06 15:00:59 +02:00
public function __construct(
2020-09-22 14:35:09 +02:00
string $appName,
2020-08-06 15:00:59 +02:00
IRequest $request,
2020-09-22 14:24:53 +02:00
ConfigProxy $config,
2020-08-06 15:00:59 +02:00
AppRepository $appRepository,
2021-02-03 10:14:03 +01:00
CategoryRepository $categoryRepository,
2020-08-11 10:40:31 +02:00
URLGenerator $router,
2020-10-15 15:46:59 +02:00
IFactory $l10nFactory
) {
2020-08-06 15:00:59 +02:00
parent::__construct($appName, $request);
$this->config = $config;
$this->appRepository = $appRepository;
2021-02-03 10:14:03 +01:00
$this->categoryRepository = $categoryRepository;
2020-08-11 10:40:31 +02:00
$this->l10nFactory = $l10nFactory;
$this->router = $router;
2020-08-06 15:00:59 +02:00
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @PublicPage
*
* @return JSONResponse
*/
public function items()
{
$user = OC::$server[IUserSession::class]->getUser();
2020-08-06 15:00:59 +02:00
$items = [];
if (!$user) {
return new JSONResponse([
'items' => $items,
]);
}
$apps = $this->appRepository->getOrderedApps($user);
2021-02-03 10:14:03 +01:00
$categoriesLabels = $this->categoryRepository->getOrderedCategories();
$hiddenApps = $this->config->getAppValueArray('big-menu-hidden-apps', '[]');
$isForced = $this->config->getAppValueBool('force', '0');
2020-09-22 14:24:53 +02:00
$topMenuApps = $this->config->getAppValueArray('top-menu-apps', '[]');
$topSideMenuApps = $this->config->getAppValueArray('top-side-menu-apps', '[]');
$userTopSideMenuApps = $this->config->getUserValueArray($user, 'top-side-menu-apps', '[]');
2020-09-22 14:24:53 +02:00
$userTopMenuApps = $this->config->getUserValueArray($user, 'top-menu-apps', '[]');
2021-02-03 10:14:03 +01:00
$appsCategories = [];
$categoriesAppsCount = [];
if (!$isForced && !empty($userTopMenuApps)) {
$topMenuApps = $userTopMenuApps;
}
if (!$isForced && !empty($userTopSideMenuApps)) {
$topSideMenuApps = $userTopSideMenuApps;
}
2020-08-06 15:00:59 +02:00
foreach ($apps as $app) {
2022-11-05 15:40:52 +01:00
$inTopMenuApps = in_array($app['id'], $topMenuApps);
$inTopSideMenuApps = in_array($app['id'], $topSideMenuApps);
$inHiddenApps = in_array($app['id'], $hiddenApps);
2022-11-05 15:40:52 +01:00
if (($inTopMenuApps && !$inTopSideMenuApps) || $inHiddenApps) {
continue;
}
$categories = (array) $app['category'];
$appsCategories[$app['id']] = [];
2020-08-06 15:00:59 +02:00
2020-08-12 16:27:43 +02:00
if (empty($categories)) {
$categories = ['other'];
}
2020-08-11 15:52:15 +02:00
2020-08-12 16:27:43 +02:00
foreach ($categories as $category) {
2020-08-06 15:00:59 +02:00
if (!isset($items[$category])) {
$items[$category] = [
2020-10-11 17:06:34 +02:00
'name' => $categoriesLabels[$category] ?? $category,
2021-02-03 10:14:03 +01:00
'categoryId' => $category,
2020-08-06 15:00:59 +02:00
'apps' => [],
];
}
if (!isset($categoriesAppsCount[$category])) {
$categoriesAppsCount[$category] = 0;
}
++$categoriesAppsCount[$category];
$appsCategories[$app['id']][] = $category;
2020-08-06 15:00:59 +02:00
$items[$category]['apps'][$app['id']] = [
'name' => $app['name'],
2020-08-14 13:30:48 +02:00
'href' => $app['href'],
2020-08-19 09:18:29 +02:00
'icon' => $app['icon'],
2020-08-06 15:00:59 +02:00
];
}
}
arsort($categoriesAppsCount);
$keys = array_keys($categoriesAppsCount);
foreach ($appsCategories as $app => $appCategories) {
$smallerIndex = count($categoriesAppsCount) - 1;
foreach ($appCategories as $appCategory) {
$appKey = array_keys($keys, $appCategory)[0];
if ($appKey < $smallerIndex) {
$smallerIndex = $appKey;
}
}
$category = $keys[$smallerIndex];
foreach ($items as $itemCategory => $value) {
if ($itemCategory !== $category && isset($value['apps'][$app])) {
unset($items[$itemCategory]['apps'][$app]);
if (empty($items[$itemCategory]['apps'])) {
unset($items[$itemCategory]);
}
}
}
}
foreach ($items as $category => $value) {
2020-08-13 16:33:02 +02:00
if (empty($items[$category]['apps'])) {
unset($items[$category]);
}
}
2021-02-03 10:14:03 +01:00
usort($items, function ($a, $b) use ($categoriesLabels) {
foreach ($categoriesLabels as $key => $value) {
2022-01-09 22:17:01 +01:00
if ($a['categoryId'] === 'other') {
return -1;
}
if ($b['categoryId'] === 'other') {
return 1;
}
2021-02-03 10:14:03 +01:00
if ($a['categoryId'] === $key) {
return -1;
2021-02-03 10:15:59 +01:00
}
2022-01-09 22:17:01 +01:00
2021-02-03 10:15:59 +01:00
if ($b['categoryId'] === $key) {
2021-02-03 10:14:03 +01:00
return 1;
}
}
return 0;
});
2020-08-06 15:00:59 +02:00
return new JSONResponse([
'items' => $items,
]);
}
}