Merge pull request #2753 from fuzionnz/CRM-14394
[civicrm-core.git] / CRM / Custom / Import / Form / MapField.php
CommitLineData
9ff5f6c0
N
1<?php
2class CRM_Custom_Import_Form_MapField extends CRM_Contact_Import_Form_MapField {
3 protected $_parser = 'CRM_Custom_Import_Parser_Api';
4 protected $_mappingType = 'Import Multi value custom data';
5 protected $_highlightedFields = array();
6 /**
7 * entity being imported to
8 * @var string
9 */
10 protected $_entity;
11 /**
12 * Function to set variables up before form is built
13 *
14 * @return void
15 * @access public
16 */
17 public function preProcess() {
18 $this->_mapperFields = $this->get('fields');
b5971ab9 19 asort($this->_mapperFields);
9ff5f6c0
N
20 $this->_columnCount = $this->get('columnCount');
21 $this->assign('columnCount', $this->_columnCount);
22 $this->_dataValues = $this->get('dataValues');
b5971ab9 23
da19dc76
N
24 //Seperate column names from actual values.
25 $columnNames = $this->_dataValues[0];
26 //actual values need to be in 2d array ($array[$i][$j]) format to be parsed by the template.
27 $dataValues[] = $this->_dataValues[1];
28 $this->assign('dataValues', $dataValues);
b5971ab9 29
9ff5f6c0 30 $this->_entity = $this->_multipleCustomData = $this->get('multipleCustomData');
b5971ab9 31 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
9ff5f6c0
N
32 $this->_onDuplicate = $this->get('onDuplicate');
33 if ($skipColumnHeader) {
da19dc76
N
34 //showColNames needs to be true to show "Column Names" column
35 $this->assign('showColNames', $skipColumnHeader);
da19dc76 36 $this->assign('columnNames', $columnNames);
b5971ab9
N
37 /* if we had a column header to skip, stash it for later */
38 $this->_columnHeaders = $this->_dataValues[0];
39 }
7f4f9d0c 40 $this->assign('rowDisplayCount', 2);
9ff5f6c0
N
41 $this->assign('highlightedFields', $this->_highlightedFields);
42 }
43
44 /**
45 * Function to actually build the form
46 *
47 * @return void
48 * @access public
49 */
50 public function buildQuickForm() {
51 parent::buildQuickForm();
52 $this->addFormRule(array('CRM_Custom_Import_Form_MapField', 'formRule'));
53 }
54
55 /**
56 * global validation rules for the form
57 *
58 * @param array $fields posted values of the form
59 *
60 * @return array list of errors to be posted back to the form
61 * @static
62 * @access public
63 */
abc40920 64 static function formRule($fields) {
9ff5f6c0
N
65 $errors = array();
66 $fieldMessage = NULL;
67 if (!array_key_exists('savedMapping', $fields)) {
68 $importKeys = array();
69 foreach ($fields['mapper'] as $mapperPart) {
70 $importKeys[] = $mapperPart[0];
71 }
72 $requiredFields = array(
73 'contact_id' => ts('Contact ID'),
74 );
75 foreach ($requiredFields as $field => $title) {
76 if (!in_array($field, $importKeys)) {
77 if (!isset($errors['_qf_default'])) {
78 $errors['_qf_default'] = '';
79 }
80 $errors['_qf_default'] .= ts('Missing required field: %1', array(1 => $title));
81 }
82 }
83 }
84
a7488080 85 if (!empty($fields['saveMapping'])) {
9ff5f6c0
N
86 $nameField = CRM_Utils_Array::value('saveMappingName', $fields);
87 if (empty($nameField)) {
88 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
89 }
90 else {
abc40920 91 $mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', 'Import Multi value custom data', 'name');
9ff5f6c0
N
92 if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) {
93 $errors['saveMappingName'] = ts('Duplicate ' . $self->_mappingType . 'Mapping Name');
94 }
95 }
96 }
97
98 //display Error if loaded mapping is not selected
99 if (array_key_exists('loadMapping', $fields)) {
100 $getMapName = CRM_Utils_Array::value('savedMapping', $fields);
101 if (empty($getMapName)) {
102 $errors['savedMapping'] = ts('Select saved mapping');
103 }
104 }
105
106 if (!empty($errors)) {
107 if (!empty($errors['saveMappingName'])) {
108 $_flag = 1;
109 $assignError = new CRM_Core_Page();
110 $assignError->assign('mappingDetailsError', $_flag);
111 }
112 return $errors;
113 }
114
115 return TRUE;
116 }
117
118 /**
119 * Process the mapped fields and map it into the uploaded file
120 * preview the file and extract some summary statistics
121 *
122 * @return void
123 * @access public
124 */
125 public function postProcess() {
126 $params = $this->controller->exportValues('MapField');
127 $this->set('multipleCustomData', $this->_multipleCustomData);
128
129 //reload the mapfield if load mapping is pressed
130 if (!empty($params['savedMapping'])) {
131 $this->set('savedMapping', $params['savedMapping']);
132 $this->controller->resetPage($this->_name);
133 return;
134 }
135
136 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
137 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
138 $this->_entity = $this->controller->exportValue('DataSource', 'entity');
139
140 $config = CRM_Core_Config::singleton();
141 $separator = $config->fieldSeparator;
142
143 $mapperKeys = array();
144 $mapper = array();
145 $mapperKeys = $this->controller->exportValue($this->_name, 'mapper');
146 $mapperKeysMain = array();
147
148 for ($i = 0; $i < $this->_columnCount; $i++) {
149 $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]];
150 $mapperKeysMain[$i] = $mapperKeys[$i][0];
151 }
152
153 $this->set('mapper', $mapper);
154
155 // store mapping Id to display it in the preview page
156 $this->set('loadMappingId', CRM_Utils_Array::value('mappingId', $params));
157
158 //Updating Mapping Records
a7488080 159 if (!empty($params['updateMapping'])) {
9ff5f6c0
N
160
161 $mappingFields = new CRM_Core_DAO_MappingField();
162 $mappingFields->mapping_id = $params['mappingId'];
163 $mappingFields->find();
164
165 $mappingFieldsId = array();
166 while ($mappingFields->fetch()) {
167 if ($mappingFields->id) {
168 $mappingFieldsId[$mappingFields->column_number] = $mappingFields->id;
169 }
170 }
171
172 for ($i = 0; $i < $this->_columnCount; $i++) {
173 $updateMappingFields = new CRM_Core_DAO_MappingField();
174 $updateMappingFields->id = $mappingFieldsId[$i];
175 $updateMappingFields->mapping_id = $params['mappingId'];
176 $updateMappingFields->column_number = $i;
177
178 $explodedValues = explode('_', $mapperKeys[$i][0]);
179 $id = CRM_Utils_Array::value(0, $explodedValues);
180 $first = CRM_Utils_Array::value(1, $explodedValues);
181 $second = CRM_Utils_Array::value(2, $explodedValues);
182
183 $updateMappingFields->name = $mapper[$i];
184 $updateMappingFields->save();
185 }
186 }
187
188 //Saving Mapping Details and Records
a7488080 189 if (!empty($params['saveMapping'])) {
9ff5f6c0
N
190 $mappingParams = array(
191 'name' => $params['saveMappingName'],
192 'description' => $params['saveMappingDesc'],
193 'mapping_type_id' => CRM_Core_OptionGroup::getValue('mapping_type',
194 $this->_mappingType,
195 'name'
196 ),
197 );
198 $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);
199
200 for ($i = 0; $i < $this->_columnCount; $i++) {
201 $saveMappingFields = new CRM_Core_DAO_MappingField();
202 $saveMappingFields->mapping_id = $saveMapping->id;
203 $saveMappingFields->column_number = $i;
204
205 $explodedValues = explode('_', $mapperKeys[$i][0]);
206 $id = CRM_Utils_Array::value(0, $explodedValues);
207 $first = CRM_Utils_Array::value(1, $explodedValues);
208 $second = CRM_Utils_Array::value(2, $explodedValues);
209
210 $saveMappingFields->name = $mapper[$i];
211 $saveMappingFields->save();
212 }
213 $this->set('savedMapping', $saveMappingFields->mapping_id);
214 }
215 $this->set('_entity', $this->_entity);
216
217 $parser = new $this->_parser($mapperKeysMain);
218 $parser->setEntity($this->_multipleCustomData);
219 $parser->run($fileName, $separator, $mapper, $skipColumnHeader,
220 CRM_Import_Parser::MODE_PREVIEW, $this->get('contactType')
221 );
222 // add all the necessary variables to the form
223 $parser->set($this);
224 }
a7488080 225}