2013-04-19 09:59:30 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Thomas Tanghus
|
2014-01-26 00:40:22 +01:00
|
|
|
* @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net)
|
|
|
|
*
|
2013-04-19 09:59:30 +02:00
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Contacts\Controller;
|
|
|
|
|
2013-09-17 18:46:59 +02:00
|
|
|
use OCA\Contacts\App,
|
|
|
|
OCA\Contacts\JSONResponse,
|
2014-03-25 01:15:57 +01:00
|
|
|
OCA\Contacts\Controller,
|
2014-04-07 22:29:15 +02:00
|
|
|
OCP\AppFramework\Http,
|
|
|
|
OCP\ITags,
|
|
|
|
OCP\IRequest;
|
2013-04-19 09:59:30 +02:00
|
|
|
|
|
|
|
/**
|
2013-04-24 23:36:54 +02:00
|
|
|
* Controller class for groups/categories
|
2013-04-19 09:59:30 +02:00
|
|
|
*/
|
2013-09-17 18:46:59 +02:00
|
|
|
class GroupController extends Controller {
|
2013-04-19 09:59:30 +02:00
|
|
|
|
2014-04-07 22:29:15 +02:00
|
|
|
public function __construct($appName, IRequest $request, App $app, ITags $tags) {
|
|
|
|
parent::__construct($appName, $request, $app);
|
|
|
|
$this->app = $app;
|
2014-08-19 14:24:17 +02:00
|
|
|
$this->tags = $tags;
|
2014-04-07 22:29:15 +02:00
|
|
|
}
|
|
|
|
|
2013-04-19 09:59:30 +02:00
|
|
|
/**
|
2013-09-24 13:39:45 +02:00
|
|
|
* @NoAdminRequired
|
2013-04-19 09:59:30 +02:00
|
|
|
*/
|
|
|
|
public function getGroups() {
|
2014-08-19 14:24:17 +02:00
|
|
|
$tags = $this->tags->getTags();
|
2014-03-08 16:22:51 +01:00
|
|
|
|
|
|
|
foreach ($tags as &$tag) {
|
2013-09-24 13:39:45 +02:00
|
|
|
try {
|
2014-08-19 14:24:17 +02:00
|
|
|
$ids = $this->tags->getIdsForTag($tag['name']);
|
2014-01-25 03:42:03 +01:00
|
|
|
$tag['contacts'] = $ids;
|
2014-10-03 13:13:36 +02:00
|
|
|
$tag['displayname'] = $this->displayName($tag);
|
2013-09-24 13:39:45 +02:00
|
|
|
} catch(\Exception $e) {
|
2014-08-09 16:48:30 +02:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__ . ', ' . $e->getMessage(), \OCP\Util::ERROR);
|
2013-09-24 13:39:45 +02:00
|
|
|
}
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
2014-08-19 14:24:17 +02:00
|
|
|
$favorites = $this->tags->getFavorites();
|
2013-04-19 09:59:30 +02:00
|
|
|
|
|
|
|
$groups = array(
|
2013-09-24 13:39:45 +02:00
|
|
|
'categories' => $tags,
|
2013-04-19 09:59:30 +02:00
|
|
|
'favorites' => $favorites,
|
2013-04-25 01:03:05 +02:00
|
|
|
'shared' => \OCP\Share::getItemsSharedWith('addressbook', \OCA\Contacts\Share\Addressbook::FORMAT_ADDRESSBOOKS),
|
2014-04-07 22:29:15 +02:00
|
|
|
'lastgroup' => \OCP\Config::getUserValue(\OCP\User::getUser(), 'contacts', 'lastgroup', 'all'),
|
|
|
|
'sortorder' => \OCP\Config::getUserValue(\OCP\User::getUser(), 'contacts', 'groupsort', ''),
|
2013-04-19 09:59:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return new JSONResponse($groups);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-24 13:39:45 +02:00
|
|
|
* @NoAdminRequired
|
2013-04-19 09:59:30 +02:00
|
|
|
*/
|
|
|
|
public function addGroup() {
|
|
|
|
$name = $this->request->post['name'];
|
|
|
|
|
|
|
|
$response = new JSONResponse();
|
2014-03-08 16:22:51 +01:00
|
|
|
|
|
|
|
if (is_null($name) || $name === "") {
|
2013-04-19 09:59:30 +02:00
|
|
|
$response->bailOut(App::$l10n->t('No group name given.'));
|
|
|
|
}
|
|
|
|
|
2014-08-19 14:24:17 +02:00
|
|
|
$id = $this->tags->add($name);
|
2013-04-19 09:59:30 +02:00
|
|
|
|
2014-03-08 16:22:51 +01:00
|
|
|
if ($id === false) {
|
2013-04-19 09:59:30 +02:00
|
|
|
$response->bailOut(App::$l10n->t('Error adding group.'));
|
|
|
|
} else {
|
|
|
|
$response->setParams(array('id'=>$id, 'name' => $name));
|
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
2013-04-19 09:59:30 +02:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-24 13:39:45 +02:00
|
|
|
* @NoAdminRequired
|
2013-04-19 09:59:30 +02:00
|
|
|
*/
|
|
|
|
public function deleteGroup() {
|
|
|
|
$name = $this->request->post['name'];
|
|
|
|
|
|
|
|
$response = new JSONResponse();
|
2014-03-08 16:22:51 +01: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-08-03 14:37:05 +02:00
|
|
|
try {
|
2014-08-19 14:24:17 +02:00
|
|
|
$ids = $this->tags->getIdsForTag($name);
|
2013-08-03 14:37:05 +02:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
$response->setErrorMessage($e->getMessage());
|
2014-02-05 22:48:29 +01:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', ' . $e->getMessage(), \OCP\Util::ERROR);
|
2013-08-03 14:37:05 +02:00
|
|
|
return $response;
|
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
|
|
|
if ($ids !== false) {
|
|
|
|
|
2013-09-24 13:39:45 +02:00
|
|
|
$backend = $this->app->getBackend('local');
|
2014-03-08 16:22:51 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
);
|
2014-03-08 16:22:51 +01:00
|
|
|
|
|
|
|
if ($obj) {
|
|
|
|
|
|
|
|
if (!$obj->inGroup($name)) {
|
2013-06-27 15:52:18 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
|
|
|
if ($obj->removeFromGroup($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
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
2013-06-27 15:52:18 +02:00
|
|
|
} else {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', could not parse card ' . $id, \OCP\Util::DEBUG);
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
2013-06-27 15:52:18 +02:00
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
2013-08-03 14:37:05 +02:00
|
|
|
try {
|
2014-08-19 14:24:17 +02:00
|
|
|
$this->tags->delete($name);
|
2013-08-03 14:37:05 +02:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
$response->setErrorMessage($e->getMessage());
|
2014-02-05 22:48:29 +01:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', ' . $e->getMessage(), \OCP\Util::ERROR);
|
2013-08-03 14:37:05 +02:00
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
2013-04-19 09:59:30 +02:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2013-05-21 23:39:50 +02:00
|
|
|
/**
|
2013-09-24 13:39:45 +02:00
|
|
|
* @NoAdminRequired
|
2013-05-21 23:39:50 +02:00
|
|
|
*/
|
|
|
|
public function renameGroup() {
|
|
|
|
$from = $this->request->post['from'];
|
|
|
|
$to = $this->request->post['to'];
|
|
|
|
|
|
|
|
$response = new JSONResponse();
|
2014-03-08 16:22:51 +01:00
|
|
|
|
|
|
|
if (is_null($from) || $from === '') {
|
2013-05-21 23:39:50 +02:00
|
|
|
$response->bailOut(App::$l10n->t('No group name to rename from given.'));
|
|
|
|
return $response;
|
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
|
|
|
if (is_null($to) || $to === '') {
|
2013-05-21 23:39:50 +02:00
|
|
|
$response->bailOut(App::$l10n->t('No group name to rename to given.'));
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2014-08-19 14:24:17 +02:00
|
|
|
if (!$this->tags->rename($from, $to)) {
|
2013-05-21 23:39:50 +02:00
|
|
|
$response->bailOut(App::$l10n->t('Error renaming group.'));
|
|
|
|
return $response;
|
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
2014-08-19 14:24:17 +02:00
|
|
|
$ids = $this->tags->getIdsForTag($to);
|
2014-03-08 16:22:51 +01:00
|
|
|
|
|
|
|
if ($ids !== false) {
|
|
|
|
|
2013-09-24 13:39:45 +02:00
|
|
|
$backend = $this->app->getBackend('local');
|
2014-03-08 16:22:51 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
);
|
2014-03-08 16:22:51 +01:00
|
|
|
|
|
|
|
if ($obj) {
|
|
|
|
|
|
|
|
if (!isset($obj->CATEGORIES)) {
|
2013-06-27 15:52:18 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
2013-06-27 15:52:18 +02:00
|
|
|
$obj->CATEGORIES->renameGroup($from, $to);
|
2013-09-03 14:01:07 +02:00
|
|
|
$backend->updateContact(null, $id, $obj, array('noCollection' => true));
|
2014-03-08 16:22:51 +01:00
|
|
|
|
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
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
2013-05-21 23:39:50 +02:00
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
2013-05-21 23:39:50 +02:00
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
2013-05-21 23:39:50 +02:00
|
|
|
return $response;
|
|
|
|
}
|
2013-04-19 09:59:30 +02:00
|
|
|
|
|
|
|
/**
|
2013-09-24 13:39:45 +02:00
|
|
|
* @NoAdminRequired
|
2013-04-19 09:59:30 +02:00
|
|
|
*/
|
|
|
|
public function addToGroup() {
|
|
|
|
$response = new JSONResponse();
|
2013-05-19 03:16:36 +02:00
|
|
|
$params = $this->request->urlParams;
|
2013-10-03 04:11:54 +02:00
|
|
|
$categoryId = $params['categoryId'];
|
2014-03-25 01:15:57 +01:00
|
|
|
$categoryName = $this->request->post['name'];
|
2013-10-03 04:11:54 +02:00
|
|
|
$ids = $this->request->post['contactIds'];
|
2013-04-30 22:36:24 +02:00
|
|
|
$response->debug('request: '.print_r($this->request->post, true));
|
2013-04-19 09:59:30 +02:00
|
|
|
|
2014-03-08 16:22:51 +01:00
|
|
|
if (is_null($categoryId) || $categoryId === '') {
|
2014-03-25 01:15:57 +01:00
|
|
|
throw new \Exception(
|
|
|
|
App::$l10n->t('Group ID missing from request.'),
|
|
|
|
Http::STATUS_PRECONDITION_FAILED
|
|
|
|
);
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
2014-03-25 01:15:57 +01:00
|
|
|
if (is_null($categoryName) || $categoryName === '') {
|
|
|
|
throw new \Exception(
|
|
|
|
App::$l10n->t('Group name missing from request.'),
|
|
|
|
Http::STATUS_PRECONDITION_FAILED
|
|
|
|
);
|
2013-05-19 03:16:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-08 16:22:51 +01:00
|
|
|
if (is_null($ids)) {
|
2014-03-25 01:15:57 +01:00
|
|
|
throw new \Exception(
|
|
|
|
App::$l10n->t('Contact ID missing from request.'),
|
|
|
|
Http::STATUS_PRECONDITION_FAILED
|
|
|
|
);
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
2013-09-24 13:39:45 +02:00
|
|
|
$backend = $this->app->getBackend('local');
|
2014-03-08 16:22:51 +01:00
|
|
|
|
|
|
|
foreach ($ids as $contactId) {
|
|
|
|
|
2013-10-03 04:11:54 +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
|
|
|
|
);
|
2014-03-08 16:22:51 +01:00
|
|
|
|
|
|
|
if ($obj) {
|
|
|
|
|
2014-03-25 01:15:57 +01:00
|
|
|
if ($obj->addToGroup($categoryName)) {
|
2014-01-25 17:08:01 +01:00
|
|
|
$backend->updateContact(null, $contactId, $obj, array('noCollection' => true));
|
2013-05-19 03:16:36 +02:00
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
2013-05-19 03:16:36 +02:00
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
2013-10-03 04:11:54 +02:00
|
|
|
$response->debug('contactId: ' . $contactId . ', categoryId: ' . $categoryId);
|
2014-08-19 14:24:17 +02:00
|
|
|
$this->tags->tagAs($contactId, $categoryId);
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-24 13:39:45 +02:00
|
|
|
* @NoAdminRequired
|
2013-04-19 09:59:30 +02:00
|
|
|
*/
|
|
|
|
public function removeFromGroup() {
|
|
|
|
$response = new JSONResponse();
|
2013-05-19 03:16:36 +02:00
|
|
|
$params = $this->request->urlParams;
|
2013-10-03 04:11:54 +02:00
|
|
|
$categoryId = $params['categoryId'];
|
2014-08-17 01:01:27 +02:00
|
|
|
$categoryName = $this->request->post['name'];
|
2013-10-03 04:11:54 +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
|
|
|
|
2014-03-08 16:22:51 +01:00
|
|
|
if (is_null($categoryId) || $categoryId === '') {
|
2014-03-25 01:15:57 +01:00
|
|
|
throw new \Exception(
|
|
|
|
App::$l10n->t('Group ID missing from request.'),
|
|
|
|
Http::STATUS_PRECONDITION_FAILED
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($categoryName) || $categoryName === '') {
|
|
|
|
throw new \Exception(
|
|
|
|
App::$l10n->t('Group name missing from request.'),
|
|
|
|
Http::STATUS_PRECONDITION_FAILED
|
|
|
|
);
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
2014-03-08 16:22:51 +01:00
|
|
|
if (is_null($ids)) {
|
2014-03-25 01:15:57 +01:00
|
|
|
throw new \Exception(
|
|
|
|
App::$l10n->t('Contact ID missing from request.'),
|
|
|
|
Http::STATUS_PRECONDITION_FAILED
|
|
|
|
);
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
2013-09-24 13:39:45 +02:00
|
|
|
$backend = $this->app->getBackend('local');
|
2014-03-08 16:22:51 +01:00
|
|
|
|
|
|
|
foreach ($ids as $contactId) {
|
|
|
|
|
2013-10-03 04:11:54 +02:00
|
|
|
$contact = $backend->getContact(null, $contactId, array('noCollection' => true));
|
2014-03-08 16:22:51 +01:00
|
|
|
|
|
|
|
if (!$contact) {
|
2013-10-03 04:11:54 +02:00
|
|
|
$response->debug('Couldn\'t get contact: ' . $contactId);
|
2013-05-25 01:03:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
2013-05-19 03:16:36 +02:00
|
|
|
$obj = \Sabre\VObject\Reader::read(
|
|
|
|
$contact['carddata'],
|
|
|
|
\Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES
|
|
|
|
);
|
2014-03-08 16:22:51 +01:00
|
|
|
|
|
|
|
if ($obj) {
|
|
|
|
|
|
|
|
if (!isset($obj->CATEGORIES)) {
|
2014-01-25 17:08:01 +01:00
|
|
|
return $response;
|
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
2014-08-17 01:01:27 +02:00
|
|
|
if ($obj->removeFromGroup($categoryName)) {
|
2014-01-25 17:08:01 +01:00
|
|
|
$backend->updateContact(null, $contactId, $obj, array('noCollection' => true));
|
2013-05-19 03:16:36 +02:00
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
2013-05-19 03:16:36 +02:00
|
|
|
} else {
|
2013-10-03 04:11:54 +02:00
|
|
|
$response->debug('Error parsing contact: ' . $contactId);
|
2013-05-19 03:16:36 +02:00
|
|
|
}
|
2014-03-08 16:22:51 +01:00
|
|
|
|
2013-10-03 04:11:54 +02:00
|
|
|
$response->debug('contactId: ' . $contactId . ', categoryId: ' . $categoryId);
|
2014-08-19 14:24:17 +02:00
|
|
|
$this->tags->unTag($contactId, $categoryId);
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2014-10-03 13:13:36 +02:00
|
|
|
/**
|
|
|
|
* Returns a tag's name as it should be displayed.
|
|
|
|
*
|
|
|
|
* @param Tag
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* If the tag belongs to the current user, simply returns the tag's name.
|
|
|
|
* Otherwise, the tag's name is returned with it's owner's name appended
|
|
|
|
* in parentheses, like "Tag (owner)".
|
|
|
|
*/
|
|
|
|
private function displayName($tag) {
|
|
|
|
if ($tag['owner'] != \OCP\User::getUser()) {
|
|
|
|
return $tag['name'] . ' ('. $tag['owner'] . ')';
|
|
|
|
}
|
|
|
|
return $tag['name'];
|
|
|
|
}
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|