diff --git a/appinfo/app.php b/appinfo/app.php index 3f2b281..86a3245 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -21,42 +21,51 @@ namespace OCA\SideMenu\Appinfo; use OC\Security\CSP\ContentSecurityPolicy; use OCP\Util; +use OCP\IUserSession; $config = \OC::$server->getConfig(); $cspnm = \OC::$server->getContentSecurityPolicyNonceManager(); +$user = \OC::$server[IUserSession::class]->getUser(); +$enabled = true; -Util::addScript('side_menu', 'sideMenu'); -Util::addStyle('side_menu', 'sideMenu'); +if ($user !== null) { + $enabled = (bool) $config->getUserValue($user->getUid(), 'side_menu', 'enabled', '1'); +} -$stylesheet = \OC::$server->getURLGenerator()->linkToRoute( - 'side_menu.Css.stylesheet', - [ - 'v' => $config->getAppValue('side_menu', 'cache', '0'), - ] -); +if ($enabled) { + Util::addScript('side_menu', 'sideMenu'); + Util::addStyle('side_menu', 'sideMenu'); -$script = \OC::$server->getURLGenerator()->linkToRoute( - 'side_menu.Js.script', - [ - 'v' => $config->getAppValue('side_menu', 'cache', '0'), - ] -); + $stylesheet = \OC::$server->getURLGenerator()->linkToRoute( + 'side_menu.Css.stylesheet', + [ + 'v' => $config->getAppValue('side_menu', 'cache', '0'), + ] + ); -Util::addHeader( - 'link', - [ - 'href' => $stylesheet, - 'rel' => 'stylesheet' - ], - '' -); + $script = \OC::$server->getURLGenerator()->linkToRoute( + 'side_menu.Js.script', + [ + 'v' => $config->getAppValue('side_menu', 'cache', '0'), + ] + ); -Util::addHeader( - 'script', - [ - 'src' => $script, - 'nonce' => $cspnm->getNonce(), - ], - '' -); + Util::addHeader( + 'link', + [ + 'href' => $stylesheet, + 'rel' => 'stylesheet' + ], + '' + ); + + Util::addHeader( + 'script', + [ + 'src' => $script, + 'nonce' => $cspnm->getNonce(), + ], + '' + ); +} diff --git a/appinfo/info.xml b/appinfo/info.xml index 4693c08..b8e382f 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -28,6 +28,8 @@ You can report a bug or request a feature by opening an issue. OCA\SideMenu\Settings\Admin - OCA\SideMenu\Settings\Section + OCA\SideMenu\Settings\AdminSection + OCA\SideMenu\Settings\Personal + OCA\SideMenu\Settings\PersonalSection diff --git a/appinfo/routes.php b/appinfo/routes.php index aab264b..dc82109 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -21,5 +21,6 @@ return [ 'routes' => [ ['name' => 'Css#stylesheet', 'url' => '/css/stylesheet', 'verb' => 'GET'], ['name' => 'Js#script', 'url' => '/js/script', 'verb' => 'GET'], + ['name' => 'PersonalSetting#valueSet', 'url' => '/personalSetting/valueSet', 'verb' => 'POST'], ], ]; diff --git a/lib/Controller/CssController.php b/lib/Controller/CssController.php index 91ec475..2edd975 100644 --- a/lib/Controller/CssController.php +++ b/lib/Controller/CssController.php @@ -27,7 +27,9 @@ use OCP\IRequest; class CssController extends Controller { - /** @var \OCP\IConfig */ + /** + * @var \OCP\IConfig + */ protected $config; /** diff --git a/lib/Controller/JsController.php b/lib/Controller/JsController.php index 9114522..e4ddce3 100644 --- a/lib/Controller/JsController.php +++ b/lib/Controller/JsController.php @@ -27,7 +27,9 @@ use OCP\IRequest; class JsController extends Controller { - /** @var \OCP\IConfig */ + /** + * @var \OCP\IConfig + */ protected $config; /** diff --git a/lib/Controller/PersonalSettingController.php b/lib/Controller/PersonalSettingController.php new file mode 100644 index 0000000..2beba19 --- /dev/null +++ b/lib/Controller/PersonalSettingController.php @@ -0,0 +1,72 @@ +. + */ + +namespace OCA\SideMenu\Controller; + +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\AppFramework\Http\Response; +use OCP\IConfig; +use OCP\IRequest; +use OCP\IUserSession; + +class PersonalSettingController extends Controller +{ + /** + * @var \OCP\IConfig + */ + protected $config; + + /** + * @var IUserSession + */ + private $userSession; + + public function __construct($appName, IRequest $request, IConfig $config, IUserSession $userSession) + { + parent::__construct($appName, $request); + + $this->config = $config; + $this->userSession = $userSession; + } + + /** + * @NoAdminRequired + * + * @return Response + */ + public function valueSet($name, $value) + { + $doSave = false; + $user = $this->userSession->getUser(); + + if ($name === 'enabled') { + $doSave = true; + + if (!in_array($value, ['0', '1'])) { + $value = '1'; + } + } + + if ($doSave) { + $this->config->setUserValue($user->getUid(), 'side_menu', 'enabled', $value); + } + + return []; + } +} diff --git a/lib/Settings/Admin.php b/lib/Settings/Admin.php index 3c3f59e..d224ed9 100644 --- a/lib/Settings/Admin.php +++ b/lib/Settings/Admin.php @@ -26,13 +26,19 @@ use OCP\IConfig; class Admin implements ISettings { - /** @var IL10N */ + /** + * @var IL10N + */ private $l; - /** @var ILogger */ + /** + * @var ILogger + */ private $logger; - /** @var IConfig */ + /** + * @var IConfig + */ private $config; public function __construct(IL10N $l, ILogger $logger, IConfig $config) diff --git a/lib/Settings/Section.php b/lib/Settings/AdminSection.php similarity index 94% rename from lib/Settings/Section.php rename to lib/Settings/AdminSection.php index f97a7f2..983259e 100644 --- a/lib/Settings/Section.php +++ b/lib/Settings/AdminSection.php @@ -22,12 +22,16 @@ use OCP\IL10N; use OCP\IURLGenerator; use OCP\Settings\IIconSection; -class Section implements IIconSection +class AdminSection implements IIconSection { - /** @var IL10N */ + /** + * @var IL10N + */ private $l; - /** @var IURLGenerator */ + /** + * @var IURLGenerator + */ private $url; /** diff --git a/lib/Settings/Personal.php b/lib/Settings/Personal.php new file mode 100644 index 0000000..beef491 --- /dev/null +++ b/lib/Settings/Personal.php @@ -0,0 +1,91 @@ +. + */ + +namespace OCA\SideMenu\Settings; + +use OCP\AppFramework\Http\TemplateResponse; +use OCP\IL10N; +use OCP\ILogger; +use OCP\Settings\ISettings; +use OCP\IConfig; +use OCP\IUserSession; + +class Personal implements ISettings +{ + /** + * @var IL10N + */ + private $l; + + /** + * @var ILogger + */ + private $logger; + + /** + * @var IConfig + */ + private $config; + + /** + * @var IUserSession + */ + private $userSession; + + public function __construct(IL10N $l, ILogger $logger, IConfig $config, IUserSession $userSession) + { + $this->l = $l; + $this->logger = $logger; + $this->config = $config; + $this->userSession = $userSession; + } + + /** + * @return TemplateResponse + */ + public function getForm() + { + $user = $this->userSession->getUser(); + + $parameters = [ + 'enabled' => $this->config->getUserValue($user->getUid(), 'side_menu', 'enabled', '1'), + ]; + + return new TemplateResponse('side_menu', 'settings/personal-form', $parameters, ''); + } + + /** + * @return string the section ID, e.g. 'sharing' + */ + public function getSection() + { + return 'side_menu'; + } + + /** + * @return int whether the form should be rather on the top or bottom of + * the admin section. The forms are arranged in ascending order of the + * priority values. It is required to return a value between 0 and 100. + * + * E.g.: 70 + */ + public function getPriority() + { + return 70; + } +} diff --git a/lib/Settings/PersonalSection.php b/lib/Settings/PersonalSection.php new file mode 100644 index 0000000..1a18ba3 --- /dev/null +++ b/lib/Settings/PersonalSection.php @@ -0,0 +1,88 @@ +. + */ + +namespace OCA\SideMenu\Settings; + +use OCP\IL10N; +use OCP\IURLGenerator; +use OCP\Settings\IIconSection; + +class PersonalSection implements IIconSection +{ + /** + * @var IL10N + */ + private $l; + + /** + * @var IURLGenerator + */ + private $url; + + /** + * @param IURLGenerator $url + * @param IL10N $l + */ + public function __construct(IURLGenerator $url, IL10N $l) + { + $this->url = $url; + $this->l = $l; + } + + /** + * returns the ID of the section. It is supposed to be a lower case string, + * e.g. 'ldap' + * + * @returns string + */ + public function getID() + { + return 'side_menu'; + } + + /** + * returns the translated name as it should be displayed, e.g. 'LDAP / AD + * integration'. Use the L10N service to translate it. + * + * @return string + */ + public function getName() + { + return $this->l->t('Side menu'); + } + + /** + * @return int whether the form should be rather on the top or bottom of + * the settings navigation. The sections are arranged in ascending order of + * the priority values. It is required to return a value between 0 and 99. + * + * E.g.: 70 + */ + public function getPriority() + { + return 70; + } + + /** + * {@inheritdoc} + */ + public function getIcon() + { + return $this->url->imagePath('theming', 'app-dark.svg'); + } +} diff --git a/src/admin.js b/src/admin.js index c0da4e8..15be7bd 100644 --- a/src/admin.js +++ b/src/admin.js @@ -15,37 +15,33 @@ * along with this program. If not, see . */ -const elements = [ - 'side-menu-background-color', - 'side-menu-background-color-to', - 'side-menu-current-app-background-color', - 'side-menu-text-color', - 'side-menu-force-light-icon', - 'side-menu-opener', - 'side-menu-opener-position', - 'side-menu-opener-hover', - 'side-menu-opener-only', - 'side-menu-display-logo', - 'side-menu-hide-when-no-apps', - 'side-menu-size-icon', - 'side-menu-size-text', - 'side-menu-cache', - 'side-menu-external-sites-in-top-menu', -]; +let elements = [] const selector = '#side-menu-message'; +const userConfig = (name, value, callbacks) => { + const url = OC.generateUrl('/apps/side_menu/personalSetting/valueSet') + + $.post(url, {name: name, value: value}, callbacks.success) + .fail(callbacks.error) +} + +const appConfig = (name, value, callbacks) => { + OCP.AppConfig.setValue('side_menu', name, value, callbacks) +} + + const saveSettings = (key) => { - const element = elements[key] - const name = $('#' + element).attr('name') - let value = $('#' + element).val() + const element = elements.get(key) + const name = $(element).attr('name') + let value = $(element).val() const size = elements.length if (element === 'side-menu-cache') { value++ } - OCP.AppConfig.setValue('side_menu', name, value, { + const callbacks = { success: () => { OC.msg.finishedSuccess( selector, @@ -61,10 +57,18 @@ const saveSettings = (key) => { error: () => { OC.msg.finishedError(selector, t('side_menu', 'Error while saving "' + element + '"')) } - }); + } + + if ($(element).is('[data-personal]')) { + userConfig(name, value, callbacks) + } else { + appConfig(name, value, callbacks) + } } $(document).ready(() => { + elements = $('.side-menu-setting') + $('#side-menu-save').on('click', (event) => { event.preventDefault() OC.msg.startSaving(selector) diff --git a/templates/settings/admin-form.php b/templates/settings/admin-form.php index c2e51bd..e10315e 100644 --- a/templates/settings/admin-form.php +++ b/templates/settings/admin-form.php @@ -49,11 +49,13 @@ $choicesSizes = [ id="side-menu-background-color" name="background-color" type="color" + class="side-menu-setting" value=""> @@ -67,6 +69,7 @@ $choicesSizes = [ id="side-menu-current-app-background-color" name="current-app-background-color" type="color" + class="side-menu-setting" value=""> @@ -80,6 +83,7 @@ $choicesSizes = [ id="side-menu-text-color" name="text-color" type="color" + class="side-menu-setting" value=""> @@ -90,7 +94,7 @@ $choicesSizes = [
- $value): ?>
- $value): ?>
- $value): ?>
- $value): ?>
- $value): ?>
- $value): ?>
- $value): ?>
- $value): ?>
- $value): ?>
- + diff --git a/templates/settings/personal-form.php b/templates/settings/personal-form.php new file mode 100644 index 0000000..f81cdce --- /dev/null +++ b/templates/settings/personal-form.php @@ -0,0 +1,72 @@ +. + */ + +script('side_menu', 'admin'); +style('side_menu', 'admin'); +style('side_menu', 'support'); + +$choicesYesNo = [ + 'No' => '0', + 'Yes' => '1', +]; + +?> + +
+
+

+ t('Menu')); ?> +

+ +
+ +
+ +
+ +
+
+ +
+ + + + +
+ +
+ + t('You like this app and you want to support me?')); ?> + + + + + +
+
+