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;
|
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IRequest;
|
2020-08-12 14:06:52 +02:00
|
|
|
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-08-11 15:52:15 +02:00
|
|
|
* @var IConfig
|
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
|
|
|
/**
|
|
|
|
* @param string $appName
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
$appName,
|
|
|
|
IRequest $request,
|
|
|
|
IConfig $config,
|
|
|
|
AppRepository $appRepository,
|
2020-08-11 10:40:31 +02:00
|
|
|
CategoryFetcher $categoryFetcher,
|
|
|
|
URLGenerator $router,
|
2020-08-13 11:21:06 +02:00
|
|
|
IL10N $trans,
|
|
|
|
IFactory $l10nFactory)
|
2020-08-06 15:00:59 +02:00
|
|
|
{
|
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
|
|
|
$this->config = $config;
|
|
|
|
$this->appRepository = $appRepository;
|
2020-08-11 10:40:31 +02:00
|
|
|
$this->categoryFetcher = $categoryFetcher;
|
|
|
|
$this->l10nFactory = $l10nFactory;
|
|
|
|
$this->router = $router;
|
2020-08-06 15:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @return JSONResponse
|
|
|
|
*/
|
|
|
|
public function items()
|
|
|
|
{
|
|
|
|
$apps = $this->appRepository->getVisibleApps();
|
|
|
|
$currentLanguage = substr($this->l10nFactory->findLanguage(), 0, 2);
|
|
|
|
$categoriesLabels = $this->categoryFetcher->get();
|
2020-08-11 15:52:15 +02:00
|
|
|
$externalSitesInTopMenu = (bool) $this->config->getAppValue('side_menu', 'external-sites-in-top-menu', 0);
|
2020-08-12 14:06:52 +02:00
|
|
|
$user = OC::$server[IUserSession::class]->getUser();
|
2020-08-11 13:34:36 +02:00
|
|
|
$appsCategories = [];
|
|
|
|
$categoriesAppsCount = [];
|
2020-08-06 15:00:59 +02:00
|
|
|
$items = [];
|
|
|
|
|
2020-08-12 14:06:52 +02:00
|
|
|
if (!$user) {
|
|
|
|
return new JSONResponse([
|
|
|
|
'items' => $items,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-08-12 15:59:51 +02:00
|
|
|
$topMenuApps = (array) json_decode($this->config->getAppValue('side_menu', 'top-menu-apps', '[]'), true);
|
|
|
|
$userTopMenuApps = (array) json_decode($this->config->getUserValue($user->getUid(), 'side_menu', 'top-menu-apps', '[]'), true);
|
|
|
|
|
|
|
|
if (!empty($userTopMenuApps)) {
|
|
|
|
$topMenuApps = $userTopMenuApps;
|
|
|
|
}
|
|
|
|
|
2020-08-06 15:00:59 +02:00
|
|
|
foreach ($categoriesLabels as $k => $category) {
|
|
|
|
$categoriesLabels[$category['id']] = $category['translations'][$currentLanguage]['name'] ?? $category['translations']['en']['name'];
|
|
|
|
|
|
|
|
unset($categoriesLabels[$k]);
|
|
|
|
}
|
|
|
|
|
2020-08-13 11:21:06 +02:00
|
|
|
$categoriesLabels['external_links'] = $this->l10nFactory->get('external')->t('External sites');
|
2020-08-11 15:52:15 +02:00
|
|
|
|
2020-08-12 16:27:43 +02:00
|
|
|
$items['other'] = [
|
|
|
|
'name' => '',
|
|
|
|
'apps' => [],
|
|
|
|
];
|
|
|
|
|
2020-08-06 15:00:59 +02:00
|
|
|
foreach ($apps as $app) {
|
2020-08-12 15:59:51 +02:00
|
|
|
if (in_array($app['id'], $topMenuApps)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-11 13:34:36 +02:00
|
|
|
$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] = [
|
|
|
|
'name' => $categoriesLabels[$category],
|
|
|
|
'apps' => [],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-08-11 13:34:36 +02:00
|
|
|
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
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 13:34:36 +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]);
|
|
|
|
} else {
|
2020-08-20 16:27:35 +02:00
|
|
|
uasort($items[$category]['apps'], function ($a, $b) {
|
|
|
|
return ($a['name'] < $b['name']) ? -1 : 1;
|
|
|
|
});
|
2020-08-13 16:33:02 +02:00
|
|
|
}
|
2020-08-11 13:34:36 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 11:21:59 +02:00
|
|
|
usort($items, function ($a, $b) {
|
2020-08-11 13:34:36 +02:00
|
|
|
return ($a['name'] < $b['name']) ? -1 : 1;
|
|
|
|
});
|
2020-08-06 15:00:59 +02:00
|
|
|
|
|
|
|
return new JSONResponse([
|
|
|
|
'items' => $items,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|