First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,62 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
* $Id$
*
*/
class CRM_Member_Import_Controller extends CRM_Core_Controller {
/**
* Class constructor.
*
* @param string $title
* @param bool|int $action
* @param bool $modal
*/
public function __construct($title = NULL, $action = CRM_Core_Action::NONE, $modal = TRUE) {
parent::__construct($title, $modal);
// lets get around the time limit issue if possible, CRM-2113
if (!ini_get('safe_mode')) {
set_time_limit(0);
}
$this->_stateMachine = new CRM_Import_StateMachine($this, $action);
// create and instantiate the pages
$this->addPages($this->_stateMachine, $action);
// add all the actions
$config = CRM_Core_Config::singleton();
$this->addActions($config->uploadDir, array('uploadFile'));
}
}

View file

@ -0,0 +1,209 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
* $Id$
*
*/
class CRM_Member_Import_Field {
/**#@+
* @var string
*/
/**
* Name of the field
*/
public $_name;
/**
* Title of the field to be used in display
*/
public $_title;
/**
* Type of field
* @var enum
*/
public $_type;
/**
* Is this field required
* @var boolean
*/
public $_required;
/**
* Data to be carried for use by a derived class
* @var object
*/
public $_payload;
/**
* Regexp to match the CSV header of this column/field
* @var string
*/
public $_headerPattern;
/**
* Regexp to match the pattern of data from various column/fields
* @var string
*/
public $_dataPattern;
/**
* Value of this field
* @var object
*/
public $_value;
/**
* @param string $name
* @param $title
* @param int $type
* @param string $headerPattern
* @param string $dataPattern
*/
public function __construct($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') {
$this->_name = $name;
$this->_title = $title;
$this->_type = $type;
$this->_headerPattern = $headerPattern;
$this->_dataPattern = $dataPattern;
$this->_value = NULL;
}
public function resetValue() {
$this->_value = NULL;
}
/**
* The value is in string format. convert the value to the type of this field
* and set the field value with the appropriate type
*
* @param $value
*/
public function setValue($value) {
$this->_value = $value;
}
/**
* @return bool
*/
public function validate() {
if (CRM_Utils_System::isNull($this->_value)) {
return TRUE;
}
switch ($this->_name) {
case 'contact_id':
// note: we validate extistence of the contact in API, upon
// insert (it would be too costlty to do a db call here)
return CRM_Utils_Rule::integer($this->_value);
case 'receive_date':
case 'cancel_date':
case 'receipt_date':
case 'thankyou_date':
return CRM_Utils_Rule::date($this->_value);
case 'non_deductible_amount':
case 'total_amount':
case 'fee_amount':
case 'net_amount':
return CRM_Utils_Rule::money($this->_value);
case 'trxn_id':
static $seenTrxnIds = array();
if (in_array($this->_value, $seenTrxnIds)) {
return FALSE;
}
elseif ($this->_value) {
$seenTrxnIds[] = $this->_value;
return TRUE;
}
else {
$this->_value = NULL;
return TRUE;
}
break;
case 'currency':
return CRM_Utils_Rule::currencyCode($this->_value);
case 'membership_type':
static $membershipTypes = NULL;
if (!$membershipTypes) {
$membershipTypes = CRM_Member_PseudoConstant::membershipType();
}
if (in_array($this->_value, $membershipTypes)) {
return TRUE;
}
else {
return FALSE;
}
break;
case 'payment_instrument':
static $paymentInstruments = NULL;
if (!$paymentInstruments) {
$paymentInstruments = CRM_Member_PseudoConstant::paymentInstrument();
}
if (in_array($this->_value, $paymentInstruments)) {
return TRUE;
}
else {
return FALSE;
}
break;
default:
break;
}
// check whether that's a valid custom field id
// and if so, check the contents' validity
if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($this->_name)) {
static $customFields = NULL;
if (!$customFields) {
$customFields = CRM_Core_BAO_CustomField::getFields('Membership');
}
if (!array_key_exists($customFieldID, $customFields)) {
return FALSE;
}
return CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID]['data_type'], $this->_value);
}
return TRUE;
}
}

View file

@ -0,0 +1,87 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
* $Id$
*
*/
/**
* This class gets the name of the file to upload
*/
class CRM_Member_Import_Form_DataSource extends CRM_Import_Form_DataSource {
const PATH = 'civicrm/member/import';
const IMPORT_ENTITY = 'Membership';
/**
* Build the form object.
*
* @return void
*/
public function buildQuickForm() {
parent::buildQuickForm();
$duplicateOptions = array();
$duplicateOptions[] = $this->createElement('radio',
NULL, NULL, ts('Insert new Membership'), CRM_Import_Parser::DUPLICATE_SKIP
);
$duplicateOptions[] = $this->createElement('radio',
NULL, NULL, ts('Update existing Membership'), CRM_Import_Parser::DUPLICATE_UPDATE
);
$this->addGroup($duplicateOptions, 'onDuplicate',
ts('Import mode')
);
$this->setDefaults(array(
'onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP,
));
$this->addContactTypeSelector();
}
/**
* Process the uploaded file.
*
* @return void
*/
public function postProcess() {
$this->storeFormValues(array(
'onDuplicate',
'contactType',
'dateFormats',
'savedMapping',
));
$this->submitFileForMapping('CRM_Member_Import_Parser_Membership');
}
}

View file

@ -0,0 +1,521 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
* $Id$
*
*/
/**
* This class gets the name of the file to upload
*/
class CRM_Member_Import_Form_MapField extends CRM_Import_Form_MapField {
/**
* store contactType.
*
* @var int
*/
static $_contactType = NULL;
/**
* Set variables up before form is built.
*
* @return void
*/
public function preProcess() {
$this->_mapperFields = $this->get('fields');
asort($this->_mapperFields);
$this->_columnCount = $this->get('columnCount');
$this->assign('columnCount', $this->_columnCount);
$this->_dataValues = $this->get('dataValues');
$this->assign('dataValues', $this->_dataValues);
$skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
$this->_onDuplicate = $this->get('onDuplicate', isset($onDuplicate) ? $onDuplicate : "");
$highlightedFields = array();
if ($skipColumnHeader) {
$this->assign('skipColumnHeader', $skipColumnHeader);
$this->assign('rowDisplayCount', 3);
/* if we had a column header to skip, stash it for later */
$this->_columnHeaders = $this->_dataValues[0];
}
else {
$this->assign('rowDisplayCount', 2);
}
//CRM-2219 removing other required fields since for updation only
//membership id is required.
if ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
$remove = array('membership_contact_id', 'email', 'first_name', 'last_name', 'external_identifier');
foreach ($remove as $value) {
unset($this->_mapperFields[$value]);
}
$highlightedFieldsArray = array('membership_id', 'membership_start_date', 'membership_type_id');
foreach ($highlightedFieldsArray as $name) {
$highlightedFields[] = $name;
}
}
elseif ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) {
unset($this->_mapperFields['membership_id']);
$highlightedFieldsArray = array(
'membership_contact_id',
'email',
'external_identifier',
'membership_start_date',
'membership_type_id',
);
foreach ($highlightedFieldsArray as $name) {
$highlightedFields[] = $name;
}
}
// modify field title
$this->_mapperFields['status_id'] = ts('Membership Status');
$this->_mapperFields['membership_type_id'] = ts('Membership Type');
self::$_contactType = $this->get('contactType');
$this->assign('highlightedFields', $highlightedFields);
}
/**
* Build the form object.
*
* @return void
*/
public function buildQuickForm() {
//to save the current mappings
if (!$this->get('savedMapping')) {
$saveDetailsName = ts('Save this field mapping');
$this->applyFilter('saveMappingName', 'trim');
$this->add('text', 'saveMappingName', ts('Name'));
$this->add('text', 'saveMappingDesc', ts('Description'));
}
else {
$savedMapping = $this->get('savedMapping');
list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping::getMappingFields($savedMapping);
$mappingName = $mappingName[1];
$mappingContactType = $mappingContactType[1];
$mappingLocation = CRM_Utils_Array::value('1', $mappingLocation);
$mappingPhoneType = CRM_Utils_Array::value('1', $mappingPhoneType);
$mappingRelation = CRM_Utils_Array::value('1', $mappingRelation);
//mapping is to be loaded from database
$params = array('id' => $savedMapping);
$temp = array();
$mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp);
$this->assign('loadedMapping', $mappingDetails->name);
$this->set('loadedMapping', $savedMapping);
$getMappingName = new CRM_Core_DAO_Mapping();
$getMappingName->id = $savedMapping;
$getMappingName->mapping_type = 'Import Memberships';
$getMappingName->find();
while ($getMappingName->fetch()) {
$mapperName = $getMappingName->name;
}
$this->assign('savedName', $mapperName);
$this->add('hidden', 'mappingId', $savedMapping);
$this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
$saveDetailsName = ts('Save as a new field mapping');
$this->add('text', 'saveMappingName', ts('Name'));
$this->add('text', 'saveMappingDesc', ts('Description'));
}
$this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
$this->addFormRule(array('CRM_Member_Import_Form_MapField', 'formRule'), $this);
//-------- end of saved mapping stuff ---------
$defaults = array();
$mapperKeys = array_keys($this->_mapperFields);
$hasHeaders = !empty($this->_columnHeaders);
$headerPatterns = $this->get('headerPatterns');
$dataPatterns = $this->get('dataPatterns');
$hasLocationTypes = $this->get('fieldTypes');
/* Initialize all field usages to false */
foreach ($mapperKeys as $key) {
$this->_fieldUsed[$key] = FALSE;
}
$this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
$sel1 = $this->_mapperFields;
if (!$this->get('onDuplicate')) {
unset($sel1['id']);
unset($sel1['membership_id']);
}
$sel2[''] = NULL;
$js = "<script type='text/javascript'>\n";
$formName = 'document.forms.' . $this->_name;
//used to warn for mismatch column count or mismatch mapping
$warning = 0;
for ($i = 0; $i < $this->_columnCount; $i++) {
$sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
$jsSet = FALSE;
if ($this->get('savedMapping')) {
if (isset($mappingName[$i])) {
if ($mappingName[$i] != ts('- do not import -')) {
$mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]);
//When locationType is not set
$js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
//When phoneType is not set
$js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
$js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
$defaults["mapper[$i]"] = array($mappingHeader[0]);
$jsSet = TRUE;
}
else {
$defaults["mapper[$i]"] = array();
}
if (!$jsSet) {
for ($k = 1; $k < 4; $k++) {
$js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
}
}
}
else {
// this load section to help mapping if we ran out of saved columns when doing Load Mapping
$js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
if ($hasHeaders) {
$defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns));
}
else {
$defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
}
}
//end of load mapping
}
else {
$js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
if ($hasHeaders) {
// Infer the default from the skipped headers if we have them
$defaults["mapper[$i]"] = array(
$this->defaultFromHeader($this->_columnHeaders[$i],
$headerPatterns
),
// $defaultLocationType->id
0,
);
}
else {
// Otherwise guess the default from the form of the data
$defaults["mapper[$i]"] = array(
$this->defaultFromData($dataPatterns, $i),
// $defaultLocationType->id
0,
);
}
}
$sel->setOptions(array($sel1, $sel2, (isset($sel3)) ? $sel3 : "", (isset($sel4)) ? $sel4 : ""));
}
$js .= "</script>\n";
$this->assign('initHideBoxes', $js);
//set warning if mismatch in more than
if (isset($mappingName)) {
if (($this->_columnCount != count($mappingName))) {
$warning++;
}
}
if ($warning != 0 && $this->get('savedMapping')) {
$session = CRM_Core_Session::singleton();
$session->setStatus(ts('The data columns in this import file appear to be different from the saved mapping. Please verify that you have selected the correct saved mapping before continuing.'));
}
else {
$session = CRM_Core_Session::singleton();
$session->setStatus(NULL);
}
$this->setDefaults($defaults);
$this->addButtons(array(
array(
'type' => 'back',
'name' => ts('Previous'),
),
array(
'type' => 'next',
'name' => ts('Continue'),
'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
'isDefault' => TRUE,
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
));
}
/**
* Global validation rules for the form.
*
* @param array $fields
* Posted values of the form.
*
* @param $files
* @param $self
*
* @return array
* list of errors to be posted back to the form
*/
public static function formRule($fields, $files, $self) {
$errors = array();
if (!array_key_exists('savedMapping', $fields)) {
$importKeys = array();
foreach ($fields['mapper'] as $mapperPart) {
$importKeys[] = $mapperPart[0];
}
// FIXME: should use the schema titles, not redeclare them
$requiredFields = array(
'membership_contact_id' => ts('Contact ID'),
'membership_type_id' => ts('Membership Type'),
'membership_start_date' => ts('Membership Start Date'),
);
$contactTypeId = $self->get('contactType');
$contactTypes = array(
CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual',
CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household',
CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization',
);
$params = array(
'used' => 'Unsupervised',
'contact_type' => $contactTypes[$contactTypeId],
);
list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params);
$weightSum = 0;
foreach ($importKeys as $key => $val) {
if (array_key_exists($val, $ruleFields)) {
$weightSum += $ruleFields[$val];
}
}
$fieldMessage = '';
foreach ($ruleFields as $field => $weight) {
$fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
}
foreach ($requiredFields as $field => $title) {
if (!in_array($field, $importKeys)) {
if ($field == 'membership_contact_id') {
if ((($weightSum >= $threshold || in_array('external_identifier', $importKeys)) &&
$self->_onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE
) ||
in_array('membership_id', $importKeys)
) {
continue;
}
else {
if (!isset($errors['_qf_default'])) {
$errors['_qf_default'] = '';
}
$errors['_qf_default'] .= ts('Missing required contact matching fields.') . " $fieldMessage " . ts('(Sum of all weights should be greater than or equal to threshold: %1).', array(
1 => $threshold,
)) . ' ' . ts('(OR Membership ID if update mode.)') . '<br />';
}
}
else {
if (!isset($errors['_qf_default'])) {
$errors['_qf_default'] = '';
}
$errors['_qf_default'] .= ts('Missing required field: %1', array(
1 => $title,
)) . '<br />';
}
}
}
}
if (!empty($fields['saveMapping'])) {
$nameField = CRM_Utils_Array::value('saveMappingName', $fields);
if (empty($nameField)) {
$errors['saveMappingName'] = ts('Name is required to save Import Mapping');
}
else {
$mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', 'Import Membership', 'name');
if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) {
$errors['saveMappingName'] = ts('Duplicate Import Membership Mapping Name');
}
}
}
if (!empty($errors)) {
if (!empty($errors['saveMappingName'])) {
$_flag = 1;
$assignError = new CRM_Core_Page();
$assignError->assign('mappingDetailsError', $_flag);
}
return $errors;
}
return TRUE;
}
/**
* Process the mapped fields and map it into the uploaded file
* preview the file and extract some summary statistics
*
* @return void
*/
public function postProcess() {
$params = $this->controller->exportValues('MapField');
//reload the mapfield if load mapping is pressed
if (!empty($params['savedMapping'])) {
$this->set('savedMapping', $params['savedMapping']);
$this->controller->resetPage($this->_name);
return;
}
$fileName = $this->controller->exportValue('DataSource', 'uploadFile');
$seperator = $this->controller->exportValue('DataSource', 'fieldSeparator');
$skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
$mapperKeys = array();
$mapper = array();
$mapperKeys = $this->controller->exportValue($this->_name, 'mapper');
$mapperKeysMain = array();
$mapperLocType = array();
$mapperPhoneType = array();
for ($i = 0; $i < $this->_columnCount; $i++) {
$mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]];
$mapperKeysMain[$i] = $mapperKeys[$i][0];
if (!empty($mapperKeys[$i][1]) && is_numeric($mapperKeys[$i][1])) {
$mapperLocType[$i] = $mapperKeys[$i][1];
}
else {
$mapperLocType[$i] = NULL;
}
if (!empty($mapperKeys[$i][2]) && (!is_numeric($mapperKeys[$i][2]))) {
$mapperPhoneType[$i] = $mapperKeys[$i][2];
}
else {
$mapperPhoneType[$i] = NULL;
}
}
$this->set('mapper', $mapper);
// store mapping Id to display it in the preview page
if (!empty($params['mappingId'])) {
$this->set('loadMappingId', $params['mappingId']);
}
//Updating Mapping Records
if (!empty($params['updateMapping'])) {
$mappingFields = new CRM_Core_DAO_MappingField();
$mappingFields->mapping_id = $params['mappingId'];
$mappingFields->find();
$mappingFieldsId = array();
while ($mappingFields->fetch()) {
if ($mappingFields->id) {
$mappingFieldsId[$mappingFields->column_number] = $mappingFields->id;
}
}
for ($i = 0; $i < $this->_columnCount; $i++) {
$updateMappingFields = new CRM_Core_DAO_MappingField();
$updateMappingFields->id = $mappingFieldsId[$i];
$updateMappingFields->mapping_id = $params['mappingId'];
$updateMappingFields->column_number = $i;
$mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
$id = isset($mapperKeyParts[0]) ? $mapperKeyParts[0] : NULL;
$first = isset($mapperKeyParts[1]) ? $mapperKeyParts[1] : NULL;
$second = isset($mapperKeyParts[2]) ? $mapperKeyParts[2] : NULL;
$updateMappingFields->name = $mapper[$i];
$updateMappingFields->save();
}
}
//Saving Mapping Details and Records
if (!empty($params['saveMapping'])) {
$mappingParams = array(
'name' => $params['saveMappingName'],
'description' => $params['saveMappingDesc'],
'mapping_type_id' => CRM_Core_OptionGroup::getValue('mapping_type',
'Import Membership',
'name'
),
);
$saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);
for ($i = 0; $i < $this->_columnCount; $i++) {
$saveMappingFields = new CRM_Core_DAO_MappingField();
$saveMappingFields->mapping_id = $saveMapping->id;
$saveMappingFields->column_number = $i;
$mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
$id = isset($mapperKeyParts[0]) ? $mapperKeyParts[0] : NULL;
$first = isset($mapperKeyParts[1]) ? $mapperKeyParts[1] : NULL;
$second = isset($mapperKeyParts[2]) ? $mapperKeyParts[2] : NULL;
$saveMappingFields->name = $mapper[$i];
$saveMappingFields->save();
}
$this->set('savedMapping', $saveMappingFields->mapping_id);
}
$parser = new CRM_Member_Import_Parser_Membership($mapperKeysMain, $mapperLocType, $mapperPhoneType);
$parser->run($fileName, $seperator, $mapper, $skipColumnHeader,
CRM_Import_Parser::MODE_PREVIEW, $this->get('contactType')
);
// add all the necessary variables to the form
$parser->set($this);
}
}

View file

@ -0,0 +1,198 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
* $Id$
*
*/
/**
* This class previews the uploaded file and returns summary
* statistics
*/
class CRM_Member_Import_Form_Preview extends CRM_Import_Form_Preview {
/**
* Set variables up before form is built.
*
* @return void
*/
public function preProcess() {
$skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
//get the data from the session
$dataValues = $this->get('dataValues');
$mapper = $this->get('mapper');
$invalidRowCount = $this->get('invalidRowCount');
$conflictRowCount = $this->get('conflictRowCount');
$mismatchCount = $this->get('unMatchCount');
//get the mapping name displayed if the mappingId is set
$mappingId = $this->get('loadMappingId');
if ($mappingId) {
$mapDAO = new CRM_Core_DAO_Mapping();
$mapDAO->id = $mappingId;
$mapDAO->find(TRUE);
$this->assign('loadedMapping', $mappingId);
$this->assign('savedName', $mapDAO->name);
}
if ($skipColumnHeader) {
$this->assign('skipColumnHeader', $skipColumnHeader);
$this->assign('rowDisplayCount', 3);
}
else {
$this->assign('rowDisplayCount', 2);
}
if ($invalidRowCount) {
$urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Member_Import_Parser';
$this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
}
if ($conflictRowCount) {
$urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Member_Import_Parser';
$this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
}
if ($mismatchCount) {
$urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Member_Import_Parser';
$this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
}
$properties = array(
'mapper',
'dataValues',
'columnCount',
'totalRowCount',
'validRowCount',
'invalidRowCount',
'conflictRowCount',
'downloadErrorRecordsUrl',
'downloadConflictRecordsUrl',
'downloadMismatchRecordsUrl',
);
foreach ($properties as $property) {
$this->assign($property, $this->get($property));
}
}
/**
* Process the mapped fields and map it into the uploaded file
* preview the file and extract some summary statistics
*
* @return void
*/
public function postProcess() {
$fileName = $this->controller->exportValue('DataSource', 'uploadFile');
$seperator = $this->controller->exportValue('DataSource', 'fieldSeparator');
$skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
$invalidRowCount = $this->get('invalidRowCount');
$conflictRowCount = $this->get('conflictRowCount');
$onDuplicate = $this->get('onDuplicate');
$mapper = $this->controller->exportValue('MapField', 'mapper');
$mapperKeys = array();
$mapperLocType = array();
$mapperPhoneType = array();
// Note: we keep the multi-dimension array (even thought it's not
// needed in the case of memberships import) so that we can merge
// the common code with contacts import later and subclass contact
// and membership imports from there
foreach ($mapper as $key => $value) {
$mapperKeys[$key] = $mapper[$key][0];
if (!empty($mapper[$key][1]) && is_numeric($mapper[$key][1])) {
$mapperLocType[$key] = $mapper[$key][1];
}
else {
$mapperLocType[$key] = NULL;
}
if (!empty($mapper[$key][2]) && (!is_numeric($mapper[$key][2]))) {
$mapperPhoneType[$key] = $mapper[$key][2];
}
else {
$mapperPhoneType[$key] = NULL;
}
}
$parser = new CRM_Member_Import_Parser_Membership($mapperKeys, $mapperLocType, $mapperPhoneType);
$mapFields = $this->get('fields');
foreach ($mapper as $key => $value) {
$header = array();
if (isset($mapFields[$mapper[$key][0]])) {
$header[] = $mapFields[$mapper[$key][0]];
}
$mapperFields[] = implode(' - ', $header);
}
$parser->run($fileName, $seperator,
$mapperFields,
$skipColumnHeader,
CRM_Import_Parser::MODE_IMPORT,
$this->get('contactType'),
$onDuplicate
);
// add all the necessary variables to the form
$parser->set($this, CRM_Import_Parser::MODE_IMPORT);
// check if there is any error occurred
$errorStack = CRM_Core_Error::singleton();
$errors = $errorStack->getErrors();
$errorMessage = array();
if (is_array($errors)) {
foreach ($errors as $key => $value) {
$errorMessage[] = $value['message'];
}
$errorFile = $fileName['name'] . '.error.log';
if ($fd = fopen($errorFile, 'w')) {
fwrite($fd, implode('\n', $errorMessage));
}
fclose($fd);
$this->set('errorFile', $errorFile);
$urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Member_Import_Parser';
$this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
$urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Member_Import_Parser';
$this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
$urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Member_Import_Parser';
$this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
}
}
}

View file

@ -0,0 +1,114 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
* $Id$
*
*/
/**
* This class summarizes the import results
*/
class CRM_Member_Import_Form_Summary extends CRM_Import_Form_Summary {
/**
* Set variables up before form is built.
*
* @return void
*/
public function preProcess() {
// set the error message path to display
$this->assign('errorFile', $this->get('errorFile'));
$totalRowCount = $this->get('totalRowCount');
$relatedCount = $this->get('relatedCount');
$totalRowCount += $relatedCount;
$this->set('totalRowCount', $totalRowCount);
$invalidRowCount = $this->get('invalidRowCount');
$conflictRowCount = $this->get('conflictRowCount');
$duplicateRowCount = $this->get('duplicateRowCount');
$onDuplicate = $this->get('onDuplicate');
$mismatchCount = $this->get('unMatchCount');
if ($duplicateRowCount > 0) {
$urlParams = 'type=' . CRM_Import_Parser::DUPLICATE . '&parser=CRM_Member_Import_Parser';
$this->set('downloadDuplicateRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
}
elseif ($mismatchCount) {
$urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Member_Import_Parser';
$this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
}
else {
$duplicateRowCount = 0;
$this->set('duplicateRowCount', $duplicateRowCount);
}
$this->assign('dupeError', FALSE);
if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
$dupeActionString = ts('These records have been updated with the imported data.');
}
elseif ($onDuplicate == CRM_Import_Parser::DUPLICATE_FILL) {
$dupeActionString = ts('These records have been filled in with the imported data.');
}
else {
/* Skip by default */
$dupeActionString = ts('These records have not been imported.');
$this->assign('dupeError', TRUE);
/* only subtract dupes from successful import if we're skipping */
$this->set('validRowCount', $totalRowCount - $invalidRowCount -
$conflictRowCount - $duplicateRowCount - $mismatchCount
);
}
$this->assign('dupeActionString', $dupeActionString);
$properties = array(
'totalRowCount',
'validRowCount',
'invalidRowCount',
'conflictRowCount',
'downloadConflictRecordsUrl',
'downloadErrorRecordsUrl',
'duplicateRowCount',
'downloadDuplicateRecordsUrl',
'downloadMismatchRecordsUrl',
'groupAdditions',
'unMatchCount',
);
foreach ($properties as $property) {
$this->assign($property, $this->get($property));
}
}
}

View file

@ -0,0 +1,437 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
* $Id$
*
*/
abstract class CRM_Member_Import_Parser extends CRM_Import_Parser {
protected $_fileName;
/**#@+
* @var integer
*/
/**
* Imported file size
*/
protected $_fileSize;
/**
* Seperator being used
*/
protected $_seperator;
/**
* Total number of lines in file
*/
protected $_lineCount;
/**
* Whether the file has a column header or not
*
* @var boolean
*/
protected $_haveColumnHeader;
/**
* @param string $fileName
* @param string $seperator
* @param $mapper
* @param bool $skipColumnHeader
* @param int $mode
* @param int $contactType
* @param int $onDuplicate
*
* @return mixed
* @throws Exception
*/
public function run(
$fileName,
$seperator = ',',
&$mapper,
$skipColumnHeader = FALSE,
$mode = self::MODE_PREVIEW,
$contactType = self::CONTACT_INDIVIDUAL,
$onDuplicate = self::DUPLICATE_SKIP
) {
if (!is_array($fileName)) {
CRM_Core_Error::fatal();
}
$fileName = $fileName['name'];
switch ($contactType) {
case self::CONTACT_INDIVIDUAL:
$this->_contactType = 'Individual';
break;
case self::CONTACT_HOUSEHOLD:
$this->_contactType = 'Household';
break;
case self::CONTACT_ORGANIZATION:
$this->_contactType = 'Organization';
}
$this->init();
$this->_haveColumnHeader = $skipColumnHeader;
$this->_seperator = $seperator;
$fd = fopen($fileName, "r");
if (!$fd) {
return FALSE;
}
$this->_lineCount = $this->_warningCount = 0;
$this->_invalidRowCount = $this->_validCount = 0;
$this->_totalCount = $this->_conflictCount = 0;
$this->_errors = array();
$this->_warnings = array();
$this->_conflicts = array();
$this->_fileSize = number_format(filesize($fileName) / 1024.0, 2);
if ($mode == self::MODE_MAPFIELD) {
$this->_rows = array();
}
else {
$this->_activeFieldCount = count($this->_activeFields);
}
while (!feof($fd)) {
$this->_lineCount++;
$values = fgetcsv($fd, 8192, $seperator);
if (!$values) {
continue;
}
self::encloseScrub($values);
// skip column header if we're not in mapfield mode
if ($mode != self::MODE_MAPFIELD && $skipColumnHeader) {
$skipColumnHeader = FALSE;
continue;
}
/* trim whitespace around the values */
$empty = TRUE;
foreach ($values as $k => $v) {
$values[$k] = trim($v, " \t\r\n");
}
if (CRM_Utils_System::isNull($values)) {
continue;
}
$this->_totalCount++;
if ($mode == self::MODE_MAPFIELD) {
$returnCode = $this->mapField($values);
}
elseif ($mode == self::MODE_PREVIEW) {
$returnCode = $this->preview($values);
}
elseif ($mode == self::MODE_SUMMARY) {
$returnCode = $this->summary($values);
}
elseif ($mode == self::MODE_IMPORT) {
$returnCode = $this->import($onDuplicate, $values);
}
else {
$returnCode = self::ERROR;
}
// note that a line could be valid but still produce a warning
if ($returnCode & self::VALID) {
$this->_validCount++;
if ($mode == self::MODE_MAPFIELD) {
$this->_rows[] = $values;
$this->_activeFieldCount = max($this->_activeFieldCount, count($values));
}
}
if ($returnCode & self::WARNING) {
$this->_warningCount++;
if ($this->_warningCount < $this->_maxWarningCount) {
$this->_warningCount[] = $line;
}
}
if ($returnCode & self::ERROR) {
$this->_invalidRowCount++;
if ($this->_invalidRowCount < $this->_maxErrorCount) {
$recordNumber = $this->_lineCount;
array_unshift($values, $recordNumber);
$this->_errors[] = $values;
}
}
if ($returnCode & self::CONFLICT) {
$this->_conflictCount++;
$recordNumber = $this->_lineCount;
array_unshift($values, $recordNumber);
$this->_conflicts[] = $values;
}
if ($returnCode & self::DUPLICATE) {
if ($returnCode & self::MULTIPLE_DUPE) {
/* TODO: multi-dupes should be counted apart from singles
* on non-skip action */
}
$this->_duplicateCount++;
$recordNumber = $this->_lineCount;
array_unshift($values, $recordNumber);
$this->_duplicates[] = $values;
if ($onDuplicate != self::DUPLICATE_SKIP) {
$this->_validCount++;
}
}
// we give the derived class a way of aborting the process
// note that the return code could be multiple code or'ed together
if ($returnCode & self::STOP) {
break;
}
// if we are done processing the maxNumber of lines, break
if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) {
break;
}
}
fclose($fd);
if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) {
$customHeaders = $mapper;
$customfields = CRM_Core_BAO_CustomField::getFields('Membership');
foreach ($customHeaders as $key => $value) {
if ($id = CRM_Core_BAO_CustomField::getKeyID($value)) {
$customHeaders[$key] = $customfields[$id][0];
}
}
if ($this->_invalidRowCount) {
// removed view url for invlaid contacts
$headers = array_merge(array(
ts('Line Number'),
ts('Reason'),
), $customHeaders);
$this->_errorFileName = self::errorFileName(self::ERROR);
self::exportCSV($this->_errorFileName, $headers, $this->_errors);
}
if ($this->_conflictCount) {
$headers = array_merge(array(
ts('Line Number'),
ts('Reason'),
), $customHeaders);
$this->_conflictFileName = self::errorFileName(self::CONFLICT);
self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts);
}
if ($this->_duplicateCount) {
$headers = array_merge(array(
ts('Line Number'),
ts('View Membership URL'),
), $customHeaders);
$this->_duplicateFileName = self::errorFileName(self::DUPLICATE);
self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates);
}
}
return $this->fini();
}
/**
* Given a list of the importable field keys that the user has selected
* set the active fields array to this list
*
* @param array $fieldKeys mapped array of values
*
* @return void
*/
public function setActiveFields($fieldKeys) {
$this->_activeFieldCount = count($fieldKeys);
foreach ($fieldKeys as $key) {
if (empty($this->_fields[$key])) {
$this->_activeFields[] = new CRM_Member_Import_Field('', ts('- do not import -'));
}
else {
$this->_activeFields[] = clone($this->_fields[$key]);
}
}
}
/**
* Format the field values for input to the api.
*
* @return array
* (reference ) associative array of name/value pairs
*/
public function &getActiveFieldParams() {
$params = array();
for ($i = 0; $i < $this->_activeFieldCount; $i++) {
if (isset($this->_activeFields[$i]->_value)
&& !isset($params[$this->_activeFields[$i]->_name])
&& !isset($this->_activeFields[$i]->_related)
) {
$params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value;
}
}
return $params;
}
/**
* @param string $name
* @param $title
* @param int $type
* @param string $headerPattern
* @param string $dataPattern
*/
public function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') {
if (empty($name)) {
$this->_fields['doNotImport'] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
}
else {
//$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null );
$tempField = CRM_Contact_BAO_Contact::importableFields('All', NULL);
if (!array_key_exists($name, $tempField)) {
$this->_fields[$name] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
}
else {
$this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
CRM_Utils_Array::value('hasLocationType', $tempField[$name])
);
}
}
}
/**
* Store parser values.
*
* @param CRM_Core_Session $store
*
* @param int $mode
*
* @return void
*/
public function set($store, $mode = self::MODE_SUMMARY) {
$store->set('fileSize', $this->_fileSize);
$store->set('lineCount', $this->_lineCount);
$store->set('seperator', $this->_seperator);
$store->set('fields', $this->getSelectValues());
$store->set('fieldTypes', $this->getSelectTypes());
$store->set('headerPatterns', $this->getHeaderPatterns());
$store->set('dataPatterns', $this->getDataPatterns());
$store->set('columnCount', $this->_activeFieldCount);
$store->set('totalRowCount', $this->_totalCount);
$store->set('validRowCount', $this->_validCount);
$store->set('invalidRowCount', $this->_invalidRowCount);
$store->set('conflictRowCount', $this->_conflictCount);
switch ($this->_contactType) {
case 'Individual':
$store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
break;
case 'Household':
$store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD);
break;
case 'Organization':
$store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION);
}
if ($this->_invalidRowCount) {
$store->set('errorsFileName', $this->_errorFileName);
}
if ($this->_conflictCount) {
$store->set('conflictsFileName', $this->_conflictFileName);
}
if (isset($this->_rows) && !empty($this->_rows)) {
$store->set('dataValues', $this->_rows);
}
if ($mode == self::MODE_IMPORT) {
$store->set('duplicateRowCount', $this->_duplicateCount);
if ($this->_duplicateCount) {
$store->set('duplicatesFileName', $this->_duplicateFileName);
}
}
}
/**
* Export data to a CSV file.
*
* @param string $fileName
* @param array $header
* @param array $data
*
* @return void
*/
public static function exportCSV($fileName, $header, $data) {
$output = array();
$fd = fopen($fileName, 'w');
foreach ($header as $key => $value) {
$header[$key] = "\"$value\"";
}
$config = CRM_Core_Config::singleton();
$output[] = implode($config->fieldSeparator, $header);
foreach ($data as $datum) {
foreach ($datum as $key => $value) {
if (is_array($value)) {
foreach ($value[0] as $k1 => $v1) {
if ($k1 == 'location_type_id') {
continue;
}
$datum[$k1] = $v1;
}
}
else {
$datum[$key] = "\"$value\"";
}
}
$output[] = implode($config->fieldSeparator, $datum);
}
fwrite($fd, implode("\n", $output));
fclose($fd);
}
}

View file

@ -0,0 +1,766 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
* $Id$
*
*/
require_once 'api/api.php';
/**
* class to parse membership csv files
*/
class CRM_Member_Import_Parser_Membership extends CRM_Member_Import_Parser {
protected $_mapperKeys;
private $_contactIdIndex;
private $_totalAmountIndex;
private $_membershipTypeIndex;
private $_membershipStatusIndex;
/**
* Array of successfully imported membership id's
*
* @array
*/
protected $_newMemberships;
/**
* Class constructor.
*
* @param $mapperKeys
* @param null $mapperLocType
* @param null $mapperPhoneType
*/
public function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) {
parent::__construct();
$this->_mapperKeys = &$mapperKeys;
}
/**
* The initializer code, called before the processing
*
* @return void
*/
public function init() {
$fields = CRM_Member_BAO_Membership::importableFields($this->_contactType, FALSE);
foreach ($fields as $name => $field) {
$field['type'] = CRM_Utils_Array::value('type', $field, CRM_Utils_Type::T_INT);
$field['dataPattern'] = CRM_Utils_Array::value('dataPattern', $field, '//');
$field['headerPattern'] = CRM_Utils_Array::value('headerPattern', $field, '//');
$this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']);
}
$this->_newMemberships = array();
$this->setActiveFields($this->_mapperKeys);
// FIXME: we should do this in one place together with Form/MapField.php
$this->_contactIdIndex = -1;
$this->_membershipTypeIndex = -1;
$this->_membershipStatusIndex = -1;
$index = 0;
foreach ($this->_mapperKeys as $key) {
switch ($key) {
case 'membership_contact_id':
$this->_contactIdIndex = $index;
break;
case 'membership_type_id':
$this->_membershipTypeIndex = $index;
break;
case 'status_id':
$this->_membershipStatusIndex = $index;
break;
}
$index++;
}
}
/**
* Handle the values in mapField mode.
*
* @param array $values
* The array of values belonging to this line.
*
* @return bool
*/
public function mapField(&$values) {
return CRM_Import_Parser::VALID;
}
/**
* Handle the values in preview mode.
*
* @param array $values
* The array of values belonging to this line.
*
* @return bool
* the result of this processing
*/
public function preview(&$values) {
return $this->summary($values);
}
/**
* Handle the values in summary mode.
*
* @param array $values
* The array of values belonging to this line.
*
* @return bool
* the result of this processing
*/
public function summary(&$values) {
$erroneousField = NULL;
$response = $this->setActiveFieldValues($values, $erroneousField);
$errorRequired = FALSE;
if ($this->_membershipTypeIndex < 0) {
$errorRequired = TRUE;
}
else {
$errorRequired = !CRM_Utils_Array::value($this->_membershipTypeIndex, $values);
}
if ($errorRequired) {
array_unshift($values, ts('Missing required fields'));
return CRM_Import_Parser::ERROR;
}
$params = $this->getActiveFieldParams();
$errorMessage = NULL;
//To check whether start date or join date is provided
if (empty($params['membership_start_date']) && empty($params['join_date'])) {
$errorMessage = 'Membership Start Date is required to create a memberships.';
CRM_Contact_Import_Parser_Contact::addToErrorMsg('Start Date', $errorMessage);
}
//for date-Formats
$session = CRM_Core_Session::singleton();
$dateType = $session->get('dateTypes');
foreach ($params as $key => $val) {
if ($val) {
switch ($key) {
case 'join_date':
if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) {
if (!CRM_Utils_Rule::date($params[$key])) {
CRM_Contact_Import_Parser_Contact::addToErrorMsg('Member Since', $errorMessage);
}
}
else {
CRM_Contact_Import_Parser_Contact::addToErrorMsg('Member Since', $errorMessage);
}
break;
case 'membership_start_date':
if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) {
if (!CRM_Utils_Rule::date($params[$key])) {
CRM_Contact_Import_Parser_Contact::addToErrorMsg('Start Date', $errorMessage);
}
}
else {
CRM_Contact_Import_Parser_Contact::addToErrorMsg('Start Date', $errorMessage);
}
break;
case 'membership_end_date':
if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) {
if (!CRM_Utils_Rule::date($params[$key])) {
CRM_Contact_Import_Parser_Contact::addToErrorMsg('End date', $errorMessage);
}
}
else {
CRM_Contact_Import_Parser_Contact::addToErrorMsg('End date', $errorMessage);
}
break;
case 'membership_type_id':
$membershipTypes = CRM_Member_PseudoConstant::membershipType();
if (!CRM_Utils_Array::crmInArray($val, $membershipTypes) &&
!array_key_exists($val, $membershipTypes)
) {
CRM_Contact_Import_Parser_Contact::addToErrorMsg('Membership Type', $errorMessage);
}
break;
case 'status_id':
if (!CRM_Utils_Array::crmInArray($val, CRM_Member_PseudoConstant::membershipStatus())) {
CRM_Contact_Import_Parser_Contact::addToErrorMsg('Membership Status', $errorMessage);
}
break;
case 'email':
if (!CRM_Utils_Rule::email($val)) {
CRM_Contact_Import_Parser_Contact::addToErrorMsg('Email Address', $errorMessage);
}
}
}
}
//date-Format part ends
$params['contact_type'] = 'Membership';
//checking error in custom data
CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage);
if ($errorMessage) {
$tempMsg = "Invalid value for field(s) : $errorMessage";
array_unshift($values, $tempMsg);
$errorMessage = NULL;
return CRM_Import_Parser::ERROR;
}
return CRM_Import_Parser::VALID;
}
/**
* Handle the values in import mode.
*
* @param int $onDuplicate
* The code for what action to take on duplicates.
* @param array $values
* The array of values belonging to this line.
*
* @return bool
* the result of this processing
*/
public function import($onDuplicate, &$values) {
try {
// first make sure this is a valid line
$response = $this->summary($values);
if ($response != CRM_Import_Parser::VALID) {
return $response;
}
$params = $this->getActiveFieldParams();
//assign join date equal to start date if join date is not provided
if (empty($params['join_date']) && !empty($params['membership_start_date'])) {
$params['join_date'] = $params['membership_start_date'];
}
$session = CRM_Core_Session::singleton();
$dateType = $session->get('dateTypes');
$formatted = array();
$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;
$dateLabels = array(
'join_date' => ts('Member Since'),
'membership_start_date' => ts('Start Date'),
'membership_end_date' => ts('End Date'),
);
foreach ($params as $key => $val) {
if ($val) {
switch ($key) {
case 'join_date':
case 'membership_start_date':
case 'membership_end_date':
if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) {
if (!CRM_Utils_Rule::date($params[$key])) {
CRM_Contact_Import_Parser_Contact::addToErrorMsg($dateLabels[$key], $errorMessage);
}
}
else {
CRM_Contact_Import_Parser_Contact::addToErrorMsg($dateLabels[$key], $errorMessage);
}
break;
case 'membership_type_id':
if (!is_numeric($val)) {
unset($params['membership_type_id']);
$params['membership_type'] = $val;
}
break;
case 'status_id':
if (!is_numeric($val)) {
unset($params['status_id']);
$params['membership_status'] = $val;
}
break;
case 'is_override':
$params[$key] = CRM_Utils_String::strtobool($val);
break;
}
if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
if ($customFields[$customFieldID]['data_type'] == 'Date') {
CRM_Contact_Import_Parser_Contact::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
static $indieFields = NULL;
if ($indieFields == NULL) {
$tempIndieFields = CRM_Member_DAO_Membership::import();
$indieFields = $tempIndieFields;
}
$formatValues = array();
foreach ($params as $key => $field) {
if ($field == NULL || $field === '') {
continue;
}
$formatValues[$key] = $field;
}
//format params to meet api v2 requirements.
//@todo find a way to test removing this formatting
$formatError = $this->membership_format_params($formatValues, $formatted, TRUE);
if ($onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) {
$formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted,
NULL,
'Membership'
);
}
else {
//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');
return CRM_Import_Parser::ERROR;
}
if (!empty($formatValues['membership_id'])) {
$dao = new CRM_Member_BAO_Membership();
$dao->id = $formatValues['membership_id'];
$dates = array('join_date', 'start_date', 'end_date');
foreach ($dates as $v) {
if (empty($formatted[$v])) {
$formatted[$v] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $formatValues['membership_id'], $v);
}
}
$formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted,
$formatValues['membership_id'],
'Membership'
);
if ($dao->find(TRUE)) {
$ids = array(
'membership' => $formatValues['membership_id'],
'userId' => $session->get('userID'),
);
if (empty($params['line_item']) && !empty($formatted['membership_type_id'])) {
CRM_Price_BAO_LineItem::getLineItemArray($formatted, NULL, 'membership', $formatted['membership_type_id']);
}
$newMembership = CRM_Member_BAO_Membership::create($formatted, $ids, TRUE);
if (civicrm_error($newMembership)) {
array_unshift($values, $newMembership['is_error'] . ' for Membership ID ' . $formatValues['membership_id'] . '. Row was skipped.');
return CRM_Import_Parser::ERROR;
}
else {
$this->_newMemberships[] = $newMembership->id;
return CRM_Import_Parser::VALID;
}
}
else {
array_unshift($values, 'Matching Membership record not found for Membership ID ' . $formatValues['membership_id'] . '. Row was skipped.');
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');
if ($this->_contactIdIndex < 0) {
$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');
return CRM_Import_Parser::ERROR;
}
else {
$cid = $matchedIDs[0];
$formatted['contact_id'] = $cid;
//fix for CRM-1924
$calcDates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($formatted['membership_type_id'],
$joinDate,
$startDate,
$endDate
);
self::formattedDates($calcDates, $formatted);
//fix for CRM-3570, exclude the statuses those having is_admin = 1
//now user can import is_admin if is override is true.
$excludeIsAdmin = FALSE;
if (empty($formatted['is_override'])) {
$formatted['exclude_is_admin'] = $excludeIsAdmin = TRUE;
}
$calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($startDate,
$endDate,
$joinDate,
'today',
$excludeIsAdmin,
$formatted['membership_type_id'],
$formatted
);
if (empty($formatted['status_id'])) {
$formatted['status_id'] = $calcStatus['id'];
}
elseif (empty($formatted['is_override'])) {
if (empty($calcStatus)) {
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.');
return CRM_Import_Parser::ERROR;
}
elseif ($formatted['status_id'] != $calcStatus['id']) {
//Status Hold" is either NOT mapped or is FALSE
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.');
return CRM_Import_Parser::ERROR;
}
}
$newMembership = civicrm_api3('membership', 'create', $formatted);
$this->_newMemberships[] = $newMembership['id'];
return CRM_Import_Parser::VALID;
}
}
else {
// Using new Dedupe rule.
$ruleParams = array(
'contact_type' => $this->_contactType,
'used' => 'Unsupervised',
);
$fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams);
$disp = '';
foreach ($fieldsArray as $value) {
if (array_key_exists(trim($value), $params)) {
$paramValue = $params[trim($value)];
if (is_array($paramValue)) {
$disp .= $params[trim($value)][0][trim($value)] . " ";
}
else {
$disp .= $params[trim($value)] . " ";
}
}
}
if (!empty($params['external_identifier'])) {
if ($disp) {
$disp .= "AND {$params['external_identifier']}";
}
else {
$disp = $params['external_identifier'];
}
}
array_unshift($values, 'No matching Contact found for (' . $disp . ')');
return CRM_Import_Parser::ERROR;
}
}
else {
if (!empty($formatValues['external_identifier'])) {
$checkCid = new CRM_Contact_DAO_Contact();
$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']);
return CRM_Import_Parser::ERROR;
}
}
//to calculate dates
$calcDates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($formatted['membership_type_id'],
$joinDate,
$startDate,
$endDate
);
self::formattedDates($calcDates, $formatted);
//end of date calculation part
//fix for CRM-3570, exclude the statuses those having is_admin = 1
//now user can import is_admin if is override is true.
$excludeIsAdmin = FALSE;
if (empty($formatted['is_override'])) {
$formatted['exclude_is_admin'] = $excludeIsAdmin = TRUE;
}
$calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($startDate,
$endDate,
$joinDate,
'today',
$excludeIsAdmin,
$formatted['membership_type_id'],
$formatted
);
if (empty($formatted['status_id'])) {
$formatted['status_id'] = CRM_Utils_Array::value('id', $calcStatus);
}
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.');
return CRM_Import_Parser::ERROR;
}
elseif ($formatted['status_id'] != $calcStatus['id']) {
//Status Hold" is either NOT mapped or is FALSE
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.');
return CRM_Import_Parser::ERROR;
}
}
$newMembership = civicrm_api3('membership', 'create', $formatted);
$this->_newMemberships[] = $newMembership['id'];
return CRM_Import_Parser::VALID;
}
}
catch (Exception $e) {
array_unshift($values, $e->getMessage());
return CRM_Import_Parser::ERROR;
}
}
/**
* Get the array of successfully imported membership id's
*
* @return array
*/
public function &getImportedMemberships() {
return $this->_newMemberships;
}
/**
* The initializer code, called before the processing
*
* @return void
*/
public function fini() {
}
/**
* to calculate join, start and end dates
*
* @param array $calcDates
* Array of dates returned by getDatesForMembershipType().
*
* @param $formatted
*
*/
public function formattedDates($calcDates, &$formatted) {
$dates = array(
'join_date',
'start_date',
'end_date',
);
foreach ($dates as $d) {
if (isset($formatted[$d]) &&
!CRM_Utils_System::isNull($formatted[$d])
) {
$formatted[$d] = CRM_Utils_Date::isoToMysql($formatted[$d]);
}
elseif (isset($calcDates[$d])) {
$formatted[$d] = CRM_Utils_Date::isoToMysql($calcDates[$d]);
}
}
}
/**
* @deprecated - this function formats params according to v2 standards but
* need to be sure about the impact of not calling it so retaining on the import class
* take the input parameter list as specified in the data model and
* convert it into the same format that we use in QF and BAO object
*
* @param array $params
* Associative array of property name/value.
* pairs to insert in new contact.
* @param array $values
* The reformatted properties that we can use internally.
*
* @param array|bool $create Is the formatted Values array going to
* be used for CRM_Member_BAO_Membership:create()
*
* @throws Exception
* @return array|error
*/
public function membership_format_params($params, &$values, $create = FALSE) {
require_once 'api/v3/utils.php';
$fields = CRM_Member_DAO_Membership::fields();
_civicrm_api3_store_values($fields, $params, $values);
$customFields = CRM_Core_BAO_CustomField::getFields('Membership');
foreach ($params as $key => $value) {
// ignore empty values or empty arrays etc
if (CRM_Utils_System::isNull($value)) {
continue;
}
//Handling Custom Data
if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
$values[$key] = $value;
$type = $customFields[$customFieldID]['html_type'];
if ($type == 'CheckBox' || $type == 'Multi-Select' || $type == 'AdvMulti-Select') {
$mulValues = explode(',', $value);
$customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
$values[$key] = array();
foreach ($mulValues as $v1) {
foreach ($customOption as $customValueID => $customLabel) {
$customValue = $customLabel['value'];
if ((strtolower($customLabel['label']) == strtolower(trim($v1))) ||
(strtolower($customValue) == strtolower(trim($v1)))
) {
if ($type == 'CheckBox') {
$values[$key][$customValue] = 1;
}
else {
$values[$key][] = $customValue;
}
}
}
}
}
}
switch ($key) {
case 'membership_contact_id':
if (!CRM_Utils_Rule::integer($value)) {
throw new Exception("contact_id not valid: $value");
}
$dao = new CRM_Core_DAO();
$qParams = array();
$svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value",
$qParams
);
if (!$svq) {
throw new Exception("Invalid Contact ID: There is no contact record with contact_id = $value.");
}
$values['contact_id'] = $values['membership_contact_id'];
unset($values['membership_contact_id']);
break;
case 'membership_type_id':
if (!CRM_Utils_Array::value($value, CRM_Member_PseudoConstant::membershipType())) {
throw new Exception('Invalid Membership Type Id');
}
$values[$key] = $value;
break;
case 'membership_type':
$membershipTypeId = CRM_Utils_Array::key(ucfirst($value),
CRM_Member_PseudoConstant::membershipType()
);
if ($membershipTypeId) {
if (!empty($values['membership_type_id']) &&
$membershipTypeId != $values['membership_type_id']
) {
throw new Exception('Mismatched membership Type and Membership Type Id');
}
}
else {
throw new Exception('Invalid Membership Type');
}
$values['membership_type_id'] = $membershipTypeId;
break;
case 'status_id':
if (!CRM_Utils_Array::value($value, CRM_Member_PseudoConstant::membershipStatus())) {
throw new Exception('Invalid Membership Status Id');
}
$values[$key] = $value;
break;
case 'membership_status':
$membershipStatusId = CRM_Utils_Array::key(ucfirst($value),
CRM_Member_PseudoConstant::membershipStatus()
);
if ($membershipStatusId) {
if (!empty($values['status_id']) &&
$membershipStatusId != $values['status_id']
) {
throw new Exception('Mismatched membership Status and Membership Status Id');
}
}
else {
throw new Exception('Invalid Membership Status');
}
$values['status_id'] = $membershipStatusId;
break;
default:
break;
}
}
_civicrm_api3_custom_format_params($params, $values, 'Membership');
if ($create) {
// CRM_Member_BAO_Membership::create() handles membership_start_date,
// membership_end_date and membership_source. So, if $values contains
// membership_start_date, membership_end_date or membership_source,
// convert it to start_date, end_date or source
$changes = array(
'membership_start_date' => 'start_date',
'membership_end_date' => 'end_date',
'membership_source' => 'source',
);
foreach ($changes as $orgVal => $changeVal) {
if (isset($values[$orgVal])) {
$values[$changeVal] = $values[$orgVal];
unset($values[$orgVal]);
}
}
}
return NULL;
}
}