Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | require_once 'CRM/Utils/DeprecatedUtils.php'; | |
37 | ||
38 | /** | |
39 | * class to parse membership csv files | |
40 | */ | |
41 | class CRM_Event_Import_Parser_Participant extends CRM_Event_Import_Parser { | |
42 | protected $_mapperKeys; | |
43 | ||
44 | private $_contactIdIndex; | |
45 | ||
46 | //private $_totalAmountIndex; | |
47 | ||
48 | private $_eventIndex; | |
49 | private $_participantStatusIndex; | |
50 | private $_participantRoleIndex; | |
51 | private $_eventTitleIndex; | |
52 | ||
53 | /** | |
ceb10dc7 | 54 | * Array of successfully imported participants id's |
6a488035 TO |
55 | * |
56 | * @array | |
57 | */ | |
58 | protected $_newParticipants; | |
59 | ||
60 | /** | |
100fef9d | 61 | * Class constructor |
6a488035 | 62 | */ |
00be9182 | 63 | public function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) { |
6a488035 TO |
64 | parent::__construct(); |
65 | $this->_mapperKeys = &$mapperKeys; | |
66 | } | |
67 | ||
68 | /** | |
100fef9d | 69 | * The initializer code, called before the processing |
6a488035 TO |
70 | * |
71 | * @return void | |
6a488035 | 72 | */ |
00be9182 | 73 | public function init() { |
6a488035 TO |
74 | $fields = CRM_Event_BAO_Participant::importableFields($this->_contactType, FALSE); |
75 | $fields['event_id']['title'] = 'Event ID'; | |
76 | $eventfields = &CRM_Event_BAO_Event::fields(); | |
77 | $fields['event_title'] = $eventfields['event_title']; | |
78 | ||
79 | foreach ($fields as $name => $field) { | |
80 | $field['type'] = CRM_Utils_Array::value('type', $field, CRM_Utils_Type::T_INT); | |
81 | $field['dataPattern'] = CRM_Utils_Array::value('dataPattern', $field, '//'); | |
82 | $field['headerPattern'] = CRM_Utils_Array::value('headerPattern', $field, '//'); | |
83 | $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']); | |
84 | } | |
85 | ||
86 | $this->_newParticipants = array(); | |
87 | $this->setActiveFields($this->_mapperKeys); | |
88 | ||
89 | // FIXME: we should do this in one place together with Form/MapField.php | |
90 | $this->_contactIdIndex = -1; | |
91 | $this->_eventIndex = -1; | |
92 | $this->_participantStatusIndex = -1; | |
93 | $this->_participantRoleIndex = -1; | |
94 | $this->_eventTitleIndex = -1; | |
95 | ||
96 | $index = 0; | |
97 | foreach ($this->_mapperKeys as $key) { | |
98 | ||
99 | switch ($key) { | |
100 | case 'participant_contact_id': | |
101 | $this->_contactIdIndex = $index; | |
102 | break; | |
103 | ||
104 | case 'event_id': | |
105 | $this->_eventIndex = $index; | |
106 | break; | |
107 | ||
108 | case 'participant_status': | |
109 | case 'participant_status_id': | |
110 | $this->_participantStatusIndex = $index; | |
111 | break; | |
112 | ||
113 | case 'participant_role_id': | |
114 | $this->_participantRoleIndex = $index; | |
115 | break; | |
116 | ||
117 | case 'event_title': | |
118 | $this->_eventTitleIndex = $index; | |
119 | break; | |
120 | } | |
121 | $index++; | |
122 | } | |
123 | } | |
124 | ||
125 | /** | |
100fef9d | 126 | * Handle the values in mapField mode |
6a488035 TO |
127 | * |
128 | * @param array $values the array of values belonging to this line | |
129 | * | |
130 | * @return boolean | |
6a488035 | 131 | */ |
00be9182 | 132 | public function mapField(&$values) { |
a05662ef | 133 | return CRM_Import_Parser::VALID; |
6a488035 TO |
134 | } |
135 | ||
136 | /** | |
100fef9d | 137 | * Handle the values in preview mode |
6a488035 TO |
138 | * |
139 | * @param array $values the array of values belonging to this line | |
140 | * | |
141 | * @return boolean the result of this processing | |
6a488035 | 142 | */ |
00be9182 | 143 | public function preview(&$values) { |
6a488035 TO |
144 | return $this->summary($values); |
145 | } | |
146 | ||
147 | /** | |
100fef9d | 148 | * Handle the values in summary mode |
6a488035 TO |
149 | * |
150 | * @param array $values the array of values belonging to this line | |
151 | * | |
152 | * @return boolean the result of this processing | |
6a488035 | 153 | */ |
00be9182 | 154 | public function summary(&$values) { |
6a488035 TO |
155 | $erroneousField = NULL; |
156 | ||
157 | $response = $this->setActiveFieldValues($values, $erroneousField); | |
158 | $errorRequired = FALSE; | |
159 | $index = -1; | |
160 | ||
161 | if ($this->_eventIndex > -1 && $this->_eventTitleIndex > -1) { | |
162 | array_unshift($values, ts('Select either EventID OR Event Title')); | |
a05662ef | 163 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
164 | } |
165 | elseif ($this->_eventTitleIndex > -1) { | |
166 | $index = $this->_eventTitleIndex; | |
167 | } | |
168 | elseif ($this->_eventIndex > -1) { | |
169 | $index = $this->_eventIndex; | |
170 | } | |
171 | $params = &$this->getActiveFieldParams(); | |
172 | ||
173 | if (!(($index < 0) || ($this->_participantStatusIndex < 0))) { | |
174 | $errorRequired = !CRM_Utils_Array::value($this->_participantStatusIndex, $values); | |
8cc574cf | 175 | if (empty($params['event_id']) && empty($params['event_title'])) { |
719a6fec | 176 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Event', $missingField); |
6a488035 | 177 | } |
a7488080 | 178 | if (empty($params['participant_status_id'])) { |
719a6fec | 179 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status', $missingField); |
6a488035 TO |
180 | } |
181 | } | |
182 | else { | |
183 | $errorRequired = TRUE; | |
184 | $missingField = NULL; | |
185 | if ($index < 0) { | |
719a6fec | 186 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Event', $missingField); |
6a488035 TO |
187 | } |
188 | if ($this->_participantStatusIndex < 0) { | |
719a6fec | 189 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status', $missingField); |
6a488035 TO |
190 | } |
191 | } | |
192 | ||
193 | if ($errorRequired) { | |
194 | array_unshift($values, ts('Missing required field(s) :') . $missingField); | |
a05662ef | 195 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
196 | } |
197 | ||
198 | $errorMessage = NULL; | |
199 | ||
200 | //for date-Formats | |
201 | $session = CRM_Core_Session::singleton(); | |
202 | $dateType = $session->get('dateTypes'); | |
203 | ||
204 | foreach ($params as $key => $val) { | |
205 | if ($val && ($key == 'participant_register_date')) { | |
206 | if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) { | |
207 | $params[$key] = $dateValue; | |
208 | } | |
209 | else { | |
719a6fec | 210 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Register Date', $errorMessage); |
6a488035 TO |
211 | } |
212 | } | |
213 | elseif ($val && ($key == 'participant_role_id' || $key == 'participant_role')) { | |
214 | $roleIDs = CRM_Event_PseudoConstant::participantRole(); | |
215 | $val = explode(',', $val); | |
216 | if ($key == 'participant_role_id') { | |
217 | foreach ($val as $role) { | |
218 | if (!in_array(trim($role), array_keys($roleIDs))) { | |
719a6fec | 219 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Role Id', $errorMessage); |
6a488035 TO |
220 | break; |
221 | } | |
222 | } | |
223 | } | |
224 | else { | |
225 | foreach ($val as $role) { | |
719a6fec CW |
226 | if (!CRM_Contact_Import_Parser_Contact::in_value(trim($role), $roleIDs)) { |
227 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Role', $errorMessage); | |
6a488035 TO |
228 | break; |
229 | } | |
230 | } | |
231 | } | |
232 | } | |
233 | elseif ($val && (($key == 'participant_status_id') || ($key == 'participant_status'))) { | |
234 | $statusIDs = CRM_Event_PseudoConstant::participantStatus(); | |
235 | if ($key == 'participant_status_id') { | |
236 | if (!in_array(trim($val), array_keys($statusIDs))) { | |
719a6fec | 237 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status Id', $errorMessage); |
6a488035 TO |
238 | break; |
239 | } | |
240 | } | |
719a6fec CW |
241 | elseif (!CRM_Contact_Import_Parser_Contact::in_value($val, $statusIDs)) { |
242 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status', $errorMessage); | |
6a488035 TO |
243 | break; |
244 | } | |
245 | } | |
246 | } | |
247 | //date-Format part ends | |
248 | ||
249 | $params['contact_type'] = 'Participant'; | |
250 | //checking error in custom data | |
719a6fec | 251 | CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage); |
6a488035 TO |
252 | |
253 | if ($errorMessage) { | |
254 | $tempMsg = "Invalid value for field(s) : $errorMessage"; | |
255 | array_unshift($values, $tempMsg); | |
256 | $errorMessage = NULL; | |
a05662ef | 257 | return CRM_Import_Parser::ERROR; |
6a488035 | 258 | } |
a05662ef | 259 | return CRM_Import_Parser::VALID; |
6a488035 TO |
260 | } |
261 | ||
262 | /** | |
100fef9d | 263 | * Handle the values in import mode |
6a488035 TO |
264 | * |
265 | * @param int $onDuplicate the code for what action to take on duplicates | |
266 | * @param array $values the array of values belonging to this line | |
267 | * | |
268 | * @return boolean the result of this processing | |
6a488035 | 269 | */ |
00be9182 | 270 | public function import($onDuplicate, &$values) { |
6a488035 TO |
271 | |
272 | // first make sure this is a valid line | |
273 | $response = $this->summary($values); | |
a05662ef | 274 | if ($response != CRM_Import_Parser::VALID) { |
6a488035 TO |
275 | return $response; |
276 | } | |
277 | $params = &$this->getActiveFieldParams(); | |
278 | $session = CRM_Core_Session::singleton(); | |
279 | $dateType = $session->get('dateTypes'); | |
280 | $formatted = array('version' => 3); | |
281 | $customFields = CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $params)); | |
282 | ||
283 | // don't add to recent items, CRM-4399 | |
284 | $formatted['skipRecentView'] = TRUE; | |
285 | ||
286 | foreach ($params as $key => $val) { | |
287 | if ($val) { | |
288 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
289 | if ($customFields[$customFieldID]['data_type'] == 'Date') { | |
719a6fec | 290 | CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key); |
6a488035 TO |
291 | unset($params[$key]); |
292 | } | |
293 | elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') { | |
294 | $params[$key] = CRM_Utils_String::strtoboolstr($val); | |
295 | } | |
296 | } | |
39d87d1e JM |
297 | if($key == 'participant_register_date') { |
298 | CRM_Utils_Date::convertToDefaultDate($params, $dateType, 'participant_register_date'); | |
299 | $formatted['participant_register_date'] = CRM_Utils_Date::processDate($params['participant_register_date']); | |
300 | } | |
6a488035 TO |
301 | } |
302 | } | |
303 | ||
8cc574cf | 304 | if (!(!empty($params['participant_role_id']) || !empty($params['participant_role']))) { |
a7488080 | 305 | if (!empty($params['event_id'])) { |
6a488035 TO |
306 | $params['participant_role_id'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'default_role_id'); |
307 | } | |
308 | else { | |
309 | $eventTitle = $params['event_title']; | |
310 | $qParams = array(); | |
311 | $dao = new CRM_Core_DAO(); | |
312 | $params['participant_role_id'] = $dao->singleValueQuery("SELECT default_role_id FROM civicrm_event WHERE title = '$eventTitle' ", | |
313 | $qParams | |
314 | ); | |
315 | } | |
316 | } | |
317 | ||
318 | //date-Format part ends | |
319 | static $indieFields = NULL; | |
320 | if ($indieFields == NULL) { | |
321 | $indieFields = CRM_Event_BAO_Participant::import(); | |
322 | } | |
323 | ||
324 | $formatValues = array(); | |
325 | foreach ($params as $key => $field) { | |
326 | if ($field == NULL || $field === '') { | |
327 | continue; | |
328 | } | |
329 | ||
330 | $formatValues[$key] = $field; | |
331 | } | |
332 | ||
333 | $formatError = _civicrm_api3_deprecated_participant_formatted_param($formatValues, $formatted, TRUE); | |
334 | ||
335 | if ($formatError) { | |
336 | array_unshift($values, $formatError['error_message']); | |
a05662ef | 337 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
338 | } |
339 | ||
340 | if (!CRM_Utils_Rule::integer($formatted['event_id'])) { | |
341 | array_unshift($values, ts('Invalid value for Event ID')); | |
a05662ef | 342 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
343 | } |
344 | ||
a05662ef | 345 | if ($onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) { |
6a488035 TO |
346 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, |
347 | CRM_Core_DAO::$_nullObject, | |
348 | NULL, | |
349 | 'Participant' | |
350 | ); | |
351 | } | |
352 | else { | |
353 | if ($formatValues['participant_id']) { | |
354 | $dao = new CRM_Event_BAO_Participant(); | |
355 | $dao->id = $formatValues['participant_id']; | |
356 | ||
357 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, | |
358 | CRM_Core_DAO::$_nullObject, | |
359 | $formatValues['participant_id'], | |
360 | 'Participant' | |
361 | ); | |
362 | if ($dao->find(TRUE)) { | |
363 | $ids = array( | |
364 | 'participant' => $formatValues['participant_id'], | |
365 | 'userId' => $session->get('userID'), | |
366 | ); | |
367 | $participantValues = array(); | |
368 | //@todo calling api functions directly is not supported | |
369 | $newParticipant = _civicrm_api3_deprecated_participant_check_params($formatted, $participantValues, FALSE); | |
370 | if ($newParticipant['error_message']) { | |
371 | array_unshift($values, $newParticipant['error_message']); | |
a05662ef | 372 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
373 | } |
374 | $newParticipant = CRM_Event_BAO_Participant::create($formatted, $ids); | |
a7488080 | 375 | if (!empty($formatted['fee_level'])) { |
6a488035 TO |
376 | $otherParams = array( |
377 | 'fee_label' => $formatted['fee_level'], | |
378 | 'event_id' => $newParticipant->event_id | |
379 | ); | |
380 | CRM_Price_BAO_LineItem::syncLineItems($newParticipant->id, 'civicrm_participant', $newParticipant->fee_amount, $otherParams); | |
381 | } | |
382 | ||
383 | $this->_newParticipant[] = $newParticipant->id; | |
a05662ef | 384 | return CRM_Import_Parser::VALID; |
6a488035 TO |
385 | } |
386 | else { | |
387 | array_unshift($values, 'Matching Participant record not found for Participant ID ' . $formatValues['participant_id'] . '. Row was skipped.'); | |
a05662ef | 388 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
389 | } |
390 | } | |
391 | } | |
392 | ||
393 | if ($this->_contactIdIndex < 0) { | |
394 | ||
395 | //retrieve contact id using contact dedupe rule | |
396 | $formatValues['contact_type'] = $this->_contactType; | |
397 | $formatValues['version'] = 3; | |
398 | $error = _civicrm_api3_deprecated_check_contact_dedupe($formatValues); | |
399 | ||
400 | if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) { | |
401 | $matchedIDs = explode(',', $error['error_message']['params'][0]); | |
402 | if (count($matchedIDs) >= 1) { | |
403 | foreach ($matchedIDs as $contactId) { | |
404 | $formatted['contact_id'] = $contactId; | |
405 | $formatted['version'] = 3; | |
406 | $newParticipant = _civicrm_api3_deprecated_create_participant_formatted($formatted, $onDuplicate); | |
407 | } | |
408 | } | |
409 | } | |
410 | else { | |
411 | // Using new Dedupe rule. | |
412 | $ruleParams = array( | |
413 | 'contact_type' => $this->_contactType, | |
414 | 'used' => 'Unsupervised', | |
415 | ); | |
416 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); | |
417 | ||
418 | $disp = ''; | |
419 | foreach ($fieldsArray as $value) { | |
420 | if (array_key_exists(trim($value), $params)) { | |
421 | $paramValue = $params[trim($value)]; | |
422 | if (is_array($paramValue)) { | |
423 | $disp .= $params[trim($value)][0][trim($value)] . " "; | |
424 | } | |
425 | else { | |
426 | $disp .= $params[trim($value)] . " "; | |
427 | } | |
428 | } | |
429 | } | |
430 | ||
a7488080 | 431 | if (!empty($params['external_identifier'])) { |
6a488035 TO |
432 | if ($disp) { |
433 | $disp .= "AND {$params['external_identifier']}"; | |
434 | } | |
435 | else { | |
436 | $disp = $params['external_identifier']; | |
437 | } | |
438 | } | |
439 | ||
440 | array_unshift($values, 'No matching Contact found for (' . $disp . ')'); | |
a05662ef | 441 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
442 | } |
443 | } | |
444 | else { | |
a7488080 | 445 | if (!empty($formatValues['external_identifier'])) { |
6a488035 TO |
446 | $checkCid = new CRM_Contact_DAO_Contact(); |
447 | $checkCid->external_identifier = $formatValues['external_identifier']; | |
448 | $checkCid->find(TRUE); | |
449 | if ($checkCid->id != $formatted['contact_id']) { | |
450 | array_unshift($values, 'Mismatch of External identifier :' . $formatValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']); | |
a05662ef | 451 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
452 | } |
453 | } | |
454 | ||
455 | $newParticipant = _civicrm_api3_deprecated_create_participant_formatted($formatted, $onDuplicate); | |
456 | } | |
457 | ||
458 | if (is_array($newParticipant) && civicrm_error($newParticipant)) { | |
a05662ef | 459 | if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { |
6a488035 TO |
460 | |
461 | $contactID = CRM_Utils_Array::value('contactID', $newParticipant); | |
462 | $participantID = CRM_Utils_Array::value('participantID', $newParticipant); | |
463 | $url = CRM_Utils_System::url('civicrm/contact/view/participant', | |
464 | "reset=1&id={$participantID}&cid={$contactID}&action=view", TRUE | |
465 | ); | |
466 | if (is_array($newParticipant['error_message']) && | |
467 | ($participantID == $newParticipant['error_message']['params'][0]) | |
468 | ) { | |
469 | array_unshift($values, $url); | |
a05662ef | 470 | return CRM_Import_Parser::DUPLICATE; |
6a488035 TO |
471 | } |
472 | elseif ($newParticipant['error_message']) { | |
473 | array_unshift($values, $newParticipant['error_message']); | |
a05662ef | 474 | return CRM_Import_Parser::ERROR; |
6a488035 | 475 | } |
a05662ef | 476 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
477 | } |
478 | } | |
479 | ||
480 | if (!(is_array($newParticipant) && civicrm_error($newParticipant))) { | |
481 | $this->_newParticipants[] = CRM_Utils_Array::value('id', $newParticipant); | |
482 | } | |
483 | ||
a05662ef | 484 | return CRM_Import_Parser::VALID; |
6a488035 TO |
485 | } |
486 | ||
487 | /** | |
ceb10dc7 | 488 | * Get the array of successfully imported Participation ids |
6a488035 TO |
489 | * |
490 | * @return array | |
6a488035 | 491 | */ |
00be9182 | 492 | public function &getImportedParticipations() { |
6a488035 TO |
493 | return $this->_newParticipants; |
494 | } | |
495 | ||
496 | /** | |
100fef9d | 497 | * The initializer code, called before the processing |
6a488035 TO |
498 | * |
499 | * @return void | |
6a488035 | 500 | */ |
00be9182 | 501 | public function fini() {} |
6a488035 | 502 | } |