Fix regression on case export from recent export fix
[civicrm-core.git] / CRM / Export / Form / Select.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
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
a0174743 101 switch ($this->getQueryMode()) {
3b4adc92
MW
102 case CRM_Contact_BAO_Query::MODE_CONTRIBUTE:
103 $entityShortname = 'Contribute';
a0174743 104 $entityDAOName = $entityShortname;
3b4adc92
MW
105 break;
106
107 case CRM_Contact_BAO_Query::MODE_MEMBER:
108 $entityShortname = 'Member';
109 $entityDAOName = 'Membership';
110 break;
111
112 case CRM_Contact_BAO_Query::MODE_EVENT:
113 $entityShortname = 'Event';
a0174743 114 $entityDAOName = $entityShortname;
3b4adc92
MW
115 break;
116
117 case CRM_Contact_BAO_Query::MODE_PLEDGE:
118 $entityShortname = 'Pledge';
a0174743 119 $entityDAOName = $entityShortname;
3b4adc92
MW
120 break;
121
122 case CRM_Contact_BAO_Query::MODE_CASE:
123 $entityShortname = 'Case';
a0174743 124 $entityDAOName = $entityShortname;
3b4adc92
MW
125 break;
126
127 case CRM_Contact_BAO_Query::MODE_GRANT:
128 $entityShortname = 'Grant';
a0174743 129 $entityDAOName = $entityShortname;
3b4adc92
MW
130 break;
131
132 case CRM_Contact_BAO_Query::MODE_ACTIVITY:
133 $entityShortname = 'Activity';
a0174743 134 $entityDAOName = $entityShortname;
3b4adc92
MW
135 break;
136
137 default:
a0174743
MW
138 // FIXME: Code cleanup, we may not need to do this $componentName code here.
139 $formName = CRM_Utils_System::getClassName($this->controller->getStateMachine());
140 $componentName = explode('_', $formName);
141 if ($formName == 'CRM_Export_StateMachine_Standalone') {
142 $componentName = array('CRM', $this->controller->get('entity'));
143 }
3b4adc92 144 $entityShortname = $componentName[1]; // Contact
a0174743 145 $entityDAOName = $entityShortname;
3b4adc92
MW
146 break;
147 }
148
149 if (in_array($entityShortname, $components)) {
150 $this->_exportMode = constant('CRM_Export_Form_Select::' . strtoupper($entityShortname) . '_EXPORT');
151 $formTaskClassName = "CRM_{$entityShortname}_Form_Task";
152 $taskClassName = "CRM_{$entityShortname}_Task";
153 if (isset($formTaskClassName::$entityShortname)) {
154 $this::$entityShortname = $formTaskClassName::$entityShortname;
155 if (isset($formTaskClassName::$tableName)) {
156 $this::$tableName = $formTaskClassName::$tableName;
157 }
158 }
159 else {
160 $this::$entityShortname = $entityShortname;
161 $this::$tableName = CRM_Core_DAO_AllCoreTables::getTableForClass(CRM_Core_DAO_AllCoreTables::getFullName($entityDAOName));
162 }
163 }
164
6a488035 165 // get the submitted values based on search
7b845814 166 if ($this->_action == CRM_Core_Action::ADVANCED) {
6a488035
TO
167 $values = $this->controller->exportValues('Advanced');
168 }
169 elseif ($this->_action == CRM_Core_Action::PROFILE) {
170 $values = $this->controller->exportValues('Builder');
171 }
172 elseif ($this->_action == CRM_Core_Action::COPY) {
173 $values = $this->controller->exportValues('Custom');
174 }
175 else {
ca524188 176 if (in_array($entityShortname, $components) && $entityShortname !== 'Contact') {
6a488035
TO
177 $values = $this->controller->exportValues('Search');
178 }
179 else {
180 $values = $this->controller->exportValues('Basic');
181 }
182 }
183
184 $count = 0;
185 $this->_matchingContacts = FALSE;
186 if (CRM_Utils_Array::value('radio_ts', $values) == 'ts_sel') {
187 foreach ($values as $key => $value) {
188 if (strstr($key, 'mark_x')) {
189 $count++;
190 }
191 if ($count > 2) {
192 $this->_matchingContacts = TRUE;
193 break;
194 }
195 }
196 }
197
a0174743 198 $formTaskClassName::preProcessCommon($this);
6886d6f2 199
3b4adc92
MW
200 // $component is used on CRM/Export/Form/Select.tpl to display extra information for contact export
201 ($this->_exportMode == self::CONTACT_EXPORT) ? $component = FALSE : $component = TRUE;
202 $this->assign('component', $component);
6a488035 203
3b4adc92
MW
204 // Set the task title
205 $componentTasks = $taskClassName::taskTitles();
6a488035 206 $this->_task = $values['task'];
3b4adc92
MW
207 $taskName = $componentTasks[$this->_task];
208 $this->assign('taskName', $taskName);
6a488035
TO
209
210 if ($this->_componentTable) {
211 $query = "
212SELECT count(*)
213FROM {$this->_componentTable}
214";
215 $totalSelectedRecords = CRM_Core_DAO::singleValueQuery($query);
216 }
217 else {
218 $totalSelectedRecords = count($this->_componentIds);
219 }
220 $this->assign('totalSelectedRecords', $totalSelectedRecords);
3b4adc92 221
6a488035
TO
222 // all records actions = save a search
223 if (($values['radio_ts'] == 'ts_all') || ($this->_task == CRM_Contact_Task::SAVE_SEARCH)) {
224 $this->_selectAll = TRUE;
225 $rowCount = $this->get('rowCount');
226 if ($rowCount > 2) {
227 $this->_matchingContacts = TRUE;
228 }
229 $this->assign('totalSelectedRecords', $rowCount);
230 }
231
232 $this->assign('matchingContacts', $this->_matchingContacts);
233 $this->set('componentIds', $this->_componentIds);
234 $this->set('selectAll', $this->_selectAll);
235 $this->set('exportMode', $this->_exportMode);
236 $this->set('componentClause', $this->_componentClause);
237 $this->set('componentTable', $this->_componentTable);
238 }
239
240 /**
fe482240 241 * Build the form object.
6a488035
TO
242 *
243 * @return void
6a488035
TO
244 */
245 public function buildQuickForm() {
246 //export option
247 $exportOptions = $mergeOptions = $postalMailing = array();
248 $exportOptions[] = $this->createElement('radio',
249 NULL, NULL,
7b845814 250 ts('Export PRIMARY fields'),
6a488035
TO
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'),
24431f7b
CW
305 array('' => ts('- select group -')) + CRM_Core_PseudoConstant::nestedGroup(),
306 array('class' => 'crm-select2 huge')
6a488035
TO
307 );
308 }
309
310 $this->buildMapping();
311
312 $this->setDefaults(array(
313 'exportOption' => self::EXPORT_ALL,
353ffa53
TO
314 'mergeOption' => self::EXPORT_MERGE_DO_NOT_MERGE,
315 ));
6a488035
TO
316
317 $this->addButtons(array(
318 array(
319 'type' => 'next',
f212d37d 320 'name' => ts('Continue'),
6a488035
TO
321 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
322 'isDefault' => TRUE,
323 ),
324 array(
325 'type' => 'cancel',
326 'name' => ts('Cancel'),
327 ),
328 )
329 );
330
331 $this->addFormRule(array('CRM_Export_Form_Select', 'formRule'), $this);
332 }
333
334 /**
fe482240 335 * Validation.
6a488035 336 *
b9add4b3
TO
337 * @param array $params
338 * (ref.) an assoc array of name/value pairs.
6a488035 339 *
fd31fa4c
EM
340 * @param $files
341 * @param $self
342 *
72b3a70c
CW
343 * @return bool|array
344 * mixed true or array of errors
6a488035 345 */
a0174743 346 public static function formRule($params, $files, $self) {
6a488035
TO
347 $errors = array();
348
349 if (CRM_Utils_Array::value('mergeOption', $params) == self::EXPORT_MERGE_SAME_ADDRESS &&
350 $self->_matchingContacts
351 ) {
352 $greetings = array(
353 'postal_greeting' => 'postal_greeting_other',
354 'addressee' => 'addressee_other',
355 );
356
357 foreach ($greetings as $key => $value) {
358 $otherOption = CRM_Utils_Array::value($key, $params);
359
8cc574cf 360 if ((CRM_Utils_Array::value($otherOption, $self->_greetingOptions[$key]) == ts('Other')) && empty($params[$value])) {
6a488035
TO
361
362 $label = ucwords(str_replace('_', ' ', $key));
363 $errors[$value] = ts('Please enter a value for %1 (merging > 2 contacts), or select a pre-configured option from the list.', array(1 => $label));
364 }
365 }
366 }
367
368 return empty($errors) ? TRUE : $errors;
369 }
370
371 /**
fe482240 372 * Process the uploaded file.
6a488035 373 *
a0174743 374 * @throws \CRM_Core_Exception
6a488035
TO
375 */
376 public function postProcess() {
353ffa53
TO
377 $params = $this->controller->exportValues($this->_name);
378 $exportOption = $params['exportOption'];
379 $mergeSameAddress = CRM_Utils_Array::value('mergeOption', $params) == self::EXPORT_MERGE_SAME_ADDRESS ? 1 : 0;
6a488035
TO
380 $mergeSameHousehold = CRM_Utils_Array::value('mergeOption', $params) == self::EXPORT_MERGE_HOUSEHOLD ? 1 : 0;
381
382 $this->set('mergeSameAddress', $mergeSameAddress);
383 $this->set('mergeSameHousehold', $mergeSameHousehold);
384
385 // instead of increasing the number of arguments to exportComponents function, we
386 // will send $exportParams as another argument, which is an array and suppose to contain
387 // all submitted options or any other argument
388 $exportParams = $params;
389
390 if (!empty($this->_greetingOptions)) {
391 foreach ($this->_greetingOptions as $key => $value) {
392 if ($option = CRM_Utils_Array::value($key, $exportParams)) {
393 if ($this->_greetingOptions[$key][$option] == ts('Other')) {
394 $exportParams[$key] = $exportParams["{$key}_other"];
395 }
396 elseif ($this->_greetingOptions[$key][$option] == ts('List of names')) {
397 $exportParams[$key] = '';
398 }
399 else {
400 $exportParams[$key] = $this->_greetingOptions[$key][$option];
401 }
402 }
403 }
404 }
405
406 $mappingId = CRM_Utils_Array::value('mapping', $params);
407 if ($mappingId) {
408 $this->set('mappingId', $mappingId);
409 }
410 else {
411 $this->set('mappingId', NULL);
412 }
413
6a488035 414 if ($exportOption == self::EXPORT_ALL) {
7b845814 415 CRM_Export_BAO_Export::exportComponents($this->_selectAll,
416 $this->_componentIds,
417 (array) $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 );
6a488035
TO
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
6a488035
TO
439 */
440 public function getTitle() {
653f9ec1 441 return ts('Export Options');
6a488035
TO
442 }
443
444 /**
fe482240 445 * Build mapping form element.
6a488035 446 */
00be9182 447 public function buildMapping() {
6a488035
TO
448 switch ($this->_exportMode) {
449 case CRM_Export_Form_Select::CONTACT_EXPORT:
450 $exportType = 'Export Contact';
451 break;
452
453 case CRM_Export_Form_Select::CONTRIBUTE_EXPORT:
454 $exportType = 'Export Contribution';
455 break;
456
457 case CRM_Export_Form_Select::MEMBER_EXPORT:
458 $exportType = 'Export Membership';
459 break;
460
461 case CRM_Export_Form_Select::EVENT_EXPORT:
462 $exportType = 'Export Participant';
463 break;
464
465 case CRM_Export_Form_Select::PLEDGE_EXPORT:
466 $exportType = 'Export Pledge';
467 break;
468
469 case CRM_Export_Form_Select::CASE_EXPORT:
470 $exportType = 'Export Case';
471 break;
472
473 case CRM_Export_Form_Select::GRANT_EXPORT:
474 $exportType = 'Export Grant';
475 break;
476
477 case CRM_Export_Form_Select::ACTIVITY_EXPORT:
478 $exportType = 'Export Activity';
479 break;
480 }
481
95f52e3b 482 $this->set('mappingTypeId', CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', $exportType));
6a488035 483
f8df7165 484 $mappings = CRM_Core_BAO_Mapping::getMappings($exportType);
6a488035
TO
485 if (!empty($mappings)) {
486 $this->add('select', 'mapping', ts('Use Saved Field Mapping'), array('' => '-select-') + $mappings);
487 }
488 }
489
e0ef6999
EM
490 /**
491 * @return array
492 */
00be9182 493 public static function getGreetingOptions() {
6a488035
TO
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 }
96025800 522
a0174743
MW
523 /**
524 * Get the query mode (eg. CRM_Core_BAO_Query::MODE_CASE)
525 *
526 * @return int
527 */
528 public function getQueryMode() {
d5fd18f6 529 return (int) ($this->queryMode ?: $this->controller->get('component_mode'));
a0174743
MW
530 }
531
6a488035 532}