diff options
author | Joas Schilling <nickvergessen@owncloud.com> | 2015-12-17 18:10:20 +0300 |
---|---|---|
committer | Joas Schilling <nickvergessen@owncloud.com> | 2015-12-17 18:10:20 +0300 |
commit | 21aa1e90d5ffe59bcc15402406f9ffcfa1ca5811 (patch) | |
tree | 09d74acdaf6488a25817ddaa6312d8f6bddd9aee | |
parent | a6dcb4628d0ed2fc51263f3e05597903364350cc (diff) |
Add a notification for the admins to enable the monthly report
-rw-r--r-- | adminnotification.php | 57 | ||||
-rw-r--r-- | appinfo/app.php | 12 | ||||
-rw-r--r-- | appinfo/application.php | 3 | ||||
-rw-r--r-- | appinfo/info.xml | 2 | ||||
-rw-r--r-- | appinfo/install.php | 24 | ||||
-rw-r--r-- | appinfo/update.php | 24 | ||||
-rw-r--r-- | controller/endpointcontroller.php | 20 | ||||
-rw-r--r-- | notifier.php | 70 |
8 files changed, 199 insertions, 13 deletions
diff --git a/adminnotification.php b/adminnotification.php new file mode 100644 index 0000000..3963e36 --- /dev/null +++ b/adminnotification.php @@ -0,0 +1,57 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\PopularityContestClient; + +use OC\BackgroundJob\QueuedJob; + +class AdminNotification extends QueuedJob { + protected function run($argument) { + $manager = \OC::$server->getNotificationManager(); + $urlGenerator = \OC::$server->getURLGenerator(); + + $notification = $manager->createNotification(); + $notification->setApp('popularitycontestclient') + ->setDateTime(new \DateTime()) + ->setSubject('updated') + ->setObject('dummy', 23) + ->setLink($urlGenerator->getAbsoluteURL('index.php/settings/admin#goto-usage-report')); + + $enableAction = $notification->createAction(); + $enableAction->setLabel('enable') + ->setLink($urlGenerator->getAbsoluteURL('ocs/v2.php/apps/popularitycontestclient/api/v1/monthly'), 'POST') + ->setPrimary(true); + $notification->addAction($enableAction); + + $disableAction = $notification->createAction(); + $disableAction->setLabel('disable') + ->setLink($urlGenerator->getAbsoluteURL('ocs/v2.php/apps/popularitycontestclient/api/v1/monthly'), 'DELETE') + ->setPrimary(false); + $notification->addAction($disableAction); + + $adminGroup = \OC::$server->getGroupManager()->get('admin'); + foreach ($adminGroup->getUsers() as $admin) { + $notification->setUser($admin->getUID()); + $manager->notify($notification); + } + + } +} diff --git a/appinfo/app.php b/appinfo/app.php index 989c921..534d3e3 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -21,12 +21,8 @@ \OCP\App::registerAdmin('popularitycontestclient', 'admin'); -if (\OC::$server->getRequest()->getParam('popularitycontestclient')) { - $collector = new \OCA\PopularityContestClient\Collector( - \OC::$server->getConfig(), - \OC::$server->getDatabaseConnection(), - \OC::$server->getIniWrapper(), - \OC::$server->getL10NFactory()->get('popularitycontestclient') +\OC::$server->getNotificationManager()->registerNotifier(function() { + return new \OCA\PopularityContestClient\Notifier( + \OC::$server->getL10NFactory() ); - throw new \OC\HintException(json_encode($collector->getReport(), JSON_PRETTY_PRINT)); -} +}); diff --git a/appinfo/application.php b/appinfo/application.php index 5007cdc..2b784aa 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -39,7 +39,8 @@ class Application extends App { $c->query('AppName'), $server->getRequest(), $c->query('OCA\PopularityContestClient\Collector'), - $server->getJobList() + $server->getJobList(), + $server->getNotificationManager() ); }); diff --git a/appinfo/info.xml b/appinfo/info.xml index 65f1a11..ac8e8a1 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -5,7 +5,7 @@ <description/> <licence>AGPL</licence> <author>Joas Schilling, Björn Schiessle</author> - <version>0.1.0</version> + <version>0.1.5</version> <namespace>PopularityContestClient</namespace> <category>other</category> <dependencies> diff --git a/appinfo/install.php b/appinfo/install.php new file mode 100644 index 0000000..29c6ea6 --- /dev/null +++ b/appinfo/install.php @@ -0,0 +1,24 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +if (!\OC::$server->getJobList()->has('OCA\PopularityContestClient\MonthlyReport', null)) { + \OC::$server->getJobList()->add('OCA\PopularityContestClient\AdminNotification'); +} diff --git a/appinfo/update.php b/appinfo/update.php new file mode 100644 index 0000000..29c6ea6 --- /dev/null +++ b/appinfo/update.php @@ -0,0 +1,24 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +if (!\OC::$server->getJobList()->has('OCA\PopularityContestClient\MonthlyReport', null)) { + \OC::$server->getJobList()->add('OCA\PopularityContestClient\AdminNotification'); +} diff --git a/controller/endpointcontroller.php b/controller/endpointcontroller.php index 8b5795c..4188ccb 100644 --- a/controller/endpointcontroller.php +++ b/controller/endpointcontroller.php @@ -21,12 +21,11 @@ namespace OCA\PopularityContestClient\Controller; +use OC\Notification\IManager; use OCA\PopularityContestClient\Collector; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\BackgroundJob\IJobList; -use OCP\Http\Client\IClientService; -use OCP\IConfig; use OCP\IRequest; class EndpointController extends Controller { @@ -37,17 +36,22 @@ class EndpointController extends Controller { /** @var IJobList */ protected $jobList; + /** @var IManager */ + protected $manager; + /** * @param string $appName * @param IRequest $request * @param Collector $collector * @param IJobList $jobList + * @param IManager $manager */ - public function __construct($appName, IRequest $request, Collector $collector, IJobList $jobList) { + public function __construct($appName, IRequest $request, Collector $collector, IJobList $jobList, IManager $manager) { parent::__construct($appName, $request); $this->collector = $collector; $this->jobList = $jobList; + $this->manager = $manager; } /** @@ -55,6 +59,11 @@ class EndpointController extends Controller { */ public function enableMonthly() { $this->jobList->add('OCA\PopularityContestClient\MonthlyReport'); + + $notification = $this->manager->createNotification(); + $notification->setApp('popularitycontestclient'); + $this->manager->markProcessed($notification); + return new \OC_OCS_Result(); } @@ -63,6 +72,11 @@ class EndpointController extends Controller { */ public function disableMonthly() { $this->jobList->remove('OCA\PopularityContestClient\MonthlyReport'); + + $notification = $this->manager->createNotification(); + $notification->setApp('popularitycontestclient'); + $this->manager->markProcessed($notification); + return new \OC_OCS_Result(); } diff --git a/notifier.php b/notifier.php new file mode 100644 index 0000000..5ea5175 --- /dev/null +++ b/notifier.php @@ -0,0 +1,70 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\PopularityContestClient; + +use OC\Notification\INotification; +use OC\Notification\INotifier; +use OCP\L10N\IFactory; + +class Notifier implements INotifier { + + /** @var IFactory */ + protected $l10nFactory; + + /** + * Notifier constructor. + * + * @param IFactory $l10nFactory + */ + public function __construct(IFactory $l10nFactory) { + $this->l10nFactory = $l10nFactory; + } + + /** + * @param INotification $notification + * @param string $languageCode The code of the language that should be used to prepare the notification + * @return INotification + * @throws \InvalidArgumentException When the notification was not prepared by a notifier + */ + public function prepare(INotification $notification, $languageCode) { + if ($notification->getApp() !== 'popularitycontestclient') { + // Not my app => throw + throw new \InvalidArgumentException(); + } + + // Read the language from the notification + $l = $this->l10nFactory->get('popularitycontestclient', $languageCode); + + $notification->setParsedSubject((string) $l->t('Do you want to send monthly usage statistics to ownCloud?')); + + foreach ($notification->getActions() as $action) { + if ($action->getLabel() === 'enable') { + $action->setParsedLabel((string) $l->t('Yes')); + } else if ($action->getLabel() === 'disable') { + $action->setParsedLabel((string) $l->t('Not now')); + } + $notification->addParsedAction($action); + } + + return $notification; + } +} |