Completed
Push — 16.1 ( a067d8...7d8eb1 )
by Nathan
15:30
created
addressbook/inc/class.addressbook_import_contacts_csv.inc.php 1 patch
Spacing   +60 added lines, -60 removed lines patch added patch discarded remove patch
@@ -15,14 +15,14 @@  discard block
 block discarded – undo
15 15
 /**
16 16
  * class import_csv for addressbook
17 17
  */
18
-class addressbook_import_contacts_csv extends importexport_basic_import_csv  {
18
+class addressbook_import_contacts_csv extends importexport_basic_import_csv {
19 19
 
20 20
 	/**
21 21
 	 * conditions for actions
22 22
 	 *
23 23
 	 * @var array
24 24
 	 */
25
-	protected static $conditions = array( 'exists', 'equal' );
25
+	protected static $conditions = array('exists', 'equal');
26 26
 
27 27
 	/**
28 28
 	 * @var bocontacts
@@ -48,7 +48,7 @@  discard block
 block discarded – undo
48 48
 	 * @param string $_charset
49 49
 	 * @param definition $_definition
50 50
 	 */
51
-	public function init(importexport_definition &$_definition ) {
51
+	public function init(importexport_definition&$_definition) {
52 52
 
53 53
 		// fetch the addressbook bo
54 54
 		$this->bocontacts = new Api\Contacts();
@@ -59,13 +59,13 @@  discard block
 block discarded – undo
59 59
 		$this->lookups = array(
60 60
 			'tid' => array('n'=>'contact')
61 61
 		);
62
-		foreach($this->bocontacts->content_types as $tid => $data)
62
+		foreach ($this->bocontacts->content_types as $tid => $data)
63 63
 		{
64 64
 			$this->lookups['tid'][$tid] = $data['name'];
65 65
 		}
66 66
 
67 67
 		// Try and set a default type, for use if file does not specify
68
-		if(!$this->lookups['tid'][Api\Contacts\Storage::DELETED_TYPE] && count($this->lookups['tid']) == 1 ||
68
+		if (!$this->lookups['tid'][Api\Contacts\Storage::DELETED_TYPE] && count($this->lookups['tid']) == 1 ||
69 69
 			$this->lookups['tid'][Api\Contacts\Storage::DELETED_TYPE] && count($this->lookups['tid']) == 2)
70 70
 		{
71 71
 			reset($this->lookups['tid']);
@@ -74,21 +74,21 @@  discard block
 block discarded – undo
74 74
 
75 75
 
76 76
 		// set contact owner
77
-		$contact_owner = isset( $_definition->plugin_options['contact_owner'] ) ?
77
+		$contact_owner = isset($_definition->plugin_options['contact_owner']) ?
78 78
 			$_definition->plugin_options['contact_owner'] : $this->user;
79 79
 
80 80
 		// Check to make sure target addressbook is valid
81
-		if(!in_array($contact_owner, array_keys($this->bocontacts->get_addressbooks(Api\Acl::ADD))))
81
+		if (!in_array($contact_owner, array_keys($this->bocontacts->get_addressbooks(Api\Acl::ADD))))
82 82
 		{
83 83
 			$this->warnings[0] = lang("Unable to import into %1, using %2",
84
-				$contact_owner . ' ('.Api\Accounts::username($record->owner) . ')',
84
+				$contact_owner.' ('.Api\Accounts::username($record->owner).')',
85 85
 				Api\Accounts::username($this->user)
86 86
 			);
87 87
 			$contact_owner = 'personal';
88 88
 		}
89 89
 
90 90
 		// Import into importer's personal addressbook
91
-		if($contact_owner == 'personal')
91
+		if ($contact_owner == 'personal')
92 92
 		{
93 93
 			$contact_owner = $this->user;
94 94
 		}
@@ -105,15 +105,15 @@  discard block
 block discarded – undo
105 105
 	 *
106 106
 	 * @return boolean success
107 107
 	 */
108
-	protected function import_record(importexport_iface_egw_record &$record, &$import_csv)
108
+	protected function import_record(importexport_iface_egw_record&$record, &$import_csv)
109 109
 	{
110 110
 
111 111
 		// Set owner, unless it's supposed to come from CSV file
112
-		if($this->definition->plugin_options['owner_from_csv'] && $record->owner) {
113
-			if(!is_numeric($record->owner)) {
112
+		if ($this->definition->plugin_options['owner_from_csv'] && $record->owner) {
113
+			if (!is_numeric($record->owner)) {
114 114
 				// Automatically handle text owner without explicit translation
115 115
 				$new_owner = importexport_helper_functions::account_name2id($record->owner);
116
-				if($new_owner == '') {
116
+				if ($new_owner == '') {
117 117
 					$this->errors[$import_csv->get_current_position()] = lang(
118 118
 						'Unable to convert "%1" to account ID.  Using plugin setting (%2) for owner.',
119 119
 						$record->owner,
@@ -129,7 +129,7 @@  discard block
 block discarded – undo
129 129
 		}
130 130
 
131 131
 		// Check that owner (addressbook) is allowed
132
-		if(!array_key_exists($record->owner, $this->bocontacts->get_addressbooks()))
132
+		if (!array_key_exists($record->owner, $this->bocontacts->get_addressbooks()))
133 133
 		{
134 134
 			$this->errors[$import_csv->get_current_position()] = lang("Unable to import into %1, using %2",
135 135
 				Api\Accounts::username($record->owner),
@@ -140,18 +140,18 @@  discard block
 block discarded – undo
140 140
 
141 141
 		// Do not allow owner == 0 (accounts) without an account_id
142 142
 		// It causes the contact to be filed as an account, and can't delete
143
-		if(!$record->owner && !$record->account_id)
143
+		if (!$record->owner && !$record->account_id)
144 144
 		{
145 145
 			$record->owner = $GLOBALS['egw_info']['user']['account_id'];
146 146
 		}
147 147
 
148 148
 		// Do not import into non-existing type, warn and change
149
-		if(!$record->tid || !$this->lookups['tid'][$record->tid])
149
+		if (!$record->tid || !$this->lookups['tid'][$record->tid])
150 150
 		{
151 151
 			// Avoid lots of warnings about type (2 types are contact and deleted)
152
-			if($record->tid && !$this->type_warned[$record->tid] && !$this->lookups['tid'][$record->tid] )
152
+			if ($record->tid && !$this->type_warned[$record->tid] && !$this->lookups['tid'][$record->tid])
153 153
 			{
154
-				$this->warnings[$import_csv->get_current_position()] = lang('Unknown type %1, imported as %2',$record->tid,lang($this->lookups['tid']['n']));
154
+				$this->warnings[$import_csv->get_current_position()] = lang('Unknown type %1, imported as %2', $record->tid, lang($this->lookups['tid']['n']));
155 155
 				$this->type_warned[$record->tid] = true;
156 156
 			}
157 157
 			$record->tid = $this->default_type;
@@ -160,45 +160,45 @@  discard block
 block discarded – undo
160 160
 		// Also handle categories in their own field
161 161
 		$record_array = $record->get_record_array();
162 162
 		$more_categories = array();
163
-		foreach($this->definition->plugin_options['field_mapping'] as $field_name) {
164
-			if(!array_key_exists($field_name, $record_array) ||
165
-				substr($field_name,0,3) != 'cat' || !$record->$field_name || $field_name == 'cat_id') continue;
163
+		foreach ($this->definition->plugin_options['field_mapping'] as $field_name) {
164
+			if (!array_key_exists($field_name, $record_array) ||
165
+				substr($field_name, 0, 3) != 'cat' || !$record->$field_name || $field_name == 'cat_id') continue;
166 166
 			list(, $cat_id) = explode('-', $field_name);
167
-			if(is_numeric($record->$field_name) && $record->$field_name != 1) {
167
+			if (is_numeric($record->$field_name) && $record->$field_name != 1) {
168 168
 				// Column has a single category ID
169 169
 				$more_categories[] = $record->$field_name;
170
-			} elseif($record->$field_name == '1' ||
170
+			} elseif ($record->$field_name == '1' ||
171 171
 				(!is_numeric($record->$field_name) && strtolower($record->$field_name) == strtolower(lang('Yes')))) {
172 172
 				// Each category got its own column.  '1' is the database value, lang('yes') is the human value
173 173
 				$more_categories[] = $cat_id;
174 174
 			} else {
175 175
 				// Text categories
176
-				$more_categories = array_merge($more_categories, importexport_helper_functions::cat_name2id(is_array($record->$field_name) ? $record->$field_name : explode(',',$record->$field_name), $cat_id));
176
+				$more_categories = array_merge($more_categories, importexport_helper_functions::cat_name2id(is_array($record->$field_name) ? $record->$field_name : explode(',', $record->$field_name), $cat_id));
177 177
 			}
178 178
 		}
179
-		if(count($more_categories) > 0) $record->cat_id = array_merge(is_array($record->cat_id) ? $record->cat_id : explode(',',$record->cat_id), $more_categories);
179
+		if (count($more_categories) > 0) $record->cat_id = array_merge(is_array($record->cat_id) ? $record->cat_id : explode(',', $record->cat_id), $more_categories);
180 180
 
181 181
 		// Private set but missing causes hidden entries
182
-		if(array_key_exists('private', $record_array) && (!isset($record_array['private']) || $record_array['private'] == '')) unset($record->private);
182
+		if (array_key_exists('private', $record_array) && (!isset($record_array['private']) || $record_array['private'] == '')) unset($record->private);
183 183
 
184 184
 		// Format birthday as backend requires - converter should give timestamp
185
-		if($record->bday && is_numeric($record->bday))
185
+		if ($record->bday && is_numeric($record->bday))
186 186
 		{
187 187
 			$time = new Api\DateTime($record->bday);
188 188
 			$record->bday = $time->format('Y-m-d');
189 189
 		}
190 190
 
191
-		if ( $this->definition->plugin_options['conditions'] ) {
192
-			foreach ( $this->definition->plugin_options['conditions'] as $condition ) {
191
+		if ($this->definition->plugin_options['conditions']) {
192
+			foreach ($this->definition->plugin_options['conditions'] as $condition) {
193 193
 				$contacts = array();
194
-				switch ( $condition['type'] ) {
194
+				switch ($condition['type']) {
195 195
 					// exists
196 196
 					case 'exists' :
197
-						if($record_array[$condition['string']]) {
198
-							$searchcondition = array( $condition['string'] => $record_array[$condition['string']]);
197
+						if ($record_array[$condition['string']]) {
198
+							$searchcondition = array($condition['string'] => $record_array[$condition['string']]);
199 199
 							// if we use account_id for the condition, we need to set the owner for filtering, as this
200 200
 							// enables Api\Contacts\Storage to decide what backend is to be used
201
-							if ($condition['string']=='account_id') $searchcondition['owner']=0;
201
+							if ($condition['string'] == 'account_id') $searchcondition['owner'] = 0;
202 202
 							$contacts = $this->bocontacts->search(
203 203
 								//array( $condition['string'] => $record[$condition['string']],),
204 204
 								'',
@@ -207,37 +207,37 @@  discard block
 block discarded – undo
207 207
 								$searchcondition
208 208
 							);
209 209
 						}
210
-						if ( is_array( $contacts ) && count( array_keys( $contacts ) ) >= 1 ) {
210
+						if (is_array($contacts) && count(array_keys($contacts)) >= 1) {
211 211
 							// apply action to all contacts matching this exists condition
212 212
 							$action = $condition['true'];
213
-							foreach ( (array)$contacts as $contact ) {
213
+							foreach ((array)$contacts as $contact) {
214 214
 								$record->id = $contact['id'];
215
-								if ( $this->definition->plugin_options['update_cats'] == 'add' ) {
216
-									if ( !is_array( $contact['cat_id'] ) ) $contact['cat_id'] = explode( ',', $contact['cat_id'] );
217
-									if ( !is_array( $record_array['cat_id'] ) ) $record->cat_id = explode( ',', $record->cat_id );
218
-									$record->cat_id = implode( ',', array_unique( array_merge( $record->cat_id, $contact['cat_id'] ) ) );
215
+								if ($this->definition->plugin_options['update_cats'] == 'add') {
216
+									if (!is_array($contact['cat_id'])) $contact['cat_id'] = explode(',', $contact['cat_id']);
217
+									if (!is_array($record_array['cat_id'])) $record->cat_id = explode(',', $record->cat_id);
218
+									$record->cat_id = implode(',', array_unique(array_merge($record->cat_id, $contact['cat_id'])));
219 219
 								}
220
-								$success = $this->action(  $action['action'], $record, $import_csv->get_current_position() );
220
+								$success = $this->action($action['action'], $record, $import_csv->get_current_position());
221 221
 							}
222 222
 						} else {
223 223
 							$action = $condition['false'];
224
-							$success = ($this->action(  $action['action'], $record, $import_csv->get_current_position() ));
224
+							$success = ($this->action($action['action'], $record, $import_csv->get_current_position()));
225 225
 						}
226 226
 						break;
227 227
 					case 'equal':
228 228
 						// Match on field
229 229
 						$result = $this->equal($record, $condition);
230
-						if($result)
230
+						if ($result)
231 231
 						{
232 232
 							// Apply true action to any matching records found
233 233
 							$action = $condition['true'];
234
-							$success = ($this->action(  $action['action'], $record, $import_csv->get_current_position() ));
234
+							$success = ($this->action($action['action'], $record, $import_csv->get_current_position()));
235 235
 						}
236 236
 						else
237 237
 						{
238 238
 							// Apply false action if no matching records found
239 239
 							$action = $condition['false'];
240
-							$success = ($this->action(  $action['action'], $record, $import_csv->get_current_position() ));
240
+							$success = ($this->action($action['action'], $record, $import_csv->get_current_position()));
241 241
 						}
242 242
 						break;
243 243
 
@@ -249,7 +249,7 @@  discard block
 block discarded – undo
249 249
 			}
250 250
 		} else {
251 251
 			// unconditional insert
252
-			$success = $this->action( 'insert', $record, $import_csv->get_current_position() );
252
+			$success = $this->action('insert', $record, $import_csv->get_current_position());
253 253
 		}
254 254
 		return $success;
255 255
 	}
@@ -261,21 +261,21 @@  discard block
 block discarded – undo
261 261
 	 * @param importexport_iface_egw_record $record contact data for the action
262 262
 	 * @return bool success or not
263 263
 	 */
264
-	protected function action ( $_action, importexport_iface_egw_record &$record, $record_num = 0 ) {
264
+	protected function action($_action, importexport_iface_egw_record&$record, $record_num = 0) {
265 265
 		$_data = $record->get_record_array();
266 266
 		switch ($_action) {
267 267
 			case 'none' :
268 268
 				return true;
269 269
 			case 'delete':
270
-				if($_data['id'])
270
+				if ($_data['id'])
271 271
 				{
272
-					if ( $this->dry_run ) {
272
+					if ($this->dry_run) {
273 273
 						//print_r($_data);
274 274
 						$this->results[$_action]++;
275 275
 						return true;
276 276
 					}
277 277
 					$result = $this->bocontacts->delete($_data);
278
-					if($result && $result === true)
278
+					if ($result && $result === true)
279 279
 					{
280 280
 						$this->results[$_action]++;
281 281
 					}
@@ -290,14 +290,14 @@  discard block
 block discarded – undo
290 290
 				// Only update if there are changes
291 291
 				$old = $this->bocontacts->read($_data['id']);
292 292
 				// if we get countrycodes as countryname, try to translate them -> the rest should be handled by bo classes.
293
-				foreach(array('adr_one_', 'adr_two_') as $c_prefix) {
294
-					if (strlen(trim($_data[$c_prefix.'countryname']))==2)
293
+				foreach (array('adr_one_', 'adr_two_') as $c_prefix) {
294
+					if (strlen(trim($_data[$c_prefix.'countryname'])) == 2)
295 295
 						$_data[$c_prefix.'countryname'] = $GLOBALS['egw']->country->get_full_name(trim($_data[$c_prefix.'countryname']), true);
296 296
 				}
297 297
 				// Don't change a user account into a contact
298
-				if($old['owner'] == 0) {
298
+				if ($old['owner'] == 0) {
299 299
 					unset($_data['owner']);
300
-				} elseif(!$this->definition->plugin_options['change_owner']) {
300
+				} elseif (!$this->definition->plugin_options['change_owner']) {
301 301
 					// Don't change addressbook of an existing contact
302 302
 					unset($_data['owner']);
303 303
 				}
@@ -305,7 +305,7 @@  discard block
 block discarded – undo
305 305
 				// Merge to deal with fields not in import record
306 306
 				$_data = array_merge($old, $_data);
307 307
 				$changed = $this->tracking->changed_fields($_data, $old);
308
-				if(count($changed) == 0) {
308
+				if (count($changed) == 0) {
309 309
 					return true;
310 310
 				} else {
311 311
 					//error_log(__METHOD__.__LINE__.array2string($changed).' Old:'.$old['adr_one_countryname'].' ('.$old['adr_one_countrycode'].') New:'.$_data['adr_one_countryname'].' ('.$_data['adr_one_countryname'].')');
@@ -316,22 +316,22 @@  discard block
 block discarded – undo
316 316
 
317 317
 				// Fall through
318 318
 			case 'insert' :
319
-				if($_action == 'insert') {
319
+				if ($_action == 'insert') {
320 320
 					// Addressbook backend doesn't like inserting with ID specified, it screws up the owner & etag
321 321
 					unset($_data['id']);
322 322
 				}
323
-				if(!isset($_data['org_name'])) {
323
+				if (!isset($_data['org_name'])) {
324 324
 					// org_name is a trigger to update n_fileas
325 325
 					$_data['org_name'] = '';
326 326
 				}
327 327
 
328
-				if ( $this->dry_run ) {
328
+				if ($this->dry_run) {
329 329
 					//print_r($_data);
330 330
 					$this->results[$_action]++;
331 331
 					return true;
332 332
 				} else {
333
-					$result = $this->bocontacts->save( $_data, $this->is_admin);
334
-					if(!$result) {
333
+					$result = $this->bocontacts->save($_data, $this->is_admin);
334
+					if (!$result) {
335 335
 						$this->errors[$record_num] = $this->bocontacts->error;
336 336
 					} else {
337 337
 						$this->results[$_action]++;
@@ -341,7 +341,7 @@  discard block
 block discarded – undo
341 341
 					return $result;
342 342
 				}
343 343
 			default:
344
-				throw new Api\Exception('Unsupported action: '. $_action);
344
+				throw new Api\Exception('Unsupported action: '.$_action);
345 345
 
346 346
 		}
347 347
 	}
Please login to merge, or discard this patch.