3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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
= array();
95 $this->_componentClause
= NULL;
97 // we need to determine component export
98 $components = array('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 switch ($this->getQueryMode()) {
102 case CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
:
103 $entityShortname = 'Contribute';
104 $entityDAOName = $entityShortname;
107 case CRM_Contact_BAO_Query
::MODE_MEMBER
:
108 $entityShortname = 'Member';
109 $entityDAOName = 'Membership';
112 case CRM_Contact_BAO_Query
::MODE_EVENT
:
113 $entityShortname = 'Event';
114 $entityDAOName = $entityShortname;
117 case CRM_Contact_BAO_Query
::MODE_PLEDGE
:
118 $entityShortname = 'Pledge';
119 $entityDAOName = $entityShortname;
122 case CRM_Contact_BAO_Query
::MODE_CASE
:
123 $entityShortname = 'Case';
124 $entityDAOName = $entityShortname;
127 case CRM_Contact_BAO_Query
::MODE_GRANT
:
128 $entityShortname = 'Grant';
129 $entityDAOName = $entityShortname;
132 case CRM_Contact_BAO_Query
::MODE_ACTIVITY
:
133 $entityShortname = 'Activity';
134 $entityDAOName = $entityShortname;
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'));
144 $entityShortname = $componentName[1]; // Contact
145 $entityDAOName = $entityShortname;
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;
160 $this::$entityShortname = $entityShortname;
161 $this::$tableName = CRM_Core_DAO_AllCoreTables
::getTableForClass(CRM_Core_DAO_AllCoreTables
::getFullName($entityDAOName));
165 // get the submitted values based on search
166 if ($this->_action
== CRM_Core_Action
::ADVANCED
) {
167 $values = $this->controller
->exportValues('Advanced');
169 elseif ($this->_action
== CRM_Core_Action
::PROFILE
) {
170 $values = $this->controller
->exportValues('Builder');
172 elseif ($this->_action
== CRM_Core_Action
::COPY
) {
173 $values = $this->controller
->exportValues('Custom');
176 if (in_array($entityShortname, $components) && $entityShortname !== 'Contact') {
177 $values = $this->controller
->exportValues('Search');
180 $values = $this->controller
->exportValues('Basic');
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')) {
192 $this->_matchingContacts
= TRUE;
198 $formTaskClassName::preProcessCommon($this);
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);
204 // Set the task title
205 $componentTasks = $taskClassName::taskTitles();
206 $this->_task
= $values['task'];
207 $taskName = $componentTasks[$this->_task
];
208 $this->assign('taskName', $taskName);
210 if ($this->_componentTable
) {
213 FROM {$this->_componentTable}
215 $totalSelectedRecords = CRM_Core_DAO
::singleValueQuery($query);
218 $totalSelectedRecords = count($this->_componentIds
);
220 $this->assign('totalSelectedRecords', $totalSelectedRecords);
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');
227 $this->_matchingContacts
= TRUE;
229 $this->assign('totalSelectedRecords', $rowCount);
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
);
241 * Build the form object.
245 public function buildQuickForm() {
247 $exportOptions = $mergeOptions = $postalMailing = array();
248 $exportOptions[] = $this->createElement('radio',
250 ts('Export PRIMARY fields'),
252 array('onClick' => 'showMappingOption( );')
254 $exportOptions[] = $this->createElement('radio',
256 ts('Select fields for export'),
257 self
::EXPORT_SELECTED
,
258 array('onClick' => 'showMappingOption( );')
261 $mergeOptions[] = $this->createElement('radio',
264 self
::EXPORT_MERGE_DO_NOT_MERGE
,
265 array('onclick' => 'showGreetingOptions( );')
267 $mergeOptions[] = $this->createElement('radio',
269 ts('Merge All Contacts with the Same Address'),
270 self
::EXPORT_MERGE_SAME_ADDRESS
,
271 array('onclick' => 'showGreetingOptions( );')
273 $mergeOptions[] = $this->createElement('radio',
275 ts('Merge Household Members into their Households'),
276 self
::EXPORT_MERGE_HOUSEHOLD
,
277 array('onclick' => 'showGreetingOptions( );')
280 $postalMailing[] = $this->createElement('advcheckbox',
281 'postal_mailing_export',
286 $this->addGroup($exportOptions, 'exportOption', ts('Export Type'), '<br/>');
288 if ($this->_matchingContacts
) {
289 $this->_greetingOptions
= self
::getGreetingOptions();
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);")
296 $this->addElement('text', "{$key}_other", '');
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/>');
304 $this->addElement('select', 'additional_group', ts('Additional Group for Export'),
305 array('' => ts('- select group -')) + CRM_Core_PseudoConstant
::nestedGroup(),
306 array('class' => 'crm-select2 huge')
310 $this->buildMapping();
312 $this->setDefaults(array(
313 'exportOption' => self
::EXPORT_ALL
,
314 'mergeOption' => self
::EXPORT_MERGE_DO_NOT_MERGE
,
317 $this->addButtons(array(
320 'name' => ts('Continue'),
321 'spacing' => ' ',
326 'name' => ts('Cancel'),
331 $this->addFormRule(array('CRM_Export_Form_Select', 'formRule'), $this);
337 * @param array $params
338 * (ref.) an assoc array of name/value pairs.
344 * mixed true or array of errors
346 public static function formRule($params, $files, $self) {
349 if (CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_SAME_ADDRESS
&&
350 $self->_matchingContacts
353 'postal_greeting' => 'postal_greeting_other',
354 'addressee' => 'addressee_other',
357 foreach ($greetings as $key => $value) {
358 $otherOption = CRM_Utils_Array
::value($key, $params);
360 if ((CRM_Utils_Array
::value($otherOption, $self->_greetingOptions
[$key]) == ts('Other')) && empty($params[$value])) {
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));
368 return empty($errors) ?
TRUE : $errors;
372 * Process the uploaded file.
374 * @throws \CRM_Core_Exception
376 public function postProcess() {
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;
380 $mergeSameHousehold = CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_HOUSEHOLD ?
1 : 0;
382 $this->set('mergeSameAddress', $mergeSameAddress);
383 $this->set('mergeSameHousehold', $mergeSameHousehold);
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;
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"];
396 elseif ($this->_greetingOptions
[$key][$option] == ts('List of names')) {
397 $exportParams[$key] = '';
400 $exportParams[$key] = $this->_greetingOptions
[$key][$option];
406 $mappingId = CRM_Utils_Array
::value('mapping', $params);
408 $this->set('mappingId', $mappingId);
411 $this->set('mappingId', NULL);
414 if ($exportOption == self
::EXPORT_ALL
) {
415 CRM_Export_BAO_Export
::exportComponents($this->_selectAll
,
416 $this->_componentIds
,
417 (array) $this->get('queryParams'),
418 $this->get(CRM_Utils_Sort
::SORT_ORDER
),
420 $this->get('returnProperties'),
422 $this->_componentClause
,
423 $this->_componentTable
,
427 $this->get('queryOperator')
432 $this->controller
->resetPage('Map');
436 * Return a descriptive name for the page, used in wizard header
440 public function getTitle() {
441 return ts('Export Options');
445 * Build mapping form element.
447 public function buildMapping() {
448 switch ($this->_exportMode
) {
449 case CRM_Export_Form_Select
::CONTACT_EXPORT
:
450 $exportType = 'Export Contact';
453 case CRM_Export_Form_Select
::CONTRIBUTE_EXPORT
:
454 $exportType = 'Export Contribution';
457 case CRM_Export_Form_Select
::MEMBER_EXPORT
:
458 $exportType = 'Export Membership';
461 case CRM_Export_Form_Select
::EVENT_EXPORT
:
462 $exportType = 'Export Participant';
465 case CRM_Export_Form_Select
::PLEDGE_EXPORT
:
466 $exportType = 'Export Pledge';
469 case CRM_Export_Form_Select
::CASE_EXPORT
:
470 $exportType = 'Export Case';
473 case CRM_Export_Form_Select
::GRANT_EXPORT
:
474 $exportType = 'Export Grant';
477 case CRM_Export_Form_Select
::ACTIVITY_EXPORT
:
478 $exportType = 'Export Activity';
482 $this->set('mappingTypeId', CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', $exportType));
484 $mappings = CRM_Core_BAO_Mapping
::getMappings($exportType);
485 if (!empty($mappings)) {
486 $this->add('select', 'mapping', ts('Use Saved Field Mapping'), array('' => '-select-') +
$mappings);
493 public static function getGreetingOptions() {
496 'postal_greeting' => 'postal_greeting_other',
497 'addressee' => 'addressee_other',
500 foreach ($greetings as $key => $value) {
502 $optionGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
504 CRM_Core_DAO
::commonRetrieveAll('CRM_Core_DAO_OptionValue', 'option_group_id', $optionGroupId,
505 $params, array('label', 'filter')
509 $options[$key] = array("$greetingCount" => ts('List of names'));
511 foreach ($params as $id => $field) {
512 if (CRM_Utils_Array
::value('filter', $field) == 4) {
513 $options[$key][++
$greetingCount] = $field['label'];
517 $options[$key][++
$greetingCount] = ts('Other');
524 * Get the query mode (eg. CRM_Core_BAO_Query::MODE_CASE)
528 public function getQueryMode() {
529 return (int) $this->controller
->get('component_mode');