adding and cleaning up services

This commit is contained in:
Hendrik Leppelsack 2015-06-23 12:08:52 +02:00
parent fd60c6d02a
commit 7361d9d77e
9 changed files with 322 additions and 189 deletions

View file

@ -29,6 +29,9 @@ use \OCA\Tasks\Controller\ListsController;
use \OCA\Tasks\Controller\SettingsController;
use \OCA\Tasks\Controller\TasksController;
use \OCA\Tasks\Service\TasksService;
use \OCA\Tasks\Service\ListsService;
use \OCA\Tasks\Service\CollectionsService;
use \OCA\Tasks\Service\SettingsService;
class Application extends App {
@ -43,7 +46,7 @@ class Application extends App {
*/
$container->registerService('PageController', function($c) {
return new PageController(
$c->query('AppName'),
$c->query('AppName'),
$c->query('Request'),
$c->query('UserId')
);
@ -51,37 +54,33 @@ class Application extends App {
$container->registerService('CollectionsController', function($c) {
return new CollectionsController(
$c->query('AppName'),
$c->query('AppName'),
$c->query('Request'),
$c->query('UserId'),
$c->query('L10N'),
$c->query('Settings')
$c->query('CollectionsService')
);
});
$container->registerService('ListsController', function($c) {
return new ListsController(
$c->query('AppName'),
$c->query('AppName'),
$c->query('Request'),
$c->query('UserId')
$c->query('ListsService')
);
});
$container->registerService('SettingsController', function($c) {
return new SettingsController(
$c->query('AppName'),
$c->query('AppName'),
$c->query('Request'),
$c->query('UserId'),
$c->query('Settings')
$c->query('SettingsService')
);
});
$container->registerService('TasksController', function($c) {
return new TasksController(
$c->query('AppName'),
$c->query('AppName'),
$c->query('Request'),
$c->query('TasksService'),
$c->query('UserId')
$c->query('TasksService')
);
});
@ -95,6 +94,28 @@ class Application extends App {
);
});
$container->registerService('ListsService', function($c) {
return new ListsService(
$c->query('UserId')
);
});
$container->registerService('CollectionsService', function($c) {
return new CollectionsService(
$c->query('UserId'),
$c->query('L10N'),
$c->query('Settings'),
$c->query('AppName')
);
});
$container->registerService('SettingsService', function($c) {
return new SettingsService(
$c->query('UserId'),
$c->query('Settings'),
$c->query('AppName')
);
});
/**
* Core

View file

@ -23,86 +23,39 @@
namespace OCA\Tasks\Controller;
use \OCA\Tasks\Service\CollectionsService;
use \OCP\IRequest;
use \OCP\IConfig;
use \OCP\AppFramework\Controller;
use \OCP\AppFramework\Http\JSONResponse;
class CollectionsController extends Controller {
private $userId;
private $l10n;
private $settings;
private $collectionsService;
public function __construct($appName, IRequest $request, $userId, $l10n, IConfig $settings){
public function __construct($appName, IRequest $request, CollectionsService $collectionsService){
parent::__construct($appName, $request);
$this->l10n = $l10n;
$this->userId = $userId;
$this->settings = $settings;
$this->collectionsService = $collectionsService;
}
/**
* @NoAdminRequired
*/
public function getCollections(){
$collections = array(
array(
'id' => "starred",
'displayname' => (string)$this->l10n->t('Important'),
'show' => 2),
array(
'id' => "today",
'displayname' => (string)$this->l10n->t('Today'),
'show' => 2),
array(
'id' => "week",
'displayname' => (string)$this->l10n->t('Week'),
'show' => 2),
array(
'id' => "all",
'displayname' => (string)$this->l10n->t('All'),
'show' => 2),
array(
'id' => "current",
'displayname' => (string)$this->l10n->t('Current'),
'show' => 2),
array(
'id' => "completed",
'displayname' => (string)$this->l10n->t('Completed'),
'show' => 2)
);
foreach ($collections as $key => $collection){
try{
$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;
}
}catch(\Exception $e) {
\OCP\Util::writeLog($this->appName, $e->getMessage(), \OCP\Util::ERROR);
}
}
$result = array(
$result = $this->collectionsService->getAll();
$response = array(
'data' => array(
'collections' => $collections
'collections' => $result
)
);
$response = new JSONResponse();
$response->setData($result);
return $response;
return (new JSONResponse())->setData($response);
}
/**
* @NoAdminRequired
*/
public function setVisibility(){
$collectionId = (string) $this->params('collectionID');
$vis = (int) $this->params('visibility');
if (in_array($vis, array(0,1,2))){
$this->settings->setUserValue($this->userId, $this->appName,'show_'.$collectionId,$vis);
}
$response = new JSONResponse();
return $response;
public function setVisibility($collectionID, $visibility){
$result = $this->collectionsService->setVisibility($collectionID, $visibility);
$response = array();
return (new JSONResponse())->setData($response);
}
}

View file

@ -23,120 +23,63 @@
namespace OCA\Tasks\Controller;
use \OCA\Tasks\Service\ListsService;
use \OCP\IRequest;
use \OCP\AppFramework\Controller;
use \OCP\AppFramework\Http\JSONResponse;
class ListsController extends Controller {
private $userId;
private $listsService;
public function __construct($appName, IRequest $request, $userId){
public function __construct($appName, IRequest $request, ListsService $listsService){
parent::__construct($appName, $request);
$this->userId = $userId;
$this->listsService = $listsService;
}
/**
* @NoAdminRequired
*/
public function getLists(){
$calendar = new \OC_Calendar_Calendar();
$lists = $calendar::allCalendars($this->userId, true);
$result = array(
$result = $this->listsService->getAll();
$response = array(
'data' => array(
'lists' => $lists
'lists' => $result
)
);
$response = new JSONResponse();
$response->setData($result);
return $response;
return (new JSONResponse())->setData($response);
}
/**
* @NoAdminRequired
*/
public function addList(){
$listName = $this->params('name');
if(trim($listName) == '') {
// OCP\JSON::error(array('message'=>'empty'));
exit;
}
$calendars = \OC_Calendar_Calendar::allCalendars($this->userId, true);
foreach($calendars as $cal) {
if($cal['displayname'] == $listName) {
// OCP\JSON::error(array('message'=>'namenotavailable'));
exit;
}
}
$color = '#CCCCCC';
$calendarId = \OC_Calendar_Calendar::addCalendar($this->userId, strip_tags($listName), 'VEVENT,VTODO,VJOURNAL', null, 0, $color);
\OC_Calendar_Calendar::setCalendarActive($calendarId, 1);
$list = \OC_Calendar_Calendar::find($calendarId);
$list['tmpID'] = $this->params('tmpID');
$result = array(
public function addList($name, $tmpID){
$result = $this->listsService->add($name, $tmpID);
$response = array(
'data' => array(
'list' => $list
'list' => $result
)
);
$response = new JSONResponse();
$response->setData($result);
return $response;
return (new JSONResponse())->setData($response);
}
/**
* @NoAdminRequired
*/
public function deleteList(){
$listId = $this->params('listID');
$response = new JSONResponse();
try {
$del = \OC_Calendar_Calendar::deleteCalendar($listId);
if($del == true) {
$result = array(
'data' => array()
);
}else{
$result = array('error'=>'dberror');
}
} catch(\Exception $e) {
$result = array('message'=>$e->getMessage());
}
$response->setData($result);
return $response;
public function deleteList($listID){
$result = $this->listsService->delete($listID);
$response = $result;
return (new JSONResponse())->setData($response);
}
/**
* @NoAdminRequired
*/
public function setListName(){
$listId = (int) $this->params('listID');
$listName = $this->params('name');
$response = new JSONResponse();
if(trim($listName) == '') {
// OCP\JSON::error(array('message'=>'empty'));
exit;
}
$calendars = \OC_Calendar_Calendar::allCalendars($this->userId, true);
foreach($calendars as $cal) {
if($cal['userid'] != $this->userId){
continue;
}
if($cal['displayname'] == $listName && $cal['id'] != $listId) {
// OCP\JSON::error(array('message'=>'namenotavailable'));
exit;
}
}
$color = '#CCCCCC';
\OC_Calendar_Calendar::editCalendar($listId, strip_tags($listName), null, null, null, $color);
$result = array(
'data' => array()
public function setListName($listID, $name){
$result = $this->listsService->setName($listID, $name);
$response = array(
'data' => $result
);
$response->setData($result);
return $response;
return (new JSONResponse())->setData($response);
}
}

View file

@ -23,52 +23,39 @@
namespace OCA\Tasks\Controller;
use \OCA\Tasks\Service\SettingsService;
use \OCP\IRequest;
use \OCP\IConfig;
use \OCP\AppFramework\Controller;
use \OCP\AppFramework\Http\JSONResponse;
class SettingsController extends Controller {
private $userId;
private $settings;
private $settingsService;
public function __construct($appName, IRequest $request, $userId, IConfig $settings){
public function __construct($appName, IRequest $request, SettingsService $settingsService){
parent::__construct($appName, $request);
$this->userId = $userId;
$this->settings = $settings;
$this->settingsService = $settingsService;
}
/**
* @NoAdminRequired
*/
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'),
'userID' => $this->userId,
'categories' => \OC_Calendar_App::getCategoryOptions()
)
);
$result = array(
$result = $this->settingsService->get();
$response = array(
'data' => array(
'settings' => $settings
'settings' => $result
)
);
$response = new JSONResponse();
$response->setData($result);
return $response;
return (new JSONResponse())->setData($response);
}
/**
* @NoAdminRequired
*/
public function set(){
$this->settings->setUserValue($this->userId, $this->appName, $this->params('type').'_'.$this->params('setting'), $this->params('value'));
$response = new JSONResponse();
return $response;
public function set($setting, $type, $value){
$result = $this->settingsService->set($setting, $type, $value);
$response = array();
return (new JSONResponse())->setData($response);
}
}

View file

@ -23,21 +23,19 @@
namespace OCA\Tasks\Controller;
use \OCA\Tasks\Service\TasksService;
use \OCP\IRequest;
use \OCP\AppFramework\Controller;
use \OCP\AppFramework\Http\JSONResponse;
use \OCA\Tasks\Controller\Helper;
class TasksController extends Controller {
private $userId;
private $tasksService;
public function __construct($appName, IRequest $request, TasksService $tasksService, $userId){
public function __construct($appName, IRequest $request, TasksService $tasksService){
parent::__construct($appName, $request);
$this->tasksService = $tasksService;
$this->userId = $userId;
}
/**
@ -165,7 +163,7 @@ class TasksController extends Controller {
* @NoAdminRequired
*/
public function setReminderDate($taskID, $type, $action, $date, $invert, $related = null, $week, $day, $hour, $minute, $second){
$result = $this->tasksService->setReminderDate($taskID, $type, $action, $date, $invert, $related = null, $week, $day, $hour, $minute, $second);
$result = $this->tasksService->setReminderDate($taskID, $type, $action, $date, $invert, $related, $week, $day, $hour, $minute, $second);
$response = array(
'data' => $result
);
@ -209,7 +207,7 @@ class TasksController extends Controller {
* @NoAdminRequired
*/
public function addComment($taskID, $comment, $tmpID){
$result = $this->tasksService->addComment($taskID, $comment);
$result = $this->tasksService->addComment($taskID, $comment, $tmpID);
$response = array(
'data' => array(
'comment' => $result

View file

@ -0,0 +1,80 @@
<?php
namespace OCA\Tasks\Service;
use OCP\IConfig;
class CollectionsService {
private $userId;
private $l10n;
private $settings;
private $appName;
public function __construct($userId, \OC_L10N $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),
array(
'id' => "today",
'displayname' => (string)$this->l10n->t('Today'),
'show' => 2),
array(
'id' => "week",
'displayname' => (string)$this->l10n->t('Week'),
'show' => 2),
array(
'id' => "all",
'displayname' => (string)$this->l10n->t('All'),
'show' => 2),
array(
'id' => "current",
'displayname' => (string)$this->l10n->t('Current'),
'show' => 2),
array(
'id' => "completed",
'displayname' => (string)$this->l10n->t('Completed'),
'show' => 2)
);
foreach ($collections as $key => $collection){
try{
$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;
}
} catch(\Exception $e) {
\OCP\Util::writeLog($this->appName, $e->getMessage(), \OCP\Util::ERROR);
}
}
return $collections;
}
/**
* set the visibility of a list by id
*
* @param int $id
* @param int $visibility
* @return bool
*/
public function setVisibility($id, $visibility){
if (in_array($visibility, array(0,1,2))){
$this->settings->setUserValue($this->userId, $this->appName,'show_'.$id,$visibility);
}
return true;
}
}

104
service/listsservice.php Normal file
View file

@ -0,0 +1,104 @@
<?php
namespace OCA\Tasks\Service;
class ListsService {
private $userId;
public function __construct($userId) {
$this->userId = $userId;
}
/**
* get all lists
*
* @return array
*/
public function getAll() {
$calendar = new \OC_Calendar_Calendar();
$lists = $calendar::allCalendars($this->userId, true);
return $lists;
}
/**
* add a list
*
* @param $name
* @param $tmpID
* @return array
* @throws \Exception
*/
public function add($name, $tmpID) {
if(trim($name) == '') {
// OCP\JSON::error(array('message'=>'empty'));
exit;
}
$calendars = \OC_Calendar_Calendar::allCalendars($this->userId, true);
foreach($calendars as $cal) {
if($cal['displayname'] == $name) {
// OCP\JSON::error(array('message'=>'namenotavailable'));
exit;
}
}
$color = '#CCCCCC';
$calendarId = \OC_Calendar_Calendar::addCalendar($this->userId, strip_tags($name), 'VEVENT,VTODO,VJOURNAL', null, 0, $color);
\OC_Calendar_Calendar::setCalendarActive($calendarId, 1);
$list = \OC_Calendar_Calendar::find($calendarId);
$list['tmpID'] = $tmpID;
return $list;
}
/**
* delete list by id
*
* @param $id
* @return array
*/
public function delete($id) {
try {
$del = \OC_Calendar_Calendar::deleteCalendar($id);
if($del == true) {
return array(
'data' => array()
);
} else {
return array('error'=>'dberror');
}
} catch(\Exception $e) {
return array('message'=>$e->getMessage());
}
}
/**
* set name of list by id
*
* @param $id
* @param $name
* @return array
* @throws \Exception
*/
public function setName($id, $name) {
if(trim($name) == '') {
// OCP\JSON::error(array('message'=>'empty'));
exit;
}
$calendars = \OC_Calendar_Calendar::allCalendars($this->userId, true);
foreach($calendars as $cal) {
if($cal['userid'] != $this->userId){
continue;
}
if($cal['displayname'] == $name && $cal['id'] != $id) {
// OCP\JSON::error(array('message'=>'namenotavailable'));
exit;
}
}
$color = '#CCCCCC';
\OC_Calendar_Calendar::editCalendar($id, strip_tags($name), null, null, null, $color);
return array();
}
}

View file

@ -0,0 +1,48 @@
<?php
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'),
'userID' => $this->userId,
'categories' => \OC_Calendar_App::getCategoryOptions()
)
);
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;
}
}

View file

@ -189,8 +189,8 @@ class TasksService {
public function setCalendarId($taskID, $calendarID) {
try {
$data = \OC_Calendar_App::getEventObject($taskID);
if ($data['calendarid'] != $calendarId) {
return \OC_Calendar_Object::moveToCalendar($taskID, $calendarId);
if ($data['calendarid'] != $calendarID) {
return \OC_Calendar_Object::moveToCalendar($taskID, $calendarID);
} else {
return true;
}
@ -341,7 +341,7 @@ class TasksService {
}
$tv = '';
if ($type == 'DATE-TIME') {
$date = new \DateTime('@'.$this->params('date'));
$date = new \DateTime('@'.$date);
$tv = $date->format('Ymd\THis\Z');
} elseif ($type == 'DURATION') {
// Create duration string
@ -391,13 +391,12 @@ class TasksService {
* @return bool
*/
public function addCategory($taskID, $category){
$taskID = $this->params('taskID');
$category = $this->params('category');
try {
$vcalendar = \OC_Calendar_App::getVCalendar($taskID);
$vtodo = $vcalendar->VTODO;
// fetch categories from TODO
$categories = $vtodo->CATEGORIES;
$taskcategories = array();
if ($categories){
$taskcategories = $categories->getParts();
}
@ -580,4 +579,4 @@ class TasksService {
throw new \Exception('Commment not found.');
}
}
}