4 * Class CRM_Custom_Import_Form_MapField
6 class CRM_Custom_Import_Form_MapField
extends CRM_Contact_Import_Form_MapField
{
7 protected $_parser = 'CRM_Custom_Import_Parser_Api';
8 protected $_mappingType = 'Import Multi value custom data';
9 protected $_highlightedFields = array();
11 * Entity being imported to
16 * Set variables up before form is built
21 public function preProcess() {
22 $this->_mapperFields
= $this->get('fields');
23 asort($this->_mapperFields
);
24 $this->_columnCount
= $this->get('columnCount');
25 $this->assign('columnCount', $this->_columnCount
);
26 $this->_dataValues
= $this->get('dataValues');
28 //Seperate column names from actual values.
29 $columnNames = $this->_dataValues
[0];
30 //actual values need to be in 2d array ($array[$i][$j]) format to be parsed by the template.
31 $dataValues[] = $this->_dataValues
[1];
32 $this->assign('dataValues', $dataValues);
34 $this->_entity
= $this->_multipleCustomData
= $this->get('multipleCustomData');
35 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
36 $this->_onDuplicate
= $this->get('onDuplicate');
37 if ($skipColumnHeader) {
38 //showColNames needs to be true to show "Column Names" column
39 $this->assign('showColNames', $skipColumnHeader);
40 $this->assign('columnNames', $columnNames);
41 /* if we had a column header to skip, stash it for later */
42 $this->_columnHeaders
= $this->_dataValues
[0];
44 $this->assign('rowDisplayCount', 2);
45 $this->assign('highlightedFields', $this->_highlightedFields
);
49 * Build the form object
54 public function buildQuickForm() {
55 parent
::buildQuickForm();
56 $this->addFormRule(array('CRM_Custom_Import_Form_MapField', 'formRule'));
60 * Global validation rules for the form
62 * @param array $fields posted values of the form
64 * @return array list of errors to be posted back to the form
68 static function formRule($fields) {
71 if (!array_key_exists('savedMapping', $fields)) {
72 $importKeys = array();
73 foreach ($fields['mapper'] as $mapperPart) {
74 $importKeys[] = $mapperPart[0];
76 $requiredFields = array(
77 'contact_id' => ts('Contact ID'),
79 foreach ($requiredFields as $field => $title) {
80 if (!in_array($field, $importKeys)) {
81 if (!isset($errors['_qf_default'])) {
82 $errors['_qf_default'] = '';
84 $errors['_qf_default'] .= ts('Missing required field: %1', array(1 => $title));
89 if (!empty($fields['saveMapping'])) {
90 $nameField = CRM_Utils_Array
::value('saveMappingName', $fields);
91 if (empty($nameField)) {
92 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
95 $mappingTypeId = CRM_Core_OptionGroup
::getValue('mapping_type', 'Import Multi value custom data', 'name');
96 if (CRM_Core_BAO_Mapping
::checkMapping($nameField, $mappingTypeId)) {
97 $errors['saveMappingName'] = ts('Duplicate ' . $self->_mappingType
. 'Mapping Name');
102 //display Error if loaded mapping is not selected
103 if (array_key_exists('loadMapping', $fields)) {
104 $getMapName = CRM_Utils_Array
::value('savedMapping', $fields);
105 if (empty($getMapName)) {
106 $errors['savedMapping'] = ts('Select saved mapping');
110 if (!empty($errors)) {
111 if (!empty($errors['saveMappingName'])) {
113 $assignError = new CRM_Core_Page();
114 $assignError->assign('mappingDetailsError', $_flag);
123 * Process the mapped fields and map it into the uploaded file
124 * preview the file and extract some summary statistics
129 public function postProcess() {
130 $params = $this->controller
->exportValues('MapField');
131 $this->set('multipleCustomData', $this->_multipleCustomData
);
133 //reload the mapfield if load mapping is pressed
134 if (!empty($params['savedMapping'])) {
135 $this->set('savedMapping', $params['savedMapping']);
136 $this->controller
->resetPage($this->_name
);
140 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
141 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
142 $this->_entity
= $this->controller
->exportValue('DataSource', 'entity');
144 $config = CRM_Core_Config
::singleton();
145 $separator = $config->fieldSeparator
;
147 $mapperKeys = array();
149 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
150 $mapperKeysMain = array();
152 for ($i = 0; $i < $this->_columnCount
; $i++
) {
153 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
154 $mapperKeysMain[$i] = $mapperKeys[$i][0];
157 $this->set('mapper', $mapper);
159 // store mapping Id to display it in the preview page
160 $this->set('loadMappingId', CRM_Utils_Array
::value('mappingId', $params));
162 //Updating Mapping Records
163 if (!empty($params['updateMapping'])) {
165 $mappingFields = new CRM_Core_DAO_MappingField();
166 $mappingFields->mapping_id
= $params['mappingId'];
167 $mappingFields->find();
169 $mappingFieldsId = array();
170 while ($mappingFields->fetch()) {
171 if ($mappingFields->id
) {
172 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
176 for ($i = 0; $i < $this->_columnCount
; $i++
) {
177 $updateMappingFields = new CRM_Core_DAO_MappingField();
178 $updateMappingFields->id
= $mappingFieldsId[$i];
179 $updateMappingFields->mapping_id
= $params['mappingId'];
180 $updateMappingFields->column_number
= $i;
182 $explodedValues = explode('_', $mapperKeys[$i][0]);
183 $id = CRM_Utils_Array
::value(0, $explodedValues);
184 $first = CRM_Utils_Array
::value(1, $explodedValues);
185 $second = CRM_Utils_Array
::value(2, $explodedValues);
187 $updateMappingFields->name
= $mapper[$i];
188 $updateMappingFields->save();
192 //Saving Mapping Details and Records
193 if (!empty($params['saveMapping'])) {
194 $mappingParams = array(
195 'name' => $params['saveMappingName'],
196 'description' => $params['saveMappingDesc'],
197 'mapping_type_id' => CRM_Core_OptionGroup
::getValue('mapping_type',
202 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
204 for ($i = 0; $i < $this->_columnCount
; $i++
) {
205 $saveMappingFields = new CRM_Core_DAO_MappingField();
206 $saveMappingFields->mapping_id
= $saveMapping->id
;
207 $saveMappingFields->column_number
= $i;
209 $explodedValues = explode('_', $mapperKeys[$i][0]);
210 $id = CRM_Utils_Array
::value(0, $explodedValues);
211 $first = CRM_Utils_Array
::value(1, $explodedValues);
212 $second = CRM_Utils_Array
::value(2, $explodedValues);
214 $saveMappingFields->name
= $mapper[$i];
215 $saveMappingFields->save();
217 $this->set('savedMapping', $saveMappingFields->mapping_id
);
219 $this->set('_entity', $this->_entity
);
221 $parser = new $this->_parser($mapperKeysMain);
222 $parser->setEntity($this->_multipleCustomData
);
223 $parser->run($fileName, $separator, $mapper, $skipColumnHeader,
224 CRM_Import_Parser
::MODE_PREVIEW
, $this->get('contactType')
226 // add all the necessary variables to the form