Blame view

sources/apps/contacts/lib/controller/groupcontroller.php 7.64 KB
d1bafeea1   Kload   [fix] Upgrade to ...
1
2
3
  <?php
  /**
   * @author Thomas Tanghus
6d9380f96   Cédric Dupont   Update sources OC...
4
5
   * @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net)
   *
d1bafeea1   Kload   [fix] Upgrade to ...
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   Cédric Dupont   Update sources OC...
15
16
17
18
  	OCA\Contacts\Controller,
  	OCP\AppFramework\Http,
  	OCP\ITags,
  	OCP\IRequest;
d1bafeea1   Kload   [fix] Upgrade to ...
19
20
21
22
23
  
  /**
   * Controller class for groups/categories
   */
  class GroupController extends Controller {
6d9380f96   Cédric Dupont   Update sources OC...
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   Kload   [fix] Upgrade to ...
29
30
31
32
  	/**
  	 * @NoAdminRequired
  	 */
  	public function getGroups() {
6d9380f96   Cédric Dupont   Update sources OC...
33
34
35
  		$tags = $this->tagMgr->getTags();
  
  		foreach ($tags as &$tag) {
d1bafeea1   Kload   [fix] Upgrade to ...
36
  			try {
6d9380f96   Cédric Dupont   Update sources OC...
37
38
  				$ids = $this->tagMgr->getIdsForTag($tag['name']);
  				$tag['contacts'] = $ids;
d1bafeea1   Kload   [fix] Upgrade to ...
39
40
41
42
  			} catch(\Exception $e) {
  				$this->api->log(__METHOD__ . ' ' . $e->getMessage());
  			}
  		}
6d9380f96   Cédric Dupont   Update sources OC...
43
  		$favorites = $this->tagMgr->getFavorites();
d1bafeea1   Kload   [fix] Upgrade to ...
44
45
46
47
48
  
  		$groups = array(
  			'categories' => $tags,
  			'favorites' => $favorites,
  			'shared' => \OCP\Share::getItemsSharedWith('addressbook', \OCA\Contacts\Share\Addressbook::FORMAT_ADDRESSBOOKS),
6d9380f96   Cédric Dupont   Update sources OC...
49
50
  			'lastgroup' => \OCP\Config::getUserValue(\OCP\User::getUser(), 'contacts', 'lastgroup', 'all'),
  			'sortorder' => \OCP\Config::getUserValue(\OCP\User::getUser(), 'contacts', 'groupsort', ''),
d1bafeea1   Kload   [fix] Upgrade to ...
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   Cédric Dupont   Update sources OC...
63
64
  
  		if (is_null($name) || $name === "") {
d1bafeea1   Kload   [fix] Upgrade to ...
65
66
  			$response->bailOut(App::$l10n->t('No group name given.'));
  		}
6d9380f96   Cédric Dupont   Update sources OC...
67
  		$id = $this->tagMgr->add($name);
d1bafeea1   Kload   [fix] Upgrade to ...
68

6d9380f96   Cédric Dupont   Update sources OC...
69
  		if ($id === false) {
d1bafeea1   Kload   [fix] Upgrade to ...
70
71
72
73
  			$response->bailOut(App::$l10n->t('Error adding group.'));
  		} else {
  			$response->setParams(array('id'=>$id, 'name' => $name));
  		}
6d9380f96   Cédric Dupont   Update sources OC...
74

d1bafeea1   Kload   [fix] Upgrade to ...
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   Cédric Dupont   Update sources OC...
85
  		if (is_null($name) || $name === '') {
d1bafeea1   Kload   [fix] Upgrade to ...
86
87
88
  			$response->bailOut(App::$l10n->t('No group name given.'));
  			return $response;
  		}
d1bafeea1   Kload   [fix] Upgrade to ...
89
  		try {
6d9380f96   Cédric Dupont   Update sources OC...
90
  			$ids = $this->tagMgr->getIdsForTag($name);
d1bafeea1   Kload   [fix] Upgrade to ...
91
92
  		} catch(\Exception $e) {
  			$response->setErrorMessage($e->getMessage());
6d9380f96   Cédric Dupont   Update sources OC...
93
  			\OCP\Util::writeLog('contacts', __METHOD__.', ' . $e->getMessage(), \OCP\Util::ERROR);
d1bafeea1   Kload   [fix] Upgrade to ...
94
95
  			return $response;
  		}
6d9380f96   Cédric Dupont   Update sources OC...
96
97
  
  		if ($ids !== false) {
d1bafeea1   Kload   [fix] Upgrade to ...
98
  			$backend = $this->app->getBackend('local');
6d9380f96   Cédric Dupont   Update sources OC...
99
100
  
  			foreach ($ids as $id) {
d1bafeea1   Kload   [fix] Upgrade to ...
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   Cédric Dupont   Update sources OC...
106
107
108
109
  
  				if ($obj) {
  
  					if (!$obj->inGroup($name)) {
d1bafeea1   Kload   [fix] Upgrade to ...
110
111
  						continue;
  					}
6d9380f96   Cédric Dupont   Update sources OC...
112
113
  
  					if ($obj->removeFromGroup($name)) {
d1bafeea1   Kload   [fix] Upgrade to ...
114
115
  						$backend->updateContact(null, $id, $obj, array('noCollection' => true, 'isBatch' => true));
  					}
6d9380f96   Cédric Dupont   Update sources OC...
116

d1bafeea1   Kload   [fix] Upgrade to ...
117
118
119
120
  				} else {
  					\OCP\Util::writeLog('contacts', __METHOD__.', could not parse card ' . $id, \OCP\Util::DEBUG);
  				}
  			}
6d9380f96   Cédric Dupont   Update sources OC...
121

d1bafeea1   Kload   [fix] Upgrade to ...
122
  		}
6d9380f96   Cédric Dupont   Update sources OC...
123

d1bafeea1   Kload   [fix] Upgrade to ...
124
  		try {
6d9380f96   Cédric Dupont   Update sources OC...
125
  			$this->tagMgr->delete($name);
d1bafeea1   Kload   [fix] Upgrade to ...
126
127
  		} catch(\Exception $e) {
  			$response->setErrorMessage($e->getMessage());
6d9380f96   Cédric Dupont   Update sources OC...
128
  			\OCP\Util::writeLog('contacts', __METHOD__.', ' . $e->getMessage(), \OCP\Util::ERROR);
d1bafeea1   Kload   [fix] Upgrade to ...
129
  		}
6d9380f96   Cédric Dupont   Update sources OC...
130

d1bafeea1   Kload   [fix] Upgrade to ...
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   Cédric Dupont   Update sources OC...
142
143
  
  		if (is_null($from) || $from === '') {
d1bafeea1   Kload   [fix] Upgrade to ...
144
145
146
  			$response->bailOut(App::$l10n->t('No group name to rename from given.'));
  			return $response;
  		}
6d9380f96   Cédric Dupont   Update sources OC...
147
148
  
  		if (is_null($to) || $to === '') {
d1bafeea1   Kload   [fix] Upgrade to ...
149
150
151
  			$response->bailOut(App::$l10n->t('No group name to rename to given.'));
  			return $response;
  		}
6d9380f96   Cédric Dupont   Update sources OC...
152
  		if (!$this->tagMgr->rename($from, $to)) {
d1bafeea1   Kload   [fix] Upgrade to ...
153
154
155
  			$response->bailOut(App::$l10n->t('Error renaming group.'));
  			return $response;
  		}
6d9380f96   Cédric Dupont   Update sources OC...
156
157
158
159
  
  		$ids = $this->tagMgr->getIdsForTag($to);
  
  		if ($ids !== false) {
d1bafeea1   Kload   [fix] Upgrade to ...
160
  			$backend = $this->app->getBackend('local');
6d9380f96   Cédric Dupont   Update sources OC...
161
162
  
  			foreach ($ids as $id) {
d1bafeea1   Kload   [fix] Upgrade to ...
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   Cédric Dupont   Update sources OC...
168
169
170
171
  
  				if ($obj) {
  
  					if (!isset($obj->CATEGORIES)) {
d1bafeea1   Kload   [fix] Upgrade to ...
172
173
  						continue;
  					}
6d9380f96   Cédric Dupont   Update sources OC...
174

d1bafeea1   Kload   [fix] Upgrade to ...
175
176
  					$obj->CATEGORIES->renameGroup($from, $to);
  					$backend->updateContact(null, $id, $obj, array('noCollection' => true));
6d9380f96   Cédric Dupont   Update sources OC...
177

d1bafeea1   Kload   [fix] Upgrade to ...
178
179
180
  				} else {
  					\OCP\Util::writeLog('contacts', __METHOD__.', could not parse card ' . $id, \OCP\Util::DEBUG);
  				}
6d9380f96   Cédric Dupont   Update sources OC...
181

d1bafeea1   Kload   [fix] Upgrade to ...
182
  			}
6d9380f96   Cédric Dupont   Update sources OC...
183

d1bafeea1   Kload   [fix] Upgrade to ...
184
  		}
6d9380f96   Cédric Dupont   Update sources OC...
185

d1bafeea1   Kload   [fix] Upgrade to ...
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   Cédric Dupont   Update sources OC...
196
  		$categoryName = $this->request->post['name'];
d1bafeea1   Kload   [fix] Upgrade to ...
197
198
  		$ids = $this->request->post['contactIds'];
  		$response->debug('request: '.print_r($this->request->post, true));
6d9380f96   Cédric Dupont   Update sources OC...
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   Kload   [fix] Upgrade to ...
204
  		}
6d9380f96   Cédric Dupont   Update sources OC...
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   Kload   [fix] Upgrade to ...
210
  		}
6d9380f96   Cédric Dupont   Update sources OC...
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   Kload   [fix] Upgrade to ...
216
217
218
  		}
  
  		$backend = $this->app->getBackend('local');
6d9380f96   Cédric Dupont   Update sources OC...
219
220
  
  		foreach ($ids as $contactId) {
d1bafeea1   Kload   [fix] Upgrade to ...
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   Cédric Dupont   Update sources OC...
226
227
228
229
  
  			if ($obj) {
  
  				if ($obj->addToGroup($categoryName)) {
d1bafeea1   Kload   [fix] Upgrade to ...
230
231
  					$backend->updateContact(null, $contactId, $obj, array('noCollection' => true));
  				}
6d9380f96   Cédric Dupont   Update sources OC...
232

d1bafeea1   Kload   [fix] Upgrade to ...
233
  			}
6d9380f96   Cédric Dupont   Update sources OC...
234

d1bafeea1   Kload   [fix] Upgrade to ...
235
  			$response->debug('contactId: ' . $contactId . ', categoryId: ' . $categoryId);
6d9380f96   Cédric Dupont   Update sources OC...
236
  			$this->tagMgr->tagAs($contactId, $categoryId);
d1bafeea1   Kload   [fix] Upgrade to ...
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   Cédric Dupont   Update sources OC...
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   Kload   [fix] Upgrade to ...
257
  		}
6d9380f96   Cédric Dupont   Update sources OC...
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   Kload   [fix] Upgrade to ...
270
271
272
  		}
  
  		$backend = $this->app->getBackend('local');
6d9380f96   Cédric Dupont   Update sources OC...
273
274
  
  		foreach ($ids as $contactId) {
d1bafeea1   Kload   [fix] Upgrade to ...
275
  			$contact = $backend->getContact(null, $contactId, array('noCollection' => true));
6d9380f96   Cédric Dupont   Update sources OC...
276
277
  
  			if (!$contact) {
d1bafeea1   Kload   [fix] Upgrade to ...
278
279
280
  				$response->debug('Couldn\'t get contact: ' . $contactId);
  				continue;
  			}
6d9380f96   Cédric Dupont   Update sources OC...
281

d1bafeea1   Kload   [fix] Upgrade to ...
282
283
284
285
  			$obj = \Sabre\VObject\Reader::read(
  				$contact['carddata'],
  				\Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES
  			);
6d9380f96   Cédric Dupont   Update sources OC...
286
287
288
289
  
  			if ($obj) {
  
  				if (!isset($obj->CATEGORIES)) {
d1bafeea1   Kload   [fix] Upgrade to ...
290
291
  					return $response;
  				}
6d9380f96   Cédric Dupont   Update sources OC...
292
293
  
  				if ($obj->removeFromGroup($categoryname)) {
d1bafeea1   Kload   [fix] Upgrade to ...
294
295
  					$backend->updateContact(null, $contactId, $obj, array('noCollection' => true));
  				}
6d9380f96   Cédric Dupont   Update sources OC...
296

d1bafeea1   Kload   [fix] Upgrade to ...
297
298
299
  			} else {
  				$response->debug('Error parsing contact: ' . $contactId);
  			}
6d9380f96   Cédric Dupont   Update sources OC...
300

d1bafeea1   Kload   [fix] Upgrade to ...
301
  			$response->debug('contactId: ' . $contactId . ', categoryId: ' . $categoryId);
6d9380f96   Cédric Dupont   Update sources OC...
302
  			$this->tagMgr->unTag($contactId, $categoryId);
d1bafeea1   Kload   [fix] Upgrade to ...
303
304
305
306
307
308
  		}
  
  		return $response;
  	}
  
  }