From d3231ad03921e9675d40254827c48c616284d338 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 22 Mar 2017 17:01:29 +0100 Subject: [PATCH 1/2] Add types of links Signed-off-by: Joas Schilling --- img/settings.svg | 4 +++ js/admin.js | 22 ++++++++---- lib/AppInfo/Application.php | 5 +++ lib/Controller/APIController.php | 22 +++++++++--- lib/Exceptions/InvalidTypeException.php | 24 +++++++++++++ lib/SitesManager.php | 46 +++++++++++++++++++++---- templates/settings.php | 9 +++++ 7 files changed, 115 insertions(+), 17 deletions(-) create mode 100644 img/settings.svg create mode 100644 lib/Exceptions/InvalidTypeException.php diff --git a/img/settings.svg b/img/settings.svg new file mode 100644 index 00000000..1f78b5b4 --- /dev/null +++ b/img/settings.svg @@ -0,0 +1,4 @@ + + + + diff --git a/js/admin.js b/js/admin.js index 62ec435f..e6049633 100644 --- a/js/admin.js +++ b/js/admin.js @@ -31,22 +31,27 @@ return currentValue === itemValue; }); - Handlebars.registerHelper('getIcons', function() { - return OCA.External.App.availableIcons; - }); - Handlebars.registerHelper('getLanguages', function() { return OCA.External.App.availableLanguages; }); + Handlebars.registerHelper('getTypes', function() { + return OCA.External.App.availableTypes; + }); + + Handlebars.registerHelper('getIcons', function() { + return OCA.External.App.availableIcons; + }); + OCA.External.Models = OCA.External.Models || {}; OCA.External.Models.Site = Backbone.Model.extend({ defaults: { name: '', url: '', - icon: '', - lang: '' + lang: '', + type: 'link', + icon: 'external.svg' }, parse: function(response) { @@ -87,6 +92,7 @@ $('#loading_sites').removeClass('icon-loading-small'); self.availableIcons = response.ocs.data.icons; self.availableLanguages = response.ocs.data.languages; + self.availableTypes = response.ocs.data.types; if (response.ocs.data.sites.length === 0) { var $el = $(self._compiledTemplate({ @@ -105,7 +111,8 @@ var $el = $(self._compiledTemplate({ id: 'undefined', - icon: 'external.svg' + icon: 'external.svg', + type: 'link' })); self._attachEvents($el); self.$list.append($el); @@ -157,6 +164,7 @@ name: $site.find('.site-name').val(), url: $site.find('.site-url').val(), lang: $site.find('.site-lang').val(), + type: $site.find('.site-type').val(), icon: $site.find('.site-icon').val() }; diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 87e69b4e..4ca61e8b 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -45,6 +45,10 @@ public function registerNavigationEntries() { $sites = $sitesManager->getSitesByLanguage($server->getL10NFactory()->findLanguage()); foreach ($sites as $id => $site) { + if ($site['type'] !== SitesManager::LINK && $site['type'] !== SitesManager::SETTING) { + continue; + } + $server->getNavigationManager()->add(function() use ($site, $server) { $url = $server->getURLGenerator(); @@ -58,6 +62,7 @@ public function registerNavigationEntries() { 'order' => 80 + $site['id'], 'href' => $url->linkToRoute('external.page.showPage', ['id'=> $site['id']]), 'icon' => $image, + 'type' => $site['type'], 'name' => $site['name'], ]; }); diff --git a/lib/Controller/APIController.php b/lib/Controller/APIController.php index 3cebc754..a25807a9 100644 --- a/lib/Controller/APIController.php +++ b/lib/Controller/APIController.php @@ -23,6 +23,7 @@ use OCA\External\Exceptions\IconNotFoundException; use OCA\External\Exceptions\InvalidNameException; +use OCA\External\Exceptions\InvalidTypeException; use OCA\External\Exceptions\InvalidURLException; use OCA\External\Exceptions\LanguageNotFoundException; use OCA\External\Exceptions\SiteNotFoundException; @@ -90,10 +91,17 @@ public function getAdmin() { $languages = $this->sitesManager->getAvailableLanguages(); array_unshift($languages, ['code' => '', 'name' => $this->l->t('All languages')]); + $types = [ + ['type' => SitesManager::LINK, 'name' => $this->l->t('Normal')], + ['type' => SitesManager::SETTING, 'name' => $this->l->t('Setting')], + ['type' => SitesManager::QUOTA, 'name' => $this->l->t('Quota')], + ]; + return new DataResponse([ 'sites' => array_values($this->sitesManager->getSites()), 'icons' => $icons, 'languages' => $languages, + 'types' => $types, ]); } @@ -101,18 +109,21 @@ public function getAdmin() { * @param string $name * @param string $url * @param string $lang + * @param string $type * @param string $icon * @return DataResponse */ - public function add($name, $url, $lang, $icon) { + public function add($name, $url, $lang, $type, $icon) { try { - return new DataResponse($this->sitesManager->addSite($name, $url, $lang, $icon)); + return new DataResponse($this->sitesManager->addSite($name, $url, $lang, $type, $icon)); } catch (InvalidNameException $e) { return new DataResponse($this->l->t('The given name is invalid'), Http::STATUS_BAD_REQUEST); } catch (InvalidURLException $e) { return new DataResponse($this->l->t('The given url is invalid'), Http::STATUS_BAD_REQUEST); } catch (LanguageNotFoundException $e) { return new DataResponse($this->l->t('The given language does not exist'), Http::STATUS_BAD_REQUEST); + } catch (InvalidTypeException $e) { + return new DataResponse($this->l->t('The given type is invalid'), Http::STATUS_BAD_REQUEST); } catch (IconNotFoundException $e) { return new DataResponse($this->l->t('The given icon does not exist'), Http::STATUS_BAD_REQUEST); } @@ -123,12 +134,13 @@ public function add($name, $url, $lang, $icon) { * @param string $name * @param string $url * @param string $lang + * @param string $type * @param string $icon * @return DataResponse */ - public function update($id, $name, $url, $lang, $icon) { + public function update($id, $name, $url, $lang, $type, $icon) { try { - return new DataResponse($this->sitesManager->updateSite($id, $name, $url, $lang, $icon)); + return new DataResponse($this->sitesManager->updateSite($id, $name, $url, $lang, $type, $icon)); } catch (SiteNotFoundException $e) { return new DataResponse($this->l->t('The site does not exist'), Http::STATUS_NOT_FOUND); } catch (InvalidNameException $e) { @@ -137,6 +149,8 @@ public function update($id, $name, $url, $lang, $icon) { return new DataResponse($this->l->t('The given url is invalid'), Http::STATUS_BAD_REQUEST); } catch (LanguageNotFoundException $e) { return new DataResponse($this->l->t('The given language does not exist'), Http::STATUS_BAD_REQUEST); + } catch (InvalidTypeException $e) { + return new DataResponse($this->l->t('The given type is invalid'), Http::STATUS_BAD_REQUEST); } catch (IconNotFoundException $e) { return new DataResponse($this->l->t('The given icon does not exist'), Http::STATUS_BAD_REQUEST); } diff --git a/lib/Exceptions/InvalidTypeException.php b/lib/Exceptions/InvalidTypeException.php new file mode 100644 index 00000000..931bd196 --- /dev/null +++ b/lib/Exceptions/InvalidTypeException.php @@ -0,0 +1,24 @@ + + * + * @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 . + * + */ + +namespace OCA\External\Exceptions; + +class InvalidTypeException extends \UnexpectedValueException {} diff --git a/lib/SitesManager.php b/lib/SitesManager.php index 67021a67..782c56f5 100644 --- a/lib/SitesManager.php +++ b/lib/SitesManager.php @@ -23,6 +23,7 @@ use OCA\External\Exceptions\IconNotFoundException; use OCA\External\Exceptions\InvalidNameException; +use OCA\External\Exceptions\InvalidTypeException; use OCA\External\Exceptions\InvalidURLException; use OCA\External\Exceptions\LanguageNotFoundException; use OCA\External\Exceptions\SiteNotFoundException; @@ -33,6 +34,10 @@ class SitesManager { + const LINK = 'link'; + const SETTING = 'settings'; + const QUOTA = 'quota'; + /** @var IConfig */ protected $config; @@ -96,21 +101,40 @@ public function getSites() { return $this->getSitesFromOldConfig($sites); } + $sites = array_map([$this, 'fillSiteArray'], $sites); + return $sites; } + /** + * Adds default values for new attributes of sites + * @param array $site + * @return array + */ + protected function fillSiteArray(array $site) { + return array_merge([ + 'icon' => 'external.svg', + 'lang' => '', + 'type' => self::LINK, + ], + $site + ); + } + /** * @param string $name * @param string $url * @param string $lang + * @param string $type * @param string $icon * @return array * @throws InvalidNameException * @throws InvalidURLException * @throws LanguageNotFoundException + * @throws InvalidTypeException * @throws IconNotFoundException */ - public function addSite($name, $url, $lang, $icon) { + public function addSite($name, $url, $lang, $type, $icon) { $id = 1 + (int) $this->config->getAppValue('external', 'max_site', 0); if ($name === '') { @@ -136,6 +160,10 @@ public function addSite($name, $url, $lang, $icon) { } } + if (!in_array($type, [self::LINK, self::SETTING, self::QUOTA], true)) { + throw new InvalidTypeException(); + } + $icons = $this->getAvailableIcons(); if ($icon === '') { $icon = 'external.svg'; @@ -150,6 +178,7 @@ public function addSite($name, $url, $lang, $icon) { 'name' => $name, 'url' => $url, 'lang' => $lang, + 'type' => $type, 'icon' => $icon, ]; $this->config->setAppValue('external', 'sites', json_encode($sites)); @@ -163,15 +192,17 @@ public function addSite($name, $url, $lang, $icon) { * @param string $name * @param string $url * @param string $lang + * @param string $type * @param string $icon * @return array * @throws SiteNotFoundException * @throws InvalidNameException * @throws InvalidURLException * @throws LanguageNotFoundException + * @throws InvalidTypeException * @throws IconNotFoundException */ - public function updateSite($id, $name, $url, $lang, $icon) { + public function updateSite($id, $name, $url, $lang, $type, $icon) { $sites = $this->getSites(); if (!isset($sites[$id])) { throw new SiteNotFoundException(); @@ -200,6 +231,10 @@ public function updateSite($id, $name, $url, $lang, $icon) { } } + if (!in_array($type, [self::LINK, self::SETTING, self::QUOTA], true)) { + throw new InvalidTypeException(); + } + $icons = $this->getAvailableIcons(); if ($icon === '') { $icon = 'external.svg'; @@ -213,6 +248,7 @@ public function updateSite($id, $name, $url, $lang, $icon) { 'name' => $name, 'url' => $url, 'lang' => $lang, + 'type' => $type, 'icon' => $icon, ]; $this->config->setAppValue('external', 'sites', json_encode($sites)); @@ -242,14 +278,12 @@ protected function getSitesFromOldConfig($sites) { /** @var array[] $sites */ foreach ($sites as $id => $site) { - $fixedSites[$id + 1] = [ + $fixedSites[$id + 1] = $this->fillSiteArray([ 'id' => $id + 1, 'name' => $site[0], 'url' => $site[1], - // TODO when php7+ is supported: 'icon' => $site[2] ?? 'external.svg', 'icon' => isset($site[2]) ? $site[2] : 'external.svg', - 'lang' => '', - ]; + ]); } $this->config->setAppValue('external', 'sites', json_encode($fixedSites)); diff --git a/templates/settings.php b/templates/settings.php index 54088e2b..dfec9c65 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -70,6 +70,15 @@ {{/if}} {{/each}} + From e62b3a86236331c87fb0e9005e953035d7fa1a76 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 22 Mar 2017 17:08:13 +0100 Subject: [PATCH 2/2] Adjust README.md Signed-off-by: Joas Schilling --- README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 745a9ea7..29ee6017 100644 --- a/README.md +++ b/README.md @@ -31,12 +31,24 @@ curl -H "OCS-APIRequest: true" \ Homepage https://localhost/index.php en - external.svg + link + https://localhost/external.svg ``` +#### Explanation + +| Field | Type | Description | +| ----- | ------ | ---------------------------------------- | +| id | int | Numeric identifier of the site | +| name | string | Name of the site, ready to use | +| url | string | URL that should be framed/linked to | +| lang | string | Language code for which this link is valid (empty string means all languages) | +| type | string | Can be one of `link`, `settings` or `quota`; see [this issue](https://github.com/nextcloud/external/issues/7) for details | +| icon | string | Full URL of the icon that should be shown next to the name of the link | + ### Capability The app registers a capability, so clients can check that before making the actual OCS request: