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