side_menu/lib/Service/AppRepository.php

149 lines
3.9 KiB
PHP
Raw Normal View History

2020-08-06 15:00:59 +02:00
<?php
namespace OCA\SideMenu\Service;
use OC\User\User;
use OCP\INavigationManager;
2020-08-13 11:21:06 +02:00
use OCP\L10N\IFactory;
2020-08-06 15:00:59 +02:00
/**
* class AppRepository.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class AppRepository
{
/**
2020-10-14 09:57:42 +02:00
* @var \OC_App
2020-08-06 15:00:59 +02:00
*/
protected $ocApp;
2020-08-13 11:21:06 +02:00
/**
* @var IFactory
*/
protected $l10nFactory;
/**
* @var ConfigProxy
*/
protected $config;
/**
* @var CategoryRepository
*/
protected $categoryRepository;
2023-07-15 15:22:09 +02:00
/**
* @var INavigationManager
*/
protected $navigationManager;
public function __construct(
\OC_App $ocApp,
2023-07-15 14:50:15 +02:00
INavigationManager $navigationManager,
IFactory $l10nFactory,
ConfigProxy $config,
CategoryRepository $categoryRepository
) {
2020-08-06 15:00:59 +02:00
$this->ocApp = $ocApp;
2020-08-13 11:21:06 +02:00
$this->l10nFactory = $l10nFactory;
$this->config = $config;
2023-07-15 14:50:15 +02:00
$this->navigationManager = $navigationManager;
$this->categoryRepository = $categoryRepository;
2020-08-06 15:00:59 +02:00
}
/**
* Retrieves visibles apps.
*
* @return array
*/
public function getVisibleApps()
{
$navigation = $this->navigationManager->getAll();
$appCategoriesCustom = $this->config->getAppValueArray('apps-categories-custom', '[]');
$categories = $this->categoryRepository->getOrderedCategories();
2020-08-13 11:21:59 +02:00
$apps = $this->ocApp->listAllApps();
2020-08-06 15:00:59 +02:00
$visibleApps = [];
foreach ($apps as $app) {
$id = $app['id'];
2020-08-19 09:18:29 +02:00
foreach ([$app['id'], $app['id'].'_index'] as $id) {
if (isset($navigation[$id])) {
$app['name'] = $this->getAppName($app);
2020-08-19 09:18:29 +02:00
$app['href'] = $navigation[$id]['href'];
$app['icon'] = $navigation[$id]['icon'];
2020-08-13 11:21:06 +02:00
2020-08-19 09:18:29 +02:00
$visibleApps[$id] = $app;
}
2020-08-06 15:00:59 +02:00
}
}
2020-08-11 15:52:15 +02:00
foreach ($navigation as $app) {
2020-08-13 11:21:59 +02:00
if ('external_index' === substr($app['id'], 0, 14)) {
2020-08-11 15:52:15 +02:00
$visibleApps[$app['id']] = [
'id' => $app['id'],
'name' => $this->getAppName($app),
2020-08-14 13:30:48 +02:00
'href' => $app['href'],
2020-08-19 09:18:29 +02:00
'icon' => $app['icon'],
2020-08-11 15:52:15 +02:00
'category' => [
'external_links',
],
];
2020-08-13 11:21:59 +02:00
} elseif ('files' === $app['id']) {
2020-08-12 16:27:43 +02:00
$visibleApps[$app['id']] = [
'id' => $app['id'],
'name' => $this->getAppName($app),
2020-08-14 13:30:48 +02:00
'href' => $app['href'],
2020-08-19 09:18:29 +02:00
'icon' => $app['icon'],
2020-08-12 16:27:43 +02:00
'category' => [],
];
2020-08-11 15:52:15 +02:00
}
}
foreach ($visibleApps as $id => $app) {
if (isset($appCategoriesCustom[$id], $categories[$appCategoriesCustom[$id]])) {
$visibleApps[$id]['category'] = [$appCategoriesCustom[$id]];
}
}
2020-08-06 15:00:59 +02:00
return $visibleApps;
}
2022-10-21 17:59:41 +02:00
public function getAppName($app)
{
return $this->config->getAppValue(
'app.navigation.name',
$this->l10nFactory->get($app['id'])->t($app['name']),
$app['id']
);
}
public function getOrderedApps(?User $user = null)
2022-10-21 17:59:41 +02:00
{
$apps = $this->getVisibleApps();
$orders = $this->config->getAppValueArray('apps-order', '[]');
2022-10-21 17:59:41 +02:00
if (null !== $user && !$this->config->getAppValueBool('force', '0')) {
$userOrders = $this->config->getUserValueArray($user, 'apps-order', '[]');
if (!empty($userOrders)) {
$orders = $userOrders;
}
}
2022-10-21 17:59:41 +02:00
usort($apps, function ($a, $b) use ($orders) {
$ak = array_keys($orders, $a['id'])[0] ?? null;
$bk = array_keys($orders, $b['id'])[0] ?? null;
if (null === $ak || null === $bk) {
2022-10-21 17:59:41 +02:00
return ($a['name'] < $b['name']) ? -1 : 1;
}
return $ak < $bk ? -1 : 1;
});
return $apps;
}
2020-08-06 15:00:59 +02:00
}