side_menu/lib/Controller/JsController.php

137 lines
4.8 KiB
PHP
Raw Normal View History

2020-04-17 13:57:23 +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;
use OC\User\User;
use OCA\Theming\ThemingDefaults;
2020-04-17 13:57:23 +02:00
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
2020-08-13 11:21:59 +02:00
use OCP\AppFramework\Http\TemplateResponse;
2020-04-17 13:57:23 +02:00
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserSession;
2020-04-17 13:57:23 +02:00
class JsController extends Controller
{
/**
* @var \OCP\IConfig
*/
2020-04-17 13:57:23 +02:00
protected $config;
/**
* @var User
*/
protected $user;
/**
* @var ThemingDefaults
*/
protected $themingDefaults;
2020-04-17 13:57:23 +02:00
/**
* @param string $appName
* @param IRequest $request
* @param IConfig $config
* @param ThemingDefaults $themingDefaults
2020-04-17 13:57:23 +02:00
*/
public function __construct($appName, IRequest $request, IConfig $config, ThemingDefaults $themingDefaults)
2020-04-17 13:57:23 +02:00
{
parent::__construct($appName, $request);
$this->themingDefaults = $themingDefaults;
$this->user = OC::$server[IUserSession::class]->getUser();
2020-04-17 13:57:23 +02:00
$this->config = $config;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @PublicPage
*/
public function script(): TemplateResponse
{
$response = new TemplateResponse('side_menu', 'js/script', $this->getConfig(), 'blank');
$response->addHeader('Content-Type', 'text/javascript');
return $response;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @PublicPage
*/
public function config(): JSONResponse
{
return new JSONResponse($this->getConfig());
}
protected function getConfig(): array
2020-04-17 13:57:23 +02:00
{
$topMenuApps = (array) json_decode($this->config->getAppValue('side_menu', 'top-menu-apps', '[]'), true);
2020-09-12 16:32:10 +02:00
$targetBlankApps = (array) json_decode($this->config->getAppValue('side_menu', 'target-blank-apps', '[]'), true);
$useAvatar = (bool) $this->config->getAppValue('side_menu', 'use-avatar', '0');
$avatar = null;
if ($this->user) {
$userTopMenuApps = (array) json_decode($this->config->getUserValue($this->user->getUid(), 'side_menu', 'top-menu-apps', '[]'), true);
if (!empty($userTopMenuApps)) {
$topMenuApps = $userTopMenuApps;
}
$userTargetBlankMode = (int) $this->config->getUserValue($this->user->getUid(), 'side_menu', 'target-blank-mode', '1');
$userTargetBlankApps = (array) json_decode($this->config->getUserValue($this->user->getUid(), 'side_menu', 'target-blank-apps', '[]'), true);
2020-09-18 11:09:14 +02:00
if (2 === $userTargetBlankMode) {
2020-09-12 16:32:10 +02:00
$targetBlankApps = $userTargetBlankApps;
}
$isAvatarSet = OC::$server->getAvatarManager()->getAvatar($this->user->getUid())->exists();
if ($useAvatar && $isAvatarSet) {
$avatar = OC::$server->getURLGenerator()->linkToRoute(
'core.avatar.getAvatar', [
'userId' => $this->user->getUid(),
'size' => 128,
'v' => $this->config->getUserValue($this->user, 'avatar', 'version', 0),
]
);
}
}
return [
2020-04-20 14:04:06 +02:00
'opener-position' => $this->config->getAppValue('side_menu', 'opener-position', 'before'),
2020-04-17 13:57:23 +02:00
'opener-hover' => (bool) $this->config->getAppValue('side_menu', 'opener-hover', '0'),
'external-sites-in-top-menu' => (bool) $this->config->getAppValue('side_menu', 'external-sites-in-top-menu', 0),
2020-05-09 14:30:24 +02:00
'force-light-icon' => (bool) $this->config->getAppValue('side_menu', 'force-light-icon', '0'),
'hide-when-no-apps' => (bool) $this->config->getAppValue('side_menu', 'hide-when-no-apps', '0'),
2020-05-19 15:17:54 +02:00
'loader-enabled' => (bool) $this->config->getAppValue('side_menu', 'loader-enabled', '1'),
'always-displayed' => (bool) $this->config->getAppValue('side_menu', 'always-displayed', '0'),
2020-08-06 15:00:59 +02:00
'big-menu' => (bool) $this->config->getAppValue('side_menu', 'big-menu', '0'),
'avatar' => $avatar,
'top-menu-apps' => $topMenuApps,
2020-09-12 16:32:10 +02:00
'target-blank-apps' => $targetBlankApps,
'logo' => $this->themingDefaults->getLogo(),
2020-04-17 13:57:23 +02:00
];
}
}