From 1127def70309d9d36a49d8c0ec7c8e1e6d2d624c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raimund=20Schl=C3=BC=C3=9Fler?= Date: Sat, 9 Sep 2017 13:54:28 +0200 Subject: Respect new controller structure --- lib/Controller/CollectionsController.php | 57 +++++++++++++++++ lib/Controller/PageController.php | 81 ++++++++++++++++++++++++ lib/Controller/Response.php | 48 ++++++++++++++ lib/Controller/SettingsController.php | 57 +++++++++++++++++ lib/Controller/searchprovider.php | 52 ++++++++++++++++ lib/Service/CollectionsService.php | 104 +++++++++++++++++++++++++++++++ lib/Service/SettingsService.php | 70 +++++++++++++++++++++ 7 files changed, 469 insertions(+) create mode 100644 lib/Controller/CollectionsController.php create mode 100644 lib/Controller/PageController.php create mode 100644 lib/Controller/Response.php create mode 100644 lib/Controller/SettingsController.php create mode 100644 lib/Controller/searchprovider.php create mode 100644 lib/Service/CollectionsService.php create mode 100644 lib/Service/SettingsService.php (limited to 'lib') diff --git a/lib/Controller/CollectionsController.php b/lib/Controller/CollectionsController.php new file mode 100644 index 00000000..e0297a11 --- /dev/null +++ b/lib/Controller/CollectionsController.php @@ -0,0 +1,57 @@ +. + * + */ + +namespace OCA\Tasks\Controller; + +use \OCA\Tasks\Service\CollectionsService; +use \OCP\IRequest; +use \OCP\AppFramework\Controller; + +class CollectionsController extends Controller { + + private $collectionsService; + + use Response; + + public function __construct($appName, IRequest $request, CollectionsService $collectionsService){ + parent::__construct($appName, $request); + $this->collectionsService = $collectionsService; + } + + /** + * @NoAdminRequired + */ + public function getCollections(){ + return $this->generateResponse(function () { + return ['collections' => $this->collectionsService->getAll()]; + }); + } + + /** + * @NoAdminRequired + */ + public function setVisibility($collectionID, $visibility){ + return $this->generateResponse(function () use ($collectionID, $visibility) { + return $this->collectionsService->setVisibility($collectionID, $visibility); + }); + } +} diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php new file mode 100644 index 00000000..88334fec --- /dev/null +++ b/lib/Controller/PageController.php @@ -0,0 +1,81 @@ +. + * + */ + +namespace OCA\Tasks\Controller; + +use \OCP\AppFramework\Controller; +use \OCP\AppFramework\Http\TemplateResponse; +use \OCP\AppFramework\Http\NotFoundResponse; +use \OCP\IRequest; +use \OCP\IConfig; + +/** + * Controller class for main page. + */ +class PageController extends Controller { + + /** + * @param string $appName + * @param IConfig $config + */ + public function __construct($appName, IRequest $request, + $userId, IConfig $config) { + parent::__construct($appName, $request); + $this->config = $config; + $this->userId = $userId; + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function index() { + + $day = new \DateTime('today'); + $day = $day->format('d'); + + $appVersion = $this->config->getAppValue($this->appName, 'installed_version'); + $response = new TemplateResponse('tasks', 'main'); + $response->setParams(array( + 'appVersion' => $appVersion, + 'DOM' => $day + )); + + return $response; + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function templates($template) { + $templates = array( 'confirmation'); + if (in_array($template, $templates)) { + $response = new TemplateResponse('tasks', $template, [], 'blank'); + } else { + $response = new NotFoundResponse(); + } + return $response; + } +} diff --git a/lib/Controller/Response.php b/lib/Controller/Response.php new file mode 100644 index 00000000..c4273a69 --- /dev/null +++ b/lib/Controller/Response.php @@ -0,0 +1,48 @@ +. + * + */ + +namespace OCA\Tasks\Controller; + +use \Closure; +use \OCP\AppFramework\Http\JSONResponse; + +trait Response { + + protected function generateResponse (Closure $callback) { + try { + $message = [ + 'status' => 'success', + 'data' => $callback(), + 'message' => null + ]; + + } catch(\Exception $e) { + $message = [ + 'status' => 'error', + 'data' => null, + 'message' => $e->getMessage() + ]; + } + return new JSONResponse($message); + } + +} diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php new file mode 100644 index 00000000..a7df5aae --- /dev/null +++ b/lib/Controller/SettingsController.php @@ -0,0 +1,57 @@ +. + * + */ + +namespace OCA\Tasks\Controller; + +use \OCA\Tasks\Service\SettingsService; +use \OCP\AppFramework\Controller; +use \OCP\IRequest; + +class SettingsController extends Controller { + + private $settingsService; + + use Response; + + public function __construct($appName, IRequest $request, SettingsService $settingsService){ + parent::__construct($appName, $request); + $this->settingsService = $settingsService; + } + + /** + * @NoAdminRequired + */ + public function get(){ + return $this->generateResponse(function () { + return ['settings' => $this->settingsService->get()]; + }); + } + + /** + * @NoAdminRequired + */ + public function set($setting, $type, $value){ + return $this->generateResponse(function () use ($setting, $type, $value) { + return $this->settingsService->set($setting, $type, $value); + }); + } +} diff --git a/lib/Controller/searchprovider.php b/lib/Controller/searchprovider.php new file mode 100644 index 00000000..f28911c4 --- /dev/null +++ b/lib/Controller/searchprovider.php @@ -0,0 +1,52 @@ +. + * + */ + +namespace OCA\Tasks\Controller; + +use OCA\Tasks\AppInfo\Application; + +/** + * Tasks search provider + */ +class SearchProvider extends \OCP\Search\Provider { + + // private $tasksService; + + public function __construct() { + $app = new Application(); + $container = $app->getContainer(); + $this->app = $app; + // $this->tasksService = $container->query('TasksService'); + } + + + /** + * Search for query in tasks + * + * @param string $query + * @return array + */ + public function search($query) { + return array(); + // return $this->tasksService->search($query); + } +} diff --git a/lib/Service/CollectionsService.php b/lib/Service/CollectionsService.php new file mode 100644 index 00000000..66a16953 --- /dev/null +++ b/lib/Service/CollectionsService.php @@ -0,0 +1,104 @@ + + * + * 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 library 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 library. If not, see . + * + */ + +namespace OCA\Tasks\Service; + +use OCP\IConfig; +use OCP\IL10N; + +class CollectionsService { + + private $userId; + private $l10n; + private $settings; + private $appName; + + public function __construct($userId, IL10N $l10n, IConfig $settings, $appName) { + $this->userId = $userId; + $this->l10n = $l10n; + $this->settings = $settings; + $this->appName = $appName; + } + + /** + * get all collections + * + * @return array + */ + public function getAll() { + $collections = array( + array( + 'id' => "starred", + 'displayname' => (string)$this->l10n->t('Important'), + 'show' => 2, + 'icon' => 'ico-star'), + array( + 'id' => "today", + 'displayname' => (string)$this->l10n->t('Today'), + 'show' => 2, + 'icon' => 'ico-calendar'), + array( + 'id' => "week", + 'displayname' => (string)$this->l10n->t('Week'), + 'show' => 2, + 'icon' => 'ico-calendar'), + array( + 'id' => "all", + 'displayname' => (string)$this->l10n->t('All'), + 'show' => 2, + 'icon' => 'ico-all'), + array( + 'id' => "current", + 'displayname' => (string)$this->l10n->t('Current'), + 'show' => 2, + 'icon' => 'ico-current'), + array( + 'id' => "completed", + 'displayname' => (string)$this->l10n->t('Completed'), + 'show' => 2, + 'icon' => 'ico-checkmark') + ); + foreach ($collections as $key => $collection){ + $tmp = $this->settings->getUserValue($this->userId, $this->appName,'show_'.$collection['id']); + if (!in_array($tmp, array('0','1','2'))) { + $this->settings->setUserValue($this->userId, $this->appName,'show_'.$collection['id'],$collections[$key]['show']); + } else { + $collections[$key]['show'] = (int)$tmp; + } + } + return $collections; + } + + /** + * set the visibility of a collection by collectionID + * + * @param int $collectionID + * @param int $visibility + * @return bool + */ + public function setVisibility($collectionID, $visibility){ + if (in_array($visibility, array(0,1,2))){ + $this->settings->setUserValue($this->userId, $this->appName,'show_'.$collectionID,$visibility); + } + return true; + } +} diff --git a/lib/Service/SettingsService.php b/lib/Service/SettingsService.php new file mode 100644 index 00000000..a0510f89 --- /dev/null +++ b/lib/Service/SettingsService.php @@ -0,0 +1,70 @@ + + * + * 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 library 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 library. If not, see . + * + */ + +namespace OCA\Tasks\Service; + +use OCP\IConfig; + +class SettingsService { + + private $userId; + private $settings; + private $appName; + + public function __construct($userId, IConfig $settings, $appName) { + $this->userId = $userId; + $this->settings = $settings; + $this->appName = $appName; + } + + /** + * get the current settings + * + * @return array + */ + public function get() { + $settings = array( + array( + 'id' => 'various', + 'showHidden' => (int)$this->settings->getUserValue($this->userId, $this->appName,'various_showHidden'), + 'startOfWeek' => (int)$this->settings->getUserValue($this->userId, $this->appName,'various_startOfWeek'), + 'sortOrder' => (string)$this->settings->getUserValue($this->userId, $this->appName,'various_sortOrder'), + 'sortDirection' => (bool)$this->settings->getUserValue($this->userId, $this->appName,'various_sortDirection'), + 'userID' => $this->userId + ) + ); + return $settings; + } + + /** + * set setting of type to new value + * + * @param $setting + * @param $type + * @param $value + * @return bool + */ + public function set($setting, $type, $value) { + $this->settings->setUserValue($this->userId, $this->appName, $type.'_'.$setting, $value); + return true; + } +} -- cgit v1.2.3