Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
6a488035 TO |
18 | /** |
19 | * class to parse membership csv files | |
20 | */ | |
21 | class CRM_Member_Import_Parser_Membership extends CRM_Member_Import_Parser { | |
22 | ||
23 | protected $_mapperKeys; | |
24 | ||
6a488035 TO |
25 | private $_membershipTypeIndex; |
26 | private $_membershipStatusIndex; | |
27 | ||
14b9e069 | 28 | /** |
29 | * Array of metadata for all available fields. | |
30 | * | |
31 | * @var array | |
32 | */ | |
33 | protected $fieldMetadata = []; | |
34 | ||
6a488035 | 35 | /** |
ceb10dc7 | 36 | * Array of successfully imported membership id's |
6a488035 | 37 | * |
971e129b | 38 | * @var array |
6a488035 TO |
39 | */ |
40 | protected $_newMemberships; | |
41 | ||
42 | /** | |
fe482240 | 43 | * Class constructor. |
c2b5a0af EM |
44 | * |
45 | * @param $mapperKeys | |
6a488035 | 46 | */ |
14b9e069 | 47 | public function __construct($mapperKeys) { |
6a488035 | 48 | parent::__construct(); |
14b9e069 | 49 | $this->_mapperKeys = $mapperKeys; |
6a488035 TO |
50 | } |
51 | ||
52 | /** | |
100fef9d | 53 | * The initializer code, called before the processing |
6a488035 TO |
54 | * |
55 | * @return void | |
6a488035 | 56 | */ |
00be9182 | 57 | public function init() { |
14b9e069 | 58 | $this->fieldMetadata = CRM_Member_BAO_Membership::importableFields($this->_contactType, FALSE); |
6a488035 | 59 | |
14b9e069 | 60 | foreach ($this->fieldMetadata as $name => $field) { |
61 | // @todo - we don't really need to do all this.... fieldMetadata is just fine to use as is. | |
6a488035 TO |
62 | $field['type'] = CRM_Utils_Array::value('type', $field, CRM_Utils_Type::T_INT); |
63 | $field['dataPattern'] = CRM_Utils_Array::value('dataPattern', $field, '//'); | |
64 | $field['headerPattern'] = CRM_Utils_Array::value('headerPattern', $field, '//'); | |
65 | $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']); | |
66 | } | |
67 | ||
be2fb01f | 68 | $this->_newMemberships = []; |
6a488035 TO |
69 | |
70 | $this->setActiveFields($this->_mapperKeys); | |
71 | ||
72 | // FIXME: we should do this in one place together with Form/MapField.php | |
6a488035 TO |
73 | $this->_membershipTypeIndex = -1; |
74 | $this->_membershipStatusIndex = -1; | |
75 | ||
76 | $index = 0; | |
77 | foreach ($this->_mapperKeys as $key) { | |
78 | switch ($key) { | |
6a488035 TO |
79 | |
80 | case 'membership_type_id': | |
81 | $this->_membershipTypeIndex = $index; | |
82 | break; | |
83 | ||
84 | case 'status_id': | |
85 | $this->_membershipStatusIndex = $index; | |
86 | break; | |
87 | } | |
88 | $index++; | |
89 | } | |
90 | } | |
91 | ||
92 | /** | |
fe482240 | 93 | * Handle the values in mapField mode. |
6a488035 | 94 | * |
b2363ea8 TO |
95 | * @param array $values |
96 | * The array of values belonging to this line. | |
6a488035 | 97 | * |
d5cc0fc2 | 98 | * @return bool |
6a488035 | 99 | */ |
00be9182 | 100 | public function mapField(&$values) { |
a05662ef | 101 | return CRM_Import_Parser::VALID; |
6a488035 TO |
102 | } |
103 | ||
104 | /** | |
fe482240 | 105 | * Handle the values in preview mode. |
6a488035 | 106 | * |
b2363ea8 TO |
107 | * @param array $values |
108 | * The array of values belonging to this line. | |
6a488035 | 109 | * |
d5cc0fc2 | 110 | * @return bool |
a6c01b45 | 111 | * the result of this processing |
6a488035 | 112 | */ |
00be9182 | 113 | public function preview(&$values) { |
6a488035 TO |
114 | return $this->summary($values); |
115 | } | |
116 | ||
117 | /** | |
fe482240 | 118 | * Handle the values in summary mode. |
6a488035 | 119 | * |
b2363ea8 TO |
120 | * @param array $values |
121 | * The array of values belonging to this line. | |
6a488035 | 122 | * |
d5cc0fc2 | 123 | * @return bool |
a6c01b45 | 124 | * the result of this processing |
6a488035 | 125 | */ |
00be9182 | 126 | public function summary(&$values) { |
6a488035 | 127 | $erroneousField = NULL; |
24a67831 | 128 | $this->setActiveFieldValues($values, $erroneousField); |
6a488035 TO |
129 | |
130 | $errorRequired = FALSE; | |
131 | ||
132 | if ($this->_membershipTypeIndex < 0) { | |
133 | $errorRequired = TRUE; | |
134 | } | |
135 | else { | |
136 | $errorRequired = !CRM_Utils_Array::value($this->_membershipTypeIndex, $values); | |
137 | } | |
138 | ||
139 | if ($errorRequired) { | |
140 | array_unshift($values, ts('Missing required fields')); | |
a05662ef | 141 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
142 | } |
143 | ||
04f72de8 | 144 | $params = $this->getActiveFieldParams(); |
6a488035 TO |
145 | $errorMessage = NULL; |
146 | ||
147 | //To check whether start date or join date is provided | |
c8dcfe3a | 148 | if (empty($params['membership_start_date']) && empty($params['membership_join_date'])) { |
6a488035 | 149 | $errorMessage = 'Membership Start Date is required to create a memberships.'; |
719a6fec | 150 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Start Date', $errorMessage); |
6a488035 | 151 | } |
6a488035 TO |
152 | |
153 | //for date-Formats | |
154 | $session = CRM_Core_Session::singleton(); | |
155 | $dateType = $session->get('dateTypes'); | |
156 | foreach ($params as $key => $val) { | |
157 | ||
158 | if ($val) { | |
159 | switch ($key) { | |
c8dcfe3a | 160 | case 'membership_join_date': |
6a488035 TO |
161 | if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) { |
162 | if (!CRM_Utils_Rule::date($params[$key])) { | |
719a6fec | 163 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Member Since', $errorMessage); |
6a488035 TO |
164 | } |
165 | } | |
166 | else { | |
719a6fec | 167 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Member Since', $errorMessage); |
6a488035 TO |
168 | } |
169 | break; | |
170 | ||
171 | case 'membership_start_date': | |
172 | if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) { | |
173 | if (!CRM_Utils_Rule::date($params[$key])) { | |
719a6fec | 174 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Start Date', $errorMessage); |
6a488035 TO |
175 | } |
176 | } | |
177 | else { | |
719a6fec | 178 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Start Date', $errorMessage); |
6a488035 TO |
179 | } |
180 | break; | |
181 | ||
182 | case 'membership_end_date': | |
183 | if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) { | |
184 | if (!CRM_Utils_Rule::date($params[$key])) { | |
719a6fec | 185 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('End date', $errorMessage); |
6a488035 TO |
186 | } |
187 | } | |
188 | else { | |
719a6fec | 189 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('End date', $errorMessage); |
6a488035 TO |
190 | } |
191 | break; | |
e136f704 O |
192 | |
193 | case 'status_override_end_date': | |
194 | if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) { | |
195 | if (!CRM_Utils_Rule::date($params[$key])) { | |
196 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Status Override End Date', $errorMessage); | |
197 | } | |
198 | } | |
199 | else { | |
200 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Status Override End Date', $errorMessage); | |
201 | } | |
202 | break; | |
6a488035 TO |
203 | |
204 | case 'membership_type_id': | |
14b9e069 | 205 | // @todo - squish into membership status - can use same lines here too. |
6a488035 TO |
206 | $membershipTypes = CRM_Member_PseudoConstant::membershipType(); |
207 | if (!CRM_Utils_Array::crmInArray($val, $membershipTypes) && | |
208 | !array_key_exists($val, $membershipTypes) | |
209 | ) { | |
719a6fec | 210 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Membership Type', $errorMessage); |
6a488035 TO |
211 | } |
212 | break; | |
213 | ||
214 | case 'status_id': | |
14b9e069 | 215 | if (!empty($val) && !$this->parsePseudoConstantField($val, $this->fieldMetadata[$key])) { |
719a6fec | 216 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Membership Status', $errorMessage); |
6a488035 TO |
217 | } |
218 | break; | |
219 | ||
220 | case 'email': | |
221 | if (!CRM_Utils_Rule::email($val)) { | |
719a6fec | 222 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Email Address', $errorMessage); |
6a488035 TO |
223 | } |
224 | } | |
225 | } | |
226 | } | |
227 | //date-Format part ends | |
228 | ||
229 | $params['contact_type'] = 'Membership'; | |
230 | ||
231 | //checking error in custom data | |
719a6fec | 232 | CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage); |
6a488035 TO |
233 | |
234 | if ($errorMessage) { | |
235 | $tempMsg = "Invalid value for field(s) : $errorMessage"; | |
236 | array_unshift($values, $tempMsg); | |
237 | $errorMessage = NULL; | |
a05662ef | 238 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
239 | } |
240 | ||
a05662ef | 241 | return CRM_Import_Parser::VALID; |
6a488035 TO |
242 | } |
243 | ||
244 | /** | |
fe482240 | 245 | * Handle the values in import mode. |
6a488035 | 246 | * |
b2363ea8 TO |
247 | * @param int $onDuplicate |
248 | * The code for what action to take on duplicates. | |
249 | * @param array $values | |
250 | * The array of values belonging to this line. | |
6a488035 | 251 | * |
d5cc0fc2 | 252 | * @return bool |
a6c01b45 | 253 | * the result of this processing |
6a488035 | 254 | */ |
00be9182 | 255 | public function import($onDuplicate, &$values) { |
92e4c2a5 | 256 | try { |
4f7b71ab | 257 | // first make sure this is a valid line |
258 | $response = $this->summary($values); | |
259 | if ($response != CRM_Import_Parser::VALID) { | |
260 | return $response; | |
261 | } | |
6a488035 | 262 | |
04f72de8 | 263 | $params = $this->getActiveFieldParams(); |
6a488035 | 264 | |
4f7b71ab | 265 | //assign join date equal to start date if join date is not provided |
c8dcfe3a SL |
266 | if (empty($params['membership_join_date']) && !empty($params['membership_start_date'])) { |
267 | $params['membership_join_date'] = $params['membership_start_date']; | |
4f7b71ab | 268 | } |
6a488035 | 269 | |
353ffa53 | 270 | $session = CRM_Core_Session::singleton(); |
24a67831 | 271 | $dateType = CRM_Core_Session::singleton()->get('dateTypes'); |
be2fb01f | 272 | $formatted = []; |
5981d156 JP |
273 | $customDataType = !empty($params['contact_type']) ? $params['contact_type'] : 'Membership'; |
274 | $customFields = CRM_Core_BAO_CustomField::getFields($customDataType); | |
4f7b71ab | 275 | |
276 | // don't add to recent items, CRM-4399 | |
277 | $formatted['skipRecentView'] = TRUE; | |
be2fb01f | 278 | $dateLabels = [ |
c8dcfe3a | 279 | 'membership_join_date' => ts('Member Since'), |
4f7b71ab | 280 | 'membership_start_date' => ts('Start Date'), |
281 | 'membership_end_date' => ts('End Date'), | |
be2fb01f | 282 | ]; |
4f7b71ab | 283 | foreach ($params as $key => $val) { |
284 | if ($val) { | |
285 | switch ($key) { | |
c8dcfe3a | 286 | case 'membership_join_date': |
4f7b71ab | 287 | case 'membership_start_date': |
288 | case 'membership_end_date': | |
289 | if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) { | |
290 | if (!CRM_Utils_Rule::date($params[$key])) { | |
291 | CRM_Contact_Import_Parser_Contact::addToErrorMsg($dateLabels[$key], $errorMessage); | |
292 | } | |
293 | } | |
294 | else { | |
87bbc876 | 295 | CRM_Contact_Import_Parser_Contact::addToErrorMsg($dateLabels[$key], $errorMessage); |
6a488035 | 296 | } |
4f7b71ab | 297 | break; |
6a488035 | 298 | |
4f7b71ab | 299 | case 'membership_type_id': |
300 | if (!is_numeric($val)) { | |
301 | unset($params['membership_type_id']); | |
302 | $params['membership_type'] = $val; | |
303 | } | |
304 | break; | |
6a488035 | 305 | |
4f7b71ab | 306 | case 'status_id': |
14b9e069 | 307 | // @todo - we can do this based on the presence of 'pseudoconstant' in the metadata rather than field specific. |
308 | $params[$key] = $this->parsePseudoConstantField($val, $this->fieldMetadata[$key]); | |
4f7b71ab | 309 | break; |
6a488035 | 310 | |
4f7b71ab | 311 | } |
312 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
481a74f4 | 313 | if ($customFields[$customFieldID]['data_type'] == 'Date') { |
4f7b71ab | 314 | CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key); |
315 | unset($params[$key]); | |
0db6c3e1 | 316 | } |
481a74f4 | 317 | elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') { |
4f7b71ab | 318 | $params[$key] = CRM_Utils_String::strtoboolstr($val); |
319 | } | |
6a488035 TO |
320 | } |
321 | } | |
322 | } | |
4f7b71ab | 323 | //date-Format part ends |
6a488035 | 324 | |
be2fb01f | 325 | $formatValues = []; |
4f7b71ab | 326 | foreach ($params as $key => $field) { |
19cea1b4 | 327 | // ignore empty values or empty arrays etc |
328 | if (CRM_Utils_System::isNull($field)) { | |
4f7b71ab | 329 | continue; |
330 | } | |
6a488035 | 331 | |
4f7b71ab | 332 | $formatValues[$key] = $field; |
6a488035 TO |
333 | } |
334 | ||
4f7b71ab | 335 | //format params to meet api v2 requirements. |
336 | //@todo find a way to test removing this formatting | |
337 | $formatError = $this->membership_format_params($formatValues, $formatted, TRUE); | |
6a488035 | 338 | |
4f7b71ab | 339 | if ($onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) { |
6a488035 | 340 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, |
4f7b71ab | 341 | NULL, |
6a488035 TO |
342 | 'Membership' |
343 | ); | |
4f7b71ab | 344 | } |
345 | else { | |
346 | //fix for CRM-2219 Update Membership | |
347 | // onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE | |
8f67d99a | 348 | if (!empty($formatted['member_is_override']) && empty($formatted['status_id'])) { |
4f7b71ab | 349 | array_unshift($values, 'Required parameter missing: Status'); |
350 | return CRM_Import_Parser::ERROR; | |
351 | } | |
6a488035 | 352 | |
1a9d4317 | 353 | if (!empty($formatValues['membership_id'])) { |
353ffa53 | 354 | $dao = new CRM_Member_BAO_Membership(); |
4f7b71ab | 355 | $dao->id = $formatValues['membership_id']; |
be2fb01f | 356 | $dates = ['join_date', 'start_date', 'end_date']; |
4f7b71ab | 357 | foreach ($dates as $v) { |
82cc6775 | 358 | if (empty($formatted[$v])) { |
4f7b71ab | 359 | $formatted[$v] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $formatValues['membership_id'], $v); |
360 | } | |
361 | } | |
362 | ||
363 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, | |
4f7b71ab | 364 | $formatValues['membership_id'], |
365 | 'Membership' | |
366 | ); | |
367 | if ($dao->find(TRUE)) { | |
82cc6775 PN |
368 | if (empty($params['line_item']) && !empty($formatted['membership_type_id'])) { |
369 | CRM_Price_BAO_LineItem::getLineItemArray($formatted, NULL, 'membership', $formatted['membership_type_id']); | |
370 | } | |
d75f2f47 | 371 | |
f3a5127d | 372 | $newMembership = civicrm_api3('Membership', 'create', $formatted); |
373 | $this->_newMemberships[] = $newMembership['id']; | |
374 | return CRM_Import_Parser::VALID; | |
6a488035 TO |
375 | } |
376 | else { | |
4f7b71ab | 377 | array_unshift($values, 'Matching Membership record not found for Membership ID ' . $formatValues['membership_id'] . '. Row was skipped.'); |
378 | return CRM_Import_Parser::ERROR; | |
6a488035 TO |
379 | } |
380 | } | |
6a488035 | 381 | } |
6a488035 | 382 | |
4f7b71ab | 383 | //Format dates |
384 | $startDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('start_date', $formatted), '%Y-%m-%d'); | |
353ffa53 TO |
385 | $endDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('end_date', $formatted), '%Y-%m-%d'); |
386 | $joinDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('join_date', $formatted), '%Y-%m-%d'); | |
6a488035 | 387 | |
64679927 | 388 | if (!$this->isContactIDColumnPresent()) { |
56316747 | 389 | $error = $this->checkContactDuplicate($formatValues); |
6a488035 | 390 | |
4f7b71ab | 391 | if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) { |
392 | $matchedIDs = explode(',', $error['error_message']['params'][0]); | |
393 | if (count($matchedIDs) > 1) { | |
394 | array_unshift($values, 'Multiple matching contact records detected for this row. The membership was not imported'); | |
395 | return CRM_Import_Parser::ERROR; | |
396 | } | |
397 | else { | |
398 | $cid = $matchedIDs[0]; | |
399 | $formatted['contact_id'] = $cid; | |
400 | ||
401 | //fix for CRM-1924 | |
402 | $calcDates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($formatted['membership_type_id'], | |
403 | $joinDate, | |
404 | $startDate, | |
405 | $endDate | |
406 | ); | |
407 | self::formattedDates($calcDates, $formatted); | |
408 | ||
409 | //fix for CRM-3570, exclude the statuses those having is_admin = 1 | |
410 | //now user can import is_admin if is override is true. | |
411 | $excludeIsAdmin = FALSE; | |
8f67d99a | 412 | if (empty($formatted['member_is_override'])) { |
4f7b71ab | 413 | $formatted['exclude_is_admin'] = $excludeIsAdmin = TRUE; |
414 | } | |
415 | $calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($startDate, | |
416 | $endDate, | |
417 | $joinDate, | |
2cb64970 | 418 | 'now', |
5f11bbcc EM |
419 | $excludeIsAdmin, |
420 | $formatted['membership_type_id'], | |
421 | $formatted | |
4f7b71ab | 422 | ); |
423 | ||
a7488080 | 424 | if (empty($formatted['status_id'])) { |
4f7b71ab | 425 | $formatted['status_id'] = $calcStatus['id']; |
426 | } | |
8f67d99a | 427 | elseif (empty($formatted['member_is_override'])) { |
4f7b71ab | 428 | if (empty($calcStatus)) { |
429 | array_unshift($values, 'Status in import row (' . $formatValues['status_id'] . ') does not match calculated status based on your configured Membership Status Rules. Record was not imported.'); | |
430 | return CRM_Import_Parser::ERROR; | |
431 | } | |
432 | elseif ($formatted['status_id'] != $calcStatus['id']) { | |
433 | //Status Hold" is either NOT mapped or is FALSE | |
434 | array_unshift($values, 'Status in import row (' . $formatValues['status_id'] . ') does not match calculated status based on your configured Membership Status Rules (' . $calcStatus['name'] . '). Record was not imported.'); | |
435 | return CRM_Import_Parser::ERROR; | |
436 | } | |
437 | } | |
438 | ||
439 | $newMembership = civicrm_api3('membership', 'create', $formatted); | |
440 | ||
441 | $this->_newMemberships[] = $newMembership['id']; | |
442 | return CRM_Import_Parser::VALID; | |
443 | } | |
6a488035 TO |
444 | } |
445 | else { | |
4f7b71ab | 446 | // Using new Dedupe rule. |
be2fb01f | 447 | $ruleParams = [ |
4f7b71ab | 448 | 'contact_type' => $this->_contactType, |
353ffa53 | 449 | 'used' => 'Unsupervised', |
be2fb01f | 450 | ]; |
4f7b71ab | 451 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); |
452 | $disp = ''; | |
453 | ||
454 | foreach ($fieldsArray as $value) { | |
455 | if (array_key_exists(trim($value), $params)) { | |
456 | $paramValue = $params[trim($value)]; | |
457 | if (is_array($paramValue)) { | |
458 | $disp .= $params[trim($value)][0][trim($value)] . " "; | |
459 | } | |
460 | else { | |
461 | $disp .= $params[trim($value)] . " "; | |
462 | } | |
463 | } | |
6a488035 | 464 | } |
6a488035 | 465 | |
a7488080 | 466 | if (!empty($params['external_identifier'])) { |
4f7b71ab | 467 | if ($disp) { |
468 | $disp .= "AND {$params['external_identifier']}"; | |
6a488035 | 469 | } |
4f7b71ab | 470 | else { |
471 | $disp = $params['external_identifier']; | |
6a488035 TO |
472 | } |
473 | } | |
474 | ||
4f7b71ab | 475 | array_unshift($values, 'No matching Contact found for (' . $disp . ')'); |
476 | return CRM_Import_Parser::ERROR; | |
6a488035 TO |
477 | } |
478 | } | |
479 | else { | |
a7488080 | 480 | if (!empty($formatValues['external_identifier'])) { |
4f7b71ab | 481 | $checkCid = new CRM_Contact_DAO_Contact(); |
482 | $checkCid->external_identifier = $formatValues['external_identifier']; | |
483 | $checkCid->find(TRUE); | |
484 | if ($checkCid->id != $formatted['contact_id']) { | |
d79be26c | 485 | array_unshift($values, 'Mismatch of External ID:' . $formatValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']); |
4f7b71ab | 486 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
487 | } |
488 | } | |
489 | ||
4f7b71ab | 490 | //to calculate dates |
491 | $calcDates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($formatted['membership_type_id'], | |
492 | $joinDate, | |
493 | $startDate, | |
494 | $endDate | |
495 | ); | |
496 | self::formattedDates($calcDates, $formatted); | |
497 | //end of date calculation part | |
498 | ||
499 | //fix for CRM-3570, exclude the statuses those having is_admin = 1 | |
500 | //now user can import is_admin if is override is true. | |
501 | $excludeIsAdmin = FALSE; | |
8f67d99a | 502 | if (empty($formatted['member_is_override'])) { |
4f7b71ab | 503 | $formatted['exclude_is_admin'] = $excludeIsAdmin = TRUE; |
504 | } | |
505 | $calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($startDate, | |
506 | $endDate, | |
507 | $joinDate, | |
2cb64970 | 508 | 'now', |
5f11bbcc EM |
509 | $excludeIsAdmin, |
510 | $formatted['membership_type_id'], | |
511 | $formatted | |
4f7b71ab | 512 | ); |
a7488080 | 513 | if (empty($formatted['status_id'])) { |
9c1bc317 | 514 | $formatted['status_id'] = $calcStatus['id'] ?? NULL; |
4f7b71ab | 515 | } |
8f67d99a | 516 | elseif (empty($formatted['member_is_override'])) { |
4f7b71ab | 517 | if (empty($calcStatus)) { |
518 | array_unshift($values, 'Status in import row (' . CRM_Utils_Array::value('status_id', $formatValues) . ') does not match calculated status based on your configured Membership Status Rules. Record was not imported.'); | |
519 | return CRM_Import_Parser::ERROR; | |
6a488035 | 520 | } |
4f7b71ab | 521 | elseif ($formatted['status_id'] != $calcStatus['id']) { |
522 | //Status Hold" is either NOT mapped or is FALSE | |
523 | array_unshift($values, 'Status in import row (' . CRM_Utils_Array::value('status_id', $formatValues) . ') does not match calculated status based on your configured Membership Status Rules (' . $calcStatus['name'] . '). Record was not imported.'); | |
524 | return CRM_Import_Parser::ERROR; | |
6a488035 TO |
525 | } |
526 | } | |
527 | ||
4f7b71ab | 528 | $newMembership = civicrm_api3('membership', 'create', $formatted); |
6a488035 | 529 | |
4f7b71ab | 530 | $this->_newMemberships[] = $newMembership['id']; |
531 | return CRM_Import_Parser::VALID; | |
6a488035 | 532 | } |
f719e41c | 533 | } |
534 | catch (Exception $e) { | |
535 | array_unshift($values, $e->getMessage()); | |
536 | return CRM_Import_Parser::ERROR; | |
537 | } | |
6a488035 TO |
538 | } |
539 | ||
540 | /** | |
ceb10dc7 | 541 | * Get the array of successfully imported membership id's |
6a488035 TO |
542 | * |
543 | * @return array | |
6a488035 | 544 | */ |
00be9182 | 545 | public function &getImportedMemberships() { |
6a488035 TO |
546 | return $this->_newMemberships; |
547 | } | |
548 | ||
549 | /** | |
100fef9d | 550 | * The initializer code, called before the processing |
6a488035 TO |
551 | * |
552 | * @return void | |
6a488035 | 553 | */ |
b09fe5ed TO |
554 | public function fini() { |
555 | } | |
6a488035 TO |
556 | |
557 | /** | |
558 | * to calculate join, start and end dates | |
559 | * | |
b2363ea8 TO |
560 | * @param array $calcDates |
561 | * Array of dates returned by getDatesForMembershipType(). | |
6a488035 | 562 | * |
2a6da8d7 | 563 | * @param $formatted |
6a488035 | 564 | * |
6a488035 | 565 | */ |
00be9182 | 566 | public function formattedDates($calcDates, &$formatted) { |
be2fb01f | 567 | $dates = [ |
6a488035 TO |
568 | 'join_date', |
569 | 'start_date', | |
570 | 'end_date', | |
be2fb01f | 571 | ]; |
6a488035 TO |
572 | |
573 | foreach ($dates as $d) { | |
574 | if (isset($formatted[$d]) && | |
575 | !CRM_Utils_System::isNull($formatted[$d]) | |
576 | ) { | |
577 | $formatted[$d] = CRM_Utils_Date::isoToMysql($formatted[$d]); | |
578 | } | |
579 | elseif (isset($calcDates[$d])) { | |
580 | $formatted[$d] = CRM_Utils_Date::isoToMysql($calcDates[$d]); | |
581 | } | |
582 | } | |
583 | } | |
77b97be7 | 584 | |
3c15495c | 585 | /** |
586 | * @deprecated - this function formats params according to v2 standards but | |
587 | * need to be sure about the impact of not calling it so retaining on the import class | |
588 | * take the input parameter list as specified in the data model and | |
589 | * convert it into the same format that we use in QF and BAO object | |
590 | * | |
b2363ea8 TO |
591 | * @param array $params |
592 | * Associative array of property name/value. | |
3c15495c | 593 | * pairs to insert in new contact. |
b2363ea8 TO |
594 | * @param array $values |
595 | * The reformatted properties that we can use internally. | |
3c15495c | 596 | * |
77b97be7 | 597 | * @param array|bool $create Is the formatted Values array going to |
3c15495c | 598 | * be used for CRM_Member_BAO_Membership:create() |
599 | * | |
77b97be7 | 600 | * @throws Exception |
3c15495c | 601 | * @return array|error |
3c15495c | 602 | */ |
00be9182 | 603 | public function membership_format_params($params, &$values, $create = FALSE) { |
3c15495c | 604 | require_once 'api/v3/utils.php'; |
605 | $fields = CRM_Member_DAO_Membership::fields(); | |
606 | _civicrm_api3_store_values($fields, $params, $values); | |
607 | ||
481a74f4 | 608 | $customFields = CRM_Core_BAO_CustomField::getFields('Membership'); |
3c15495c | 609 | |
610 | foreach ($params as $key => $value) { | |
3c15495c | 611 | |
612 | //Handling Custom Data | |
613 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
614 | $values[$key] = $value; | |
615 | $type = $customFields[$customFieldID]['html_type']; | |
726e45e7 | 616 | if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID])) { |
be40742b | 617 | $values[$key] = self::unserializeCustomValue($customFieldID, $value, $type); |
3c15495c | 618 | } |
619 | } | |
620 | ||
621 | switch ($key) { | |
622 | case 'membership_contact_id': | |
623 | if (!CRM_Utils_Rule::integer($value)) { | |
f719e41c | 624 | throw new Exception("contact_id not valid: $value"); |
3c15495c | 625 | } |
353ffa53 | 626 | $dao = new CRM_Core_DAO(); |
be2fb01f | 627 | $qParams = []; |
353ffa53 | 628 | $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value", |
3c15495c | 629 | $qParams |
630 | ); | |
631 | if (!$svq) { | |
f719e41c | 632 | throw new Exception("Invalid Contact ID: There is no contact record with contact_id = $value."); |
3c15495c | 633 | } |
634 | $values['contact_id'] = $values['membership_contact_id']; | |
635 | unset($values['membership_contact_id']); | |
636 | break; | |
637 | ||
638 | case 'membership_type_id': | |
f3acfdd9 | 639 | if (!array_key_exists($value, CRM_Member_PseudoConstant::membershipType())) { |
f719e41c | 640 | throw new Exception('Invalid Membership Type Id'); |
3c15495c | 641 | } |
642 | $values[$key] = $value; | |
643 | break; | |
644 | ||
645 | case 'membership_type': | |
646 | $membershipTypeId = CRM_Utils_Array::key(ucfirst($value), | |
353ffa53 | 647 | CRM_Member_PseudoConstant::membershipType() |
3c15495c | 648 | ); |
649 | if ($membershipTypeId) { | |
a7488080 | 650 | if (!empty($values['membership_type_id']) && |
3c15495c | 651 | $membershipTypeId != $values['membership_type_id'] |
652 | ) { | |
f719e41c | 653 | throw new Exception('Mismatched membership Type and Membership Type Id'); |
3c15495c | 654 | } |
655 | } | |
656 | else { | |
f719e41c | 657 | throw new Exception('Invalid Membership Type'); |
3c15495c | 658 | } |
659 | $values['membership_type_id'] = $membershipTypeId; | |
660 | break; | |
661 | ||
3c15495c | 662 | default: |
663 | break; | |
664 | } | |
665 | } | |
666 | ||
667 | _civicrm_api3_custom_format_params($params, $values, 'Membership'); | |
668 | ||
3c15495c | 669 | if ($create) { |
c8dcfe3a | 670 | // CRM_Member_BAO_Membership::create() handles membership_start_date, membership_join_date, |
3c15495c | 671 | // membership_end_date and membership_source. So, if $values contains |
c8dcfe3a SL |
672 | // membership_start_date, membership_end_date, membership_join_date or membership_source, |
673 | // convert it to start_date, end_date, join_date or source | |
be2fb01f | 674 | $changes = [ |
c8dcfe3a | 675 | 'membership_join_date' => 'join_date', |
3c15495c | 676 | 'membership_start_date' => 'start_date', |
677 | 'membership_end_date' => 'end_date', | |
678 | 'membership_source' => 'source', | |
be2fb01f | 679 | ]; |
3c15495c | 680 | |
681 | foreach ($changes as $orgVal => $changeVal) { | |
682 | if (isset($values[$orgVal])) { | |
683 | $values[$changeVal] = $values[$orgVal]; | |
684 | unset($values[$orgVal]); | |
685 | } | |
686 | } | |
687 | } | |
688 | ||
689 | return NULL; | |
690 | } | |
96025800 | 691 | |
64679927 | 692 | /** |
693 | * Is the contact ID mapped. | |
694 | * | |
695 | * @return bool | |
696 | */ | |
697 | protected function isContactIDColumnPresent(): bool { | |
698 | return in_array('membership_contact_id', $this->_mapperKeys, TRUE); | |
699 | } | |
700 | ||
6a488035 | 701 | } |