Merge pull request #12641 from mfb/reply-forwarding
[civicrm-core.git] / CRM / Export / Form / Select.php
CommitLineData
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 */
31aaf096 39class CRM_Export_Form_Select extends CRM_Core_Form_Task {
6a488035
TO
40
41 /**
fe482240 42 * Various Contact types.
6a488035 43 */
7da04cde 44 const
0e6e8724
DL
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 /**
fe482240 52 * Export modes.
6a488035 53 */
7da04cde 54 const
0e6e8724
DL
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 /**
fe482240 65 * Current export mode.
6a488035
TO
66 *
67 * @var int
68 */
69 public $_exportMode;
70
71 public $_componentTable;
72
73 /**
fe482240 74 * Build all the data structures needed to build the form.
6a488035
TO
75 *
76 * @param
77 *
78 * @return void
6a488035 79 */
00be9182 80 public function preProcess() {
df4c8e9c
CW
81 $this->preventAjaxSubmit();
82
6a488035 83 //special case for custom search, directly give option to download csv file
7b845814 84 $customSearchID = $this->get('customSearchID');
85 if ($customSearchID) {
86 CRM_Export_BAO_Export::exportCustom($this->get('customSearchClass'),
87 $this->get('formValues'),
88 $this->get(CRM_Utils_Sort::SORT_ORDER)
89 );
90 }
6a488035
TO
91
92 $this->_selectAll = FALSE;
93 $this->_exportMode = self::CONTACT_EXPORT;
94 $this->_componentIds = array();
95 $this->_componentClause = NULL;
96
3b4adc92 97 // we need to determine component export
3b4adc92
MW
98 $components = array('Contact', 'Contribute', 'Member', 'Event', 'Pledge', 'Case', 'Grant', 'Activity');
99
3b4adc92 100 // FIXME: This should use a modified version of CRM_Contact_Form_Search::getModeValue but it doesn't have all the contexts
ae70f47e 101 // FIXME: Or better still, use CRM_Core_DAO_AllCoreTables::getBriefName($daoName) to get the $entityShortName
a0174743 102 switch ($this->getQueryMode()) {
3b4adc92
MW
103 case CRM_Contact_BAO_Query::MODE_CONTRIBUTE:
104 $entityShortname = 'Contribute';
a0174743 105 $entityDAOName = $entityShortname;
3b4adc92
MW
106 break;
107
108 case CRM_Contact_BAO_Query::MODE_MEMBER:
109 $entityShortname = 'Member';
110 $entityDAOName = 'Membership';
111 break;
112
113 case CRM_Contact_BAO_Query::MODE_EVENT:
114 $entityShortname = 'Event';
a0174743 115 $entityDAOName = $entityShortname;
3b4adc92
MW
116 break;
117
118 case CRM_Contact_BAO_Query::MODE_PLEDGE:
119 $entityShortname = 'Pledge';
a0174743 120 $entityDAOName = $entityShortname;
3b4adc92
MW
121 break;
122
123 case CRM_Contact_BAO_Query::MODE_CASE:
124 $entityShortname = 'Case';
a0174743 125 $entityDAOName = $entityShortname;
3b4adc92
MW
126 break;
127
128 case CRM_Contact_BAO_Query::MODE_GRANT:
129 $entityShortname = 'Grant';
a0174743 130 $entityDAOName = $entityShortname;
3b4adc92
MW
131 break;
132
133 case CRM_Contact_BAO_Query::MODE_ACTIVITY:
134 $entityShortname = 'Activity';
a0174743 135 $entityDAOName = $entityShortname;
3b4adc92
MW
136 break;
137
138 default:
a0174743
MW
139 // FIXME: Code cleanup, we may not need to do this $componentName code here.
140 $formName = CRM_Utils_System::getClassName($this->controller->getStateMachine());
141 $componentName = explode('_', $formName);
142 if ($formName == 'CRM_Export_StateMachine_Standalone') {
143 $componentName = array('CRM', $this->controller->get('entity'));
144 }
3b4adc92 145 $entityShortname = $componentName[1]; // Contact
a0174743 146 $entityDAOName = $entityShortname;
3b4adc92
MW
147 break;
148 }
149
150 if (in_array($entityShortname, $components)) {
151 $this->_exportMode = constant('CRM_Export_Form_Select::' . strtoupper($entityShortname) . '_EXPORT');
152 $formTaskClassName = "CRM_{$entityShortname}_Form_Task";
153 $taskClassName = "CRM_{$entityShortname}_Task";
154 if (isset($formTaskClassName::$entityShortname)) {
155 $this::$entityShortname = $formTaskClassName::$entityShortname;
156 if (isset($formTaskClassName::$tableName)) {
157 $this::$tableName = $formTaskClassName::$tableName;
158 }
159 }
160 else {
161 $this::$entityShortname = $entityShortname;
162 $this::$tableName = CRM_Core_DAO_AllCoreTables::getTableForClass(CRM_Core_DAO_AllCoreTables::getFullName($entityDAOName));
163 }
164 }
165
6a488035 166 // get the submitted values based on search
7b845814 167 if ($this->_action == CRM_Core_Action::ADVANCED) {
6a488035
TO
168 $values = $this->controller->exportValues('Advanced');
169 }
170 elseif ($this->_action == CRM_Core_Action::PROFILE) {
171 $values = $this->controller->exportValues('Builder');
172 }
173 elseif ($this->_action == CRM_Core_Action::COPY) {
174 $values = $this->controller->exportValues('Custom');
175 }
176 else {
ca524188 177 if (in_array($entityShortname, $components) && $entityShortname !== 'Contact') {
6a488035
TO
178 $values = $this->controller->exportValues('Search');
179 }
180 else {
181 $values = $this->controller->exportValues('Basic');
182 }
183 }
184
185 $count = 0;
186 $this->_matchingContacts = FALSE;
187 if (CRM_Utils_Array::value('radio_ts', $values) == 'ts_sel') {
188 foreach ($values as $key => $value) {
189 if (strstr($key, 'mark_x')) {
190 $count++;
191 }
192 if ($count > 2) {
193 $this->_matchingContacts = TRUE;
194 break;
195 }
196 }
197 }
198
a0174743 199 $formTaskClassName::preProcessCommon($this);
6886d6f2 200
3b4adc92
MW
201 // $component is used on CRM/Export/Form/Select.tpl to display extra information for contact export
202 ($this->_exportMode == self::CONTACT_EXPORT) ? $component = FALSE : $component = TRUE;
203 $this->assign('component', $component);
6a488035 204
3b4adc92
MW
205 // Set the task title
206 $componentTasks = $taskClassName::taskTitles();
6a488035 207 $this->_task = $values['task'];
3b4adc92
MW
208 $taskName = $componentTasks[$this->_task];
209 $this->assign('taskName', $taskName);
6a488035
TO
210
211 if ($this->_componentTable) {
212 $query = "
213SELECT count(*)
214FROM {$this->_componentTable}
215";
216 $totalSelectedRecords = CRM_Core_DAO::singleValueQuery($query);
217 }
218 else {
219 $totalSelectedRecords = count($this->_componentIds);
220 }
221 $this->assign('totalSelectedRecords', $totalSelectedRecords);
3b4adc92 222
6a488035
TO
223 // all records actions = save a search
224 if (($values['radio_ts'] == 'ts_all') || ($this->_task == CRM_Contact_Task::SAVE_SEARCH)) {
225 $this->_selectAll = TRUE;
226 $rowCount = $this->get('rowCount');
227 if ($rowCount > 2) {
228 $this->_matchingContacts = TRUE;
229 }
230 $this->assign('totalSelectedRecords', $rowCount);
231 }
232
233 $this->assign('matchingContacts', $this->_matchingContacts);
234 $this->set('componentIds', $this->_componentIds);
235 $this->set('selectAll', $this->_selectAll);
236 $this->set('exportMode', $this->_exportMode);
237 $this->set('componentClause', $this->_componentClause);
238 $this->set('componentTable', $this->_componentTable);
239 }
240
241 /**
fe482240 242 * Build the form object.
6a488035
TO
243 *
244 * @return void
6a488035
TO
245 */
246 public function buildQuickForm() {
247 //export option
248 $exportOptions = $mergeOptions = $postalMailing = array();
249 $exportOptions[] = $this->createElement('radio',
250 NULL, NULL,
7b845814 251 ts('Export PRIMARY fields'),
6a488035
TO
252 self::EXPORT_ALL,
253 array('onClick' => 'showMappingOption( );')
254 );
255 $exportOptions[] = $this->createElement('radio',
256 NULL, NULL,
257 ts('Select fields for export'),
258 self::EXPORT_SELECTED,
259 array('onClick' => 'showMappingOption( );')
260 );
261
262 $mergeOptions[] = $this->createElement('radio',
263 NULL, NULL,
264 ts('Do not merge'),
265 self::EXPORT_MERGE_DO_NOT_MERGE,
266 array('onclick' => 'showGreetingOptions( );')
267 );
268 $mergeOptions[] = $this->createElement('radio',
269 NULL, NULL,
270 ts('Merge All Contacts with the Same Address'),
271 self::EXPORT_MERGE_SAME_ADDRESS,
272 array('onclick' => 'showGreetingOptions( );')
273 );
274 $mergeOptions[] = $this->createElement('radio',
275 NULL, NULL,
276 ts('Merge Household Members into their Households'),
277 self::EXPORT_MERGE_HOUSEHOLD,
278 array('onclick' => 'showGreetingOptions( );')
279 );
280
281 $postalMailing[] = $this->createElement('advcheckbox',
282 'postal_mailing_export',
283 NULL,
284 NULL
285 );
286
287 $this->addGroup($exportOptions, 'exportOption', ts('Export Type'), '<br/>');
288
289 if ($this->_matchingContacts) {
290 $this->_greetingOptions = self::getGreetingOptions();
291
292 foreach ($this->_greetingOptions as $key => $value) {
293 $fieldLabel = ts('%1 (merging > 2 contacts)', array(1 => ucwords(str_replace('_', ' ', $key))));
294 $this->addElement('select', $key, $fieldLabel,
295 $value, array('onchange' => "showOther(this);")
296 );
297 $this->addElement('text', "{$key}_other", '');
298 }
299 }
300
301 if ($this->_exportMode == self::CONTACT_EXPORT) {
302 $this->addGroup($mergeOptions, 'mergeOption', ts('Merge Options'), '<br/>');
303 $this->addGroup($postalMailing, 'postal_mailing_export', ts('Postal Mailing Export'), '<br/>');
304
305 $this->addElement('select', 'additional_group', ts('Additional Group for Export'),
24431f7b
CW
306 array('' => ts('- select group -')) + CRM_Core_PseudoConstant::nestedGroup(),
307 array('class' => 'crm-select2 huge')
6a488035
TO
308 );
309 }
310
311 $this->buildMapping();
312
313 $this->setDefaults(array(
314 'exportOption' => self::EXPORT_ALL,
353ffa53
TO
315 'mergeOption' => self::EXPORT_MERGE_DO_NOT_MERGE,
316 ));
6a488035
TO
317
318 $this->addButtons(array(
319 array(
320 'type' => 'next',
f212d37d 321 'name' => ts('Continue'),
6a488035
TO
322 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
323 'isDefault' => TRUE,
324 ),
325 array(
326 'type' => 'cancel',
327 'name' => ts('Cancel'),
328 ),
329 )
330 );
331
332 $this->addFormRule(array('CRM_Export_Form_Select', 'formRule'), $this);
333 }
334
335 /**
fe482240 336 * Validation.
6a488035 337 *
b9add4b3
TO
338 * @param array $params
339 * (ref.) an assoc array of name/value pairs.
6a488035 340 *
fd31fa4c
EM
341 * @param $files
342 * @param $self
343 *
72b3a70c
CW
344 * @return bool|array
345 * mixed true or array of errors
6a488035 346 */
a0174743 347 public static function formRule($params, $files, $self) {
6a488035
TO
348 $errors = array();
349
350 if (CRM_Utils_Array::value('mergeOption', $params) == self::EXPORT_MERGE_SAME_ADDRESS &&
351 $self->_matchingContacts
352 ) {
353 $greetings = array(
354 'postal_greeting' => 'postal_greeting_other',
355 'addressee' => 'addressee_other',
356 );
357
358 foreach ($greetings as $key => $value) {
359 $otherOption = CRM_Utils_Array::value($key, $params);
360
8cc574cf 361 if ((CRM_Utils_Array::value($otherOption, $self->_greetingOptions[$key]) == ts('Other')) && empty($params[$value])) {
6a488035
TO
362
363 $label = ucwords(str_replace('_', ' ', $key));
364 $errors[$value] = ts('Please enter a value for %1 (merging > 2 contacts), or select a pre-configured option from the list.', array(1 => $label));
365 }
366 }
367 }
368
369 return empty($errors) ? TRUE : $errors;
370 }
371
372 /**
fe482240 373 * Process the uploaded file.
6a488035 374 *
a0174743 375 * @throws \CRM_Core_Exception
6a488035
TO
376 */
377 public function postProcess() {
353ffa53
TO
378 $params = $this->controller->exportValues($this->_name);
379 $exportOption = $params['exportOption'];
380 $mergeSameAddress = CRM_Utils_Array::value('mergeOption', $params) == self::EXPORT_MERGE_SAME_ADDRESS ? 1 : 0;
6a488035
TO
381 $mergeSameHousehold = CRM_Utils_Array::value('mergeOption', $params) == self::EXPORT_MERGE_HOUSEHOLD ? 1 : 0;
382
383 $this->set('mergeSameAddress', $mergeSameAddress);
384 $this->set('mergeSameHousehold', $mergeSameHousehold);
385
386 // instead of increasing the number of arguments to exportComponents function, we
387 // will send $exportParams as another argument, which is an array and suppose to contain
388 // all submitted options or any other argument
389 $exportParams = $params;
390
391 if (!empty($this->_greetingOptions)) {
392 foreach ($this->_greetingOptions as $key => $value) {
393 if ($option = CRM_Utils_Array::value($key, $exportParams)) {
394 if ($this->_greetingOptions[$key][$option] == ts('Other')) {
395 $exportParams[$key] = $exportParams["{$key}_other"];
396 }
397 elseif ($this->_greetingOptions[$key][$option] == ts('List of names')) {
398 $exportParams[$key] = '';
399 }
400 else {
401 $exportParams[$key] = $this->_greetingOptions[$key][$option];
402 }
403 }
404 }
405 }
406
407 $mappingId = CRM_Utils_Array::value('mapping', $params);
408 if ($mappingId) {
409 $this->set('mappingId', $mappingId);
410 }
411 else {
412 $this->set('mappingId', NULL);
413 }
414
6a488035 415 if ($exportOption == self::EXPORT_ALL) {
7b845814 416 CRM_Export_BAO_Export::exportComponents($this->_selectAll,
417 $this->_componentIds,
418 (array) $this->get('queryParams'),
419 $this->get(CRM_Utils_Sort::SORT_ORDER),
420 NULL,
421 $this->get('returnProperties'),
422 $this->_exportMode,
423 $this->_componentClause,
424 $this->_componentTable,
425 $mergeSameAddress,
426 $mergeSameHousehold,
427 $exportParams,
428 $this->get('queryOperator')
429 );
6a488035
TO
430 }
431
432 //reset map page
433 $this->controller->resetPage('Map');
434 }
435
436 /**
437 * Return a descriptive name for the page, used in wizard header
438 *
439 * @return string
6a488035
TO
440 */
441 public function getTitle() {
653f9ec1 442 return ts('Export Options');
6a488035
TO
443 }
444
445 /**
fe482240 446 * Build mapping form element.
6a488035 447 */
00be9182 448 public function buildMapping() {
6a488035
TO
449 switch ($this->_exportMode) {
450 case CRM_Export_Form_Select::CONTACT_EXPORT:
451 $exportType = 'Export Contact';
452 break;
453
454 case CRM_Export_Form_Select::CONTRIBUTE_EXPORT:
455 $exportType = 'Export Contribution';
456 break;
457
458 case CRM_Export_Form_Select::MEMBER_EXPORT:
459 $exportType = 'Export Membership';
460 break;
461
462 case CRM_Export_Form_Select::EVENT_EXPORT:
463 $exportType = 'Export Participant';
464 break;
465
466 case CRM_Export_Form_Select::PLEDGE_EXPORT:
467 $exportType = 'Export Pledge';
468 break;
469
470 case CRM_Export_Form_Select::CASE_EXPORT:
471 $exportType = 'Export Case';
472 break;
473
474 case CRM_Export_Form_Select::GRANT_EXPORT:
475 $exportType = 'Export Grant';
476 break;
477
478 case CRM_Export_Form_Select::ACTIVITY_EXPORT:
479 $exportType = 'Export Activity';
480 break;
481 }
482
95f52e3b 483 $this->set('mappingTypeId', CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', $exportType));
6a488035 484
f8df7165 485 $mappings = CRM_Core_BAO_Mapping::getMappings($exportType);
6a488035
TO
486 if (!empty($mappings)) {
487 $this->add('select', 'mapping', ts('Use Saved Field Mapping'), array('' => '-select-') + $mappings);
488 }
489 }
490
e0ef6999
EM
491 /**
492 * @return array
493 */
00be9182 494 public static function getGreetingOptions() {
6a488035
TO
495 $options = array();
496 $greetings = array(
497 'postal_greeting' => 'postal_greeting_other',
498 'addressee' => 'addressee_other',
499 );
500
501 foreach ($greetings as $key => $value) {
502 $params = array();
503 $optionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
504
505 CRM_Core_DAO::commonRetrieveAll('CRM_Core_DAO_OptionValue', 'option_group_id', $optionGroupId,
506 $params, array('label', 'filter')
507 );
508
509 $greetingCount = 1;
510 $options[$key] = array("$greetingCount" => ts('List of names'));
511
512 foreach ($params as $id => $field) {
513 if (CRM_Utils_Array::value('filter', $field) == 4) {
514 $options[$key][++$greetingCount] = $field['label'];
515 }
516 }
517
518 $options[$key][++$greetingCount] = ts('Other');
519 }
520
521 return $options;
522 }
96025800 523
a0174743
MW
524 /**
525 * Get the query mode (eg. CRM_Core_BAO_Query::MODE_CASE)
526 *
527 * @return int
528 */
529 public function getQueryMode() {
d5fd18f6 530 return (int) ($this->queryMode ?: $this->controller->get('component_mode'));
a0174743
MW
531 }
532
6a488035 533}