3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class gets the name of the file to upload
39 class CRM_Member_Import_Form_MapField
extends CRM_Import_Form_MapField
{
44 * To store contactType
49 static $_contactType = NULL;
53 * Set variables up before form is built
57 public function preProcess() {
58 $this->_mapperFields
= $this->get('fields');
59 asort($this->_mapperFields
);
61 $this->_columnCount
= $this->get('columnCount');
62 $this->assign('columnCount', $this->_columnCount
);
63 $this->_dataValues
= $this->get('dataValues');
64 $this->assign('dataValues', $this->_dataValues
);
66 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
67 $this->_onDuplicate
= $this->get('onDuplicate', isset($onDuplicate) ?
$onDuplicate : "");
69 $highlightedFields = array();
70 if ($skipColumnHeader) {
71 $this->assign('skipColumnHeader', $skipColumnHeader);
72 $this->assign('rowDisplayCount', 3);
73 /* if we had a column header to skip, stash it for later */
75 $this->_columnHeaders
= $this->_dataValues
[0];
78 $this->assign('rowDisplayCount', 2);
81 //CRM-2219 removing other required fields since for updation only
82 //membership id is required.
83 if ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE
) {
84 $remove = array('membership_contact_id', 'email', 'first_name', 'last_name', 'external_identifier');
85 foreach ($remove as $value) {
86 unset($this->_mapperFields
[$value]);
88 $highlightedFieldsArray = array('membership_id', 'membership_start_date', 'membership_type_id');
89 foreach ($highlightedFieldsArray as $name) {
90 $highlightedFields[] = $name;
93 elseif ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_SKIP
) {
94 unset($this->_mapperFields
['membership_id']);
95 $highlightedFieldsArray = array('membership_contact_id', 'email', 'external_identifier', 'membership_start_date', 'membership_type_id');
96 foreach ($highlightedFieldsArray as $name) {
97 $highlightedFields[] = $name;
101 // modify field title
102 $this->_mapperFields
['status_id'] = ts('Membership Status');
103 $this->_mapperFields
['membership_type_id'] = ts('Membership Type');
105 self
::$_contactType = $this->get('contactType');
106 $this->assign('highlightedFields', $highlightedFields);
110 * Build the form object
114 public function buildQuickForm() {
115 //to save the current mappings
116 if (!$this->get('savedMapping')) {
117 $saveDetailsName = ts('Save this field mapping');
118 $this->applyFilter('saveMappingName', 'trim');
119 $this->add('text', 'saveMappingName', ts('Name'));
120 $this->add('text', 'saveMappingDesc', ts('Description'));
123 $savedMapping = $this->get('savedMapping');
125 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping
::getMappingFields($savedMapping);
127 $mappingName = $mappingName[1];
128 $mappingContactType = $mappingContactType[1];
129 $mappingLocation = CRM_Utils_Array
::value('1', $mappingLocation);
130 $mappingPhoneType = CRM_Utils_Array
::value('1', $mappingPhoneType);
131 $mappingRelation = CRM_Utils_Array
::value('1', $mappingRelation);
133 //mapping is to be loaded from database
135 $params = array('id' => $savedMapping);
137 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
139 $this->assign('loadedMapping', $mappingDetails->name
);
140 $this->set('loadedMapping', $savedMapping);
142 $getMappingName = new CRM_Core_DAO_Mapping();
143 $getMappingName->id
= $savedMapping;
144 $getMappingName->mapping_type
= 'Import Memberships';
145 $getMappingName->find();
146 while ($getMappingName->fetch()) {
147 $mapperName = $getMappingName->name
;
150 $this->assign('savedName', $mapperName);
152 $this->add('hidden', 'mappingId', $savedMapping);
154 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
155 $saveDetailsName = ts('Save as a new field mapping');
156 $this->add('text', 'saveMappingName', ts('Name'));
157 $this->add('text', 'saveMappingDesc', ts('Description'));
160 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
162 $this->addFormRule(array('CRM_Member_Import_Form_MapField', 'formRule'), $this);
164 //-------- end of saved mapping stuff ---------
167 $mapperKeys = array_keys($this->_mapperFields
);
168 $hasHeaders = !empty($this->_columnHeaders
);
169 $headerPatterns = $this->get('headerPatterns');
170 $dataPatterns = $this->get('dataPatterns');
171 $hasLocationTypes = $this->get('fieldTypes');
173 /* Initialize all field usages to false */
175 foreach ($mapperKeys as $key) {
176 $this->_fieldUsed
[$key] = FALSE;
178 $this->_location_types
= CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
179 $sel1 = $this->_mapperFields
;
180 if (!$this->get('onDuplicate')) {
182 unset($sel1['membership_id']);
187 $js = "<script type='text/javascript'>\n";
188 $formName = 'document.forms.' . $this->_name
;
190 //used to warn for mismatch column count or mismatch mapping
193 for ($i = 0; $i < $this->_columnCount
; $i++
) {
194 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
196 if ($this->get('savedMapping')) {
197 if (isset($mappingName[$i])) {
198 if ($mappingName[$i] != ts('- do not import -')) {
200 $mappingHeader = array_keys($this->_mapperFields
, $mappingName[$i]);
202 //When locationType is not set
203 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
205 //When phoneType is not set
206 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
208 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
210 $defaults["mapper[$i]"] = array($mappingHeader[0]);
214 $defaults["mapper[$i]"] = array();
217 for ($k = 1; $k < 4; $k++
) {
218 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
223 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
224 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
227 $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders
[$i], $headerPatterns));
230 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
233 //end of load mapping
236 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
238 // Infer the default from the skipped headers if we have them
239 $defaults["mapper[$i]"] = array(
240 $this->defaultFromHeader($this->_columnHeaders
[$i],
243 // $defaultLocationType->id
248 // Otherwise guess the default from the form of the data
249 $defaults["mapper[$i]"] = array(
250 $this->defaultFromData($dataPatterns, $i),
251 // $defaultLocationType->id
256 $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ?
$sel3 : "", (isset($sel4)) ?
$sel4 : ""));
258 $js .= "</script>\n";
259 $this->assign('initHideBoxes', $js);
261 //set warning if mismatch in more than
262 if (isset($mappingName)) {
263 if (($this->_columnCount
!= count($mappingName))) {
267 if ($warning != 0 && $this->get('savedMapping')) {
268 $session = CRM_Core_Session
::singleton();
269 $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.'));
272 $session = CRM_Core_Session
::singleton();
273 $session->setStatus(NULL);
276 $this->setDefaults($defaults);
278 $this->addButtons(array(
281 'name' => ts('Previous'),
285 'name' => ts('Continue'),
286 'spacing' => ' ',
291 'name' => ts('Cancel'),
298 * Global validation rules for the form
300 * @param array $fields
301 * Posted values of the form.
306 * @return array list of errors to be posted back to the form
309 public static function formRule($fields, $files, $self) {
312 if (!array_key_exists('savedMapping', $fields)) {
313 $importKeys = array();
314 foreach ($fields['mapper'] as $mapperPart) {
315 $importKeys[] = $mapperPart[0];
317 // FIXME: should use the schema titles, not redeclare them
318 $requiredFields = array(
319 'membership_contact_id' => ts('Contact ID'),
320 'membership_type_id' => ts('Membership Type'),
321 'membership_start_date' => ts('Membership Start Date'),
324 $contactTypeId = $self->get('contactType');
325 $contactTypes = array(
326 CRM_Import_Parser
::CONTACT_INDIVIDUAL
=> 'Individual',
327 CRM_Import_Parser
::CONTACT_HOUSEHOLD
=> 'Household',
328 CRM_Import_Parser
::CONTACT_ORGANIZATION
=> 'Organization',
331 'used' => 'Unsupervised',
332 'contact_type' => $contactTypes[$contactTypeId],
334 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup
::dedupeRuleFieldsWeight($params);
336 foreach ($importKeys as $key => $val) {
337 if (array_key_exists($val, $ruleFields)) {
338 $weightSum +
= $ruleFields[$val];
342 foreach ($ruleFields as $field => $weight) {
343 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
346 foreach ($requiredFields as $field => $title) {
347 if (!in_array($field, $importKeys)) {
348 if ($field == 'membership_contact_id') {
349 if ((($weightSum >= $threshold ||
in_array('external_identifier', $importKeys)) &&
350 $self->_onDuplicate
!= CRM_Import_Parser
::DUPLICATE_UPDATE
352 in_array('membership_id', $importKeys)
357 if (!isset($errors['_qf_default'])) {
358 $errors['_qf_default'] = '';
360 $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(
361 1 => $threshold)) . ' ' . ts('(OR Membership ID if update mode.)') . '<br />';
365 if (!isset($errors['_qf_default'])) {
366 $errors['_qf_default'] = '';
368 $errors['_qf_default'] .= ts('Missing required field: %1', array(
369 1 => $title)) . '<br />';
375 if (!empty($fields['saveMapping'])) {
376 $nameField = CRM_Utils_Array
::value('saveMappingName', $fields);
377 if (empty($nameField)) {
378 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
381 $mappingTypeId = CRM_Core_OptionGroup
::getValue('mapping_type', 'Import Membership', 'name');
383 if (CRM_Core_BAO_Mapping
::checkMapping($nameField, $mappingTypeId)) {
384 $errors['saveMappingName'] = ts('Duplicate Import Membership Mapping Name');
389 if (!empty($errors)) {
390 if (!empty($errors['saveMappingName'])) {
392 $assignError = new CRM_Core_Page();
393 $assignError->assign('mappingDetailsError', $_flag);
402 * Process the mapped fields and map it into the uploaded file
403 * preview the file and extract some summary statistics
407 public function postProcess() {
408 $params = $this->controller
->exportValues('MapField');
409 //reload the mapfield if load mapping is pressed
410 if (!empty($params['savedMapping'])) {
411 $this->set('savedMapping', $params['savedMapping']);
412 $this->controller
->resetPage($this->_name
);
416 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
417 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
419 $config = CRM_Core_Config
::singleton();
420 $seperator = $config->fieldSeparator
;
422 $mapperKeys = array();
424 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
425 $mapperKeysMain = array();
426 $mapperLocType = array();
427 $mapperPhoneType = array();
429 for ($i = 0; $i < $this->_columnCount
; $i++
) {
430 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
431 $mapperKeysMain[$i] = $mapperKeys[$i][0];
433 if (!empty($mapperKeys[$i][1]) && is_numeric($mapperKeys[$i][1])) {
434 $mapperLocType[$i] = $mapperKeys[$i][1];
437 $mapperLocType[$i] = NULL;
440 if (!empty($mapperKeys[$i][2]) && (!is_numeric($mapperKeys[$i][2]))) {
441 $mapperPhoneType[$i] = $mapperKeys[$i][2];
444 $mapperPhoneType[$i] = NULL;
448 $this->set('mapper', $mapper);
450 // store mapping Id to display it in the preview page
451 if (!empty($params['mappingId'])) {
452 $this->set('loadMappingId', $params['mappingId']);
454 //Updating Mapping Records
455 if (!empty($params['updateMapping'])) {
456 $mappingFields = new CRM_Core_DAO_MappingField();
457 $mappingFields->mapping_id
= $params['mappingId'];
458 $mappingFields->find();
460 $mappingFieldsId = array();
461 while ($mappingFields->fetch()) {
462 if ($mappingFields->id
) {
463 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
467 for ($i = 0; $i < $this->_columnCount
; $i++
) {
468 $updateMappingFields = new CRM_Core_DAO_MappingField();
469 $updateMappingFields->id
= $mappingFieldsId[$i];
470 $updateMappingFields->mapping_id
= $params['mappingId'];
471 $updateMappingFields->column_number
= $i;
473 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
474 $id = isset($mapperKeyParts[0]) ?
$mapperKeyParts[0] : NULL;
475 $first = isset($mapperKeyParts[1]) ?
$mapperKeyParts[1] : NULL;
476 $second = isset($mapperKeyParts[2]) ?
$mapperKeyParts[2] : NULL;
477 $updateMappingFields->name
= $mapper[$i];
478 $updateMappingFields->save();
482 //Saving Mapping Details and Records
483 if (!empty($params['saveMapping'])) {
484 $mappingParams = array(
485 'name' => $params['saveMappingName'],
486 'description' => $params['saveMappingDesc'],
487 'mapping_type_id' => CRM_Core_OptionGroup
::getValue('mapping_type',
492 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
494 for ($i = 0; $i < $this->_columnCount
; $i++
) {
496 $saveMappingFields = new CRM_Core_DAO_MappingField();
497 $saveMappingFields->mapping_id
= $saveMapping->id
;
498 $saveMappingFields->column_number
= $i;
500 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
501 $id = isset($mapperKeyParts[0]) ?
$mapperKeyParts[0] : NULL;
502 $first = isset($mapperKeyParts[1]) ?
$mapperKeyParts[1] : NULL;
503 $second = isset($mapperKeyParts[2]) ?
$mapperKeyParts[2] : NULL;
504 $saveMappingFields->name
= $mapper[$i];
505 $saveMappingFields->save();
507 $this->set('savedMapping', $saveMappingFields->mapping_id
);
510 $parser = new CRM_Member_Import_Parser_Membership($mapperKeysMain, $mapperLocType, $mapperPhoneType);
511 $parser->run($fileName, $seperator, $mapper, $skipColumnHeader,
512 CRM_Import_Parser
::MODE_PREVIEW
, $this->get('contactType')
514 // add all the necessary variables to the form