Hooks::scanCategories()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42
Metric Value
dl 0
loc 33
ccs 0
cts 27
cp 0
rs 8.439
cc 6
eloc 21
nc 10
nop 0
crap 42
1
<?php
2
/**
3
 * ownCloud - Addressbook
4
 *
5
 * @author Jakob Sack
6
 * @author Thomas Tanghus
7
 * @copyright 2011 Jakob Sack [email protected]
8
 * @copyright 2012-2014 Thomas Tanghus ([email protected])
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
12
 * License as published by the Free Software Foundation; either
13
 * version 3 of the License, or any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public
21
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
/**
26
 * The following signals are being emitted:
27
 *
28
 * OCA\Contacts\VCard::post_moveToAddressbook(array('aid' => $aid, 'id' => $id))
29
 * OCA\Contacts\VCard::pre_deleteVCard(array('aid' => $aid, 'id' => $id, 'uri' = $uri)); (NOTE: the values can be null depending on which method emits them)
30
 * OCA\Contacts\VCard::post_updateVCard($id)
31
 * OCA\Contacts\VCard::post_createVCard($newid)
32
 */
33
34
namespace OCA\Contacts;
35
36
use \Sabre\VObject;
37
38
/**
39
 * This class contains all hooks.
40
 */
41
class Hooks{
42
	/**
43
	 * @brief Add default Addressbook for a certain user
44
	 * @param paramters parameters from postCreateUser-Hook
45
	 * @return boolean
46
	 */
47
	public static function userCreated($parameters) {
0 ignored issues
show
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
		//Addressbook::addDefault($parameters['uid']);
49
		return true;
50
	}
51
52
	/**
53
	 * @brief Deletes all Addressbooks of a certain user
54
	 * @param paramters parameters from postDeleteUser-Hook
55
	 * @return array
56
	 */
57
	public static function userDeleted($parameters) {
58
		$backend = new Backend\Database($parameters['uid']);
59
		$addressBooks = $backend->getAddressBooksForUser();
60
61
		foreach($addressBooks as $addressBook) {
62
			// Purging of contact categories and and properties is done by backend.
63
			$backend->deleteAddressBook($addressBook['id']);
64
		}
65
	}
66
67
	/**
68
	* Delete any registred address books (Future)
69
	*/
70
	public static function addressBookDeletion($parameters) {
71
		// Clean up sharing
72
		\OCP\Share::unshareAll('addressbook', $parameters['addressbookid']);
73
74
		if(count($parameters['contactids'])) {
75
			// Remove contacts from groups
76
			$tagMgr = \OC::$server->getTagManager()->load('contact');
77
			$tagMgr->purgeObjects($parameters['contactids']);
78
79
			// Purge property indexes
80
			Utils\Properties::purgeIndexes($parameters['contactids']);
81
		}
82
	}
83
84
	/**
85
	 * A contact has been deleted and cleanup for property indexes and
86
	 * group/contact relations must be performed.
87
	 *
88
	 * NOTE: When deleting an entire address book the cleanup is done in the
89
	 * addressBookDeletion() hook. Any cleanup procedures most be implemented
90
	 * in both.
91
	 *
92
	 * @param array $parameters Currently only the id of the contact.
93
	 */
94
	public static function contactDeletion($parameters) {
95
		\OCP\Util::writeLog('contacts', __METHOD__.' id: '.print_r($parameters['id'], true), \OCP\Util::DEBUG);
96
		$ids = is_array($parameters['id']) ? $parameters['id'] : array($parameters['id']);
97
		$tagMgr = \OC::$server->getTagManager()->load('contact');
98
		$tagMgr->purgeObjects($ids);
99
		Utils\Properties::purgeIndexes($ids);
100
101
		// Contact sharing not implemented, but keep for future.
102
		//\OCP\Share::unshareAll('contact', $id);
103
	}
104
105
	public static function contactAdded($parameters) {
106
		\OCP\Util::writeLog('contacts', __METHOD__.' id: '.$parameters['id'], \OCP\Util::DEBUG);
107
		$app = new App();
108
		$backend = $app->getBackend( (isset($parameters['backend'])) ? $parameters['backend'] :'local' );
109
		$ab = $backend->getAddressBook( $parameters['addressBookId'] );
110
111
		$contact = $parameters['contact'];
112
		if(isset($contact->CATEGORIES)) {
113
			\OCP\Util::writeLog('contacts', __METHOD__.' groups: '.print_r($contact->CATEGORIES->getParts(), true), \OCP\Util::DEBUG);
114
			$tagMgr = \OC::$server->getTagManager()->load('contact');
115
			foreach($contact->CATEGORIES->getParts() as $group) {
116
				\OCP\Util::writeLog('contacts', __METHOD__.' group: '.$group, \OCP\Util::DEBUG);
117
				$tagMgr->tagAs($parameters['id'], $group);
118
			}
119
		}
120
		Utils\Properties::updateIndex($parameters['id'], $contact, $ab['owner']);
121
	}
122
123
	public static function contactUpdated($parameters) {
124
		//\OCP\Util::writeLog('contacts', __METHOD__.' parameters: '.print_r($parameters, true), \OCP\Util::DEBUG);
125
		$app = new App();
126
		$backend = $app->getBackend( (isset($parameters['backend'])) ? $parameters['backend'] :'local' );
127
		$ab = $backend->getAddressBook( $parameters['addressBookId'] );
128
129
		$contact = $parameters['contact'];
130
		Utils\Properties::updateIndex($parameters['contactId'], $contact, $ab['owner']);
131
		// If updated via CardDAV we don't know if PHOTO has changed
132
		if(isset($parameters['carddav']) && $parameters['carddav']) {
133
			if(isset($contact->PHOTO) || isset($contact->LOGO)) {
134
				Utils\Properties::cacheThumbnail(
135
					$parameters['backend'],
136
					$parameters['addressBookId'],
137
					$parameters['contactId'],
138
					null,
139
					$contact,
140
					array('update' => true)
141
				);
142
			}
143
			$tagMgr = \OC::$server->getTagManager()->load('contact');
144
			$tagMgr->purgeObjects(array($parameters['contactId']));
145
			if(isset($contact->CATEGORIES)) {
146
				$tagMgr->addMultiple($contact->CATEGORIES->getParts(), true, $parameters['contactId']);
147
			}
148
		}
149
	}
150
151
	/**
152
	 * Scan vCards for categories.
153
	 */
154
	public static function scanCategories() {
155
		$offset = 0;
156
		$limit = 10;
157
158
		$tagMgr = \OC::$server->getTagManager()->load('contact');
159
		$tags = array();
160
161
		foreach ($tagMgr->getTags() as $tag) {
162
			$tags[] = $tag['name'];
163
		}
164
165
		// reset tags
166
		$tagMgr->delete($tags);
167
168
		$app = new App();
169
		$backend = $app->getBackend('local');
170
		$addressBookInfos = $backend->getAddressBooksForUser();
171
172
		foreach ($addressBookInfos as $addressBookInfo) {
173
			$addressBook = new AddressBook($backend, $addressBookInfo);
174
			while ($contacts = $addressBook->getChildren($limit, $offset, false)) {
175
				foreach ($contacts as $contact) {
176
					if (isset($contact->CATEGORIES)) {
177
						$tagMgr->addMultiple($contact->CATEGORIES->getParts(), true, $contact->getId());
178
					}
179
				}
180
				\OCP\Util::writeLog('contacts',
181
					__METHOD__ .', scanning: ' . $limit . ' starting from ' . $offset,
182
					\OCP\Util::DEBUG);
183
				$offset += $limit;
184
			}
185
		}
186
	}
187
188
	/**
189
	 * Scan vCards for properties.
190
	 */
191
	public static function indexProperties() {
192
		$offset = 0;
193
		$limit = 10;
194
195
		$app = new App();
196
		$backend = $app->getBackend('local');
197
		$addressBookInfos = $backend->getAddressBooksForUser();
198
199
		foreach ($addressBookInfos as $addressBookInfo) {
200
			$addressBook = new AddressBook($backend, $addressBookInfo);
201
			$contacts = $addressBook->getChildren($limit, $offset, false);
202
			\OCP\Util::writeLog('contacts',
203
				__METHOD__ . ', indexing: ' . $limit . ' starting from ' . $offset,
204
				\OCP\Util::DEBUG);
205
			foreach ($contacts as $contact) {
206 View Code Duplication
				if(!$contact->retrieve()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
207
					\OCP\Util::writeLog('contacts',
208
						__METHOD__ . ', Error loading contact ' .print_r($contact, true),
209
						\OCP\Util::DEBUG);
210
				}
211
				Utils\Properties::updateIndex($contact->getId(), $contact);
212
			}
213
			$offset += $limit;
214
		}
215
		$stmt = \OCP\DB::prepare('DELETE FROM `*PREFIX*contacts_cards_properties`
216
							WHERE NOT EXISTS(SELECT NULL
217
							FROM `*PREFIX*contacts_cards`
218
							WHERE `*PREFIX*contacts_cards`.id = `*PREFIX*contacts_cards_properties`.contactid)');
219
		$result = $stmt->execute(array());
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
220
	}
221
222
	public static function getCalenderSources($parameters) {
223
		//\OCP\Util::writeLog('contacts', __METHOD__.' parameters: '.print_r($parameters, true), \OCP\Util::DEBUG);
224
225
		$app = new App();
226
		$addressBooks = $app->getAddressBooksForUser();
227
		$baseUrl = \OCP\Util::linkTo('calendar', 'ajax/events.php').'?calendar_id=';
228
229
		foreach ($addressBooks as $addressBook) {
230
			$info = $addressBook->getMetaData();
231
			$parameters['sources'][]
232
				= array(
233
					'url' => $baseUrl . 'birthday_'. $info['backend'].'_' . $info['id'],
234
					'backgroundColor' => '#cccccc',
235
					'borderColor' => '#888',
236
					'textColor' => 'black',
237
					'cache' => true,
238
					'editable' => false,
239
				);
240
		}
241
	}
242
243
	public static function getBirthdayEvents($parameters) {
244
		//\OCP\Util::writeLog('contacts', __METHOD__.' parameters: '.print_r($parameters, true), \OCP\Util::DEBUG);
245
		$name = $parameters['calendar_id'];
246
247
		if (strpos($name, 'birthday_') != 0) {
248
			return;
249
		}
250
251
		$info = explode('_', $name);
252
		$backend = $info[1];
253
		$aid = $info[2];
254
		$app = new App();
255
		$addressBook = $app->getAddressBook($backend, $aid);
256
257
		foreach ($addressBook->getBirthdayEvents() as $vevent) {
258
			$parameters['events'][] = array(
259
				'id' => 0,
260
				'vevent' => $vevent,
261
				'repeating' => true,
262
				'summary' => $vevent->SUMMARY,
263
				'calendardata' => $vevent->serialize()
264
			);
265
		}
266
	}
267
}
268