3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
37 * This class gets the name of the file to upload
39 class CRM_Export_Form_Select
extends CRM_Core_Form_Task
{
42 * Various Contact types.
47 EXPORT_MERGE_DO_NOT_MERGE
= 0,
48 EXPORT_MERGE_SAME_ADDRESS
= 1,
49 EXPORT_MERGE_HOUSEHOLD
= 2;
56 CONTRIBUTE_EXPORT
= 2,
65 * Current export mode.
71 public $_componentTable;
74 * Build all the data structures needed to build the form.
80 public function preProcess() {
81 $this->preventAjaxSubmit();
83 //special case for custom search, directly give option to download csv file
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
)
92 $this->_selectAll
= FALSE;
93 $this->_exportMode
= self
::CONTACT_EXPORT
;
94 $this->_componentIds
= [];
95 $this->_componentClause
= NULL;
97 // we need to determine component export
98 $components = ['Contact', 'Contribute', 'Member', 'Event', 'Pledge', 'Case', 'Grant', 'Activity'];
100 // FIXME: This should use a modified version of CRM_Contact_Form_Search::getModeValue but it doesn't have all the contexts
101 // FIXME: Or better still, use CRM_Core_DAO_AllCoreTables::getBriefName($daoName) to get the $entityShortName
102 switch ($this->getQueryMode()) {
103 case CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
:
104 $entityShortname = 'Contribute';
105 $entityDAOName = $entityShortname;
108 case CRM_Contact_BAO_Query
::MODE_MEMBER
:
109 $entityShortname = 'Member';
110 $entityDAOName = 'Membership';
113 case CRM_Contact_BAO_Query
::MODE_EVENT
:
114 $entityShortname = 'Event';
115 $entityDAOName = $entityShortname;
118 case CRM_Contact_BAO_Query
::MODE_PLEDGE
:
119 $entityShortname = 'Pledge';
120 $entityDAOName = $entityShortname;
123 case CRM_Contact_BAO_Query
::MODE_CASE
:
124 $entityShortname = 'Case';
125 $entityDAOName = $entityShortname;
128 case CRM_Contact_BAO_Query
::MODE_GRANT
:
129 $entityShortname = 'Grant';
130 $entityDAOName = $entityShortname;
133 case CRM_Contact_BAO_Query
::MODE_ACTIVITY
:
134 $entityShortname = 'Activity';
135 $entityDAOName = $entityShortname;
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 = ['CRM', $this->controller
->get('entity')];
146 $entityShortname = $componentName[1];
147 $entityDAOName = $entityShortname;
151 if (in_array($entityShortname, $components)) {
152 $this->_exportMode
= constant('CRM_Export_Form_Select::' . strtoupper($entityShortname) . '_EXPORT');
153 $formTaskClassName = "CRM_{$entityShortname}_Form_Task";
154 $taskClassName = "CRM_{$entityShortname}_Task";
155 if (isset($formTaskClassName::$entityShortname)) {
156 $this::$entityShortname = $formTaskClassName::$entityShortname;
157 if (isset($formTaskClassName::$tableName)) {
158 $this::$tableName = $formTaskClassName::$tableName;
162 $this::$entityShortname = $entityShortname;
163 $this::$tableName = CRM_Core_DAO_AllCoreTables
::getTableForClass(CRM_Core_DAO_AllCoreTables
::getFullName($entityDAOName));
167 // get the submitted values based on search
168 if ($this->_action
== CRM_Core_Action
::ADVANCED
) {
169 $values = $this->controller
->exportValues('Advanced');
171 elseif ($this->_action
== CRM_Core_Action
::PROFILE
) {
172 $values = $this->controller
->exportValues('Builder');
174 elseif ($this->_action
== CRM_Core_Action
::COPY
) {
175 $values = $this->controller
->exportValues('Custom');
178 if (in_array($entityShortname, $components) && $entityShortname !== 'Contact') {
179 $values = $this->controller
->exportValues('Search');
182 $values = $this->controller
->exportValues('Basic');
187 $this->_matchingContacts
= FALSE;
188 if (CRM_Utils_Array
::value('radio_ts', $values) == 'ts_sel') {
189 foreach ($values as $key => $value) {
190 if (strstr($key, 'mark_x')) {
194 $this->_matchingContacts
= TRUE;
200 $formTaskClassName::preProcessCommon($this);
202 // $component is used on CRM/Export/Form/Select.tpl to display extra information for contact export
203 ($this->_exportMode
== self
::CONTACT_EXPORT
) ?
$component = FALSE : $component = TRUE;
204 $this->assign('component', $component);
206 // Set the task title
207 $componentTasks = $taskClassName::taskTitles();
208 $this->_task
= $values['task'];
209 $taskName = $componentTasks[$this->_task
];
210 $this->assign('taskName', $taskName);
212 if ($this->_componentTable
) {
215 FROM {$this->_componentTable}
217 $totalSelectedRecords = CRM_Core_DAO
::singleValueQuery($query);
220 $totalSelectedRecords = count($this->_componentIds
);
222 $this->assign('totalSelectedRecords', $totalSelectedRecords);
224 // all records actions = save a search
225 if (($values['radio_ts'] == 'ts_all') ||
($this->_task
== CRM_Contact_Task
::SAVE_SEARCH
)) {
226 $this->_selectAll
= TRUE;
227 $rowCount = $this->get('rowCount');
229 $this->_matchingContacts
= TRUE;
231 $this->assign('totalSelectedRecords', $rowCount);
234 $this->assign('matchingContacts', $this->_matchingContacts
);
235 $this->set('componentIds', $this->_componentIds
);
236 $this->set('selectAll', $this->_selectAll
);
237 $this->set('exportMode', $this->_exportMode
);
238 $this->set('componentClause', $this->_componentClause
);
239 $this->set('componentTable', $this->_componentTable
);
243 * Build the form object.
247 public function buildQuickForm() {
249 $exportOptions = $mergeOptions = $postalMailing = [];
250 $exportOptions[] = $this->createElement('radio',
252 ts('Export PRIMARY fields'),
254 ['onClick' => 'showMappingOption( );']
256 $exportOptions[] = $this->createElement('radio',
258 ts('Select fields for export'),
259 self
::EXPORT_SELECTED
,
260 ['onClick' => 'showMappingOption( );']
263 $mergeOptions[] = $this->createElement('radio',
266 self
::EXPORT_MERGE_DO_NOT_MERGE
,
267 ['onclick' => 'showGreetingOptions( );']
269 $mergeOptions[] = $this->createElement('radio',
271 ts('Merge All Contacts with the Same Address'),
272 self
::EXPORT_MERGE_SAME_ADDRESS
,
273 ['onclick' => 'showGreetingOptions( );']
275 $mergeOptions[] = $this->createElement('radio',
277 ts('Merge Household Members into their Households'),
278 self
::EXPORT_MERGE_HOUSEHOLD
,
279 ['onclick' => 'showGreetingOptions( );']
282 $postalMailing[] = $this->createElement('advcheckbox',
283 'postal_mailing_export',
288 $this->addGroup($exportOptions, 'exportOption', ts('Export Type'), '<br/>');
290 if ($this->_matchingContacts
) {
291 $this->_greetingOptions
= self
::getGreetingOptions();
293 foreach ($this->_greetingOptions
as $key => $value) {
294 $fieldLabel = ts('%1 (when merging contacts)', [1 => ucwords(str_replace('_', ' ', $key))]);
295 $this->addElement('select', $key, $fieldLabel,
296 $value, ['onchange' => "showOther(this);"]
298 $this->addElement('text', "{$key}_other", '');
302 if ($this->_exportMode
== self
::CONTACT_EXPORT
) {
303 $this->addGroup($mergeOptions, 'mergeOption', ts('Merge Options'), '<br/>');
304 $this->addGroup($postalMailing, 'postal_mailing_export', ts('Postal Mailing Export'), '<br/>');
306 $this->addElement('select', 'additional_group', ts('Additional Group for Export'),
307 ['' => ts('- select group -')] + CRM_Core_PseudoConstant
::nestedGroup(),
308 ['class' => 'crm-select2 huge']
312 $this->buildMapping();
315 'exportOption' => self
::EXPORT_ALL
,
316 'mergeOption' => self
::EXPORT_MERGE_DO_NOT_MERGE
,
322 'name' => ts('Continue'),
323 'spacing' => ' ',
328 'name' => ts('Cancel'),
332 $this->addFormRule(['CRM_Export_Form_Select', 'formRule'], $this);
338 * @param array $params
339 * (ref.) an assoc array of name/value pairs.
345 * mixed true or array of errors
347 public static function formRule($params, $files, $self) {
350 if (CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_SAME_ADDRESS
&&
351 $self->_matchingContacts
354 'postal_greeting' => 'postal_greeting_other',
355 'addressee' => 'addressee_other',
358 foreach ($greetings as $key => $value) {
359 $otherOption = CRM_Utils_Array
::value($key, $params);
361 if ((CRM_Utils_Array
::value($otherOption, $self->_greetingOptions
[$key]) == ts('Other')) && empty($params[$value])) {
363 $label = ucwords(str_replace('_', ' ', $key));
364 $errors[$value] = ts('Please enter a value for %1 (when merging contacts), or select a pre-configured option from the list.', [1 => $label]);
369 return empty($errors) ?
TRUE : $errors;
373 * Process the uploaded file.
375 * @throws \CRM_Core_Exception
377 public function postProcess() {
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;
381 $mergeSameHousehold = CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_HOUSEHOLD ?
1 : 0;
383 $this->set('mergeSameAddress', $mergeSameAddress);
384 $this->set('mergeSameHousehold', $mergeSameHousehold);
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;
391 $mappingId = CRM_Utils_Array
::value('mapping', $params);
393 $this->set('mappingId', $mappingId);
396 $this->set('mappingId', NULL);
399 if ($exportOption == self
::EXPORT_ALL
) {
400 CRM_Export_BAO_Export
::exportComponents($this->_selectAll
,
401 $this->_componentIds
,
402 (array) $this->get('queryParams'),
403 $this->get(CRM_Utils_Sort
::SORT_ORDER
),
405 $this->get('returnProperties'),
407 $this->_componentClause
,
408 $this->_componentTable
,
412 $this->get('queryOperator')
417 $this->controller
->resetPage('Map');
421 * Return a descriptive name for the page, used in wizard header
425 public function getTitle() {
426 return ts('Export Options');
430 * Build mapping form element.
432 public function buildMapping() {
433 switch ($this->_exportMode
) {
434 case CRM_Export_Form_Select
::CONTACT_EXPORT
:
435 $exportType = 'Export Contact';
438 case CRM_Export_Form_Select
::CONTRIBUTE_EXPORT
:
439 $exportType = 'Export Contribution';
442 case CRM_Export_Form_Select
::MEMBER_EXPORT
:
443 $exportType = 'Export Membership';
446 case CRM_Export_Form_Select
::EVENT_EXPORT
:
447 $exportType = 'Export Participant';
450 case CRM_Export_Form_Select
::PLEDGE_EXPORT
:
451 $exportType = 'Export Pledge';
454 case CRM_Export_Form_Select
::CASE_EXPORT
:
455 $exportType = 'Export Case';
458 case CRM_Export_Form_Select
::GRANT_EXPORT
:
459 $exportType = 'Export Grant';
462 case CRM_Export_Form_Select
::ACTIVITY_EXPORT
:
463 $exportType = 'Export Activity';
467 $this->set('mappingTypeId', CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', $exportType));
469 $mappings = CRM_Core_BAO_Mapping
::getMappings($exportType, TRUE);
470 if (!empty($mappings)) {
471 $this->add('select2', 'mapping', ts('Use Saved Field Mapping'), $mappings, FALSE, ['placeholder' => ts('- select -')]);
478 public static function getGreetingOptions() {
481 'postal_greeting' => 'postal_greeting_other',
482 'addressee' => 'addressee_other',
485 foreach ($greetings as $key => $value) {
487 $optionGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
489 CRM_Core_DAO
::commonRetrieveAll('CRM_Core_DAO_OptionValue', 'option_group_id', $optionGroupId,
490 $params, ['label', 'filter']
494 $options[$key] = ["$greetingCount" => ts('List of names')];
496 foreach ($params as $id => $field) {
497 if (CRM_Utils_Array
::value('filter', $field) == 4) {
498 $options[$key][++
$greetingCount] = $field['label'];
502 $options[$key][++
$greetingCount] = ts('Other');
509 * Get the query mode (eg. CRM_Core_BAO_Query::MODE_CASE)
513 public function getQueryMode() {
514 return (int) ($this->queryMode ?
: $this->controller
->get('component_mode'));