Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
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 | /** | |
100fef9d | 42 | * Mapper fields |
6a488035 TO |
43 | * |
44 | * @var array | |
6a488035 TO |
45 | */ |
46 | protected $_mapperFields; | |
47 | ||
48 | /** | |
100fef9d | 49 | * Number of columns in import file |
6a488035 TO |
50 | * |
51 | * @var int | |
6a488035 TO |
52 | */ |
53 | protected $_exportColumnCount; | |
54 | ||
55 | /** | |
100fef9d | 56 | * Loaded mapping ID |
6a488035 TO |
57 | * |
58 | * @var int | |
6a488035 TO |
59 | */ |
60 | protected $_mappingId; | |
61 | ||
62 | /** | |
fe482240 | 63 | * Build the form object. |
6a488035 | 64 | * |
355ba699 | 65 | * @return void |
6a488035 TO |
66 | */ |
67 | public function preProcess() { | |
68 | $this->_exportColumnCount = $this->get('exportColumnCount'); | |
eac832c5 CW |
69 | $this->_mappingId = $this->get('mappingId'); |
70 | ||
6a488035 | 71 | if (!$this->_exportColumnCount) { |
eac832c5 CW |
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 | } | |
6a488035 TO |
81 | } |
82 | else { | |
eac832c5 | 83 | $this->_exportColumnCount += 10; |
6a488035 | 84 | } |
6a488035 TO |
85 | } |
86 | ||
87 | public function buildQuickForm() { | |
88 | CRM_Core_BAO_Mapping::buildMappingForm($this, | |
89 | 'Export', | |
90 | $this->_mappingId, | |
91 | $this->_exportColumnCount, | |
35801459 | 92 | 2, |
6a488035 TO |
93 | $this->get('exportMode') |
94 | ); | |
95 | ||
be2fb01f | 96 | $this->addButtons([ |
7b966967 SL |
97 | [ |
98 | 'type' => 'back', | |
99 | 'name' => ts('Previous'), | |
100 | ], | |
101 | [ | |
102 | 'type' => 'next', | |
103 | 'name' => ts('Export'), | |
104 | 'spacing' => ' ', | |
105 | ], | |
106 | [ | |
107 | 'type' => 'done', | |
108 | 'icon' => 'fa-times', | |
109 | 'name' => ts('Done'), | |
110 | ], | |
111 | ]); | |
6a488035 TO |
112 | } |
113 | ||
114 | /** | |
fe482240 | 115 | * Global validation rules for the form. |
6a488035 | 116 | * |
b9add4b3 TO |
117 | * @param array $fields |
118 | * Posted values of the form. | |
6a488035 | 119 | * |
dd244018 | 120 | * @param $values |
100fef9d | 121 | * @param int $mappingTypeId |
dd244018 | 122 | * |
a6c01b45 CW |
123 | * @return array |
124 | * list of errors to be posted back to the form | |
6a488035 | 125 | */ |
00be9182 | 126 | public static function formRule($fields, $values, $mappingTypeId) { |
be2fb01f | 127 | $errors = []; |
6a488035 | 128 | |
8cc574cf | 129 | if (!empty($fields['saveMapping']) && !empty($fields['_qf_Map_next'])) { |
6a488035 TO |
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 | /** | |
fe482240 | 154 | * Process the uploaded file. |
6a488035 TO |
155 | * |
156 | * @return void | |
6a488035 TO |
157 | */ |
158 | public function postProcess() { | |
159 | $params = $this->controller->exportValues($this->_name); | |
160 | $exportParams = $this->controller->exportValues('Select'); | |
161 | ||
6a488035 TO |
162 | $currentPath = CRM_Utils_System::currentPath(); |
163 | ||
164 | $urlParams = NULL; | |
165 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this); | |
166 | if (CRM_Utils_Rule::qfKey($qfKey)) { | |
167 | $urlParams = "&qfKey=$qfKey"; | |
168 | } | |
169 | ||
170 | //get the button name | |
171 | $buttonName = $this->controller->getButtonName('done'); | |
172 | $buttonName1 = $this->controller->getButtonName('next'); | |
173 | if ($buttonName == '_qf_Map_done') { | |
174 | $this->set('exportColumnCount', NULL); | |
175 | $this->controller->resetPage($this->_name); | |
176 | return CRM_Utils_System::redirect(CRM_Utils_System::url($currentPath, 'force=1' . $urlParams)); | |
177 | } | |
178 | ||
179 | if ($this->controller->exportValue($this->_name, 'addMore')) { | |
180 | $this->set('exportColumnCount', $this->_exportColumnCount); | |
181 | return; | |
182 | } | |
183 | ||
184 | $mapperKeys = $params['mapper'][1]; | |
185 | ||
7d0e773c CW |
186 | $mappedFields = []; |
187 | foreach ((array) $mapperKeys as $field) { | |
188 | if (!empty($field[1])) { | |
189 | $mappedFields[] = CRM_Core_BAO_Mapping::getMappingParams([], $field); | |
6a488035 TO |
190 | } |
191 | } | |
192 | ||
7d0e773c | 193 | if (!$mappedFields) { |
6a488035 TO |
194 | $this->set('mappingId', NULL); |
195 | CRM_Utils_System::redirect(CRM_Utils_System::url($currentPath, '_qf_Map_display=true' . $urlParams)); | |
196 | } | |
197 | ||
198 | if ($buttonName1 == '_qf_Map_next') { | |
a7488080 | 199 | if (!empty($params['updateMapping'])) { |
6a488035 TO |
200 | //save mapping fields |
201 | CRM_Core_BAO_Mapping::saveMappingFields($params, $params['mappingId']); | |
202 | } | |
203 | ||
a7488080 | 204 | if (!empty($params['saveMapping'])) { |
be2fb01f | 205 | $mappingParams = [ |
6a488035 TO |
206 | 'name' => $params['saveMappingName'], |
207 | 'description' => $params['saveMappingDesc'], | |
208 | 'mapping_type_id' => $this->get('mappingTypeId'), | |
be2fb01f | 209 | ]; |
6a488035 TO |
210 | |
211 | $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams); | |
212 | ||
213 | //save mapping fields | |
214 | CRM_Core_BAO_Mapping::saveMappingFields($params, $saveMapping->id); | |
215 | } | |
216 | } | |
217 | ||
218 | //get the csv file | |
219 | CRM_Export_BAO_Export::exportComponents($this->get('selectAll'), | |
220 | $this->get('componentIds'), | |
78b22ce9 | 221 | (array) $this->get('queryParams'), |
6a488035 | 222 | $this->get(CRM_Utils_Sort::SORT_ORDER), |
7d0e773c | 223 | $mappedFields, |
6a488035 TO |
224 | $this->get('returnProperties'), |
225 | $this->get('exportMode'), | |
226 | $this->get('componentClause'), | |
227 | $this->get('componentTable'), | |
228 | $this->get('mergeSameAddress'), | |
229 | $this->get('mergeSameHousehold'), | |
9a8044dd | 230 | $exportParams, |
231 | $this->get('queryOperator') | |
6a488035 TO |
232 | ); |
233 | } | |
234 | ||
235 | /** | |
236 | * Return a descriptive name for the page, used in wizard header | |
237 | * | |
238 | * @return string | |
6a488035 TO |
239 | */ |
240 | public function getTitle() { | |
241 | return ts('Select Fields to Export'); | |
242 | } | |
96025800 | 243 | |
6a488035 | 244 | } |