Blame view

sources/apps/contacts/lib/hooks.php 8.01 KB
d1bafeea1   Kload   [fix] Upgrade to ...
1
2
3
4
5
  <?php
  /**
   * ownCloud - Addressbook
   *
   * @author Jakob Sack
6d9380f96   Cédric Dupont   Update sources OC...
6
   * @author Thomas Tanghus
d1bafeea1   Kload   [fix] Upgrade to ...
7
   * @copyright 2011 Jakob Sack mail@jakobsack.de
6d9380f96   Cédric Dupont   Update sources OC...
8
   * @copyright 2012-2014 Thomas Tanghus (thomas@tanghus.net)
d1bafeea1   Kload   [fix] Upgrade to ...
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
   *
   * You should have received a copy of the GNU Affero General Public
   * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
   *
   */
  
  /**
   * The following signals are being emitted:
   *
   * OCA\Contacts\VCard::post_moveToAddressbook(array('aid' => $aid, 'id' => $id))
   * OCA\Contacts\VCard::pre_deleteVCard(array('aid' => $aid, 'id' => $id, 'uri' = $uri)); (NOTE: the values can be null depending on which method emits them)
   * OCA\Contacts\VCard::post_updateVCard($id)
   * OCA\Contacts\VCard::post_createVCard($newid)
   */
  
  namespace OCA\Contacts;
  
  use \Sabre\VObject;
  
  /**
   * This class contains all hooks.
   */
  class Hooks{
  	/**
  	 * @brief Add default Addressbook for a certain user
  	 * @param paramters parameters from postCreateUser-Hook
  	 * @return array
  	 */
  	public static function userCreated($parameters) {
  		//Addressbook::addDefault($parameters['uid']);
  		return true;
  	}
  
  	/**
  	 * @brief Deletes all Addressbooks of a certain user
  	 * @param paramters parameters from postDeleteUser-Hook
  	 * @return array
  	 */
  	public static function userDeleted($parameters) {
  		$backend = new Backend\Database($parameters['uid']);
  		$addressBooks = $backend->getAddressBooksForUser();
  
  		foreach($addressBooks as $addressBook) {
  			// Purging of contact categories and and properties is done by backend.
  			$backend->deleteAddressBook($addressBook['id']);
  		}
  	}
  
  	/**
  	* Delete any registred address books (Future)
  	*/
  	public static function addressBookDeletion($parameters) {
  		// Clean up sharing
  		\OCP\Share::unshareAll('addressbook', $parameters['addressbookid']);
  
  		if(count($parameters['contactids'])) {
  			// Remove contacts from groups
  			$tagMgr = \OC::$server->getTagManager()->load('contact');
  			$tagMgr->purgeObjects($parameters['contactids']);
  
  			// Purge property indexes
  			Utils\Properties::purgeIndexes($parameters['contactids']);
  		}
  	}
  
  	/**
  	 * A contact has been deleted and cleanup for property indexes and
  	 * group/contact relations must be performed.
  	 *
  	 * NOTE: When deleting an entire address book the cleanup is done in the
  	 * addressBookDeletion() hook. Any cleanup procedures most be implemented
  	 * in both.
  	 *
  	 * @param array $parameters Currently only the id of the contact.
  	 */
  	public static function contactDeletion($parameters) {
6d9380f96   Cédric Dupont   Update sources OC...
95
  		\OCP\Util::writeLog('contacts', __METHOD__.' id: '.print_r($parameters['id'], true), \OCP\Util::DEBUG);
d1bafeea1   Kload   [fix] Upgrade to ...
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  		$ids = is_array($parameters['id']) ? $parameters['id'] : array($parameters['id']);
  		$tagMgr = \OC::$server->getTagManager()->load('contact');
  		$tagMgr->purgeObjects($ids);
  		Utils\Properties::purgeIndexes($ids);
  
  		// Contact sharing not implemented, but keep for future.
  		//\OCP\Share::unshareAll('contact', $id);
  	}
  
  	public static function contactAdded($parameters) {
  		\OCP\Util::writeLog('contacts', __METHOD__.' id: '.$parameters['id'], \OCP\Util::DEBUG);
  		$contact = $parameters['contact'];
  		if(isset($contact->CATEGORIES)) {
  			\OCP\Util::writeLog('contacts', __METHOD__.' groups: '.print_r($contact->CATEGORIES->getParts(), true), \OCP\Util::DEBUG);
  			$tagMgr = \OC::$server->getTagManager()->load('contact');
  			foreach($contact->CATEGORIES->getParts() as $group) {
  				\OCP\Util::writeLog('contacts', __METHOD__.' group: '.$group, \OCP\Util::DEBUG);
  				$tagMgr->tagAs($parameters['id'], $group);
  			}
  		}
  		Utils\Properties::updateIndex($parameters['id'], $contact);
  	}
  
  	public static function contactUpdated($parameters) {
  		//\OCP\Util::writeLog('contacts', __METHOD__.' parameters: '.print_r($parameters, true), \OCP\Util::DEBUG);
  		$contact = $parameters['contact'];
  		Utils\Properties::updateIndex($parameters['contactId'], $contact);
  		// If updated via CardDAV we don't know if PHOTO has changed
  		if(isset($parameters['carddav']) && $parameters['carddav']) {
  			if(isset($contact->PHOTO) || isset($contact->LOGO)) {
  				Utils\Properties::cacheThumbnail(
  					$parameters['backend'],
  					$parameters['addressBookId'],
  					$parameters['contactId'],
  					null,
  					$contact,
  					array('update' => true)
  				);
  			}
  			$tagMgr = \OC::$server->getTagManager()->load('contact');
  			$tagMgr->purgeObjects(array($parameters['contactId']));
  			if(isset($contact->CATEGORIES)) {
  				$tagMgr->addMultiple($contact->CATEGORIES->getParts(), true, $parameters['contactId']);
  			}
  		}
  	}
  
  	/**
  	 * Scan vCards for categories.
  	 */
  	public static function scanCategories() {
  		$offset = 0;
  		$limit = 10;
  
  		$tagMgr = \OC::$server->getTagManager()->load('contact');
  		$tags = array();
6d9380f96   Cédric Dupont   Update sources OC...
152
  		foreach ($tagMgr->getTags() as $tag) {
d1bafeea1   Kload   [fix] Upgrade to ...
153
154
155
156
157
  			$tags[] = $tag['name'];
  		}
  
  		// reset tags
  		$tagMgr->delete($tags);
6d9380f96   Cédric Dupont   Update sources OC...
158
159
  		$app = new App();
  		$backend = $app->getBackend('local');
d1bafeea1   Kload   [fix] Upgrade to ...
160
  		$addressBookInfos = $backend->getAddressBooksForUser();
6d9380f96   Cédric Dupont   Update sources OC...
161
  		foreach ($addressBookInfos as $addressBookInfo) {
d1bafeea1   Kload   [fix] Upgrade to ...
162
  			$addressBook = new AddressBook($backend, $addressBookInfo);
6d9380f96   Cédric Dupont   Update sources OC...
163
164
165
  			while ($contacts = $addressBook->getChildren($limit, $offset, false)) {
  				foreach ($contacts as $contact) {
  					if (isset($contact->CATEGORIES)) {
d1bafeea1   Kload   [fix] Upgrade to ...
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  						$tagMgr->addMultiple($contact->CATEGORIES->getParts(), true, $contact->getId());
  					}
  				}
  				\OCP\Util::writeLog('contacts',
  					__METHOD__ .', scanning: ' . $limit . ' starting from ' . $offset,
  					\OCP\Util::DEBUG);
  				$offset += $limit;
  			}
  		}
  	}
  
  	/**
  	 * Scan vCards for properties.
  	 */
  	public static function indexProperties() {
  		$offset = 0;
  		$limit = 10;
  
  		$app = new App();
  		$backend = $app->getBackend('local');
  		$addressBookInfos = $backend->getAddressBooksForUser();
6d9380f96   Cédric Dupont   Update sources OC...
187
  		foreach ($addressBookInfos as $addressBookInfo) {
d1bafeea1   Kload   [fix] Upgrade to ...
188
  			$addressBook = new AddressBook($backend, $addressBookInfo);
6d9380f96   Cédric Dupont   Update sources OC...
189
  			while ($contacts = $addressBook->getChildren($limit, $offset, false)) {
d1bafeea1   Kload   [fix] Upgrade to ...
190
  				\OCP\Util::writeLog('contacts',
6d9380f96   Cédric Dupont   Update sources OC...
191
  					__METHOD__ . ', indexing: ' . $limit . ' starting from ' . $offset,
d1bafeea1   Kload   [fix] Upgrade to ...
192
  					\OCP\Util::DEBUG);
6d9380f96   Cédric Dupont   Update sources OC...
193
194
195
196
197
198
199
200
  				foreach ($contacts as $contact) {
  					if(!$contact->retrieve()) {
  						\OCP\Util::writeLog('contacts',
  							__METHOD__ . ', Error loading contact ' .print_r($contact, true),
  							\OCP\Util::DEBUG);
  					}
  					Utils\Properties::updateIndex($contact->getId(), $contact);
  				}
d1bafeea1   Kload   [fix] Upgrade to ...
201
202
203
204
205
206
207
208
209
210
  				$offset += $limit;
  			}
  		}
  	}
  
  	public static function getCalenderSources($parameters) {
  		//\OCP\Util::writeLog('contacts', __METHOD__.' parameters: '.print_r($parameters, true), \OCP\Util::DEBUG);
  
  		$app = new App();
  		$addressBooks = $app->getAddressBooksForUser();
6d9380f96   Cédric Dupont   Update sources OC...
211
212
213
  		$baseUrl = \OCP\Util::linkTo('calendar', 'ajax/events.php').'?calendar_id=';
  
  		foreach ($addressBooks as $addressBook) {
d1bafeea1   Kload   [fix] Upgrade to ...
214
215
216
  			$info = $addressBook->getMetaData();
  			$parameters['sources'][]
  				= array(
6d9380f96   Cédric Dupont   Update sources OC...
217
  					'url' => $baseUrl . 'birthday_'. $info['backend'].'_' . $info['id'],
d1bafeea1   Kload   [fix] Upgrade to ...
218
219
220
221
222
223
224
225
226
227
228
229
  					'backgroundColor' => '#cccccc',
  					'borderColor' => '#888',
  					'textColor' => 'black',
  					'cache' => true,
  					'editable' => false,
  				);
  		}
  	}
  
  	public static function getBirthdayEvents($parameters) {
  		//\OCP\Util::writeLog('contacts', __METHOD__.' parameters: '.print_r($parameters, true), \OCP\Util::DEBUG);
  		$name = $parameters['calendar_id'];
6d9380f96   Cédric Dupont   Update sources OC...
230

d1bafeea1   Kload   [fix] Upgrade to ...
231
232
233
  		if (strpos($name, 'birthday_') != 0) {
  			return;
  		}
6d9380f96   Cédric Dupont   Update sources OC...
234

d1bafeea1   Kload   [fix] Upgrade to ...
235
236
237
238
239
  		$info = explode('_', $name);
  		$backend = $info[1];
  		$aid = $info[2];
  		$app = new App();
  		$addressBook = $app->getAddressBook($backend, $aid);
6d9380f96   Cédric Dupont   Update sources OC...
240
241
  
  		foreach ($addressBook->getBirthdayEvents() as $vevent) {
d1bafeea1   Kload   [fix] Upgrade to ...
242
243
244
245
246
247
248
249
250
251
  			$parameters['events'][] = array(
  				'id' => 0,
  				'vevent' => $vevent,
  				'repeating' => true,
  				'summary' => $vevent->SUMMARY,
  				'calendardata' => $vevent->serialize()
  			);
  		}
  	}
  }