From f5b9daa01550a5a46f088ba7af8c4a4efee57c86 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 9 Mar 2017 15:05:01 +0100 Subject: [PATCH 01/12] Settings page via the new system Signed-off-by: Joas Schilling --- appinfo/app.php | 2 -- appinfo/info.xml | 7 +++- lib/Settings/Admin.php | 72 ++++++++++++++++++++++++++++++++++++++++++ settings.php | 12 ------- templates/settings.php | 32 +++++++++++++++++-- 5 files changed, 108 insertions(+), 17 deletions(-) create mode 100644 lib/Settings/Admin.php delete mode 100644 settings.php diff --git a/appinfo/app.php b/appinfo/app.php index b27fa4ef..8557ff5f 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -23,8 +23,6 @@ use OCA\External\External; -OCP\App::registerAdmin('external', 'settings'); - $sites = External::getSites(); if (!empty($sites)) { $urlGenerator = \OC::$server->getURLGenerator(); diff --git a/appinfo/info.xml b/appinfo/info.xml index 77c92018..c795bf72 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -24,9 +24,14 @@ https://github.com/nextcloud/external/issues https://github.com/nextcloud/external.git - 1.2 + 1.2.1 + External + + + OCA\External\Settings\Admin + diff --git a/lib/Settings/Admin.php b/lib/Settings/Admin.php new file mode 100644 index 00000000..9270fc34 --- /dev/null +++ b/lib/Settings/Admin.php @@ -0,0 +1,72 @@ + + * + * @author Joas Schilling + * + * @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\Settings; + +use OCP\App\AppPathNotFoundException; +use OCP\App\IAppManager; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\Settings\ISettings; + +class Admin implements ISettings { + + /** @var IAppManager */ + protected $appManager; + + /** + * @param IAppManager $appManager + */ + public function __construct(IAppManager $appManager) { + $this->appManager = $appManager; + } + + /** + * @return TemplateResponse + * @throws AppPathNotFoundException + */ + public function getForm() { + $images = glob($this->appManager->getAppPath('external') . '/img/*.*'); + + return new TemplateResponse('external', 'settings', [ + 'images' => $images, + ], 'blank'); + } + + /** + * @return string the section ID, e.g. 'sharing' + */ + public function getSection() { + return 'additional'; + } + + /** + * @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 55; + } +} diff --git a/settings.php b/settings.php deleted file mode 100644 index 3b082333..00000000 --- a/settings.php +++ /dev/null @@ -1,12 +0,0 @@ -assign('images', glob(\OC_App::getAppPath('external') . '/img/*.*')); - -return $tmpl->fetchPage(); diff --git a/templates/settings.php b/templates/settings.php index 512a4a67..c9ba82df 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -1,3 +1,31 @@ + + * + * @author Frank Karlitschek + * + * @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 . + * + */ + +script('external', 'admin'); + +/** @var array $_ */ +/** @var \OCP\IL10N $l */ +?>

t('External sites'));?>

@@ -19,7 +47,7 @@ From ac797ec57ddd71cd84b47e2b3741e94d8c94ed91 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 9 Mar 2017 15:37:20 +0100 Subject: [PATCH 02/12] Move displaying to a controller Signed-off-by: Joas Schilling --- appinfo/app.php | 2 +- appinfo/routes.php | 8 +++-- css/style.css | 15 +++++---- index.php | 47 -------------------------- lib/Controller/PageController.php | 56 +++++++++++++++++++++++++++++++ templates/frame.php | 30 +++++++++++++++-- 6 files changed, 99 insertions(+), 59 deletions(-) delete mode 100644 index.php create mode 100644 lib/Controller/PageController.php diff --git a/appinfo/app.php b/appinfo/app.php index 8557ff5f..8b410358 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -32,7 +32,7 @@ return [ 'id' => 'external_index' . ($i + 1), 'order' => 80 + $i, - 'href' => $urlGenerator->linkToRoute('external_index', ['id'=> $i + 1]), + 'href' => $urlGenerator->linkToRoute('external.page.showPage', ['id'=> $i + 1]), 'icon' => $urlGenerator->imagePath('external', !empty($sites[$i][2]) ? $sites[$i][2] : 'external.svg'), 'name' => $sites[$i][0], ]; diff --git a/appinfo/routes.php b/appinfo/routes.php index d6cf34df..2fc33e65 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -6,7 +6,11 @@ */ /** @var $this \OCP\Route\IRouter */ -$this->create('external_index', '/{id}') - ->actionInclude('external/index.php'); $this->create('external_ajax_setsites', 'ajax/setsites.php') ->actionInclude('external/ajax/setsites.php'); + +return [ + 'routes' => [ + ['name' => 'page#showPage', 'url' => '/{id}', 'verb' => 'GET'], + ], +]; diff --git a/css/style.css b/css/style.css index bbf8b2ea..c2af138d 100644 --- a/css/style.css +++ b/css/style.css @@ -1,14 +1,17 @@ -/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- / -/* vim: set shiftwidth=4 tabstop=8 autoindent cindent expandtab: */ - .site_url { - width: 250px; + width: 250px; } .delete_button { - display: none; + display: none; } .external_sites { - width: 470px; + width: 470px; +} + +#ifm { + display: block; + width: 100%; + height: 100%; } diff --git a/index.php b/index.php deleted file mode 100644 index aee54d4d..00000000 --- a/index.php +++ /dev/null @@ -1,47 +0,0 @@ -. - * - */ - - -use OCA\External\External; - -OCP\JSON::checkAppEnabled('external'); -OCP\User::checkLoggedIn(); -OCP\Util::addStyle( 'external', 'style'); - -$id = isset($_GET['id']) ? (int)$_GET['id'] : 1; - -$sites = External::getSites(); -if (sizeof($sites) >= $id) { - $url = $sites[$id - 1][1]; - OCP\App::setActiveNavigationEntry('external_index' . $id); - - $tmpl = new OCP\Template('external', 'frame', 'user'); - //overwrite x-frame-options - header('X-Frame-Options: ALLOW-FROM *'); - - $tmpl->assign('url', $url); - $tmpl->printPage(); -} else { - \OC_Util::redirectToDefaultPage(); -} - diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php new file mode 100644 index 00000000..4818d2d6 --- /dev/null +++ b/lib/Controller/PageController.php @@ -0,0 +1,56 @@ + + * + * @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\Controller; + +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\ContentSecurityPolicy; +use OCP\AppFramework\Http\RedirectResponse; +use OCP\AppFramework\Http\TemplateResponse; + +class PageController extends Controller { + + /** + * @NoAdminRequired + * @NoCSRFRequired + * + * @param int $id + * @return TemplateResponse|RedirectResponse + */ + public function showPage($id) { + + $sites = \OCA\External\External::getSites(); + if (isset($sites[$id - 1])) { + $url = $sites[$id - 1][1]; + \OCP\App::setActiveNavigationEntry('external_index' . $id); + + $response = new TemplateResponse('external', 'frame', [ + 'url' => $url + ], 'user'); + $policy = new ContentSecurityPolicy(); + $policy->addAllowedChildSrcDomain('*'); + $response->setContentSecurityPolicy($policy); + return $response; + } else { + return new RedirectResponse(\OC_Util::getDefaultPageUrl()); + } + } +} diff --git a/templates/frame.php b/templates/frame.php index 212bd9a0..f5c54372 100644 --- a/templates/frame.php +++ b/templates/frame.php @@ -1,5 +1,29 @@ - + + * + * @author Frank Karlitschek + * + * @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 . + * + */ +script('external', 'external'); +style('external', 'style'); - + From ef3f9d7632a1d78e91e1077e2c8f1f136d8cd67c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 10 Mar 2017 12:33:53 +0100 Subject: [PATCH 03/12] Remove static External class and other deprecated stuff Signed-off-by: Joas Schilling --- appinfo/app.php | 17 +++++++-------- lib/Controller/PageController.php | 25 +++++++++++++++++++-- lib/Settings/Admin.php | 16 +++++++++++++- lib/external.php | 36 ------------------------------- templates/settings.php | 19 ++++++++-------- 5 files changed, 55 insertions(+), 58 deletions(-) delete mode 100644 lib/external.php diff --git a/appinfo/app.php b/appinfo/app.php index 8b410358..6d6e25aa 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -1,5 +1,4 @@ getConfig()->getAppValue('external', 'sites', ''); +$sites = json_decode($jsonEncodedList); +if (is_array($sites) && !empty($sites)) { $urlGenerator = \OC::$server->getURLGenerator(); $navigationManager = \OC::$server->getNavigationManager(); - for ($i = 0; $i < sizeof($sites); $i++) { - $navigationEntry = function () use ($i, $urlGenerator, $sites) { + + foreach ($sites as $i => $site) { + $navigationEntry = function () use ($i, $urlGenerator, $site) { return [ 'id' => 'external_index' . ($i + 1), 'order' => 80 + $i, 'href' => $urlGenerator->linkToRoute('external.page.showPage', ['id'=> $i + 1]), - 'icon' => $urlGenerator->imagePath('external', !empty($sites[$i][2]) ? $sites[$i][2] : 'external.svg'), - 'name' => $sites[$i][0], + 'icon' => $urlGenerator->imagePath('external', !empty($site[2]) ? $site[2] : 'external.svg'), + 'name' => $site[0], ]; }; $navigationManager->add($navigationEntry); diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index 4818d2d6..cc7eea51 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -25,9 +25,24 @@ use OCP\AppFramework\Http\ContentSecurityPolicy; use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\TemplateResponse; +use OCP\IConfig; +use OCP\INavigationManager; +use OCP\IRequest; class PageController extends Controller { + /** @var IConfig */ + protected $config; + + /** @var INavigationManager */ + protected $navigationManager; + + public function __construct($appName, IRequest $request, INavigationManager $navigationManager, IConfig $config) { + parent::__construct($appName, $request); + $this->config = $config; + $this->navigationManager = $navigationManager; + } + /** * @NoAdminRequired * @NoCSRFRequired @@ -37,10 +52,10 @@ class PageController extends Controller { */ public function showPage($id) { - $sites = \OCA\External\External::getSites(); + $sites = $this->getSites(); if (isset($sites[$id - 1])) { $url = $sites[$id - 1][1]; - \OCP\App::setActiveNavigationEntry('external_index' . $id); + $this->navigationManager->setActiveEntry('external_index' . $id); $response = new TemplateResponse('external', 'frame', [ 'url' => $url @@ -53,4 +68,10 @@ public function showPage($id) { return new RedirectResponse(\OC_Util::getDefaultPageUrl()); } } + + protected function getSites() { + $jsonEncodedList = $this->config->getAppValue('external', 'sites', ''); + $sites = json_decode($jsonEncodedList); + return !is_array($sites) ? [] : $sites; + } } diff --git a/lib/Settings/Admin.php b/lib/Settings/Admin.php index 9270fc34..6024f82d 100644 --- a/lib/Settings/Admin.php +++ b/lib/Settings/Admin.php @@ -26,6 +26,7 @@ use OCP\App\AppPathNotFoundException; use OCP\App\IAppManager; use OCP\AppFramework\Http\TemplateResponse; +use OCP\IConfig; use OCP\Settings\ISettings; class Admin implements ISettings { @@ -33,11 +34,16 @@ class Admin implements ISettings { /** @var IAppManager */ protected $appManager; + /** @var IConfig */ + protected $config; + /** * @param IAppManager $appManager + * @param IConfig $config */ - public function __construct(IAppManager $appManager) { + public function __construct(IAppManager $appManager, IConfig $config) { $this->appManager = $appManager; + $this->config = $config; } /** @@ -46,9 +52,11 @@ public function __construct(IAppManager $appManager) { */ public function getForm() { $images = glob($this->appManager->getAppPath('external') . '/img/*.*'); + $sites = $this->getSites(); return new TemplateResponse('external', 'settings', [ 'images' => $images, + 'sites' => $sites, ], 'blank'); } @@ -69,4 +77,10 @@ public function getSection() { public function getPriority() { return 55; } + + protected function getSites() { + $jsonEncodedList = $this->config->getAppValue('external', 'sites', ''); + $sites = json_decode($jsonEncodedList); + return !is_array($sites) ? [] : $sites; + } } diff --git a/lib/external.php b/lib/external.php deleted file mode 100644 index 6f2268a9..00000000 --- a/lib/external.php +++ /dev/null @@ -1,36 +0,0 @@ -. - * - */ - -namespace OCA\External; - -class External { - - public static function getSites() { - if (($sites = json_decode(\OCP\Config::getAppValue("external", "sites", ''))) != null) { - return $sites; - } - - return array(); - } - -} diff --git a/templates/settings.php b/templates/settings.php index c9ba82df..0717ce71 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -39,15 +39,14 @@
    - - + + - + '); } - if (empty($sites)) { + if (empty($_['sites'])) { print_unescaped('
  • - +
  • '); } From 323522efd0e7d256d476040918fdb33dc3b43004 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 20 Mar 2017 16:46:28 +0100 Subject: [PATCH 04/12] Add SitesManager Signed-off-by: Joas Schilling --- appinfo/app.php | 42 +++++---------- lib/AppInfo/Application.php | 57 ++++++++++++++++++++ lib/Controller/PageController.php | 29 +++++----- lib/Settings/Admin.php | 20 +++---- lib/SiteNotFoundException.php | 24 +++++++++ lib/SitesManager.php | 89 +++++++++++++++++++++++++++++++ templates/settings.php | 6 +-- 7 files changed, 204 insertions(+), 63 deletions(-) create mode 100644 lib/AppInfo/Application.php create mode 100644 lib/SiteNotFoundException.php create mode 100644 lib/SitesManager.php diff --git a/appinfo/app.php b/appinfo/app.php index 6d6e25aa..fd013498 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -1,41 +1,23 @@ * - * @author Frank Karlitschek - * @copyright 2012 Frank Karlitschek frank@owncloud.org + * @license GNU AGPL version 3 or any later version * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE - * License as published by the Free Software Foundation; either - * version 3 of the License, 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 library is distributed in the hope that it will be useful, + * 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. + * GNU Affero General Public License for more details. * - * You should have received a copy of the GNU Affero General Public - * License along with this library. If not, see . + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . * */ -$jsonEncodedList = \OC::$server->getConfig()->getAppValue('external', 'sites', ''); -$sites = json_decode($jsonEncodedList); -if (is_array($sites) && !empty($sites)) { - $urlGenerator = \OC::$server->getURLGenerator(); - $navigationManager = \OC::$server->getNavigationManager(); - - foreach ($sites as $i => $site) { - $navigationEntry = function () use ($i, $urlGenerator, $site) { - return [ - 'id' => 'external_index' . ($i + 1), - 'order' => 80 + $i, - 'href' => $urlGenerator->linkToRoute('external.page.showPage', ['id'=> $i + 1]), - 'icon' => $urlGenerator->imagePath('external', !empty($site[2]) ? $site[2] : 'external.svg'), - 'name' => $site[0], - ]; - }; - $navigationManager->add($navigationEntry); - } -} +$app = new \OCA\External\AppInfo\Application(); +$app->register(); diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php new file mode 100644 index 00000000..2879d407 --- /dev/null +++ b/lib/AppInfo/Application.php @@ -0,0 +1,57 @@ + + * + * @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\AppInfo; + +use OCA\External\SitesManager; +use OCP\AppFramework\App; + +class Application extends App { + + public function __construct() { + parent::__construct('external'); + } + + public function register() { + $this->registerNavigationEntries(); + } + + public function registerNavigationEntries() { + $server = $this->getContainer()->getServer(); + /** @var SitesManager $sitesManager */ + $sitesManager = $this->getContainer()->query(SitesManager::class); + + $sites = $sitesManager->getSites(); + + foreach ($sites as $id => $site) { + $server->getNavigationManager()->add(function() use ($site, $server) { + $url = $server->getURLGenerator(); + return [ + 'id' => 'external_index' . $site['id'], + 'order' => 80 + $site['id'], + 'href' => $url->linkToRoute('external.page.showPage', ['id'=> $site['id']]), + 'icon' => $url->imagePath('external', $site['icon']), + 'name' => $site['name'], + ]; + }); + } + } +} diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index cc7eea51..39f4626c 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -21,25 +21,26 @@ namespace OCA\External\Controller; +use OCA\External\SiteNotFoundException; +use OCA\External\SitesManager; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\ContentSecurityPolicy; use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\TemplateResponse; -use OCP\IConfig; use OCP\INavigationManager; use OCP\IRequest; class PageController extends Controller { - /** @var IConfig */ - protected $config; + /** @var SitesManager */ + protected $sitesManager; /** @var INavigationManager */ protected $navigationManager; - public function __construct($appName, IRequest $request, INavigationManager $navigationManager, IConfig $config) { + public function __construct($appName, IRequest $request, INavigationManager $navigationManager, SitesManager $sitesManager) { parent::__construct($appName, $request); - $this->config = $config; + $this->sitesManager = $sitesManager; $this->navigationManager = $navigationManager; } @@ -51,27 +52,21 @@ public function __construct($appName, IRequest $request, INavigationManager $nav * @return TemplateResponse|RedirectResponse */ public function showPage($id) { - - $sites = $this->getSites(); - if (isset($sites[$id - 1])) { - $url = $sites[$id - 1][1]; + try { + $site = $this->sitesManager->getSiteById($id); $this->navigationManager->setActiveEntry('external_index' . $id); $response = new TemplateResponse('external', 'frame', [ - 'url' => $url + 'url' => $site['url'], ], 'user'); + $policy = new ContentSecurityPolicy(); $policy->addAllowedChildSrcDomain('*'); $response->setContentSecurityPolicy($policy); + return $response; - } else { + } catch (SiteNotFoundException $e) { return new RedirectResponse(\OC_Util::getDefaultPageUrl()); } } - - protected function getSites() { - $jsonEncodedList = $this->config->getAppValue('external', 'sites', ''); - $sites = json_decode($jsonEncodedList); - return !is_array($sites) ? [] : $sites; - } } diff --git a/lib/Settings/Admin.php b/lib/Settings/Admin.php index 6024f82d..a982079b 100644 --- a/lib/Settings/Admin.php +++ b/lib/Settings/Admin.php @@ -23,10 +23,10 @@ namespace OCA\External\Settings; +use OCA\External\SitesManager; use OCP\App\AppPathNotFoundException; use OCP\App\IAppManager; use OCP\AppFramework\Http\TemplateResponse; -use OCP\IConfig; use OCP\Settings\ISettings; class Admin implements ISettings { @@ -34,16 +34,16 @@ class Admin implements ISettings { /** @var IAppManager */ protected $appManager; - /** @var IConfig */ - protected $config; + /** @var SitesManager */ + protected $sitesManager; /** * @param IAppManager $appManager - * @param IConfig $config + * @param SitesManager $sitesManager */ - public function __construct(IAppManager $appManager, IConfig $config) { + public function __construct(IAppManager $appManager, SitesManager $sitesManager) { $this->appManager = $appManager; - $this->config = $config; + $this->sitesManager = $sitesManager; } /** @@ -52,7 +52,7 @@ public function __construct(IAppManager $appManager, IConfig $config) { */ public function getForm() { $images = glob($this->appManager->getAppPath('external') . '/img/*.*'); - $sites = $this->getSites(); + $sites = $this->sitesManager->getSites(); return new TemplateResponse('external', 'settings', [ 'images' => $images, @@ -77,10 +77,4 @@ public function getSection() { public function getPriority() { return 55; } - - protected function getSites() { - $jsonEncodedList = $this->config->getAppValue('external', 'sites', ''); - $sites = json_decode($jsonEncodedList); - return !is_array($sites) ? [] : $sites; - } } diff --git a/lib/SiteNotFoundException.php b/lib/SiteNotFoundException.php new file mode 100644 index 00000000..4f754234 --- /dev/null +++ b/lib/SiteNotFoundException.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; + +class SiteNotFoundException extends \OutOfBoundsException {} diff --git a/lib/SitesManager.php b/lib/SitesManager.php new file mode 100644 index 00000000..a7be58bc --- /dev/null +++ b/lib/SitesManager.php @@ -0,0 +1,89 @@ + + * + * @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; + +use OCP\IConfig; + +class SitesManager { + + /** @var IConfig */ + protected $config; + + public function __construct(IConfig $config) { + $this->config = $config; + } + + /** + * @param int $id + * @return array + * @throws SiteNotFoundException + */ + public function getSiteById($id) { + $sites = $this->getSites(); + + if (isset($sites[$id])) { + return $sites[$id]; + } + + throw new SiteNotFoundException(); + } + + /** + * @return array[] + */ + public function getSites() { + $jsonEncodedList = $this->config->getAppValue('external', 'sites', ''); + $sites = json_decode($jsonEncodedList, true); + + if (!is_array($sites) || empty($sites)) { + return []; + } + + if (isset($sites[0][0])) { + return $this->getSitesFromOldConfig($sites); + } + + return $sites; + } + + /** + * @param array[] $sites + * @return array[] + */ + protected function getSitesFromOldConfig($sites) { + $fixedSites = []; + + /** @var array[] $sites */ + foreach ($sites as $id => $site) { + $fixedSites[$id + 1] = [ + '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', + ]; + } + + $this->config->setAppValue('external', 'sites', json_encode($fixedSites)); + return $fixedSites; + } +} diff --git a/templates/settings.php b/templates/settings.php index 0717ce71..195936df 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -41,12 +41,12 @@ - - + + - - - - '); - } - if (empty($_['sites'])) { - print_unescaped('
  • - - - - -
  • '); - } +
    - ?> +
      -
    - - " /> + + + +
    From 65fc9427c21e73b1dfb9e157a22f7f7fc1ee5e5c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 21 Mar 2017 08:42:51 +0100 Subject: [PATCH 07/12] Allow limiting links to a certain language Signed-off-by: Joas Schilling --- js/admin.js | 12 +- lib/AppInfo/Application.php | 2 +- lib/Controller/APIController.php | 23 +++- lib/Exceptions/LanguageNotFoundException.php | 24 ++++ lib/Settings/Admin.php | 3 - lib/SitesManager.php | 118 ++++++++++++++++++- templates/settings.php | 11 +- 7 files changed, 175 insertions(+), 18 deletions(-) create mode 100644 lib/Exceptions/LanguageNotFoundException.php diff --git a/js/admin.js b/js/admin.js index aded338c..23e09010 100644 --- a/js/admin.js +++ b/js/admin.js @@ -27,7 +27,7 @@ OCA.External = {}; } - Handlebars.registerHelper('isSelectedIcon', function(currentValue, itemValue) { + Handlebars.registerHelper('isSelected', function(currentValue, itemValue) { return currentValue === itemValue; }); @@ -35,6 +35,10 @@ return OCA.External.App.availableIcons; }); + Handlebars.registerHelper('getLanguages', function() { + return OCA.External.App.availableLanguages; + }); + OCA.External.Models = OCA.External.Models || {}; OCA.External.Models.Site = Backbone.Model.extend({ @@ -51,9 +55,6 @@ url: OC.linkToOCS('apps/external/api/v1', 2) + 'sites', parse: function(response) { - console.log(response); - console.log(response.ocs.data.sites); - console.log(response.ocs.data.icon); return response.ocs.data.sites; } }); @@ -75,6 +76,7 @@ success: function(_, response) { $('#loading_sites').removeClass('icon-loading-small'); self.availableIcons = response.ocs.data.icons; + self.availableLanguages = response.ocs.data.languages; if (response.ocs.data.sites.length === 0) { var $el = $(self._compiledTemplate({ @@ -149,6 +151,7 @@ site.save({ name: $site.find('.site-name').val(), url: $site.find('.site-url').val(), + lang: $site.find('.site-lang').val(), icon: $site.find('.site-icon').val() }, { success: function() { @@ -167,6 +170,7 @@ this._sites.create({ name: $site.find('.site-name').val(), url: $site.find('.site-url').val(), + lang: $site.find('.site-lang').val(), icon: $site.find('.site-icon').val() }, { success: function() { diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 92d6cdea..42c9989e 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -39,7 +39,7 @@ public function registerNavigationEntries() { /** @var SitesManager $sitesManager */ $sitesManager = $this->getContainer()->query(SitesManager::class); - $sites = $sitesManager->getSites(); + $sites = $sitesManager->getSitesByLanguage($server->getL10NFactory()->findLanguage()); foreach ($sites as $id => $site) { $server->getNavigationManager()->add(function() use ($site, $server) { diff --git a/lib/Controller/APIController.php b/lib/Controller/APIController.php index 216b25b7..4651a636 100644 --- a/lib/Controller/APIController.php +++ b/lib/Controller/APIController.php @@ -24,6 +24,7 @@ use OCA\External\Exceptions\IconNotFoundException; use OCA\External\Exceptions\InvalidNameException; use OCA\External\Exceptions\InvalidURLException; +use OCA\External\Exceptions\LanguageNotFoundException; use OCA\External\Exceptions\SiteNotFoundException; use OCA\External\SitesManager; use OCP\AppFramework\Http; @@ -59,7 +60,7 @@ public function __construct($appName, IRequest $request, SitesManager $sitesMana * @return DataResponse */ public function get() { - return new DataResponse(array_values($this->sitesManager->getSites())); + return new DataResponse(array_values($this->sitesManager->getSitesByLanguage($this->l->getLanguageCode()))); } /** @@ -71,27 +72,34 @@ public function getAdmin() { $icons = array_map(function($icon) { return ['icon' => $icon, 'name' => $icon]; }, $this->sitesManager->getAvailableIcons()); - array_unshift($icons, ['icon' => '', 'name' => $this->l->t('Select an icon')]); + + $languages = $this->sitesManager->getAvailableLanguages(); + array_unshift($languages, ['code' => '', 'name' => $this->l->t('All languages')]); + return new DataResponse([ 'sites' => array_values($this->sitesManager->getSites()), 'icons' => $icons, + 'languages' => $languages, ]); } /** * @param string $name * @param string $url + * @param string $lang * @param string $icon * @return DataResponse */ - public function add($name, $url, $icon) { + public function add($name, $url, $lang, $icon) { try { - return new DataResponse($this->sitesManager->addSite($name, $url, $icon)); + return new DataResponse($this->sitesManager->addSite($name, $url, $lang, $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 (IconNotFoundException $e) { return new DataResponse($this->l->t('The given icon does not exist'), Http::STATUS_BAD_REQUEST); } @@ -101,18 +109,21 @@ public function add($name, $url, $icon) { * @param int $id * @param string $name * @param string $url + * @param string $lang * @param string $icon * @return DataResponse */ - public function update($id, $name, $url, $icon) { + public function update($id, $name, $url, $lang, $icon) { try { - return new DataResponse($this->sitesManager->updateSite($id, $name, $url, $icon)); + return new DataResponse($this->sitesManager->updateSite($id, $name, $url, $lang, $icon)); } catch (SiteNotFoundException $e) { return new DataResponse($this->l->t('The site does not exist'), Http::STATUS_NOT_FOUND); } 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 (IconNotFoundException $e) { return new DataResponse($this->l->t('The given icon does not exist'), Http::STATUS_BAD_REQUEST); } diff --git a/lib/Exceptions/LanguageNotFoundException.php b/lib/Exceptions/LanguageNotFoundException.php new file mode 100644 index 00000000..ec9cdc67 --- /dev/null +++ b/lib/Exceptions/LanguageNotFoundException.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 LanguageNotFoundException extends \OutOfBoundsException {} diff --git a/lib/Settings/Admin.php b/lib/Settings/Admin.php index f5d370a0..9f2f2987 100644 --- a/lib/Settings/Admin.php +++ b/lib/Settings/Admin.php @@ -23,8 +23,6 @@ namespace OCA\External\Settings; -use OCA\External\SitesManager; -use OCP\App\AppPathNotFoundException; use OCP\AppFramework\Http\TemplateResponse; use OCP\Settings\ISettings; @@ -32,7 +30,6 @@ class Admin implements ISettings { /** * @return TemplateResponse - * @throws AppPathNotFoundException */ public function getForm() { return new TemplateResponse('external', 'settings', [], 'blank'); diff --git a/lib/SitesManager.php b/lib/SitesManager.php index 102cdcac..67021a67 100644 --- a/lib/SitesManager.php +++ b/lib/SitesManager.php @@ -24,22 +24,28 @@ use OCA\External\Exceptions\IconNotFoundException; use OCA\External\Exceptions\InvalidNameException; use OCA\External\Exceptions\InvalidURLException; +use OCA\External\Exceptions\LanguageNotFoundException; use OCA\External\Exceptions\SiteNotFoundException; use OCP\App\AppPathNotFoundException; use OCP\App\IAppManager; use OCP\IConfig; +use OCP\L10N\IFactory; class SitesManager { /** @var IConfig */ protected $config; + /** @var IFactory */ + protected $languageFactory; + /** @var IAppManager */ protected $appManager; - public function __construct(IConfig $config, IAppManager $appManager) { + public function __construct(IConfig $config, IAppManager $appManager, IFactory $languageFactory) { $this->config = $config; $this->appManager = $appManager; + $this->languageFactory = $languageFactory; } /** @@ -57,6 +63,24 @@ public function getSiteById($id) { throw new SiteNotFoundException(); } + /** + * @param string $lang + * @return array[] + */ + public function getSitesByLanguage($lang) { + $sites = $this->getSites(); + + $langSites = []; + foreach ($sites as $id => $site) { + if ($site['lang'] !== '' && $site['lang'] !== $lang) { + continue; + } + $langSites[$id] = $site; + } + + return $langSites; + } + /** * @return array[] */ @@ -78,13 +102,15 @@ public function getSites() { /** * @param string $name * @param string $url + * @param string $lang * @param string $icon * @return array * @throws InvalidNameException * @throws InvalidURLException + * @throws LanguageNotFoundException * @throws IconNotFoundException */ - public function addSite($name, $url, $icon) { + public function addSite($name, $url, $lang, $icon) { $id = 1 + (int) $this->config->getAppValue('external', 'max_site', 0); if ($name === '') { @@ -96,6 +122,20 @@ public function addSite($name, $url, $icon) { throw new InvalidURLException(); } + if ($lang !== '') { + $valid = false; + foreach ($this->getAvailableLanguages() as $language) { + if ($language['code'] === $lang) { + $valid = true; + break; + } + } + + if (!$valid) { + throw new LanguageNotFoundException(); + } + } + $icons = $this->getAvailableIcons(); if ($icon === '') { $icon = 'external.svg'; @@ -109,6 +149,7 @@ public function addSite($name, $url, $icon) { 'id' => $id, 'name' => $name, 'url' => $url, + 'lang' => $lang, 'icon' => $icon, ]; $this->config->setAppValue('external', 'sites', json_encode($sites)); @@ -121,14 +162,16 @@ public function addSite($name, $url, $icon) { * @param int $id * @param string $name * @param string $url + * @param string $lang * @param string $icon * @return array * @throws SiteNotFoundException * @throws InvalidNameException * @throws InvalidURLException + * @throws LanguageNotFoundException * @throws IconNotFoundException */ - public function updateSite($id, $name, $url, $icon) { + public function updateSite($id, $name, $url, $lang, $icon) { $sites = $this->getSites(); if (!isset($sites[$id])) { throw new SiteNotFoundException(); @@ -143,6 +186,20 @@ public function updateSite($id, $name, $url, $icon) { throw new InvalidURLException(); } + if ($lang !== '') { + $valid = false; + foreach ($this->getAvailableLanguages() as $language) { + if ($language['code'] === $lang) { + $valid = true; + break; + } + } + + if (!$valid) { + throw new LanguageNotFoundException(); + } + } + $icons = $this->getAvailableIcons(); if ($icon === '') { $icon = 'external.svg'; @@ -155,6 +212,7 @@ public function updateSite($id, $name, $url, $icon) { 'id' => $id, 'name' => $name, 'url' => $url, + 'lang' => $lang, 'icon' => $icon, ]; $this->config->setAppValue('external', 'sites', json_encode($sites)); @@ -190,6 +248,7 @@ protected function getSitesFromOldConfig($sites) { 'url' => $site[1], // TODO when php7+ is supported: 'icon' => $site[2] ?? 'external.svg', 'icon' => isset($site[2]) ? $site[2] : 'external.svg', + 'lang' => '', ]; } @@ -208,4 +267,57 @@ public function getAvailableIcons() { return ['external.svg']; } } + + /** + * @return string[] + */ + public function getAvailableLanguages() { + $languageCodes = $this->languageFactory->findAvailableLanguages(); + + $languages = []; + foreach ($languageCodes as $lang) { + $l = $this->languageFactory->get('settings', $lang); + $potentialName = $l->t('__language_name__'); + + $ln = ['code' => $lang, 'name' => $lang]; + if ($l->getLanguageCode() === $lang && strpos($potentialName, '_') !== 0) { + $ln = ['code' => $lang, 'name' => $potentialName]; + } else if ($lang === 'en') { + $ln = ['code' => $lang, 'name' => 'English (US)']; + } + + $languages[] = $ln; + } + + $commonLangCodes = ['en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it', 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko']; + + usort($languages, function ($a, $b) use ($commonLangCodes) { + $aC = array_search($a['code'], $commonLangCodes, true); + $bC = array_search($b['code'], $commonLangCodes, true); + + if ($aC === false && $bC !== false) { + // If a is common, but b is not, list a before b + return 1; + } + if ($aC !== false && $bC === false) { + // If a is common, but b is not, list a before b + return -1; + } + if ($aC !== false && $bC !== false) { + // If a is common, but b is not, list a before b + return $aC - $bC; + } + if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) { + // If a doesn't have a name, but b does, list b before a + return 1; + } + if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) { + // If a does have a name, but b doesn't, list a before b + return -1; + } + // Otherwise compare the names + return strcmp($a['name'], $b['name']); + }); + return $languages; + } } diff --git a/templates/settings.php b/templates/settings.php index 0c9cfe1d..54088e2b 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -54,13 +54,22 @@ + From 88c829d1142d95a3677edad42aa8a81c74233764 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 21 Mar 2017 13:32:23 +0100 Subject: [PATCH 08/12] Add a capability Signed-off-by: Joas Schilling --- lib/AppInfo/Application.php | 3 +++ lib/Capabilities.php | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 lib/Capabilities.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 42c9989e..87e69b4e 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -21,6 +21,7 @@ namespace OCA\External\AppInfo; +use OCA\External\Capabilities; use OCA\External\SitesManager; use OCP\AppFramework\App; @@ -28,6 +29,8 @@ class Application extends App { public function __construct() { parent::__construct('external'); + + $this->getContainer()->registerCapability(Capabilities::class); } public function register() { diff --git a/lib/Capabilities.php b/lib/Capabilities.php new file mode 100644 index 00000000..3f8f65e9 --- /dev/null +++ b/lib/Capabilities.php @@ -0,0 +1,47 @@ + + * + * @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; + +use OCP\Capabilities\ICapability; + +/** + * Class Capabilities + * + * @package OCA\External + */ +class Capabilities implements ICapability { + + /** + * Return this classes capabilities + * + * @return array + */ + public function getCapabilities() { + return [ + 'external' => [ + 'v1' => [ + 'sites', + ], + ], + ]; + } +} From f022100ebb67351abd7431edfa76855d438b6de8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 21 Mar 2017 13:38:46 +0100 Subject: [PATCH 09/12] Increase version and add docs about the new OCS endpoint Signed-off-by: Joas Schilling --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ appinfo/info.xml | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 77f58ec9..745a9ea7 100644 --- a/README.md +++ b/README.md @@ -6,3 +6,51 @@ the name for the external site, an icon appears. When this icon is clicked by a user, the external website appears in the Nextcloud frame. For the user, this external site appears as if it is part of Nextcloud but, in fact, this can be any external URL. + +## OCS API + +It is also possible to get the sites via an OCS endpoint. The request must be authenticated. +Only sites for the user“s language are returned: +```bash +curl -H "OCS-APIRequest: true" \ + https://admin:admin@localhost/ocs/v2.php/apps/external/api/v1 +``` + +### Response +```xml + + + + ok + 200 + OK + + + + 23 + Homepage + https://localhost/index.php + en + external.svg + + + +``` + +### Capability + +The app registers a capability, so clients can check that before making the actual OCS request: +```xml + + + ... + + + ... + + + sites + + + ... +``` diff --git a/appinfo/info.xml b/appinfo/info.xml index c795bf72..6cd37eee 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -24,7 +24,7 @@ https://github.com/nextcloud/external/issues https://github.com/nextcloud/external.git - 1.2.1 + 2.0.0 External From d533c44378dd75f0fe32a460031d98301506081e Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 22 Mar 2017 08:00:11 +0100 Subject: [PATCH 10/12] Set the site-id on create so one can update the site again Signed-off-by: Joas Schilling --- js/admin.js | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/js/admin.js b/js/admin.js index 23e09010..5de37e27 100644 --- a/js/admin.js +++ b/js/admin.js @@ -47,6 +47,16 @@ url: '', icon: '', lang: '' + }, + + parse: function(response) { + if (!_.isUndefined(response.ocs)) { + // Parse of single response from save/create + return response.ocs.data; + } + + // Parse of entry from collection data + return response; } }); @@ -141,19 +151,20 @@ var $target = $(e.target), $site = $target.closest('li'), - site = this._sites.get($site.data('site-id')); + site = this._sites.get($site.data('site-id')), + data = { + name: $site.find('.site-name').val(), + url: $site.find('.site-url').val(), + lang: $site.find('.site-lang').val(), + icon: $site.find('.site-icon').val() + }; $site.find('.failure').addClass('hidden'); $site.find('.saved').addClass('hidden'); $site.find('.saving').removeClass('hidden'); if (!_.isUndefined(site)) { - site.save({ - name: $site.find('.site-name').val(), - url: $site.find('.site-url').val(), - lang: $site.find('.site-lang').val(), - icon: $site.find('.site-icon').val() - }, { + site.save(data, { success: function() { $site.find('.saving').addClass('hidden'); $site.find('.saved').removeClass('hidden'); @@ -167,13 +178,9 @@ } }); } else { - this._sites.create({ - name: $site.find('.site-name').val(), - url: $site.find('.site-url').val(), - lang: $site.find('.site-lang').val(), - icon: $site.find('.site-icon').val() - }, { - success: function() { + this._sites.create(data, { + success: function(site) { + $site.data('site-id', site.get('id')); $site.find('.saving').addClass('hidden'); $site.find('.saved').removeClass('hidden'); setTimeout(function() { From 8ae74db3d19f7b4b1cd48638656652c27818323b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 22 Mar 2017 08:06:05 +0100 Subject: [PATCH 11/12] Select a valid icon Signed-off-by: Joas Schilling --- js/admin.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/js/admin.js b/js/admin.js index 5de37e27..91435b18 100644 --- a/js/admin.js +++ b/js/admin.js @@ -104,7 +104,8 @@ e.preventDefault(); var $el = $(self._compiledTemplate({ - id: 'undefined' + id: 'undefined', + icon: 'external.svg' })); self._attachEvents($el); self.$list.append($el); From 16e0450ecf7cdeb7778c4c2ee2cfe61e3b2d0a11 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 22 Mar 2017 08:13:18 +0100 Subject: [PATCH 12/12] Make the icon URL absolute Signed-off-by: Joas Schilling --- lib/Controller/APIController.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/Controller/APIController.php b/lib/Controller/APIController.php index 4651a636..3cebc754 100644 --- a/lib/Controller/APIController.php +++ b/lib/Controller/APIController.php @@ -32,11 +32,15 @@ use OCP\AppFramework\OCSController; use OCP\IL10N; use OCP\IRequest; +use OCP\IURLGenerator; class APIController extends OCSController { /** @var SitesManager */ private $sitesManager; + /** @var IURLGenerator */ + private $url; + /** @var IL10N */ private $l; @@ -44,12 +48,14 @@ class APIController extends OCSController { * @param string $appName * @param IRequest $request * @param SitesManager $sitesManager + * @param IURLGenerator $url * @param IL10N $l */ - public function __construct($appName, IRequest $request, SitesManager $sitesManager, IL10N $l) { + public function __construct($appName, IRequest $request, SitesManager $sitesManager, IURLGenerator $url, IL10N $l) { parent::__construct($appName, $request); $this->sitesManager = $sitesManager; + $this->url = $url; $this->l = $l; } @@ -60,7 +66,14 @@ public function __construct($appName, IRequest $request, SitesManager $sitesMana * @return DataResponse */ public function get() { - return new DataResponse(array_values($this->sitesManager->getSitesByLanguage($this->l->getLanguageCode()))); + $data = $this->sitesManager->getSitesByLanguage($this->l->getLanguageCode()); + + $sites = []; + foreach ($data as $site) { + $site['icon'] = $this->url->getAbsoluteURL($this->url->imagePath('external', $site['icon'])); + $sites[] = $site; + } + return new DataResponse($sites); } /**