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