1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* eGroupWare |
4
|
|
|
* |
5
|
|
|
* Plugin to import events from a CSV file |
6
|
|
|
* |
7
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License |
8
|
|
|
* @package calendar |
9
|
|
|
* @subpackage importexport |
10
|
|
|
* @link http://www.egroupware.org |
11
|
|
|
* @author Nathan Gray |
12
|
|
|
* @copyright Nathan Gray |
13
|
|
|
* @version $Id$ |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* class import_csv for calendar |
19
|
|
|
*/ |
20
|
|
|
class calendar_import_csv extends importexport_basic_import_csv { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* actions wich could be done to data entries |
24
|
|
|
*/ |
25
|
|
|
protected static $actions = array( 'none', 'update', 'insert' ); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* conditions for actions |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected static $conditions = array('exists'); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* For figuring out if an entry has changed |
36
|
|
|
*/ |
37
|
|
|
protected $tracking; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* List of import warnings |
41
|
|
|
*/ |
42
|
|
|
protected $warnings = array(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Set up tracker |
46
|
|
|
*/ |
47
|
|
|
protected function init(importexport_definition &$definition) |
48
|
|
|
{ |
49
|
|
|
// fetch the addressbook bo |
50
|
|
|
$this->bo= new calendar_boupdate(); |
51
|
|
|
|
52
|
|
|
// Get the tracker for changes |
53
|
|
|
$this->tracking = new calendar_tracking(); |
54
|
|
|
|
55
|
|
|
// Used for participants |
56
|
|
|
$this->status_map = array_flip(array_map('lang',$this->bo->verbose_status)); |
57
|
|
|
$this->role_map = array_flip($this->bo->roles); |
58
|
|
|
|
59
|
|
|
$this->lookups = array( |
60
|
|
|
'priority' => Array( |
61
|
|
|
0 => lang('None'), |
62
|
|
|
1 => lang('Low'), |
63
|
|
|
2 => lang('Normal'), |
64
|
|
|
3 => lang('High') |
65
|
|
|
), |
66
|
|
|
'recurrence' => $this->bo->recur_types |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* imports a single entry according to given definition object. |
72
|
|
|
* Handles the conditions and the actions taken. |
73
|
|
|
* |
74
|
|
|
* @param importepport_iface_egw_record record The egw_record object being imported |
75
|
|
|
* @param importexport_iface_import_record import_csv Import object contains current state |
76
|
|
|
* |
77
|
|
|
* @return boolean success |
78
|
|
|
*/ |
79
|
|
|
public function import_record(\importexport_iface_egw_record &$record, &$import_csv) |
80
|
|
|
{ |
81
|
|
|
// set eventOwner |
82
|
|
|
$options =& $this->definition->plugin_options; |
83
|
|
|
$options['owner'] = $options['owner'] ? $options['owner'] : $this->user; |
84
|
|
|
|
85
|
|
|
// Set owner, unless it's supposed to come from CSV file |
86
|
|
|
if($options['owner_from_csv']) { |
87
|
|
|
if(!is_numeric($record['owner'])) { |
88
|
|
|
$this->errors[$import_csv->get_current_position()] = lang( |
89
|
|
|
'Invalid owner ID: %1. Might be a bad field translation. Used %2 instead.', |
90
|
|
|
$record->owner, |
|
|
|
|
91
|
|
|
$options['owner'] |
92
|
|
|
); |
93
|
|
|
$record->owner = $options['owner']; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
else |
97
|
|
|
{ |
98
|
|
|
$record->owner = $options['owner']; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Handle errors in length or start/end date |
102
|
|
|
if($record->start > $record->end) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$record->end = $record->start + $GLOBALS['egw_info']['user']['preferences']['calendar']['defaultlength'] * 60; |
|
|
|
|
105
|
|
|
$this->warnings[$import_csv->get_current_position()] = lang('error: starttime has to be before the endtime !!!'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Parse particpants |
109
|
|
|
if ($record->participants && !is_array($record->participants)) { |
|
|
|
|
110
|
|
|
// Importing participants in human friendly format: |
111
|
|
|
// Name (quantity)? (status) Role[, Name (quantity)? (status) Role]+ |
112
|
|
|
preg_match_all('/(([^(]+?)(?: \(([\d]+)\))? \(([^,)]+)\)(?: ([^ ,]+))?)(?:, )?/',$record->participants,$participants); |
|
|
|
|
113
|
|
|
$p_participants = array(); |
114
|
|
|
$missing = array(); |
115
|
|
|
list($lines, $p, $names, $quantity, $status, $role) = $participants; |
|
|
|
|
116
|
|
|
foreach($names as $key => $name) { |
117
|
|
|
//error_log("Name: $name Quantity: {$quantity[$key]} Status: {$status[$key]} Role: {$role[$key]}"); |
118
|
|
|
|
119
|
|
|
// Search for direct account name, then user in accounts first |
120
|
|
|
$search = "\"$name\""; |
121
|
|
|
$id = importexport_helper_functions::account_name2id($name); |
122
|
|
|
|
123
|
|
|
// If not found, or not an exact match to a user (account_name2id is pretty generous) |
124
|
|
|
if(!$id || $names[$key] !== $this->bo->participant_name($id)) { |
125
|
|
|
$contacts = ExecMethod2('addressbook.addressbook_bo.search', $search,array('contact_id','account_id'),'org_name,n_family,n_given,cat_id,contact_email','','%',false,'OR',array(0,1)); |
126
|
|
|
if($contacts) $id = $contacts[0]['account_id'] ? $contacts[0]['account_id'] : 'c'.$contacts[0]['contact_id']; |
127
|
|
|
} |
128
|
|
|
if(!$id) |
129
|
|
|
{ |
130
|
|
|
// Use calendar's registered resources to find participant |
131
|
|
|
foreach($this->bo->resources as $resource) |
132
|
|
|
{ |
133
|
|
|
// Can't search for email |
134
|
|
|
if($resource['app'] == 'email') continue; |
135
|
|
|
// Special resource search, since it does special stuff in link_query |
136
|
|
|
if($resource['app'] == 'resources') |
137
|
|
|
{ |
138
|
|
|
if(!$this->resource_so) |
139
|
|
|
{ |
140
|
|
|
$this->resource_so = new resources_so(); |
141
|
|
|
} |
142
|
|
|
$result = $this->resource_so->search($search,'res_id'); |
143
|
|
|
if(count($result) >= 1) { |
144
|
|
|
$id = $resource['type'].$result[0]['res_id']; |
145
|
|
|
break; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
else |
149
|
|
|
{ |
150
|
|
|
// Search app via link query |
151
|
|
|
$link_options = array(); |
152
|
|
|
$result = Link::query($resource['app'], $search, $link_options); |
153
|
|
|
|
154
|
|
|
if($result) |
155
|
|
|
{ |
156
|
|
|
$id = $resource['type'] . key($result); |
157
|
|
|
break; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
if($id) { |
163
|
|
|
$p_participants[$id] = calendar_so::combine_status( |
164
|
|
|
$this->status_map[lang($status[$key])] ? $this->status_map[lang($status[$key])] : $status[$key][0], |
165
|
|
|
$quantity[$key] ? $quantity[$key] : 1, |
166
|
|
|
$this->role_map[lang($role[$key])] ? $this->role_map[lang($role[$key])] : $role[$key] |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
else |
170
|
|
|
{ |
171
|
|
|
$missing[] = $name; |
172
|
|
|
} |
173
|
|
|
if(count($missing) > 0) |
174
|
|
|
{ |
175
|
|
|
$this->warnings[$import_csv->get_current_position()] = $record->title . ' ' . lang('participants') . ': ' . |
|
|
|
|
176
|
|
|
lang('Contact not found!') . '<br />'.implode(", ",$missing); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
$record->participants = $p_participants; |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if($record->recurrence) |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
list($record->recur_type, $record->recur_interval) = explode('/',$record->recurrence,2); |
|
|
|
|
185
|
|
|
$record->recur_interval = trim($record->recur_interval); |
|
|
|
|
186
|
|
|
$record->recur_type = array_search(strtolower(trim($record->recur_type)), array_map('strtolower',$this->lookups['recurrence'])); |
|
|
|
|
187
|
|
|
unset($record->recurrence); |
188
|
|
|
} |
189
|
|
|
$record->tzid = calendar_timezones::id2tz($record->tz_id); |
|
|
|
|
190
|
|
|
|
191
|
|
|
if ( $options['conditions'] ) { |
192
|
|
|
foreach ( $options['conditions'] as $condition ) { |
193
|
|
|
$records = array(); |
194
|
|
|
switch ( $condition['type'] ) { |
195
|
|
|
// exists |
196
|
|
|
case 'exists' : |
197
|
|
|
// Check for that record |
198
|
|
|
$result = $this->exists($record, $condition, $records); |
199
|
|
|
|
200
|
|
|
if ( is_array( $records ) && count( $records ) >= 1) { |
201
|
|
|
// apply action to all records matching this exists condition |
202
|
|
|
$action = $condition['true']; |
203
|
|
|
foreach ( (array)$records as $event ) { |
204
|
|
|
$record->id = $event['id']; |
|
|
|
|
205
|
|
|
if ( $this->definition->plugin_options['update_cats'] == 'add' ) { |
206
|
|
|
if ( !is_array( $record->category ) ) $record->category = explode( ',', $record->category ); |
|
|
|
|
207
|
|
|
$record->category = implode( ',', array_unique( array_merge( $record->category, $event['category'] ) ) ); |
|
|
|
|
208
|
|
|
} |
209
|
|
|
$success = $this->action( $action['action'], $record, $import_csv->get_current_position() ); |
210
|
|
|
} |
211
|
|
|
} else { |
212
|
|
|
$action = $condition['false']; |
213
|
|
|
$success = ($this->action( $action['action'], $record, $import_csv->get_current_position() )); |
214
|
|
|
} |
215
|
|
|
break; |
216
|
|
|
|
217
|
|
|
// not supported action |
218
|
|
|
default : |
219
|
|
|
die('condition / action not supported!!!'); |
220
|
|
|
break; |
|
|
|
|
221
|
|
|
} |
222
|
|
|
if ($action['last']) break; |
223
|
|
|
} |
224
|
|
|
} else { |
225
|
|
|
// unconditional insert |
226
|
|
|
$success = $this->action( 'insert', $record, $import_csv->get_current_position() ); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $success; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Search for matching records, based on the the given condition |
234
|
|
|
* |
235
|
|
|
* @param record |
236
|
|
|
* @param condition array = array('string' => field name) |
237
|
|
|
* @param matches - On return, will be filled with matching records |
238
|
|
|
* |
239
|
|
|
* @return boolean |
240
|
|
|
*/ |
241
|
|
|
protected function exists(importexport_iface_egw_record &$record, Array &$condition, &$records = array()) |
242
|
|
|
{ |
243
|
|
|
if($record->__get($condition['string']) && $condition['string'] == 'id') { |
244
|
|
|
$event = $this->bo->read($record->__get($condition['string'])); |
245
|
|
|
$records = array($event); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
if ( is_array( $records ) && count( $records ) >= 1) { |
249
|
|
|
return true; |
250
|
|
|
} |
251
|
|
|
return false; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* perform the required action |
256
|
|
|
* |
257
|
|
|
* @param int $_action one of $this->actions |
258
|
|
|
* @param array $_data record data for the action |
259
|
|
|
* @return bool success or not |
260
|
|
|
*/ |
261
|
|
|
protected function action ( $_action, importexport_iface_egw_record &$record, $record_num = 0 ) |
262
|
|
|
{ |
263
|
|
|
$_data = $record->get_record_array(); |
264
|
|
|
switch ($_action) { |
265
|
|
|
case 'none' : |
266
|
|
|
return true; |
267
|
|
|
case 'update' : |
268
|
|
|
// Only update if there are changes |
269
|
|
|
$old = $this->bo->read($_data['id']); |
270
|
|
|
|
271
|
|
|
// Don't change a user account into a record |
272
|
|
|
if(!$this->definition->plugin_options['change_owner']) { |
273
|
|
|
// Don't change owner of an existing record |
274
|
|
|
unset($_data['owner']); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
// Merge to deal with fields not in import record |
278
|
|
|
$_data = array_merge($old, $_data); |
279
|
|
|
$changed = $this->tracking->changed_fields($_data, $old); |
280
|
|
|
if(count($changed) == 0) { |
281
|
|
|
return true; |
282
|
|
|
} |
283
|
|
|
// Fall through |
284
|
|
|
case 'insert' : |
285
|
|
|
if($_action == 'insert') { |
286
|
|
|
// Backend doesn't like inserting with ID specified, can overwrite existing |
287
|
|
|
unset($_data['id']); |
288
|
|
|
} |
289
|
|
|
// Make sure participants are set |
290
|
|
|
if(!$_data['participants']) { |
291
|
|
|
$user = $_data['owner'] ? $_data['owner'] : $this->user; |
292
|
|
|
$_data['participants'] = array( |
293
|
|
|
$user => 'U' |
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
if ( $this->dry_run ) { |
297
|
|
|
//print_r($_data); |
298
|
|
|
// User is interested in conflict checks, do so for dry run |
299
|
|
|
// Otherwise, conflicts are just ignored and imported anyway |
300
|
|
|
if($this->definition->plugin_options['skip_conflicts'] && !$_data['non_blocking']) |
301
|
|
|
{ |
302
|
|
|
$conflicts = $this->bo->conflicts($_data); |
303
|
|
|
if($conflicts) |
304
|
|
|
{ |
305
|
|
|
$this->conflict_warning($record_num, $conflicts); |
306
|
|
|
$this->results['skipped']++; |
307
|
|
|
return false; |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
$this->results[$_action]++; |
311
|
|
|
return true; |
312
|
|
|
} else { |
313
|
|
|
$messages = null; |
314
|
|
|
$result = $this->bo->update( $_data, |
315
|
|
|
!$this->definition->plugin_options['skip_conflicts'], |
316
|
|
|
true, $this->is_admin, true, $messages, |
317
|
|
|
$this->definition->plugin_options['no_notification'] |
318
|
|
|
); |
319
|
|
|
if(!$result) |
320
|
|
|
{ |
321
|
|
|
$this->errors[$record_num] = lang('Unable to save') . "\n" . |
322
|
|
|
implode("\n",$messages); |
323
|
|
|
} |
324
|
|
|
else if (is_array($result)) |
325
|
|
|
{ |
326
|
|
|
$this->conflict_warning($record_num, $result); |
327
|
|
|
$this->results['skipped']++; |
328
|
|
|
return false; |
329
|
|
|
} |
330
|
|
|
else |
331
|
|
|
{ |
332
|
|
|
$this->results[$_action]++; |
333
|
|
|
// This does nothing (yet?) but update the identifier |
334
|
|
|
$record->save($result); |
335
|
|
|
} |
336
|
|
|
return $result; |
337
|
|
|
} |
338
|
|
|
default: |
339
|
|
|
throw new egw_exception('Unsupported action'); |
340
|
|
|
|
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Add a warning message about conflicting events |
346
|
|
|
* |
347
|
|
|
* @param int $record_num Current record index |
348
|
|
|
* @param Array $conflicts List of found conflicting events |
349
|
|
|
*/ |
350
|
|
|
protected function conflict_warning($record_num, &$conflicts) |
351
|
|
|
{ |
352
|
|
|
$this->warnings[$record_num] = lang('Conflicts') . ':'; |
353
|
|
|
foreach($conflicts as $conflict) |
354
|
|
|
{ |
355
|
|
|
$this->warnings[$record_num] .= "<br />\n" . Api\DateTime::to($conflict['start']) . "\t" . $conflict['title']; |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* returns translated name of plugin |
361
|
|
|
* |
362
|
|
|
* @return string name |
363
|
|
|
*/ |
364
|
|
|
public static function get_name() { |
365
|
|
|
return lang('Calendar CSV import'); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* returns translated (user) description of plugin |
370
|
|
|
* |
371
|
|
|
* @return string descriprion |
372
|
|
|
*/ |
373
|
|
|
public static function get_description() { |
374
|
|
|
return lang("Imports events into your Calendar from a CSV File. CSV means 'Comma Seperated Values'. However in the options Tab you can also choose other seperators."); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* retruns file suffix(s) plugin can handle (e.g. csv) |
379
|
|
|
* |
380
|
|
|
* @return string suffix (comma seperated) |
381
|
|
|
*/ |
382
|
|
|
public static function get_filesuffix() { |
383
|
|
|
return 'csv'; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Alter a row for preview to show multiple participants instead of Array |
388
|
|
|
* |
389
|
|
|
* @param egw_record $row_entry |
390
|
|
|
*/ |
391
|
|
|
protected function row_preview(importexport_iface_egw_record &$row_entry) |
392
|
|
|
{ |
393
|
|
|
$row_entry->participants = implode('<br />', $this->bo->participants(array('participants' => $row_entry->participants),true)); |
|
|
|
|
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
} // end of iface_export_plugin |
397
|
|
|
?> |
398
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: