side_menu/lib/Service/AppRepository.php

110 lines
3 KiB
PHP
Raw Normal View History

2020-08-06 15:00:59 +02:00
<?php
namespace OCA\SideMenu\Service;
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;
public function __construct(
\OC_App $ocApp,
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;
$this->categoryRepository = $categoryRepository;
2020-08-06 15:00:59 +02:00
}
/**
* Retrieves visibles apps.
*
* @return array
*/
public function getVisibleApps()
{
2020-08-13 11:21:59 +02:00
$navigation = $this->ocApp->getNavigation();
$appCategoriesCustom = $this->config->getAppValueArray('apps-categories-custom', '[]');
$categoriesCustom = $this->config->getAppValueArray('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->l10nFactory->get($id)->t($app['name']);
$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'],
2020-08-13 11:21:06 +02:00
'name' => $this->l10nFactory->get($app['id'])->t($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-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'],
2020-08-13 11:21:06 +02:00
'name' => $this->l10nFactory->get($app['id'])->t($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-12 16:27:43 +02:00
'category' => [],
];
2020-08-11 15:52:15 +02:00
}
}
foreach ($visibleApps as $id => $app) {
if (isset($appCategoriesCustom[$id]) && (isset($categoriesCustom[$id]) || isset($categories[$id]))) {
$visibleApps[$id]['category'] = [$appCategoriesCustom[$id]];
}
}
2020-08-13 11:21:59 +02:00
usort($visibleApps, function ($a, $b) {
2020-08-12 16:32:26 +02:00
return ($a['name'] < $b['name']) ? -1 : 1;
});
2020-08-06 15:00:59 +02:00
return $visibleApps;
}
}