2013-04-19 09:59:30 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Thomas Tanghus
|
|
|
|
* Copyright (c) 2013 Thomas Tanghus (thomas@tanghus.net)
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Contacts\Controller;
|
|
|
|
|
|
|
|
use OCA\Contacts\App;
|
|
|
|
use OCA\Contacts\JSONResponse;
|
2013-04-24 23:36:54 +02:00
|
|
|
use OCA\AppFramework\Controller\Controller as BaseController;
|
2013-04-19 09:59:30 +02:00
|
|
|
use OCA\AppFramework\Core\API;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-04-24 23:36:54 +02:00
|
|
|
* Controller class for groups/categories
|
2013-04-19 09:59:30 +02:00
|
|
|
*/
|
|
|
|
class GroupController extends BaseController {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @IsAdminExemption
|
|
|
|
* @IsSubAdminExemption
|
|
|
|
* @Ajax
|
|
|
|
*/
|
|
|
|
public function getGroups() {
|
2013-04-25 04:21:14 +02:00
|
|
|
$app = new App($this->api->getUserId());
|
|
|
|
$catmgr = new \OC_VCategories('contact', $this->api->getUserId());
|
2013-04-19 09:59:30 +02:00
|
|
|
$categories = $catmgr->categories(\OC_VCategories::FORMAT_MAP);
|
|
|
|
foreach($categories as &$category) {
|
|
|
|
$ids = $catmgr->idsForCategory($category['name']);
|
|
|
|
$category['contacts'] = $ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
$favorites = $catmgr->getFavorites();
|
|
|
|
|
|
|
|
$groups = array(
|
|
|
|
'categories' => $categories,
|
|
|
|
'favorites' => $favorites,
|
2013-04-25 01:03:05 +02:00
|
|
|
'shared' => \OCP\Share::getItemsSharedWith('addressbook', \OCA\Contacts\Share\Addressbook::FORMAT_ADDRESSBOOKS),
|
2013-04-25 04:21:14 +02:00
|
|
|
'lastgroup' => \OCP\Config::getUserValue($this->api->getUserId(), 'contacts', 'lastgroup', 'all'),
|
|
|
|
'sortorder' => \OCP\Config::getUserValue($this->api->getUserId(), 'contacts', 'groupsort', ''),
|
2013-04-19 09:59:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return new JSONResponse($groups);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @IsAdminExemption
|
|
|
|
* @IsSubAdminExemption
|
|
|
|
* @Ajax
|
|
|
|
*/
|
|
|
|
public function addGroup() {
|
|
|
|
$name = $this->request->post['name'];
|
|
|
|
|
|
|
|
$response = new JSONResponse();
|
|
|
|
if(is_null($name) || $name === "") {
|
|
|
|
$response->bailOut(App::$l10n->t('No group name given.'));
|
|
|
|
}
|
|
|
|
|
2013-04-25 04:21:14 +02:00
|
|
|
$catman = new \OC_VCategories('contact', $this->api->getUserId());
|
2013-04-19 09:59:30 +02:00
|
|
|
$id = $catman->add($name);
|
|
|
|
|
|
|
|
if($id === false) {
|
|
|
|
$response->bailOut(App::$l10n->t('Error adding group.'));
|
|
|
|
} else {
|
|
|
|
$response->setParams(array('id'=>$id, 'name' => $name));
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @IsAdminExemption
|
|
|
|
* @IsSubAdminExemption
|
|
|
|
* @Ajax
|
|
|
|
*/
|
|
|
|
public function deleteGroup() {
|
|
|
|
$name = $this->request->post['name'];
|
|
|
|
|
|
|
|
$response = new JSONResponse();
|
2013-05-21 23:39:50 +02:00
|
|
|
if(is_null($name) || $name === '') {
|
2013-04-19 09:59:30 +02:00
|
|
|
$response->bailOut(App::$l10n->t('No group name given.'));
|
2013-05-21 23:39:50 +02:00
|
|
|
return $response;
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
2013-04-25 04:21:14 +02:00
|
|
|
$catman = new \OC_VCategories('contact', $this->api->getUserId());
|
2013-08-03 14:37:05 +02:00
|
|
|
try {
|
|
|
|
$ids = $catman->idsForCategory($name);
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
$response->setErrorMessage($e->getMessage());
|
|
|
|
return $response;
|
|
|
|
}
|
2013-06-27 15:52:18 +02:00
|
|
|
if($ids !== false) {
|
|
|
|
$app = new App($this->api->getUserId());
|
|
|
|
$backend = $app->getBackend('local');
|
|
|
|
foreach($ids as $id) {
|
2013-08-26 20:10:29 +02:00
|
|
|
$contact = $backend->getContact(null, $id, array('noCollection' => true));
|
2013-06-27 15:52:18 +02:00
|
|
|
$obj = \Sabre\VObject\Reader::read(
|
|
|
|
$contact['carddata'],
|
|
|
|
\Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES
|
|
|
|
);
|
|
|
|
if($obj) {
|
|
|
|
if(!isset($obj->CATEGORIES)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if($obj->CATEGORIES->removeGroup($name)) {
|
2013-09-03 19:23:11 +02:00
|
|
|
$backend->updateContact(null, $id, $obj, array('noCollection' => true, 'isBatch' => true));
|
2013-06-27 15:52:18 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', could not parse card ' . $id, \OCP\Util::DEBUG);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-03 14:37:05 +02:00
|
|
|
try {
|
|
|
|
$catman->delete($name);
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
$response->setErrorMessage($e->getMessage());
|
|
|
|
}
|
2013-04-19 09:59:30 +02:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2013-05-21 23:39:50 +02:00
|
|
|
/**
|
|
|
|
* @IsAdminExemption
|
|
|
|
* @IsSubAdminExemption
|
|
|
|
* @Ajax
|
|
|
|
*/
|
|
|
|
public function renameGroup() {
|
|
|
|
$from = $this->request->post['from'];
|
|
|
|
$to = $this->request->post['to'];
|
|
|
|
|
|
|
|
$response = new JSONResponse();
|
|
|
|
if(is_null($from) || $from === '') {
|
|
|
|
$response->bailOut(App::$l10n->t('No group name to rename from given.'));
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
if(is_null($to) || $to === '') {
|
|
|
|
$response->bailOut(App::$l10n->t('No group name to rename to given.'));
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
$catman = new \OC_VCategories('contact', $this->api->getUserId());
|
|
|
|
if(!$catman->rename($from, $to)) {
|
|
|
|
$response->bailOut(App::$l10n->t('Error renaming group.'));
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
$ids = $catman->idsForCategory($to);
|
2013-06-27 15:52:18 +02:00
|
|
|
if($ids !== false) {
|
|
|
|
$app = new App($this->api->getUserId());
|
|
|
|
$backend = $app->getBackend('local');
|
|
|
|
foreach($ids as $id) {
|
2013-09-03 14:01:07 +02:00
|
|
|
$contact = $backend->getContact(null, $id, array('noCollection' => true));
|
2013-06-27 15:52:18 +02:00
|
|
|
$obj = \Sabre\VObject\Reader::read(
|
|
|
|
$contact['carddata'],
|
|
|
|
\Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES
|
|
|
|
);
|
|
|
|
if($obj) {
|
|
|
|
if(!isset($obj->CATEGORIES)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$obj->CATEGORIES->renameGroup($from, $to);
|
2013-09-03 14:01:07 +02:00
|
|
|
$backend->updateContact(null, $id, $obj, array('noCollection' => true));
|
2013-06-27 15:52:18 +02:00
|
|
|
} else {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', could not parse card ' . $id, \OCP\Util::DEBUG);
|
2013-05-21 23:39:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
2013-04-19 09:59:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @IsAdminExemption
|
|
|
|
* @IsSubAdminExemption
|
|
|
|
* @Ajax
|
|
|
|
*/
|
|
|
|
public function addToGroup() {
|
|
|
|
$response = new JSONResponse();
|
2013-05-19 03:16:36 +02:00
|
|
|
$params = $this->request->urlParams;
|
|
|
|
$categoryid = $params['categoryid'];
|
|
|
|
$categoryname = $this->request->post['name'];
|
2013-04-30 22:36:24 +02:00
|
|
|
$ids = $this->request->post['contactids'];
|
|
|
|
$response->debug('request: '.print_r($this->request->post, true));
|
2013-04-19 09:59:30 +02:00
|
|
|
|
|
|
|
if(is_null($categoryid) || $categoryid === '') {
|
|
|
|
$response->bailOut(App::$l10n->t('Group ID missing from request.'));
|
2013-05-22 03:37:56 +02:00
|
|
|
return $response;
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 03:16:36 +02:00
|
|
|
if(is_null($categoryid) || $categoryid === '') {
|
|
|
|
$response->bailOut(App::$l10n->t('Group name missing from request.'));
|
2013-05-22 03:37:56 +02:00
|
|
|
return $response;
|
2013-05-19 03:16:36 +02:00
|
|
|
}
|
|
|
|
|
2013-04-19 09:59:30 +02:00
|
|
|
if(is_null($ids)) {
|
|
|
|
$response->bailOut(App::$l10n->t('Contact ID missing from request.'));
|
2013-05-22 03:37:56 +02:00
|
|
|
return $response;
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 03:16:36 +02:00
|
|
|
$app = new App($this->api->getUserId());
|
|
|
|
$backend = $app->getBackend('local');
|
2013-04-25 04:21:14 +02:00
|
|
|
$catman = new \OC_VCategories('contact', $this->api->getUserId());
|
2013-04-19 09:59:30 +02:00
|
|
|
foreach($ids as $contactid) {
|
2013-09-03 14:01:07 +02:00
|
|
|
$contact = $backend->getContact(null, $contactid, array('noCollection' => true));
|
2013-05-19 03:16:36 +02:00
|
|
|
$obj = \Sabre\VObject\Reader::read(
|
|
|
|
$contact['carddata'],
|
|
|
|
\Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES
|
|
|
|
);
|
|
|
|
if($obj) {
|
|
|
|
if(!isset($obj->CATEGORIES)) {
|
|
|
|
$obj->add('CATEGORIES');
|
|
|
|
}
|
|
|
|
$obj->CATEGORIES->addGroup($categoryname);
|
2013-09-03 14:01:07 +02:00
|
|
|
$backend->updateContact(null, $contactid, $obj, array('noCollection' => true));
|
2013-05-19 03:16:36 +02:00
|
|
|
}
|
2013-04-19 09:59:30 +02:00
|
|
|
$response->debug('contactid: ' . $contactid . ', categoryid: ' . $categoryid);
|
|
|
|
$catman->addToCategory($contactid, $categoryid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @IsAdminExemption
|
|
|
|
* @IsSubAdminExemption
|
|
|
|
* @Ajax
|
|
|
|
*/
|
|
|
|
public function removeFromGroup() {
|
|
|
|
$response = new JSONResponse();
|
2013-05-19 03:16:36 +02:00
|
|
|
$params = $this->request->urlParams;
|
|
|
|
$categoryid = $params['categoryid'];
|
|
|
|
$categoryname = $this->request->post['name'];
|
2013-04-30 22:36:24 +02:00
|
|
|
$ids = $this->request->post['contactids'];
|
2013-05-19 03:16:36 +02:00
|
|
|
//$response->debug('request: '.print_r($this->request->post, true));
|
2013-04-19 09:59:30 +02:00
|
|
|
|
|
|
|
if(is_null($categoryid) || $categoryid === '') {
|
|
|
|
$response->bailOut(App::$l10n->t('Group ID missing from request.'));
|
2013-05-22 03:37:56 +02:00
|
|
|
return $response;
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(is_null($ids)) {
|
|
|
|
$response->bailOut(App::$l10n->t('Contact ID missing from request.'));
|
2013-05-22 03:37:56 +02:00
|
|
|
return $response;
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 03:16:36 +02:00
|
|
|
$app = new App($this->api->getUserId());
|
|
|
|
$backend = $app->getBackend('local');
|
2013-04-25 04:21:14 +02:00
|
|
|
$catman = new \OC_VCategories('contact', $this->api->getUserId());
|
2013-04-19 09:59:30 +02:00
|
|
|
foreach($ids as $contactid) {
|
2013-08-29 17:40:45 +02:00
|
|
|
$contact = $backend->getContact(null, $contactid, array('noCollection' => true));
|
2013-05-25 01:03:45 +02:00
|
|
|
if(!$contact) {
|
|
|
|
$response->debug('Couldn\'t get contact: ' . $contactid);
|
|
|
|
continue;
|
|
|
|
}
|
2013-05-19 03:16:36 +02:00
|
|
|
$obj = \Sabre\VObject\Reader::read(
|
|
|
|
$contact['carddata'],
|
|
|
|
\Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES
|
|
|
|
);
|
|
|
|
if($obj) {
|
|
|
|
if(!isset($obj->CATEGORIES)) {
|
|
|
|
$obj->add('CATEGORIES');
|
|
|
|
}
|
|
|
|
$obj->CATEGORIES->removeGroup($categoryname);
|
2013-08-29 17:40:45 +02:00
|
|
|
$backend->updateContact(null, $contactid, $obj, array('noCollection' => true));
|
2013-05-19 03:16:36 +02:00
|
|
|
} else {
|
|
|
|
$response->debug('Error parsing contact: ' . $contactid);
|
|
|
|
}
|
|
|
|
$response->debug('contactid: ' . $contactid . ', categoryid: ' . $categoryid);
|
|
|
|
$catman->removeFromCategory($contactid, $categoryid);
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|