side_menu/lib/Controller/NavController.php

140 lines
3.7 KiB
PHP

<?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;
use OCA\SideMenu\Service\AppRepository;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OC\App\AppStore\Fetcher\CategoryFetcher;
use OCP\L10N\IFactory;
class NavController extends Controller
{
/**
* @var \OCP\IConfig
*/
protected $config;
/**
* @var AppRepository
*/
protected $appRepository;
/**
* @var IL10N
*/
protected $trans;
/**
* @var IFactory
*/
protected $l10nFactory;
/**
* @var CategoryFetcher
*/
protected $categoryFetcher;
/**
* @param string $appName
*/
public function __construct(
$appName,
IRequest $request,
IConfig $config,
AppRepository $appRepository,
CategoryFetcher $categoryFetcher,
IFactory $l10nFactory,
IL10N $trans)
{
parent::__construct($appName, $request);
$this->config = $config;
$this->appRepository = $appRepository;
$this->categoryFetcher = $categoryFetcher;
$this->l10nFactory = $l10nFactory;
$this->trans = $trans;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @PublicPage
*
* @return JSONResponse
*/
public function items()
{
$apps = $this->appRepository->getVisibleApps();
$currentLanguage = substr($this->l10nFactory->findLanguage(), 0, 2);
$categoriesLabels = $this->categoryFetcher->get();
$items = [];
foreach ($categoriesLabels as $k => $category) {
$categoriesLabels[$category['id']] = $category['translations'][$currentLanguage]['name'] ?? $category['translations']['en']['name'];
unset($categoriesLabels[$k]);
}
foreach ($apps as $app) {
$categories = $app['category'];
foreach ($categories as $category) {
if (!isset($items[$category])) {
$items[$category] = [
'name' => $categoriesLabels[$category],
'apps' => [],
];
}
$items[$category]['apps'][$app['id']] = [
'name' => $app['name'],
'href' => '#',
'icon' => $app['previewAsIcon'] ? $app['preview'] : null,
];
}
}
$items['other'] = [
'label' => $this->trans->t('Other'),
'apps' => [],
];
// foreach ($items as $category => $value) {
// if ($category !== 'other') {
// if (count($value['apps']) < 0) {
// $items['other']['apps'] = array_merge(
// $items['other']['apps'],
// $value['apps']
// );
//
// unset($items[$category]);
// }
// }
// }
return new JSONResponse([
'items' => $items,
]);
}
}