Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class gets the name of the file to upload | |
38 | */ | |
39 | class CRM_Export_Form_Select extends CRM_Core_Form { | |
40 | ||
41 | /** | |
42 | * various Contact types | |
43 | */ | |
0e6e8724 DL |
44 | CONST |
45 | EXPORT_ALL = 1, | |
46 | EXPORT_SELECTED = 2, | |
47 | EXPORT_MERGE_DO_NOT_MERGE = 0, | |
48 | EXPORT_MERGE_SAME_ADDRESS = 1, | |
49 | EXPORT_MERGE_HOUSEHOLD = 2; | |
6a488035 TO |
50 | |
51 | /** | |
52 | * export modes | |
53 | */ | |
0e6e8724 DL |
54 | CONST |
55 | CONTACT_EXPORT = 1, | |
56 | CONTRIBUTE_EXPORT = 2, | |
57 | MEMBER_EXPORT = 3, | |
58 | EVENT_EXPORT = 4, | |
59 | PLEDGE_EXPORT = 5, | |
60 | CASE_EXPORT = 6, | |
61 | GRANT_EXPORT = 7, | |
62 | ACTIVITY_EXPORT = 8; | |
6a488035 TO |
63 | |
64 | /** | |
65 | * current export mode | |
66 | * | |
67 | * @var int | |
68 | */ | |
69 | public $_exportMode; | |
70 | ||
71 | public $_componentTable; | |
72 | ||
73 | /** | |
74 | * build all the data structures needed to build the form | |
75 | * | |
76 | * @param | |
77 | * | |
78 | * @return void | |
79 | * @access public | |
80 | */ | |
81 | function preProcess() { | |
82 | //special case for custom search, directly give option to download csv file | |
83 | $customSearchID = $this->get('customSearchID'); | |
84 | if ($customSearchID) { | |
85 | CRM_Export_BAO_Export::exportCustom($this->get('customSearchClass'), | |
86 | $this->get('formValues'), | |
87 | $this->get(CRM_Utils_Sort::SORT_ORDER) | |
88 | ); | |
89 | } | |
90 | ||
91 | $this->_selectAll = FALSE; | |
92 | $this->_exportMode = self::CONTACT_EXPORT; | |
93 | $this->_componentIds = array(); | |
94 | $this->_componentClause = NULL; | |
95 | ||
96 | // get the submitted values based on search | |
97 | if ($this->_action == CRM_Core_Action::ADVANCED) { | |
98 | $values = $this->controller->exportValues('Advanced'); | |
99 | } | |
100 | elseif ($this->_action == CRM_Core_Action::PROFILE) { | |
101 | $values = $this->controller->exportValues('Builder'); | |
102 | } | |
103 | elseif ($this->_action == CRM_Core_Action::COPY) { | |
104 | $values = $this->controller->exportValues('Custom'); | |
105 | } | |
106 | else { | |
107 | // we need to determine component export | |
529d773e KJ |
108 | $stateMachine = $this->controller->getStateMachine(); |
109 | ||
6a488035 TO |
110 | $formName = CRM_Utils_System::getClassName($stateMachine); |
111 | $componentName = explode('_', $formName); | |
112 | $components = array('Contribute', 'Member', 'Event', 'Pledge', 'Case', 'Grant', 'Activity'); | |
113 | ||
114 | if (in_array($componentName[1], $components)) { | |
0e6e8724 DL |
115 | $fieldName = strtoupper($componentName[1]) . '_EXPORT'; |
116 | $this->_exportMode = self::$fieldName; | |
117 | $className = "CRM_{$componentName[1]}_Form_Task"; | |
118 | $className::preProcessCommon( $this, true ); | |
6a488035 TO |
119 | $values = $this->controller->exportValues('Search'); |
120 | } | |
121 | else { | |
122 | $values = $this->controller->exportValues('Basic'); | |
123 | } | |
124 | } | |
125 | ||
126 | $count = 0; | |
127 | $this->_matchingContacts = FALSE; | |
128 | if (CRM_Utils_Array::value('radio_ts', $values) == 'ts_sel') { | |
129 | foreach ($values as $key => $value) { | |
130 | if (strstr($key, 'mark_x')) { | |
131 | $count++; | |
132 | } | |
133 | if ($count > 2) { | |
134 | $this->_matchingContacts = TRUE; | |
135 | break; | |
136 | } | |
137 | } | |
138 | } | |
139 | ||
140 | $componentMode = $this->get('component_mode'); | |
141 | switch ($componentMode) { | |
142 | case 2: | |
143 | CRM_Contribute_Form_Task::preProcessCommon($this, TRUE); | |
144 | $this->_exportMode = self::CONTRIBUTE_EXPORT; | |
145 | $componentName = array('', 'Contribute'); | |
146 | break; | |
147 | ||
148 | case 3: | |
149 | CRM_Event_Form_Task::preProcessCommon($this, TRUE); | |
150 | $this->_exportMode = self::EVENT_EXPORT; | |
151 | $componentName = array('', 'Event'); | |
152 | break; | |
153 | ||
154 | case 4: | |
155 | CRM_Activity_Form_Task::preProcessCommon($this, TRUE); | |
156 | $this->_exportMode = self::ACTIVITY_EXPORT; | |
157 | $componentName = array('', 'Activity'); | |
158 | break; | |
159 | case 5: | |
160 | CRM_Member_Form_Task::preProcessCommon($this, TRUE); | |
161 | $this->_exportMode = self::MEMBER_EXPORT; | |
162 | $componentName = array('', 'Member'); | |
163 | break; | |
164 | case 6: | |
165 | CRM_Case_Form_Task::preProcessCommon($this, TRUE); | |
166 | $this->_exportMode = self::CASE_EXPORT; | |
167 | $componentName = array('', 'Case'); | |
03e04002 | 168 | break; |
6a488035 TO |
169 | } |
170 | ||
171 | $this->_task = $values['task']; | |
172 | if ($this->_exportMode == self::CONTACT_EXPORT) { | |
173 | $contactTasks = CRM_Contact_Task::taskTitles(); | |
174 | $taskName = $contactTasks[$this->_task]; | |
175 | $component = FALSE; | |
176 | CRM_Contact_Form_Task::preProcessCommon($this, TRUE); | |
177 | } | |
178 | else { | |
179 | $this->assign('taskName', "Export $componentName[1]"); | |
0e6e8724 DL |
180 | $className = "CRM_{$componentName[1]}_Task"; |
181 | $componentTasks = $className::tasks(); | |
6a488035 TO |
182 | $taskName = $componentTasks[$this->_task]; |
183 | $component = TRUE; | |
184 | } | |
185 | ||
186 | if ($this->_componentTable) { | |
187 | $query = " | |
188 | SELECT count(*) | |
189 | FROM {$this->_componentTable} | |
190 | "; | |
191 | $totalSelectedRecords = CRM_Core_DAO::singleValueQuery($query); | |
192 | } | |
193 | else { | |
194 | $totalSelectedRecords = count($this->_componentIds); | |
195 | } | |
196 | $this->assign('totalSelectedRecords', $totalSelectedRecords); | |
197 | $this->assign('taskName', $taskName); | |
198 | $this->assign('component', $component); | |
199 | // all records actions = save a search | |
200 | if (($values['radio_ts'] == 'ts_all') || ($this->_task == CRM_Contact_Task::SAVE_SEARCH)) { | |
201 | $this->_selectAll = TRUE; | |
202 | $rowCount = $this->get('rowCount'); | |
203 | if ($rowCount > 2) { | |
204 | $this->_matchingContacts = TRUE; | |
205 | } | |
206 | $this->assign('totalSelectedRecords', $rowCount); | |
207 | } | |
208 | ||
209 | $this->assign('matchingContacts', $this->_matchingContacts); | |
210 | $this->set('componentIds', $this->_componentIds); | |
211 | $this->set('selectAll', $this->_selectAll); | |
212 | $this->set('exportMode', $this->_exportMode); | |
213 | $this->set('componentClause', $this->_componentClause); | |
214 | $this->set('componentTable', $this->_componentTable); | |
215 | } | |
216 | ||
217 | /** | |
218 | * Function to actually build the form | |
219 | * | |
220 | * @return void | |
221 | * @access public | |
222 | */ | |
223 | public function buildQuickForm() { | |
224 | //export option | |
225 | $exportOptions = $mergeOptions = $postalMailing = array(); | |
226 | $exportOptions[] = $this->createElement('radio', | |
227 | NULL, NULL, | |
228 | ts('Export PRIMARY fields'), | |
229 | self::EXPORT_ALL, | |
230 | array('onClick' => 'showMappingOption( );') | |
231 | ); | |
232 | $exportOptions[] = $this->createElement('radio', | |
233 | NULL, NULL, | |
234 | ts('Select fields for export'), | |
235 | self::EXPORT_SELECTED, | |
236 | array('onClick' => 'showMappingOption( );') | |
237 | ); | |
238 | ||
239 | $mergeOptions[] = $this->createElement('radio', | |
240 | NULL, NULL, | |
241 | ts('Do not merge'), | |
242 | self::EXPORT_MERGE_DO_NOT_MERGE, | |
243 | array('onclick' => 'showGreetingOptions( );') | |
244 | ); | |
245 | $mergeOptions[] = $this->createElement('radio', | |
246 | NULL, NULL, | |
247 | ts('Merge All Contacts with the Same Address'), | |
248 | self::EXPORT_MERGE_SAME_ADDRESS, | |
249 | array('onclick' => 'showGreetingOptions( );') | |
250 | ); | |
251 | $mergeOptions[] = $this->createElement('radio', | |
252 | NULL, NULL, | |
253 | ts('Merge Household Members into their Households'), | |
254 | self::EXPORT_MERGE_HOUSEHOLD, | |
255 | array('onclick' => 'showGreetingOptions( );') | |
256 | ); | |
257 | ||
258 | $postalMailing[] = $this->createElement('advcheckbox', | |
259 | 'postal_mailing_export', | |
260 | NULL, | |
261 | NULL | |
262 | ); | |
263 | ||
264 | $this->addGroup($exportOptions, 'exportOption', ts('Export Type'), '<br/>'); | |
265 | ||
266 | if ($this->_matchingContacts) { | |
267 | $this->_greetingOptions = self::getGreetingOptions(); | |
268 | ||
269 | foreach ($this->_greetingOptions as $key => $value) { | |
270 | $fieldLabel = ts('%1 (merging > 2 contacts)', array(1 => ucwords(str_replace('_', ' ', $key)))); | |
271 | $this->addElement('select', $key, $fieldLabel, | |
272 | $value, array('onchange' => "showOther(this);") | |
273 | ); | |
274 | $this->addElement('text', "{$key}_other", ''); | |
275 | } | |
276 | } | |
277 | ||
278 | if ($this->_exportMode == self::CONTACT_EXPORT) { | |
279 | $this->addGroup($mergeOptions, 'mergeOption', ts('Merge Options'), '<br/>'); | |
280 | $this->addGroup($postalMailing, 'postal_mailing_export', ts('Postal Mailing Export'), '<br/>'); | |
281 | ||
282 | $this->addElement('select', 'additional_group', ts('Additional Group for Export'), | |
283 | array('' => ts('- select group -')) + CRM_Core_PseudoConstant::group() | |
284 | ); | |
285 | } | |
286 | ||
287 | $this->buildMapping(); | |
288 | ||
289 | $this->setDefaults(array( | |
290 | 'exportOption' => self::EXPORT_ALL, | |
291 | 'mergeOption' => self::EXPORT_MERGE_DO_NOT_MERGE, | |
292 | )); | |
293 | ||
294 | $this->addButtons(array( | |
295 | array( | |
296 | 'type' => 'next', | |
297 | 'name' => ts('Continue >>'), | |
298 | 'spacing' => ' ', | |
299 | 'isDefault' => TRUE, | |
300 | ), | |
301 | array( | |
302 | 'type' => 'cancel', | |
303 | 'name' => ts('Cancel'), | |
304 | ), | |
305 | ) | |
306 | ); | |
307 | ||
308 | $this->addFormRule(array('CRM_Export_Form_Select', 'formRule'), $this); | |
309 | } | |
310 | ||
311 | /** | |
312 | * Function for validation | |
313 | * | |
314 | * @param array $params (ref.) an assoc array of name/value pairs | |
315 | * | |
316 | * @return mixed true or array of errors | |
317 | * @access public | |
318 | * @static | |
319 | */ | |
529d773e | 320 | static public function formRule($params, $files, $self) { |
6a488035 TO |
321 | $errors = array(); |
322 | ||
323 | if (CRM_Utils_Array::value('mergeOption', $params) == self::EXPORT_MERGE_SAME_ADDRESS && | |
324 | $self->_matchingContacts | |
325 | ) { | |
326 | $greetings = array( | |
327 | 'postal_greeting' => 'postal_greeting_other', | |
328 | 'addressee' => 'addressee_other', | |
329 | ); | |
330 | ||
331 | foreach ($greetings as $key => $value) { | |
332 | $otherOption = CRM_Utils_Array::value($key, $params); | |
333 | ||
334 | if ((CRM_Utils_Array::value($otherOption, $self->_greetingOptions[$key]) == ts('Other')) && | |
335 | !CRM_Utils_Array::value($value, $params) | |
336 | ) { | |
337 | ||
338 | $label = ucwords(str_replace('_', ' ', $key)); | |
339 | $errors[$value] = ts('Please enter a value for %1 (merging > 2 contacts), or select a pre-configured option from the list.', array(1 => $label)); | |
340 | } | |
341 | } | |
342 | } | |
343 | ||
344 | return empty($errors) ? TRUE : $errors; | |
345 | } | |
346 | ||
347 | /** | |
348 | * Process the uploaded file | |
349 | * | |
350 | * @return void | |
351 | * @access public | |
352 | */ | |
353 | public function postProcess() { | |
354 | $params = $this->controller->exportValues($this->_name); | |
355 | $exportOption = $params['exportOption']; | |
356 | $mergeSameAddress = CRM_Utils_Array::value('mergeOption', $params) == self::EXPORT_MERGE_SAME_ADDRESS ? 1 : 0; | |
357 | $mergeSameHousehold = CRM_Utils_Array::value('mergeOption', $params) == self::EXPORT_MERGE_HOUSEHOLD ? 1 : 0; | |
358 | ||
359 | $this->set('mergeSameAddress', $mergeSameAddress); | |
360 | $this->set('mergeSameHousehold', $mergeSameHousehold); | |
361 | ||
362 | // instead of increasing the number of arguments to exportComponents function, we | |
363 | // will send $exportParams as another argument, which is an array and suppose to contain | |
364 | // all submitted options or any other argument | |
365 | $exportParams = $params; | |
366 | ||
367 | if (!empty($this->_greetingOptions)) { | |
368 | foreach ($this->_greetingOptions as $key => $value) { | |
369 | if ($option = CRM_Utils_Array::value($key, $exportParams)) { | |
370 | if ($this->_greetingOptions[$key][$option] == ts('Other')) { | |
371 | $exportParams[$key] = $exportParams["{$key}_other"]; | |
372 | } | |
373 | elseif ($this->_greetingOptions[$key][$option] == ts('List of names')) { | |
374 | $exportParams[$key] = ''; | |
375 | } | |
376 | else { | |
377 | $exportParams[$key] = $this->_greetingOptions[$key][$option]; | |
378 | } | |
379 | } | |
380 | } | |
381 | } | |
382 | ||
383 | $mappingId = CRM_Utils_Array::value('mapping', $params); | |
384 | if ($mappingId) { | |
385 | $this->set('mappingId', $mappingId); | |
386 | } | |
387 | else { | |
388 | $this->set('mappingId', NULL); | |
389 | } | |
390 | ||
391 | ||
392 | if ($exportOption == self::EXPORT_ALL) { | |
393 | CRM_Export_BAO_Export::exportComponents($this->_selectAll, | |
394 | $this->_componentIds, | |
395 | $this->get('queryParams'), | |
396 | $this->get(CRM_Utils_Sort::SORT_ORDER), | |
397 | NULL, | |
398 | $this->get('returnProperties'), | |
399 | $this->_exportMode, | |
400 | $this->_componentClause, | |
401 | $this->_componentTable, | |
402 | $mergeSameAddress, | |
403 | $mergeSameHousehold, | |
404 | $exportParams, | |
405 | $this->get('queryOperator') | |
406 | ); | |
407 | } | |
408 | ||
409 | //reset map page | |
410 | $this->controller->resetPage('Map'); | |
411 | } | |
412 | ||
413 | /** | |
414 | * Return a descriptive name for the page, used in wizard header | |
415 | * | |
416 | * @return string | |
417 | * @access public | |
418 | */ | |
419 | public function getTitle() { | |
420 | return ts('Export All or Selected Fields'); | |
421 | } | |
422 | ||
423 | /** | |
424 | * Function to build mapping form element | |
425 | * | |
426 | */ | |
427 | function buildMapping() { | |
428 | switch ($this->_exportMode) { | |
429 | case CRM_Export_Form_Select::CONTACT_EXPORT: | |
430 | $exportType = 'Export Contact'; | |
431 | break; | |
432 | ||
433 | case CRM_Export_Form_Select::CONTRIBUTE_EXPORT: | |
434 | $exportType = 'Export Contribution'; | |
435 | break; | |
436 | ||
437 | case CRM_Export_Form_Select::MEMBER_EXPORT: | |
438 | $exportType = 'Export Membership'; | |
439 | break; | |
440 | ||
441 | case CRM_Export_Form_Select::EVENT_EXPORT: | |
442 | $exportType = 'Export Participant'; | |
443 | break; | |
444 | ||
445 | case CRM_Export_Form_Select::PLEDGE_EXPORT: | |
446 | $exportType = 'Export Pledge'; | |
447 | break; | |
448 | ||
449 | case CRM_Export_Form_Select::CASE_EXPORT: | |
450 | $exportType = 'Export Case'; | |
451 | break; | |
452 | ||
453 | case CRM_Export_Form_Select::GRANT_EXPORT: | |
454 | $exportType = 'Export Grant'; | |
455 | break; | |
456 | ||
457 | case CRM_Export_Form_Select::ACTIVITY_EXPORT: | |
458 | $exportType = 'Export Activity'; | |
459 | break; | |
460 | } | |
461 | ||
462 | $mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', $exportType, 'name'); | |
463 | $this->set('mappingTypeId', $mappingTypeId); | |
464 | ||
465 | $mappings = CRM_Core_BAO_Mapping::getMappings($mappingTypeId); | |
466 | if (!empty($mappings)) { | |
467 | $this->add('select', 'mapping', ts('Use Saved Field Mapping'), array('' => '-select-') + $mappings); | |
468 | } | |
469 | } | |
470 | ||
471 | static function getGreetingOptions() { | |
472 | $options = array(); | |
473 | $greetings = array( | |
474 | 'postal_greeting' => 'postal_greeting_other', | |
475 | 'addressee' => 'addressee_other', | |
476 | ); | |
477 | ||
478 | foreach ($greetings as $key => $value) { | |
479 | $params = array(); | |
480 | $optionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name'); | |
481 | ||
482 | CRM_Core_DAO::commonRetrieveAll('CRM_Core_DAO_OptionValue', 'option_group_id', $optionGroupId, | |
483 | $params, array('label', 'filter') | |
484 | ); | |
485 | ||
486 | $greetingCount = 1; | |
487 | $options[$key] = array("$greetingCount" => ts('List of names')); | |
488 | ||
489 | foreach ($params as $id => $field) { | |
490 | if (CRM_Utils_Array::value('filter', $field) == 4) { | |
491 | $options[$key][++$greetingCount] = $field['label']; | |
492 | } | |
493 | } | |
494 | ||
495 | $options[$key][++$greetingCount] = ts('Other'); | |
496 | } | |
497 | ||
498 | return $options; | |
499 | } | |
500 | } | |
501 |