$params['join_date'] = $params['start_date'];
}
- $session = CRM_Core_Session::singleton();
- $dateType = CRM_Core_Session::singleton()->get('dateTypes');
$formatted = $params;
-
- $customDataType = !empty($params['contact_type']) ? $params['contact_type'] : 'Membership';
- $customFields = CRM_Core_BAO_CustomField::getFields($customDataType);
-
// don't add to recent items, CRM-4399
$formatted['skipRecentView'] = TRUE;
- foreach ($params as $key => $val) {
- if ($val) {
- switch ($key) {
- case 'status_id':
- // @todo - we can do this based on the presence of 'pseudoconstant' in the metadata rather than field specific.
- $params[$key] = $this->parsePseudoConstantField($val, $this->fieldMetadata[$key]);
- break;
-
- }
- if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
- if ($customFields[$customFieldID]['data_type'] == 'Date') {
- $this->formatCustomDate($params, $formatted, $dateType, $key);
- unset($params[$key]);
- }
- elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
- $params[$key] = CRM_Utils_String::strtoboolstr($val);
- }
- }
- }
- }
- //date-Format part ends
$formatValues = [];
foreach ($params as $key => $field) {
//fix for CRM-2219 Update Membership
// onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE
if (!empty($formatted['is_override']) && empty($formatted['status_id'])) {
- array_unshift($values, 'Required parameter missing: Status');
$this->setImportStatus($rowNumber, 'ERROR', 'Required parameter missing: Status');
return CRM_Import_Parser::ERROR;
}
}
//Format dates
- $startDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('start_date', $formatted), '%Y-%m-%d');
- $endDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('end_date', $formatted), '%Y-%m-%d');
- $joinDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('join_date', $formatted), '%Y-%m-%d');
+ $startDate = $formatted['start_date'];
+ $endDate = $formatted['end_date'] ?? NULL;
+ $joinDate = $formatted['join_date'];
- if (empty($formatValues['contact_id'])) {
+ if (empty($formatValues['id']) && empty($formatValues['contact_id'])) {
$error = $this->checkContactDuplicate($formatValues);
if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) {
$matchedIDs = explode(',', $error['error_message']['params'][0]);
if (count($matchedIDs) > 1) {
- array_unshift($values, 'Multiple matching contact records detected for this row. The membership was not imported');
$this->setImportStatus($rowNumber, 'ERROR', 'Multiple matching contact records detected for this row. The membership was not imported');
return CRM_Import_Parser::ERROR;
}
$checkCid->external_identifier = $formatValues['external_identifier'];
$checkCid->find(TRUE);
if ($checkCid->id != $formatted['contact_id']) {
- array_unshift($values, 'Mismatch of External ID:' . $formatValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']);
$this->setImportStatus($rowNumber, 'ERROR', 'Mismatch of External ID:' . $formatValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']);
return CRM_Import_Parser::ERROR;
}
}
elseif (empty($formatted['is_override'])) {
if (empty($calcStatus)) {
- 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.');
$this->setImportStatus($rowNumber, 'ERROR', '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.');
return CRM_Import_Parser::ERROR;
}
foreach ($params as $key => $value) {
- //Handling Custom Data
- if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
- $values[$key] = $value;
- $type = $customFields[$customFieldID]['html_type'];
- if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID])) {
- $values[$key] = self::unserializeCustomValue($customFieldID, $value, $type);
- }
- }
-
switch ($key) {
case 'contact_id':
if (!CRM_Utils_Rule::integer($value)) {
$values[$key] = $value;
break;
- case 'membership_type_id':
- if (!array_key_exists($value, CRM_Member_PseudoConstant::membershipType())) {
- throw new Exception('Invalid Membership Type Id');
- }
- $values[$key] = $value;
- break;
-
default:
break;
}
*
* @var string
*/
- protected $_membershipTypeID = NULL;
+ protected $_membershipTypeID;
+
+ protected $entity = 'Membership';
/**
* Set up for test.
* Test the full form-flow import.
*/
public function testImportCSV() :void {
- $this->importCSV('memberships.csv', [
+ $this->importCSV('memberships_invalid.csv', [
['name' => 'membership_contact_id'],
['name' => 'membership_source'],
['name' => 'membership_type_id'],
$this->callAPISuccessGetSingle('Membership', []);
}
+ /**
+ * Test dates are parsed.
+ */
+ public function testUpdateWithCustomDates(): void {
+ $this->createCustomGroupWithFieldOfType([], 'date');
+ $contactID = $this->individualCreate(['external_identifier' => 'ext-1']);
+ $this->callAPISuccess('Membership', 'create', [
+ 'contact_id' => $contactID,
+ 'membership_type_id' => 'General',
+ 'start_date' => '2020-10-01',
+ ]);
+ $mapping = [
+ ['name' => 'membership_id'],
+ ['name' => 'membership_source'],
+ ['name' => 'membership_type_id'],
+ ['name' => 'membership_start_date'],
+ ['name' => $this->getCustomFieldName('date')],
+ ];
+ $this->importCSV('memberships_update_custom_date.csv', $mapping, ['dateFormats' => 32]);
+ $membership = $this->callAPISuccessGetSingle('Membership', []);
+ $this->assertEquals('2021-03-23', $membership['start_date']);
+ $this->assertEquals('2019-03-23 00:00:00', $membership[$this->getCustomFieldName('date')]);
+ }
+
/**
* Import the csv file values.
*