Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
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; | |
6a488035 TO |
45 | private $_eventIndex; |
46 | private $_participantStatusIndex; | |
47 | private $_participantRoleIndex; | |
48 | private $_eventTitleIndex; | |
49 | ||
50 | /** | |
ceb10dc7 | 51 | * Array of successfully imported participants id's |
6a488035 | 52 | * |
90b461f1 | 53 | * @var array |
6a488035 TO |
54 | */ |
55 | protected $_newParticipants; | |
56 | ||
57 | /** | |
fe482240 | 58 | * Class constructor. |
54957108 | 59 | * |
60 | * @param array $mapperKeys | |
61 | * @param null $mapperLocType | |
62 | * @param null $mapperPhoneType | |
6a488035 | 63 | */ |
00be9182 | 64 | public function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) { |
6a488035 TO |
65 | parent::__construct(); |
66 | $this->_mapperKeys = &$mapperKeys; | |
67 | } | |
68 | ||
69 | /** | |
54957108 | 70 | * The initializer code, called before the processing. |
6a488035 | 71 | */ |
00be9182 | 72 | public function init() { |
6a488035 TO |
73 | $fields = CRM_Event_BAO_Participant::importableFields($this->_contactType, FALSE); |
74 | $fields['event_id']['title'] = 'Event ID'; | |
75 | $eventfields = &CRM_Event_BAO_Event::fields(); | |
76 | $fields['event_title'] = $eventfields['event_title']; | |
77 | ||
78 | foreach ($fields as $name => $field) { | |
79 | $field['type'] = CRM_Utils_Array::value('type', $field, CRM_Utils_Type::T_INT); | |
80 | $field['dataPattern'] = CRM_Utils_Array::value('dataPattern', $field, '//'); | |
81 | $field['headerPattern'] = CRM_Utils_Array::value('headerPattern', $field, '//'); | |
82 | $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']); | |
83 | } | |
84 | ||
be2fb01f | 85 | $this->_newParticipants = []; |
6a488035 TO |
86 | $this->setActiveFields($this->_mapperKeys); |
87 | ||
88 | // FIXME: we should do this in one place together with Form/MapField.php | |
89 | $this->_contactIdIndex = -1; | |
90 | $this->_eventIndex = -1; | |
91 | $this->_participantStatusIndex = -1; | |
92 | $this->_participantRoleIndex = -1; | |
93 | $this->_eventTitleIndex = -1; | |
94 | ||
95 | $index = 0; | |
96 | foreach ($this->_mapperKeys as $key) { | |
97 | ||
98 | switch ($key) { | |
99 | case 'participant_contact_id': | |
100 | $this->_contactIdIndex = $index; | |
101 | break; | |
102 | ||
103 | case 'event_id': | |
104 | $this->_eventIndex = $index; | |
105 | break; | |
106 | ||
107 | case 'participant_status': | |
108 | case 'participant_status_id': | |
109 | $this->_participantStatusIndex = $index; | |
110 | break; | |
111 | ||
112 | case 'participant_role_id': | |
113 | $this->_participantRoleIndex = $index; | |
114 | break; | |
115 | ||
116 | case 'event_title': | |
117 | $this->_eventTitleIndex = $index; | |
118 | break; | |
119 | } | |
120 | $index++; | |
121 | } | |
122 | } | |
123 | ||
124 | /** | |
fe482240 | 125 | * Handle the values in mapField mode. |
6a488035 | 126 | * |
d4dd1e85 TO |
127 | * @param array $values |
128 | * The array of values belonging to this line. | |
6a488035 | 129 | * |
79d7553f | 130 | * @return bool |
6a488035 | 131 | */ |
00be9182 | 132 | public function mapField(&$values) { |
a05662ef | 133 | return CRM_Import_Parser::VALID; |
6a488035 TO |
134 | } |
135 | ||
136 | /** | |
fe482240 | 137 | * Handle the values in preview mode. |
6a488035 | 138 | * |
d4dd1e85 TO |
139 | * @param array $values |
140 | * The array of values belonging to this line. | |
6a488035 | 141 | * |
79d7553f | 142 | * @return bool |
a6c01b45 | 143 | * the result of this processing |
6a488035 | 144 | */ |
00be9182 | 145 | public function preview(&$values) { |
6a488035 TO |
146 | return $this->summary($values); |
147 | } | |
148 | ||
149 | /** | |
fe482240 | 150 | * Handle the values in summary mode. |
6a488035 | 151 | * |
d4dd1e85 TO |
152 | * @param array $values |
153 | * The array of values belonging to this line. | |
6a488035 | 154 | * |
79d7553f | 155 | * @return bool |
a6c01b45 | 156 | * the result of this processing |
6a488035 | 157 | */ |
00be9182 | 158 | public function summary(&$values) { |
6a488035 TO |
159 | $erroneousField = NULL; |
160 | ||
353ffa53 | 161 | $response = $this->setActiveFieldValues($values, $erroneousField); |
6a488035 | 162 | $errorRequired = FALSE; |
353ffa53 | 163 | $index = -1; |
6a488035 TO |
164 | |
165 | if ($this->_eventIndex > -1 && $this->_eventTitleIndex > -1) { | |
166 | array_unshift($values, ts('Select either EventID OR Event Title')); | |
a05662ef | 167 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
168 | } |
169 | elseif ($this->_eventTitleIndex > -1) { | |
170 | $index = $this->_eventTitleIndex; | |
171 | } | |
172 | elseif ($this->_eventIndex > -1) { | |
173 | $index = $this->_eventIndex; | |
174 | } | |
175 | $params = &$this->getActiveFieldParams(); | |
176 | ||
177 | if (!(($index < 0) || ($this->_participantStatusIndex < 0))) { | |
178 | $errorRequired = !CRM_Utils_Array::value($this->_participantStatusIndex, $values); | |
8cc574cf | 179 | if (empty($params['event_id']) && empty($params['event_title'])) { |
719a6fec | 180 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Event', $missingField); |
6a488035 | 181 | } |
a7488080 | 182 | if (empty($params['participant_status_id'])) { |
719a6fec | 183 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status', $missingField); |
6a488035 TO |
184 | } |
185 | } | |
186 | else { | |
187 | $errorRequired = TRUE; | |
188 | $missingField = NULL; | |
189 | if ($index < 0) { | |
719a6fec | 190 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Event', $missingField); |
6a488035 TO |
191 | } |
192 | if ($this->_participantStatusIndex < 0) { | |
719a6fec | 193 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status', $missingField); |
6a488035 TO |
194 | } |
195 | } | |
196 | ||
197 | if ($errorRequired) { | |
198 | array_unshift($values, ts('Missing required field(s) :') . $missingField); | |
a05662ef | 199 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
200 | } |
201 | ||
202 | $errorMessage = NULL; | |
203 | ||
204 | //for date-Formats | |
205 | $session = CRM_Core_Session::singleton(); | |
206 | $dateType = $session->get('dateTypes'); | |
207 | ||
208 | foreach ($params as $key => $val) { | |
209 | if ($val && ($key == 'participant_register_date')) { | |
210 | if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) { | |
211 | $params[$key] = $dateValue; | |
212 | } | |
213 | else { | |
719a6fec | 214 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Register Date', $errorMessage); |
6a488035 TO |
215 | } |
216 | } | |
217 | elseif ($val && ($key == 'participant_role_id' || $key == 'participant_role')) { | |
218 | $roleIDs = CRM_Event_PseudoConstant::participantRole(); | |
219 | $val = explode(',', $val); | |
220 | if ($key == 'participant_role_id') { | |
221 | foreach ($val as $role) { | |
222 | if (!in_array(trim($role), array_keys($roleIDs))) { | |
719a6fec | 223 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Role Id', $errorMessage); |
6a488035 TO |
224 | break; |
225 | } | |
226 | } | |
227 | } | |
228 | else { | |
229 | foreach ($val as $role) { | |
719a6fec | 230 | if (!CRM_Contact_Import_Parser_Contact::in_value(trim($role), $roleIDs)) { |
353ffa53 | 231 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Role', $errorMessage); |
6a488035 TO |
232 | break; |
233 | } | |
234 | } | |
235 | } | |
236 | } | |
237 | elseif ($val && (($key == 'participant_status_id') || ($key == 'participant_status'))) { | |
238 | $statusIDs = CRM_Event_PseudoConstant::participantStatus(); | |
239 | if ($key == 'participant_status_id') { | |
240 | if (!in_array(trim($val), array_keys($statusIDs))) { | |
719a6fec | 241 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status Id', $errorMessage); |
6a488035 TO |
242 | break; |
243 | } | |
244 | } | |
719a6fec CW |
245 | elseif (!CRM_Contact_Import_Parser_Contact::in_value($val, $statusIDs)) { |
246 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status', $errorMessage); | |
6a488035 TO |
247 | break; |
248 | } | |
249 | } | |
250 | } | |
251 | //date-Format part ends | |
252 | ||
253 | $params['contact_type'] = 'Participant'; | |
254 | //checking error in custom data | |
719a6fec | 255 | CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage); |
6a488035 TO |
256 | |
257 | if ($errorMessage) { | |
258 | $tempMsg = "Invalid value for field(s) : $errorMessage"; | |
259 | array_unshift($values, $tempMsg); | |
260 | $errorMessage = NULL; | |
a05662ef | 261 | return CRM_Import_Parser::ERROR; |
6a488035 | 262 | } |
a05662ef | 263 | return CRM_Import_Parser::VALID; |
6a488035 TO |
264 | } |
265 | ||
266 | /** | |
fe482240 | 267 | * Handle the values in import mode. |
6a488035 | 268 | * |
d4dd1e85 TO |
269 | * @param int $onDuplicate |
270 | * The code for what action to take on duplicates. | |
271 | * @param array $values | |
272 | * The array of values belonging to this line. | |
6a488035 | 273 | * |
79d7553f | 274 | * @return bool |
a6c01b45 | 275 | * the result of this processing |
6a488035 | 276 | */ |
00be9182 | 277 | public function import($onDuplicate, &$values) { |
6a488035 TO |
278 | |
279 | // first make sure this is a valid line | |
280 | $response = $this->summary($values); | |
a05662ef | 281 | if ($response != CRM_Import_Parser::VALID) { |
6a488035 TO |
282 | return $response; |
283 | } | |
353ffa53 TO |
284 | $params = &$this->getActiveFieldParams(); |
285 | $session = CRM_Core_Session::singleton(); | |
286 | $dateType = $session->get('dateTypes'); | |
be2fb01f | 287 | $formatted = ['version' => 3]; |
1828a237 | 288 | $customFields = CRM_Core_BAO_CustomField::getFields('Participant'); |
6a488035 TO |
289 | |
290 | // don't add to recent items, CRM-4399 | |
291 | $formatted['skipRecentView'] = TRUE; | |
292 | ||
293 | foreach ($params as $key => $val) { | |
294 | if ($val) { | |
295 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
296 | if ($customFields[$customFieldID]['data_type'] == 'Date') { | |
719a6fec | 297 | CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key); |
6a488035 TO |
298 | unset($params[$key]); |
299 | } | |
300 | elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') { | |
301 | $params[$key] = CRM_Utils_String::strtoboolstr($val); | |
302 | } | |
303 | } | |
22e263ad | 304 | if ($key == 'participant_register_date') { |
39d87d1e JM |
305 | CRM_Utils_Date::convertToDefaultDate($params, $dateType, 'participant_register_date'); |
306 | $formatted['participant_register_date'] = CRM_Utils_Date::processDate($params['participant_register_date']); | |
307 | } | |
6a488035 TO |
308 | } |
309 | } | |
310 | ||
8cc574cf | 311 | if (!(!empty($params['participant_role_id']) || !empty($params['participant_role']))) { |
a7488080 | 312 | if (!empty($params['event_id'])) { |
6a488035 TO |
313 | $params['participant_role_id'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'default_role_id'); |
314 | } | |
315 | else { | |
316 | $eventTitle = $params['event_title']; | |
be2fb01f | 317 | $qParams = []; |
6a488035 TO |
318 | $dao = new CRM_Core_DAO(); |
319 | $params['participant_role_id'] = $dao->singleValueQuery("SELECT default_role_id FROM civicrm_event WHERE title = '$eventTitle' ", | |
320 | $qParams | |
321 | ); | |
322 | } | |
323 | } | |
324 | ||
325 | //date-Format part ends | |
326 | static $indieFields = NULL; | |
327 | if ($indieFields == NULL) { | |
328 | $indieFields = CRM_Event_BAO_Participant::import(); | |
329 | } | |
330 | ||
be2fb01f | 331 | $formatValues = []; |
6a488035 TO |
332 | foreach ($params as $key => $field) { |
333 | if ($field == NULL || $field === '') { | |
334 | continue; | |
335 | } | |
336 | ||
337 | $formatValues[$key] = $field; | |
338 | } | |
339 | ||
c3fc9a44 | 340 | $formatError = $this->formatValues($formatted, $formatValues); |
6a488035 TO |
341 | |
342 | if ($formatError) { | |
343 | array_unshift($values, $formatError['error_message']); | |
a05662ef | 344 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
345 | } |
346 | ||
347 | if (!CRM_Utils_Rule::integer($formatted['event_id'])) { | |
348 | array_unshift($values, ts('Invalid value for Event ID')); | |
a05662ef | 349 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
350 | } |
351 | ||
a05662ef | 352 | if ($onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) { |
6a488035 | 353 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, |
6a488035 TO |
354 | NULL, |
355 | 'Participant' | |
356 | ); | |
357 | } | |
358 | else { | |
359 | if ($formatValues['participant_id']) { | |
360 | $dao = new CRM_Event_BAO_Participant(); | |
361 | $dao->id = $formatValues['participant_id']; | |
362 | ||
363 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, | |
6a488035 TO |
364 | $formatValues['participant_id'], |
365 | 'Participant' | |
366 | ); | |
367 | if ($dao->find(TRUE)) { | |
be2fb01f | 368 | $ids = [ |
6a488035 TO |
369 | 'participant' => $formatValues['participant_id'], |
370 | 'userId' => $session->get('userID'), | |
be2fb01f CW |
371 | ]; |
372 | $participantValues = []; | |
6a488035 TO |
373 | //@todo calling api functions directly is not supported |
374 | $newParticipant = _civicrm_api3_deprecated_participant_check_params($formatted, $participantValues, FALSE); | |
375 | if ($newParticipant['error_message']) { | |
376 | array_unshift($values, $newParticipant['error_message']); | |
a05662ef | 377 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
378 | } |
379 | $newParticipant = CRM_Event_BAO_Participant::create($formatted, $ids); | |
a7488080 | 380 | if (!empty($formatted['fee_level'])) { |
be2fb01f | 381 | $otherParams = [ |
6a488035 | 382 | 'fee_label' => $formatted['fee_level'], |
21dfd5f5 | 383 | 'event_id' => $newParticipant->event_id, |
be2fb01f | 384 | ]; |
6a488035 TO |
385 | CRM_Price_BAO_LineItem::syncLineItems($newParticipant->id, 'civicrm_participant', $newParticipant->fee_amount, $otherParams); |
386 | } | |
387 | ||
388 | $this->_newParticipant[] = $newParticipant->id; | |
a05662ef | 389 | return CRM_Import_Parser::VALID; |
6a488035 TO |
390 | } |
391 | else { | |
392 | array_unshift($values, 'Matching Participant record not found for Participant ID ' . $formatValues['participant_id'] . '. Row was skipped.'); | |
a05662ef | 393 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
394 | } |
395 | } | |
396 | } | |
397 | ||
398 | if ($this->_contactIdIndex < 0) { | |
56316747 | 399 | $error = $this->checkContactDuplicate($formatValues); |
6a488035 TO |
400 | |
401 | if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) { | |
402 | $matchedIDs = explode(',', $error['error_message']['params'][0]); | |
403 | if (count($matchedIDs) >= 1) { | |
404 | foreach ($matchedIDs as $contactId) { | |
405 | $formatted['contact_id'] = $contactId; | |
406 | $formatted['version'] = 3; | |
407 | $newParticipant = _civicrm_api3_deprecated_create_participant_formatted($formatted, $onDuplicate); | |
408 | } | |
409 | } | |
410 | } | |
411 | else { | |
412 | // Using new Dedupe rule. | |
be2fb01f | 413 | $ruleParams = [ |
6a488035 | 414 | 'contact_type' => $this->_contactType, |
353ffa53 | 415 | 'used' => 'Unsupervised', |
be2fb01f | 416 | ]; |
6a488035 TO |
417 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); |
418 | ||
419 | $disp = ''; | |
420 | foreach ($fieldsArray as $value) { | |
421 | if (array_key_exists(trim($value), $params)) { | |
422 | $paramValue = $params[trim($value)]; | |
423 | if (is_array($paramValue)) { | |
424 | $disp .= $params[trim($value)][0][trim($value)] . " "; | |
425 | } | |
426 | else { | |
427 | $disp .= $params[trim($value)] . " "; | |
428 | } | |
429 | } | |
430 | } | |
431 | ||
a7488080 | 432 | if (!empty($params['external_identifier'])) { |
6a488035 TO |
433 | if ($disp) { |
434 | $disp .= "AND {$params['external_identifier']}"; | |
435 | } | |
436 | else { | |
437 | $disp = $params['external_identifier']; | |
438 | } | |
439 | } | |
440 | ||
441 | array_unshift($values, 'No matching Contact found for (' . $disp . ')'); | |
a05662ef | 442 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
443 | } |
444 | } | |
445 | else { | |
a7488080 | 446 | if (!empty($formatValues['external_identifier'])) { |
6a488035 TO |
447 | $checkCid = new CRM_Contact_DAO_Contact(); |
448 | $checkCid->external_identifier = $formatValues['external_identifier']; | |
449 | $checkCid->find(TRUE); | |
450 | if ($checkCid->id != $formatted['contact_id']) { | |
d79be26c | 451 | array_unshift($values, 'Mismatch of External ID:' . $formatValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']); |
a05662ef | 452 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
453 | } |
454 | } | |
455 | ||
456 | $newParticipant = _civicrm_api3_deprecated_create_participant_formatted($formatted, $onDuplicate); | |
457 | } | |
458 | ||
459 | if (is_array($newParticipant) && civicrm_error($newParticipant)) { | |
a05662ef | 460 | if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { |
6a488035 | 461 | |
353ffa53 | 462 | $contactID = CRM_Utils_Array::value('contactID', $newParticipant); |
6a488035 | 463 | $participantID = CRM_Utils_Array::value('participantID', $newParticipant); |
353ffa53 | 464 | $url = CRM_Utils_System::url('civicrm/contact/view/participant', |
6a488035 TO |
465 | "reset=1&id={$participantID}&cid={$contactID}&action=view", TRUE |
466 | ); | |
467 | if (is_array($newParticipant['error_message']) && | |
468 | ($participantID == $newParticipant['error_message']['params'][0]) | |
469 | ) { | |
470 | array_unshift($values, $url); | |
a05662ef | 471 | return CRM_Import_Parser::DUPLICATE; |
6a488035 TO |
472 | } |
473 | elseif ($newParticipant['error_message']) { | |
474 | array_unshift($values, $newParticipant['error_message']); | |
a05662ef | 475 | return CRM_Import_Parser::ERROR; |
6a488035 | 476 | } |
a05662ef | 477 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
478 | } |
479 | } | |
480 | ||
481 | if (!(is_array($newParticipant) && civicrm_error($newParticipant))) { | |
482 | $this->_newParticipants[] = CRM_Utils_Array::value('id', $newParticipant); | |
483 | } | |
484 | ||
a05662ef | 485 | return CRM_Import_Parser::VALID; |
6a488035 TO |
486 | } |
487 | ||
488 | /** | |
fe482240 | 489 | * Get the array of successfully imported Participation ids. |
6a488035 TO |
490 | * |
491 | * @return array | |
6a488035 | 492 | */ |
00be9182 | 493 | public function &getImportedParticipations() { |
6a488035 TO |
494 | return $this->_newParticipants; |
495 | } | |
496 | ||
497 | /** | |
100fef9d | 498 | * The initializer code, called before the processing |
6a488035 TO |
499 | * |
500 | * @return void | |
6a488035 | 501 | */ |
6ea503d4 TO |
502 | public function fini() { |
503 | } | |
96025800 | 504 | |
c3fc9a44 | 505 | /** |
506 | * Format values | |
507 | * | |
508 | * @todo lots of tidy up needed here - very old function relocated. | |
509 | * | |
510 | * @param array $values | |
511 | * @param array $params | |
512 | * | |
513 | * @return array|null | |
514 | */ | |
515 | protected function formatValues(&$values, $params) { | |
516 | $fields = CRM_Event_DAO_Participant::fields(); | |
517 | _civicrm_api3_store_values($fields, $params, $values); | |
518 | ||
519 | $customFields = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE); | |
520 | ||
521 | foreach ($params as $key => $value) { | |
522 | // ignore empty values or empty arrays etc | |
523 | if (CRM_Utils_System::isNull($value)) { | |
524 | continue; | |
525 | } | |
526 | ||
527 | // Handling Custom Data | |
528 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
529 | $values[$key] = $value; | |
530 | $type = $customFields[$customFieldID]['html_type']; | |
531 | if ($type == 'CheckBox' || $type == 'Multi-Select') { | |
532 | $mulValues = explode(',', $value); | |
533 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
be2fb01f | 534 | $values[$key] = []; |
c3fc9a44 | 535 | foreach ($mulValues as $v1) { |
536 | foreach ($customOption as $customValueID => $customLabel) { | |
537 | $customValue = $customLabel['value']; | |
538 | if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) || | |
539 | (strtolower(trim($customValue)) == strtolower(trim($v1))) | |
540 | ) { | |
541 | if ($type == 'CheckBox') { | |
542 | $values[$key][$customValue] = 1; | |
543 | } | |
544 | else { | |
545 | $values[$key][] = $customValue; | |
546 | } | |
547 | } | |
548 | } | |
549 | } | |
550 | } | |
551 | elseif ($type == 'Select' || $type == 'Radio') { | |
552 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
553 | foreach ($customOption as $customFldID => $customValue) { | |
554 | $val = CRM_Utils_Array::value('value', $customValue); | |
555 | $label = CRM_Utils_Array::value('label', $customValue); | |
556 | $label = strtolower($label); | |
557 | $value = strtolower(trim($value)); | |
558 | if (($value == $label) || ($value == strtolower($val))) { | |
559 | $values[$key] = $val; | |
560 | } | |
561 | } | |
562 | } | |
563 | } | |
564 | ||
565 | switch ($key) { | |
566 | case 'participant_contact_id': | |
567 | if (!CRM_Utils_Rule::integer($value)) { | |
568 | return civicrm_api3_create_error("contact_id not valid: $value"); | |
569 | } | |
08c6b770 | 570 | if (!CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value")) { |
c3fc9a44 | 571 | return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value."); |
572 | } | |
573 | $values['contact_id'] = $values['participant_contact_id']; | |
574 | unset($values['participant_contact_id']); | |
575 | break; | |
576 | ||
577 | case 'participant_register_date': | |
578 | if (!CRM_Utils_Rule::dateTime($value)) { | |
579 | return civicrm_api3_create_error("$key not a valid date: $value"); | |
580 | } | |
581 | break; | |
582 | ||
583 | case 'event_title': | |
584 | $id = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Event", $value, 'id', 'title'); | |
585 | $values['event_id'] = $id; | |
586 | break; | |
587 | ||
588 | case 'event_id': | |
589 | if (!CRM_Utils_Rule::integer($value)) { | |
590 | return civicrm_api3_create_error("Event ID is not valid: $value"); | |
591 | } | |
592 | $dao = new CRM_Core_DAO(); | |
be2fb01f | 593 | $qParams = []; |
c3fc9a44 | 594 | $svq = $dao->singleValueQuery("SELECT id FROM civicrm_event WHERE id = $value", |
595 | $qParams | |
596 | ); | |
597 | if (!$svq) { | |
598 | return civicrm_api3_create_error("Invalid Event ID: There is no event record with event_id = $value."); | |
599 | } | |
600 | break; | |
601 | ||
602 | case 'participant_status_id': | |
603 | if (!CRM_Utils_Rule::integer($value)) { | |
604 | return civicrm_api3_create_error("Event Status ID is not valid: $value"); | |
605 | } | |
606 | break; | |
607 | ||
608 | case 'participant_status': | |
609 | $status = CRM_Event_PseudoConstant::participantStatus(); | |
610 | $values['participant_status_id'] = CRM_Utils_Array::key($value, $status);; | |
611 | break; | |
612 | ||
613 | case 'participant_role_id': | |
614 | case 'participant_role': | |
615 | $role = CRM_Event_PseudoConstant::participantRole(); | |
616 | $participantRoles = explode(",", $value); | |
617 | foreach ($participantRoles as $k => $v) { | |
618 | $v = trim($v); | |
619 | if ($key == 'participant_role') { | |
620 | $participantRoles[$k] = CRM_Utils_Array::key($v, $role); | |
621 | } | |
622 | else { | |
623 | $participantRoles[$k] = $v; | |
624 | } | |
625 | } | |
626 | $values['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $participantRoles); | |
627 | unset($values[$key]); | |
628 | break; | |
629 | ||
630 | default: | |
631 | break; | |
632 | } | |
633 | } | |
634 | ||
635 | if (array_key_exists('participant_note', $params)) { | |
636 | $values['participant_note'] = $params['participant_note']; | |
637 | } | |
638 | ||
639 | // CRM_Event_BAO_Participant::create() handles register_date, | |
640 | // status_id and source. So, if $values contains | |
641 | // participant_register_date, participant_status_id or participant_source, | |
642 | // convert it to register_date, status_id or source | |
be2fb01f | 643 | $changes = [ |
c3fc9a44 | 644 | 'participant_register_date' => 'register_date', |
645 | 'participant_source' => 'source', | |
646 | 'participant_status_id' => 'status_id', | |
647 | 'participant_role_id' => 'role_id', | |
648 | 'participant_fee_level' => 'fee_level', | |
649 | 'participant_fee_amount' => 'fee_amount', | |
650 | 'participant_id' => 'id', | |
be2fb01f | 651 | ]; |
c3fc9a44 | 652 | |
653 | foreach ($changes as $orgVal => $changeVal) { | |
654 | if (isset($values[$orgVal])) { | |
655 | $values[$changeVal] = $values[$orgVal]; | |
656 | unset($values[$orgVal]); | |
657 | } | |
658 | } | |
659 | ||
660 | return NULL; | |
661 | } | |
662 | ||
6a488035 | 663 | } |