Commit | Line | Data |
---|---|---|
9ff5f6c0 | 1 | <?php |
4c6ce474 EM |
2 | |
3 | /** | |
4 | * Class CRM_Custom_Import_Form_MapField | |
5 | */ | |
9ff5f6c0 N |
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(); | |
10 | /** | |
11 | * entity being imported to | |
12 | * @var string | |
13 | */ | |
14 | protected $_entity; | |
15 | /** | |
16 | * Function to set variables up before form is built | |
17 | * | |
18 | * @return void | |
19 | * @access public | |
20 | */ | |
21 | public function preProcess() { | |
22 | $this->_mapperFields = $this->get('fields'); | |
b5971ab9 | 23 | asort($this->_mapperFields); |
9ff5f6c0 N |
24 | $this->_columnCount = $this->get('columnCount'); |
25 | $this->assign('columnCount', $this->_columnCount); | |
26 | $this->_dataValues = $this->get('dataValues'); | |
b5971ab9 | 27 | |
da19dc76 N |
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); | |
b5971ab9 | 33 | |
9ff5f6c0 | 34 | $this->_entity = $this->_multipleCustomData = $this->get('multipleCustomData'); |
b5971ab9 | 35 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); |
9ff5f6c0 N |
36 | $this->_onDuplicate = $this->get('onDuplicate'); |
37 | if ($skipColumnHeader) { | |
da19dc76 N |
38 | //showColNames needs to be true to show "Column Names" column |
39 | $this->assign('showColNames', $skipColumnHeader); | |
da19dc76 | 40 | $this->assign('columnNames', $columnNames); |
b5971ab9 N |
41 | /* if we had a column header to skip, stash it for later */ |
42 | $this->_columnHeaders = $this->_dataValues[0]; | |
43 | } | |
7f4f9d0c | 44 | $this->assign('rowDisplayCount', 2); |
9ff5f6c0 N |
45 | $this->assign('highlightedFields', $this->_highlightedFields); |
46 | } | |
47 | ||
48 | /** | |
49 | * Function to actually build the form | |
50 | * | |
51 | * @return void | |
52 | * @access public | |
53 | */ | |
4c6ce474 | 54 | public function buildQuickForm() { |
9ff5f6c0 N |
55 | parent::buildQuickForm(); |
56 | $this->addFormRule(array('CRM_Custom_Import_Form_MapField', 'formRule')); | |
57 | } | |
58 | ||
59 | /** | |
60 | * global validation rules for the form | |
61 | * | |
62 | * @param array $fields posted values of the form | |
63 | * | |
64 | * @return array list of errors to be posted back to the form | |
65 | * @static | |
66 | * @access public | |
67 | */ | |
abc40920 | 68 | static function formRule($fields) { |
9ff5f6c0 N |
69 | $errors = array(); |
70 | $fieldMessage = NULL; | |
71 | if (!array_key_exists('savedMapping', $fields)) { | |
72 | $importKeys = array(); | |
73 | foreach ($fields['mapper'] as $mapperPart) { | |
74 | $importKeys[] = $mapperPart[0]; | |
75 | } | |
76 | $requiredFields = array( | |
77 | 'contact_id' => ts('Contact ID'), | |
78 | ); | |
79 | foreach ($requiredFields as $field => $title) { | |
80 | if (!in_array($field, $importKeys)) { | |
81 | if (!isset($errors['_qf_default'])) { | |
82 | $errors['_qf_default'] = ''; | |
83 | } | |
84 | $errors['_qf_default'] .= ts('Missing required field: %1', array(1 => $title)); | |
85 | } | |
86 | } | |
87 | } | |
88 | ||
a7488080 | 89 | if (!empty($fields['saveMapping'])) { |
9ff5f6c0 N |
90 | $nameField = CRM_Utils_Array::value('saveMappingName', $fields); |
91 | if (empty($nameField)) { | |
92 | $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); | |
93 | } | |
94 | else { | |
abc40920 | 95 | $mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', 'Import Multi value custom data', 'name'); |
9ff5f6c0 N |
96 | if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) { |
97 | $errors['saveMappingName'] = ts('Duplicate ' . $self->_mappingType . 'Mapping Name'); | |
98 | } | |
99 | } | |
100 | } | |
101 | ||
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'); | |
107 | } | |
108 | } | |
109 | ||
110 | if (!empty($errors)) { | |
111 | if (!empty($errors['saveMappingName'])) { | |
112 | $_flag = 1; | |
113 | $assignError = new CRM_Core_Page(); | |
114 | $assignError->assign('mappingDetailsError', $_flag); | |
115 | } | |
116 | return $errors; | |
117 | } | |
118 | ||
119 | return TRUE; | |
120 | } | |
121 | ||
122 | /** | |
123 | * Process the mapped fields and map it into the uploaded file | |
124 | * preview the file and extract some summary statistics | |
125 | * | |
126 | * @return void | |
127 | * @access public | |
128 | */ | |
129 | public function postProcess() { | |
130 | $params = $this->controller->exportValues('MapField'); | |
131 | $this->set('multipleCustomData', $this->_multipleCustomData); | |
132 | ||
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); | |
137 | return; | |
138 | } | |
139 | ||
140 | $fileName = $this->controller->exportValue('DataSource', 'uploadFile'); | |
141 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); | |
142 | $this->_entity = $this->controller->exportValue('DataSource', 'entity'); | |
143 | ||
144 | $config = CRM_Core_Config::singleton(); | |
145 | $separator = $config->fieldSeparator; | |
146 | ||
147 | $mapperKeys = array(); | |
148 | $mapper = array(); | |
149 | $mapperKeys = $this->controller->exportValue($this->_name, 'mapper'); | |
150 | $mapperKeysMain = array(); | |
151 | ||
152 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
153 | $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]]; | |
154 | $mapperKeysMain[$i] = $mapperKeys[$i][0]; | |
155 | } | |
156 | ||
157 | $this->set('mapper', $mapper); | |
158 | ||
159 | // store mapping Id to display it in the preview page | |
160 | $this->set('loadMappingId', CRM_Utils_Array::value('mappingId', $params)); | |
161 | ||
162 | //Updating Mapping Records | |
a7488080 | 163 | if (!empty($params['updateMapping'])) { |
9ff5f6c0 N |
164 | |
165 | $mappingFields = new CRM_Core_DAO_MappingField(); | |
166 | $mappingFields->mapping_id = $params['mappingId']; | |
167 | $mappingFields->find(); | |
168 | ||
169 | $mappingFieldsId = array(); | |
170 | while ($mappingFields->fetch()) { | |
171 | if ($mappingFields->id) { | |
172 | $mappingFieldsId[$mappingFields->column_number] = $mappingFields->id; | |
173 | } | |
174 | } | |
175 | ||
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; | |
181 | ||
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); | |
186 | ||
187 | $updateMappingFields->name = $mapper[$i]; | |
188 | $updateMappingFields->save(); | |
189 | } | |
190 | } | |
191 | ||
192 | //Saving Mapping Details and Records | |
a7488080 | 193 | if (!empty($params['saveMapping'])) { |
9ff5f6c0 N |
194 | $mappingParams = array( |
195 | 'name' => $params['saveMappingName'], | |
196 | 'description' => $params['saveMappingDesc'], | |
197 | 'mapping_type_id' => CRM_Core_OptionGroup::getValue('mapping_type', | |
198 | $this->_mappingType, | |
199 | 'name' | |
200 | ), | |
201 | ); | |
202 | $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams); | |
203 | ||
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; | |
208 | ||
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); | |
213 | ||
214 | $saveMappingFields->name = $mapper[$i]; | |
215 | $saveMappingFields->save(); | |
216 | } | |
217 | $this->set('savedMapping', $saveMappingFields->mapping_id); | |
218 | } | |
219 | $this->set('_entity', $this->_entity); | |
220 | ||
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') | |
225 | ); | |
226 | // add all the necessary variables to the form | |
227 | $parser->set($this); | |
228 | } | |
a7488080 | 229 | } |