Blame view
sources/apps/contacts/lib/controller/groupcontroller.php
7.64 KB
|
d1bafeea1
|
1 2 3 |
<?php /** * @author Thomas Tanghus |
|
6d9380f96
|
4 5 |
* @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net) * |
|
d1bafeea1
|
6 7 8 9 10 11 12 13 14 |
* 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, OCA\Contacts\JSONResponse, |
|
6d9380f96
|
15 16 17 18 |
OCA\Contacts\Controller, OCP\AppFramework\Http, OCP\ITags, OCP\IRequest; |
|
d1bafeea1
|
19 20 21 22 23 |
/**
* Controller class for groups/categories
*/
class GroupController extends Controller {
|
|
6d9380f96
|
24 25 26 27 28 |
public function __construct($appName, IRequest $request, App $app, ITags $tags) {
parent::__construct($appName, $request, $app);
$this->app = $app;
$this->tagMgr = $tags;
}
|
|
d1bafeea1
|
29 30 31 32 |
/**
* @NoAdminRequired
*/
public function getGroups() {
|
|
6d9380f96
|
33 34 35 |
$tags = $this->tagMgr->getTags();
foreach ($tags as &$tag) {
|
|
d1bafeea1
|
36 |
try {
|
|
6d9380f96
|
37 38 |
$ids = $this->tagMgr->getIdsForTag($tag['name']); $tag['contacts'] = $ids; |
|
d1bafeea1
|
39 40 41 42 |
} catch(\Exception $e) {
$this->api->log(__METHOD__ . ' ' . $e->getMessage());
}
}
|
|
6d9380f96
|
43 |
$favorites = $this->tagMgr->getFavorites(); |
|
d1bafeea1
|
44 45 46 47 48 |
$groups = array(
'categories' => $tags,
'favorites' => $favorites,
'shared' => \OCP\Share::getItemsSharedWith('addressbook', \OCA\Contacts\Share\Addressbook::FORMAT_ADDRESSBOOKS),
|
|
6d9380f96
|
49 50 |
'lastgroup' => \OCP\Config::getUserValue(\OCP\User::getUser(), 'contacts', 'lastgroup', 'all'), 'sortorder' => \OCP\Config::getUserValue(\OCP\User::getUser(), 'contacts', 'groupsort', ''), |
|
d1bafeea1
|
51 52 53 54 55 56 57 58 59 60 61 62 |
);
return new JSONResponse($groups);
}
/**
* @NoAdminRequired
*/
public function addGroup() {
$name = $this->request->post['name'];
$response = new JSONResponse();
|
|
6d9380f96
|
63 64 |
if (is_null($name) || $name === "") {
|
|
d1bafeea1
|
65 66 |
$response->bailOut(App::$l10n->t('No group name given.'));
}
|
|
6d9380f96
|
67 |
$id = $this->tagMgr->add($name); |
|
d1bafeea1
|
68 |
|
|
6d9380f96
|
69 |
if ($id === false) {
|
|
d1bafeea1
|
70 71 72 73 |
$response->bailOut(App::$l10n->t('Error adding group.'));
} else {
$response->setParams(array('id'=>$id, 'name' => $name));
}
|
|
6d9380f96
|
74 |
|
|
d1bafeea1
|
75 76 77 78 79 80 81 82 83 84 |
return $response;
}
/**
* @NoAdminRequired
*/
public function deleteGroup() {
$name = $this->request->post['name'];
$response = new JSONResponse();
|
|
6d9380f96
|
85 |
if (is_null($name) || $name === '') {
|
|
d1bafeea1
|
86 87 88 |
$response->bailOut(App::$l10n->t('No group name given.'));
return $response;
}
|
|
d1bafeea1
|
89 |
try {
|
|
6d9380f96
|
90 |
$ids = $this->tagMgr->getIdsForTag($name); |
|
d1bafeea1
|
91 92 |
} catch(\Exception $e) {
$response->setErrorMessage($e->getMessage());
|
|
6d9380f96
|
93 |
\OCP\Util::writeLog('contacts', __METHOD__.', ' . $e->getMessage(), \OCP\Util::ERROR);
|
|
d1bafeea1
|
94 95 |
return $response; } |
|
6d9380f96
|
96 97 |
if ($ids !== false) {
|
|
d1bafeea1
|
98 |
$backend = $this->app->getBackend('local');
|
|
6d9380f96
|
99 100 |
foreach ($ids as $id) {
|
|
d1bafeea1
|
101 102 103 104 105 |
$contact = $backend->getContact(null, $id, array('noCollection' => true));
$obj = \Sabre\VObject\Reader::read(
$contact['carddata'],
\Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES
);
|
|
6d9380f96
|
106 107 108 109 |
if ($obj) {
if (!$obj->inGroup($name)) {
|
|
d1bafeea1
|
110 111 |
continue; } |
|
6d9380f96
|
112 113 |
if ($obj->removeFromGroup($name)) {
|
|
d1bafeea1
|
114 115 |
$backend->updateContact(null, $id, $obj, array('noCollection' => true, 'isBatch' => true));
}
|
|
6d9380f96
|
116 |
|
|
d1bafeea1
|
117 118 119 120 |
} else {
\OCP\Util::writeLog('contacts', __METHOD__.', could not parse card ' . $id, \OCP\Util::DEBUG);
}
}
|
|
6d9380f96
|
121 |
|
|
d1bafeea1
|
122 |
} |
|
6d9380f96
|
123 |
|
|
d1bafeea1
|
124 |
try {
|
|
6d9380f96
|
125 |
$this->tagMgr->delete($name); |
|
d1bafeea1
|
126 127 |
} catch(\Exception $e) {
$response->setErrorMessage($e->getMessage());
|
|
6d9380f96
|
128 |
\OCP\Util::writeLog('contacts', __METHOD__.', ' . $e->getMessage(), \OCP\Util::ERROR);
|
|
d1bafeea1
|
129 |
} |
|
6d9380f96
|
130 |
|
|
d1bafeea1
|
131 132 133 134 135 136 137 138 139 140 141 |
return $response;
}
/**
* @NoAdminRequired
*/
public function renameGroup() {
$from = $this->request->post['from'];
$to = $this->request->post['to'];
$response = new JSONResponse();
|
|
6d9380f96
|
142 143 |
if (is_null($from) || $from === '') {
|
|
d1bafeea1
|
144 145 146 |
$response->bailOut(App::$l10n->t('No group name to rename from given.'));
return $response;
}
|
|
6d9380f96
|
147 148 |
if (is_null($to) || $to === '') {
|
|
d1bafeea1
|
149 150 151 |
$response->bailOut(App::$l10n->t('No group name to rename to given.'));
return $response;
}
|
|
6d9380f96
|
152 |
if (!$this->tagMgr->rename($from, $to)) {
|
|
d1bafeea1
|
153 154 155 |
$response->bailOut(App::$l10n->t('Error renaming group.'));
return $response;
}
|
|
6d9380f96
|
156 157 158 159 |
$ids = $this->tagMgr->getIdsForTag($to);
if ($ids !== false) {
|
|
d1bafeea1
|
160 |
$backend = $this->app->getBackend('local');
|
|
6d9380f96
|
161 162 |
foreach ($ids as $id) {
|
|
d1bafeea1
|
163 164 165 166 167 |
$contact = $backend->getContact(null, $id, array('noCollection' => true));
$obj = \Sabre\VObject\Reader::read(
$contact['carddata'],
\Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES
);
|
|
6d9380f96
|
168 169 170 171 |
if ($obj) {
if (!isset($obj->CATEGORIES)) {
|
|
d1bafeea1
|
172 173 |
continue; } |
|
6d9380f96
|
174 |
|
|
d1bafeea1
|
175 176 |
$obj->CATEGORIES->renameGroup($from, $to);
$backend->updateContact(null, $id, $obj, array('noCollection' => true));
|
|
6d9380f96
|
177 |
|
|
d1bafeea1
|
178 179 180 |
} else {
\OCP\Util::writeLog('contacts', __METHOD__.', could not parse card ' . $id, \OCP\Util::DEBUG);
}
|
|
6d9380f96
|
181 |
|
|
d1bafeea1
|
182 |
} |
|
6d9380f96
|
183 |
|
|
d1bafeea1
|
184 |
} |
|
6d9380f96
|
185 |
|
|
d1bafeea1
|
186 187 188 189 190 191 192 193 194 195 |
return $response;
}
/**
* @NoAdminRequired
*/
public function addToGroup() {
$response = new JSONResponse();
$params = $this->request->urlParams;
$categoryId = $params['categoryId'];
|
|
6d9380f96
|
196 |
$categoryName = $this->request->post['name']; |
|
d1bafeea1
|
197 198 |
$ids = $this->request->post['contactIds'];
$response->debug('request: '.print_r($this->request->post, true));
|
|
6d9380f96
|
199 200 201 202 203 |
if (is_null($categoryId) || $categoryId === '') {
throw new \Exception(
App::$l10n->t('Group ID missing from request.'),
Http::STATUS_PRECONDITION_FAILED
);
|
|
d1bafeea1
|
204 |
} |
|
6d9380f96
|
205 206 207 208 209 |
if (is_null($categoryName) || $categoryName === '') {
throw new \Exception(
App::$l10n->t('Group name missing from request.'),
Http::STATUS_PRECONDITION_FAILED
);
|
|
d1bafeea1
|
210 |
} |
|
6d9380f96
|
211 212 213 214 215 |
if (is_null($ids)) {
throw new \Exception(
App::$l10n->t('Contact ID missing from request.'),
Http::STATUS_PRECONDITION_FAILED
);
|
|
d1bafeea1
|
216 217 218 |
}
$backend = $this->app->getBackend('local');
|
|
6d9380f96
|
219 220 |
foreach ($ids as $contactId) {
|
|
d1bafeea1
|
221 222 223 224 225 |
$contact = $backend->getContact(null, $contactId, array('noCollection' => true));
$obj = \Sabre\VObject\Reader::read(
$contact['carddata'],
\Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES
);
|
|
6d9380f96
|
226 227 228 229 |
if ($obj) {
if ($obj->addToGroup($categoryName)) {
|
|
d1bafeea1
|
230 231 |
$backend->updateContact(null, $contactId, $obj, array('noCollection' => true));
}
|
|
6d9380f96
|
232 |
|
|
d1bafeea1
|
233 |
} |
|
6d9380f96
|
234 |
|
|
d1bafeea1
|
235 |
$response->debug('contactId: ' . $contactId . ', categoryId: ' . $categoryId);
|
|
6d9380f96
|
236 |
$this->tagMgr->tagAs($contactId, $categoryId); |
|
d1bafeea1
|
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
}
return $response;
}
/**
* @NoAdminRequired
*/
public function removeFromGroup() {
$response = new JSONResponse();
$params = $this->request->urlParams;
$categoryId = $params['categoryId'];
$categoryname = $this->request->post['name'];
$ids = $this->request->post['contactIds'];
//$response->debug('request: '.print_r($this->request->post, true));
|
|
6d9380f96
|
252 253 254 255 256 |
if (is_null($categoryId) || $categoryId === '') {
throw new \Exception(
App::$l10n->t('Group ID missing from request.'),
Http::STATUS_PRECONDITION_FAILED
);
|
|
d1bafeea1
|
257 |
} |
|
6d9380f96
|
258 259 260 261 262 263 264 265 266 267 268 269 |
if (is_null($categoryName) || $categoryName === '') {
throw new \Exception(
App::$l10n->t('Group name missing from request.'),
Http::STATUS_PRECONDITION_FAILED
);
}
if (is_null($ids)) {
throw new \Exception(
App::$l10n->t('Contact ID missing from request.'),
Http::STATUS_PRECONDITION_FAILED
);
|
|
d1bafeea1
|
270 271 272 |
}
$backend = $this->app->getBackend('local');
|
|
6d9380f96
|
273 274 |
foreach ($ids as $contactId) {
|
|
d1bafeea1
|
275 |
$contact = $backend->getContact(null, $contactId, array('noCollection' => true));
|
|
6d9380f96
|
276 277 |
if (!$contact) {
|
|
d1bafeea1
|
278 279 280 |
$response->debug('Couldn\'t get contact: ' . $contactId);
continue;
}
|
|
6d9380f96
|
281 |
|
|
d1bafeea1
|
282 283 284 285 |
$obj = \Sabre\VObject\Reader::read( $contact['carddata'], \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES ); |
|
6d9380f96
|
286 287 288 289 |
if ($obj) {
if (!isset($obj->CATEGORIES)) {
|
|
d1bafeea1
|
290 291 |
return $response; } |
|
6d9380f96
|
292 293 |
if ($obj->removeFromGroup($categoryname)) {
|
|
d1bafeea1
|
294 295 |
$backend->updateContact(null, $contactId, $obj, array('noCollection' => true));
}
|
|
6d9380f96
|
296 |
|
|
d1bafeea1
|
297 298 299 |
} else {
$response->debug('Error parsing contact: ' . $contactId);
}
|
|
6d9380f96
|
300 |
|
|
d1bafeea1
|
301 |
$response->debug('contactId: ' . $contactId . ', categoryId: ' . $categoryId);
|
|
6d9380f96
|
302 |
$this->tagMgr->unTag($contactId, $categoryId); |
|
d1bafeea1
|
303 304 305 306 307 308 |
} return $response; } } |