| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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 |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 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 | /** |
| 54 | * Array of successfully imported participants id's |
| 55 | * |
| 56 | * @array |
| 57 | */ |
| 58 | protected $_newParticipants; |
| 59 | |
| 60 | /** |
| 61 | * Class constructor. |
| 62 | */ |
| 63 | public function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) { |
| 64 | parent::__construct(); |
| 65 | $this->_mapperKeys = &$mapperKeys; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * The initializer code, called before the processing |
| 70 | * |
| 71 | * @return void |
| 72 | */ |
| 73 | public function init() { |
| 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 | /** |
| 126 | * Handle the values in mapField mode. |
| 127 | * |
| 128 | * @param array $values |
| 129 | * The array of values belonging to this line. |
| 130 | * |
| 131 | * @return bool |
| 132 | */ |
| 133 | public function mapField(&$values) { |
| 134 | return CRM_Import_Parser::VALID; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Handle the values in preview mode. |
| 139 | * |
| 140 | * @param array $values |
| 141 | * The array of values belonging to this line. |
| 142 | * |
| 143 | * @return bool |
| 144 | * the result of this processing |
| 145 | */ |
| 146 | public function preview(&$values) { |
| 147 | return $this->summary($values); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Handle the values in summary mode. |
| 152 | * |
| 153 | * @param array $values |
| 154 | * The array of values belonging to this line. |
| 155 | * |
| 156 | * @return bool |
| 157 | * the result of this processing |
| 158 | */ |
| 159 | public function summary(&$values) { |
| 160 | $erroneousField = NULL; |
| 161 | |
| 162 | $response = $this->setActiveFieldValues($values, $erroneousField); |
| 163 | $errorRequired = FALSE; |
| 164 | $index = -1; |
| 165 | |
| 166 | if ($this->_eventIndex > -1 && $this->_eventTitleIndex > -1) { |
| 167 | array_unshift($values, ts('Select either EventID OR Event Title')); |
| 168 | return CRM_Import_Parser::ERROR; |
| 169 | } |
| 170 | elseif ($this->_eventTitleIndex > -1) { |
| 171 | $index = $this->_eventTitleIndex; |
| 172 | } |
| 173 | elseif ($this->_eventIndex > -1) { |
| 174 | $index = $this->_eventIndex; |
| 175 | } |
| 176 | $params = &$this->getActiveFieldParams(); |
| 177 | |
| 178 | if (!(($index < 0) || ($this->_participantStatusIndex < 0))) { |
| 179 | $errorRequired = !CRM_Utils_Array::value($this->_participantStatusIndex, $values); |
| 180 | if (empty($params['event_id']) && empty($params['event_title'])) { |
| 181 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Event', $missingField); |
| 182 | } |
| 183 | if (empty($params['participant_status_id'])) { |
| 184 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status', $missingField); |
| 185 | } |
| 186 | } |
| 187 | else { |
| 188 | $errorRequired = TRUE; |
| 189 | $missingField = NULL; |
| 190 | if ($index < 0) { |
| 191 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Event', $missingField); |
| 192 | } |
| 193 | if ($this->_participantStatusIndex < 0) { |
| 194 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status', $missingField); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if ($errorRequired) { |
| 199 | array_unshift($values, ts('Missing required field(s) :') . $missingField); |
| 200 | return CRM_Import_Parser::ERROR; |
| 201 | } |
| 202 | |
| 203 | $errorMessage = NULL; |
| 204 | |
| 205 | //for date-Formats |
| 206 | $session = CRM_Core_Session::singleton(); |
| 207 | $dateType = $session->get('dateTypes'); |
| 208 | |
| 209 | foreach ($params as $key => $val) { |
| 210 | if ($val && ($key == 'participant_register_date')) { |
| 211 | if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) { |
| 212 | $params[$key] = $dateValue; |
| 213 | } |
| 214 | else { |
| 215 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Register Date', $errorMessage); |
| 216 | } |
| 217 | } |
| 218 | elseif ($val && ($key == 'participant_role_id' || $key == 'participant_role')) { |
| 219 | $roleIDs = CRM_Event_PseudoConstant::participantRole(); |
| 220 | $val = explode(',', $val); |
| 221 | if ($key == 'participant_role_id') { |
| 222 | foreach ($val as $role) { |
| 223 | if (!in_array(trim($role), array_keys($roleIDs))) { |
| 224 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Role Id', $errorMessage); |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | else { |
| 230 | foreach ($val as $role) { |
| 231 | if (!CRM_Contact_Import_Parser_Contact::in_value(trim($role), $roleIDs)) { |
| 232 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Role', $errorMessage); |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | elseif ($val && (($key == 'participant_status_id') || ($key == 'participant_status'))) { |
| 239 | $statusIDs = CRM_Event_PseudoConstant::participantStatus(); |
| 240 | if ($key == 'participant_status_id') { |
| 241 | if (!in_array(trim($val), array_keys($statusIDs))) { |
| 242 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status Id', $errorMessage); |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | elseif (!CRM_Contact_Import_Parser_Contact::in_value($val, $statusIDs)) { |
| 247 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status', $errorMessage); |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | //date-Format part ends |
| 253 | |
| 254 | $params['contact_type'] = 'Participant'; |
| 255 | //checking error in custom data |
| 256 | CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage); |
| 257 | |
| 258 | if ($errorMessage) { |
| 259 | $tempMsg = "Invalid value for field(s) : $errorMessage"; |
| 260 | array_unshift($values, $tempMsg); |
| 261 | $errorMessage = NULL; |
| 262 | return CRM_Import_Parser::ERROR; |
| 263 | } |
| 264 | return CRM_Import_Parser::VALID; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Handle the values in import mode. |
| 269 | * |
| 270 | * @param int $onDuplicate |
| 271 | * The code for what action to take on duplicates. |
| 272 | * @param array $values |
| 273 | * The array of values belonging to this line. |
| 274 | * |
| 275 | * @return bool |
| 276 | * the result of this processing |
| 277 | */ |
| 278 | public function import($onDuplicate, &$values) { |
| 279 | |
| 280 | // first make sure this is a valid line |
| 281 | $response = $this->summary($values); |
| 282 | if ($response != CRM_Import_Parser::VALID) { |
| 283 | return $response; |
| 284 | } |
| 285 | $params = &$this->getActiveFieldParams(); |
| 286 | $session = CRM_Core_Session::singleton(); |
| 287 | $dateType = $session->get('dateTypes'); |
| 288 | $formatted = array('version' => 3); |
| 289 | $customFields = CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $params)); |
| 290 | |
| 291 | // don't add to recent items, CRM-4399 |
| 292 | $formatted['skipRecentView'] = TRUE; |
| 293 | |
| 294 | foreach ($params as $key => $val) { |
| 295 | if ($val) { |
| 296 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { |
| 297 | if ($customFields[$customFieldID]['data_type'] == 'Date') { |
| 298 | CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key); |
| 299 | unset($params[$key]); |
| 300 | } |
| 301 | elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') { |
| 302 | $params[$key] = CRM_Utils_String::strtoboolstr($val); |
| 303 | } |
| 304 | } |
| 305 | if ($key == 'participant_register_date') { |
| 306 | CRM_Utils_Date::convertToDefaultDate($params, $dateType, 'participant_register_date'); |
| 307 | $formatted['participant_register_date'] = CRM_Utils_Date::processDate($params['participant_register_date']); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | if (!(!empty($params['participant_role_id']) || !empty($params['participant_role']))) { |
| 313 | if (!empty($params['event_id'])) { |
| 314 | $params['participant_role_id'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'default_role_id'); |
| 315 | } |
| 316 | else { |
| 317 | $eventTitle = $params['event_title']; |
| 318 | $qParams = array(); |
| 319 | $dao = new CRM_Core_DAO(); |
| 320 | $params['participant_role_id'] = $dao->singleValueQuery("SELECT default_role_id FROM civicrm_event WHERE title = '$eventTitle' ", |
| 321 | $qParams |
| 322 | ); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | //date-Format part ends |
| 327 | static $indieFields = NULL; |
| 328 | if ($indieFields == NULL) { |
| 329 | $indieFields = CRM_Event_BAO_Participant::import(); |
| 330 | } |
| 331 | |
| 332 | $formatValues = array(); |
| 333 | foreach ($params as $key => $field) { |
| 334 | if ($field == NULL || $field === '') { |
| 335 | continue; |
| 336 | } |
| 337 | |
| 338 | $formatValues[$key] = $field; |
| 339 | } |
| 340 | |
| 341 | $formatError = _civicrm_api3_deprecated_participant_formatted_param($formatValues, $formatted, TRUE); |
| 342 | |
| 343 | if ($formatError) { |
| 344 | array_unshift($values, $formatError['error_message']); |
| 345 | return CRM_Import_Parser::ERROR; |
| 346 | } |
| 347 | |
| 348 | if (!CRM_Utils_Rule::integer($formatted['event_id'])) { |
| 349 | array_unshift($values, ts('Invalid value for Event ID')); |
| 350 | return CRM_Import_Parser::ERROR; |
| 351 | } |
| 352 | |
| 353 | if ($onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) { |
| 354 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, |
| 355 | NULL, |
| 356 | 'Participant' |
| 357 | ); |
| 358 | } |
| 359 | else { |
| 360 | if ($formatValues['participant_id']) { |
| 361 | $dao = new CRM_Event_BAO_Participant(); |
| 362 | $dao->id = $formatValues['participant_id']; |
| 363 | |
| 364 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, |
| 365 | $formatValues['participant_id'], |
| 366 | 'Participant' |
| 367 | ); |
| 368 | if ($dao->find(TRUE)) { |
| 369 | $ids = array( |
| 370 | 'participant' => $formatValues['participant_id'], |
| 371 | 'userId' => $session->get('userID'), |
| 372 | ); |
| 373 | $participantValues = array(); |
| 374 | //@todo calling api functions directly is not supported |
| 375 | $newParticipant = _civicrm_api3_deprecated_participant_check_params($formatted, $participantValues, FALSE); |
| 376 | if ($newParticipant['error_message']) { |
| 377 | array_unshift($values, $newParticipant['error_message']); |
| 378 | return CRM_Import_Parser::ERROR; |
| 379 | } |
| 380 | $newParticipant = CRM_Event_BAO_Participant::create($formatted, $ids); |
| 381 | if (!empty($formatted['fee_level'])) { |
| 382 | $otherParams = array( |
| 383 | 'fee_label' => $formatted['fee_level'], |
| 384 | 'event_id' => $newParticipant->event_id, |
| 385 | ); |
| 386 | CRM_Price_BAO_LineItem::syncLineItems($newParticipant->id, 'civicrm_participant', $newParticipant->fee_amount, $otherParams); |
| 387 | } |
| 388 | |
| 389 | $this->_newParticipant[] = $newParticipant->id; |
| 390 | return CRM_Import_Parser::VALID; |
| 391 | } |
| 392 | else { |
| 393 | array_unshift($values, 'Matching Participant record not found for Participant ID ' . $formatValues['participant_id'] . '. Row was skipped.'); |
| 394 | return CRM_Import_Parser::ERROR; |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | if ($this->_contactIdIndex < 0) { |
| 400 | |
| 401 | //retrieve contact id using contact dedupe rule |
| 402 | $formatValues['contact_type'] = $this->_contactType; |
| 403 | $formatValues['version'] = 3; |
| 404 | $error = _civicrm_api3_deprecated_check_contact_dedupe($formatValues); |
| 405 | |
| 406 | if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) { |
| 407 | $matchedIDs = explode(',', $error['error_message']['params'][0]); |
| 408 | if (count($matchedIDs) >= 1) { |
| 409 | foreach ($matchedIDs as $contactId) { |
| 410 | $formatted['contact_id'] = $contactId; |
| 411 | $formatted['version'] = 3; |
| 412 | $newParticipant = _civicrm_api3_deprecated_create_participant_formatted($formatted, $onDuplicate); |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | else { |
| 417 | // Using new Dedupe rule. |
| 418 | $ruleParams = array( |
| 419 | 'contact_type' => $this->_contactType, |
| 420 | 'used' => 'Unsupervised', |
| 421 | ); |
| 422 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); |
| 423 | |
| 424 | $disp = ''; |
| 425 | foreach ($fieldsArray as $value) { |
| 426 | if (array_key_exists(trim($value), $params)) { |
| 427 | $paramValue = $params[trim($value)]; |
| 428 | if (is_array($paramValue)) { |
| 429 | $disp .= $params[trim($value)][0][trim($value)] . " "; |
| 430 | } |
| 431 | else { |
| 432 | $disp .= $params[trim($value)] . " "; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if (!empty($params['external_identifier'])) { |
| 438 | if ($disp) { |
| 439 | $disp .= "AND {$params['external_identifier']}"; |
| 440 | } |
| 441 | else { |
| 442 | $disp = $params['external_identifier']; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | array_unshift($values, 'No matching Contact found for (' . $disp . ')'); |
| 447 | return CRM_Import_Parser::ERROR; |
| 448 | } |
| 449 | } |
| 450 | else { |
| 451 | if (!empty($formatValues['external_identifier'])) { |
| 452 | $checkCid = new CRM_Contact_DAO_Contact(); |
| 453 | $checkCid->external_identifier = $formatValues['external_identifier']; |
| 454 | $checkCid->find(TRUE); |
| 455 | if ($checkCid->id != $formatted['contact_id']) { |
| 456 | array_unshift($values, 'Mismatch of External ID:' . $formatValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']); |
| 457 | return CRM_Import_Parser::ERROR; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | $newParticipant = _civicrm_api3_deprecated_create_participant_formatted($formatted, $onDuplicate); |
| 462 | } |
| 463 | |
| 464 | if (is_array($newParticipant) && civicrm_error($newParticipant)) { |
| 465 | if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { |
| 466 | |
| 467 | $contactID = CRM_Utils_Array::value('contactID', $newParticipant); |
| 468 | $participantID = CRM_Utils_Array::value('participantID', $newParticipant); |
| 469 | $url = CRM_Utils_System::url('civicrm/contact/view/participant', |
| 470 | "reset=1&id={$participantID}&cid={$contactID}&action=view", TRUE |
| 471 | ); |
| 472 | if (is_array($newParticipant['error_message']) && |
| 473 | ($participantID == $newParticipant['error_message']['params'][0]) |
| 474 | ) { |
| 475 | array_unshift($values, $url); |
| 476 | return CRM_Import_Parser::DUPLICATE; |
| 477 | } |
| 478 | elseif ($newParticipant['error_message']) { |
| 479 | array_unshift($values, $newParticipant['error_message']); |
| 480 | return CRM_Import_Parser::ERROR; |
| 481 | } |
| 482 | return CRM_Import_Parser::ERROR; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if (!(is_array($newParticipant) && civicrm_error($newParticipant))) { |
| 487 | $this->_newParticipants[] = CRM_Utils_Array::value('id', $newParticipant); |
| 488 | } |
| 489 | |
| 490 | return CRM_Import_Parser::VALID; |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Get the array of successfully imported Participation ids. |
| 495 | * |
| 496 | * @return array |
| 497 | */ |
| 498 | public function &getImportedParticipations() { |
| 499 | return $this->_newParticipants; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * The initializer code, called before the processing |
| 504 | * |
| 505 | * @return void |
| 506 | */ |
| 507 | public function fini() { |
| 508 | } |
| 509 | |
| 510 | } |