Merge pull request #14011 from colemanw/typeValidateCleanup
[civicrm-core.git] / CRM / Export / Form / Map.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 * $Id$
33 *
34 */
35
36 /**
37 * This class gets the name of the file to upload
38 */
39 class CRM_Export_Form_Map extends CRM_Core_Form {
40
41 /**
42 * Mapper fields
43 *
44 * @var array
45 */
46 protected $_mapperFields;
47
48 /**
49 * Number of columns in import file
50 *
51 * @var int
52 */
53 protected $_exportColumnCount;
54
55 /**
56 * Loaded mapping ID
57 *
58 * @var int
59 */
60 protected $_mappingId;
61
62 /**
63 * Build the form object.
64 *
65 * @return void
66 */
67 public function preProcess() {
68 $this->_exportColumnCount = $this->get('exportColumnCount');
69 $this->_mappingId = $this->get('mappingId');
70
71 if (!$this->_exportColumnCount) {
72 // Set default from saved mapping
73 if ($this->_mappingId) {
74 $mapping = new CRM_Core_DAO_MappingField();
75 $mapping->mapping_id = $this->_mappingId;
76 $this->_exportColumnCount = $mapping->count();
77 }
78 else {
79 $this->_exportColumnCount = 10;
80 }
81 }
82 else {
83 $this->_exportColumnCount += 10;
84 }
85 }
86
87 public function buildQuickForm() {
88 CRM_Core_BAO_Mapping::buildMappingForm($this,
89 'Export',
90 $this->_mappingId,
91 $this->_exportColumnCount,
92 2,
93 $this->get('exportMode')
94 );
95
96 $this->addButtons([
97 [
98 'type' => 'back',
99 'name' => ts('Previous'),
100 ],
101 [
102 'type' => 'next',
103 'name' => ts('Export'),
104 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
105 ],
106 [
107 'type' => 'done',
108 'icon' => 'fa-times',
109 'name' => ts('Done'),
110 ],
111 ]);
112 }
113
114 /**
115 * Global validation rules for the form.
116 *
117 * @param array $fields
118 * Posted values of the form.
119 *
120 * @param $values
121 * @param int $mappingTypeId
122 *
123 * @return array
124 * list of errors to be posted back to the form
125 */
126 public static function formRule($fields, $values, $mappingTypeId) {
127 $errors = [];
128
129 if (!empty($fields['saveMapping']) && !empty($fields['_qf_Map_next'])) {
130 $nameField = CRM_Utils_Array::value('saveMappingName', $fields);
131 if (empty($nameField)) {
132 $errors['saveMappingName'] = ts('Name is required to save Export Mapping');
133 }
134 else {
135 //check for Duplicate mappingName
136 if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) {
137 $errors['saveMappingName'] = ts('Duplicate Export Mapping Name');
138 }
139 }
140 }
141
142 if (!empty($errors)) {
143 $_flag = 1;
144 $assignError = new CRM_Core_Page();
145 $assignError->assign('mappingDetailsError', $_flag);
146 return $errors;
147 }
148 else {
149 return TRUE;
150 }
151 }
152
153 /**
154 * Process the uploaded file.
155 *
156 * @return void
157 */
158 public function postProcess() {
159 $params = $this->controller->exportValues($this->_name);
160 $exportParams = $this->controller->exportValues('Select');
161
162 $greetingOptions = CRM_Export_Form_Select::getGreetingOptions();
163
164 if (!empty($greetingOptions)) {
165 foreach ($greetingOptions as $key => $value) {
166 if ($option = CRM_Utils_Array::value($key, $exportParams)) {
167 if ($greetingOptions[$key][$option] == ts('Other')) {
168 $exportParams[$key] = $exportParams["{$key}_other"];
169 }
170 elseif ($greetingOptions[$key][$option] == ts('List of names')) {
171 $exportParams[$key] = '';
172 }
173 else {
174 $exportParams[$key] = $greetingOptions[$key][$option];
175 }
176 }
177 }
178 }
179
180 $currentPath = CRM_Utils_System::currentPath();
181
182 $urlParams = NULL;
183 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
184 if (CRM_Utils_Rule::qfKey($qfKey)) {
185 $urlParams = "&qfKey=$qfKey";
186 }
187
188 //get the button name
189 $buttonName = $this->controller->getButtonName('done');
190 $buttonName1 = $this->controller->getButtonName('next');
191 if ($buttonName == '_qf_Map_done') {
192 $this->set('exportColumnCount', NULL);
193 $this->controller->resetPage($this->_name);
194 return CRM_Utils_System::redirect(CRM_Utils_System::url($currentPath, 'force=1' . $urlParams));
195 }
196
197 if ($this->controller->exportValue($this->_name, 'addMore')) {
198 $this->set('exportColumnCount', $this->_exportColumnCount);
199 return;
200 }
201
202 $mapperKeys = $params['mapper'][1];
203
204 $checkEmpty = 0;
205 foreach ($mapperKeys as $value) {
206 if ($value[0]) {
207 $checkEmpty++;
208 }
209 }
210
211 if (!$checkEmpty) {
212 $this->set('mappingId', NULL);
213 CRM_Utils_System::redirect(CRM_Utils_System::url($currentPath, '_qf_Map_display=true' . $urlParams));
214 }
215
216 if ($buttonName1 == '_qf_Map_next') {
217 if (!empty($params['updateMapping'])) {
218 //save mapping fields
219 CRM_Core_BAO_Mapping::saveMappingFields($params, $params['mappingId']);
220 }
221
222 if (!empty($params['saveMapping'])) {
223 $mappingParams = [
224 'name' => $params['saveMappingName'],
225 'description' => $params['saveMappingDesc'],
226 'mapping_type_id' => $this->get('mappingTypeId'),
227 ];
228
229 $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);
230
231 //save mapping fields
232 CRM_Core_BAO_Mapping::saveMappingFields($params, $saveMapping->id);
233 }
234 }
235
236 //get the csv file
237 CRM_Export_BAO_Export::exportComponents($this->get('selectAll'),
238 $this->get('componentIds'),
239 (array) $this->get('queryParams'),
240 $this->get(CRM_Utils_Sort::SORT_ORDER),
241 $mapperKeys,
242 $this->get('returnProperties'),
243 $this->get('exportMode'),
244 $this->get('componentClause'),
245 $this->get('componentTable'),
246 $this->get('mergeSameAddress'),
247 $this->get('mergeSameHousehold'),
248 $exportParams,
249 $this->get('queryOperator')
250 );
251 }
252
253 /**
254 * Return a descriptive name for the page, used in wizard header
255 *
256 * @return string
257 */
258 public function getTitle() {
259 return ts('Select Fields to Export');
260 }
261
262 }