3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * This class gets the name of the file to upload
39 class CRM_Export_Form_Select
extends CRM_Core_Form
{
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,
71 public $_componentTable;
74 * build all the data structures needed to build the form
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
)
91 $this->_selectAll
= FALSE;
92 $this->_exportMode
= self
::CONTACT_EXPORT
;
93 $this->_componentIds
= array();
94 $this->_componentClause
= NULL;
96 // get the submitted values based on search
97 if ($this->_action
== CRM_Core_Action
::ADVANCED
) {
98 $values = $this->controller
->exportValues('Advanced');
100 elseif ($this->_action
== CRM_Core_Action
::PROFILE
) {
101 $values = $this->controller
->exportValues('Builder');
103 elseif ($this->_action
== CRM_Core_Action
::COPY
) {
104 $values = $this->controller
->exportValues('Custom');
107 // we need to determine component export
108 $stateMachine = $this->controller
->getStateMachine();
110 $formName = CRM_Utils_System
::getClassName($stateMachine);
111 $componentName = explode('_', $formName);
112 $components = array('Contribute', 'Member', 'Event', 'Pledge', 'Case', 'Grant', 'Activity');
114 if (in_array($componentName[1], $components)) {
115 switch ($componentName[1]) {
117 $this->_exportMode
= self
::CONTRIBUTE_EXPORT
;
120 $this->_exportMode
= self
::MEMBER_EXPORT
;
123 $this->_exportMode
= self
::EVENT_EXPORT
;
126 $this->_exportMode
= self
::PLEDGE_EXPORT
;
129 $this->_exportMode
= self
::CASE_EXPORT
;
132 $this->_exportMode
= self
::GRANT_EXPORT
;
135 $this->_exportMode
= self
::ACTIVITY_EXPORT
;
139 $className = "CRM_{$componentName[1]}_Form_Task";
140 $className::preProcessCommon( $this, true );
141 $values = $this->controller
->exportValues('Search');
144 $values = $this->controller
->exportValues('Basic');
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')) {
156 $this->_matchingContacts
= TRUE;
162 $componentMode = $this->get('component_mode');
163 switch ($componentMode) {
165 CRM_Contribute_Form_Task
::preProcessCommon($this, TRUE);
166 $this->_exportMode
= self
::CONTRIBUTE_EXPORT
;
167 $componentName = array('', 'Contribute');
171 CRM_Event_Form_Task
::preProcessCommon($this, TRUE);
172 $this->_exportMode
= self
::EVENT_EXPORT
;
173 $componentName = array('', 'Event');
177 CRM_Activity_Form_Task
::preProcessCommon($this, TRUE);
178 $this->_exportMode
= self
::ACTIVITY_EXPORT
;
179 $componentName = array('', 'Activity');
182 CRM_Member_Form_Task
::preProcessCommon($this, TRUE);
183 $this->_exportMode
= self
::MEMBER_EXPORT
;
184 $componentName = array('', 'Member');
187 CRM_Case_Form_Task
::preProcessCommon($this, TRUE);
188 $this->_exportMode
= self
::CASE_EXPORT
;
189 $componentName = array('', 'Case');
193 $this->_task
= $values['task'];
194 if ($this->_exportMode
== self
::CONTACT_EXPORT
) {
195 $contactTasks = CRM_Contact_Task
::taskTitles();
196 $taskName = $contactTasks[$this->_task
];
198 CRM_Contact_Form_Task
::preProcessCommon($this, TRUE);
201 $this->assign('taskName', "Export $componentName[1]");
202 $className = "CRM_{$componentName[1]}_Task";
203 $componentTasks = $className::tasks();
204 $taskName = $componentTasks[$this->_task
];
208 if ($this->_componentTable
) {
211 FROM {$this->_componentTable}
213 $totalSelectedRecords = CRM_Core_DAO
::singleValueQuery($query);
216 $totalSelectedRecords = count($this->_componentIds
);
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');
226 $this->_matchingContacts
= TRUE;
228 $this->assign('totalSelectedRecords', $rowCount);
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
);
240 * Function to actually build the form
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
::group()
309 $this->buildMapping();
311 $this->setDefaults(array(
312 'exportOption' => self
::EXPORT_ALL
,
313 'mergeOption' => self
::EXPORT_MERGE_DO_NOT_MERGE
,
316 $this->addButtons(array(
319 'name' => ts('Continue >>'),
320 'spacing' => ' ',
325 'name' => ts('Cancel'),
330 $this->addFormRule(array('CRM_Export_Form_Select', 'formRule'), $this);
334 * Function for validation
336 * @param array $params (ref.) an assoc array of name/value pairs
338 * @return mixed true or array of errors
342 static public function formRule($params, $files, $self) {
345 if (CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_SAME_ADDRESS
&&
346 $self->_matchingContacts
349 'postal_greeting' => 'postal_greeting_other',
350 'addressee' => 'addressee_other',
353 foreach ($greetings as $key => $value) {
354 $otherOption = CRM_Utils_Array
::value($key, $params);
356 if ((CRM_Utils_Array
::value($otherOption, $self->_greetingOptions
[$key]) == ts('Other')) && empty($params[$value])) {
358 $label = ucwords(str_replace('_', ' ', $key));
359 $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 return empty($errors) ?
TRUE : $errors;
368 * Process the uploaded file
373 public function postProcess() {
374 $params = $this->controller
->exportValues($this->_name
);
375 $exportOption = $params['exportOption'];
376 $mergeSameAddress = CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_SAME_ADDRESS ?
1 : 0;
377 $mergeSameHousehold = CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_HOUSEHOLD ?
1 : 0;
379 $this->set('mergeSameAddress', $mergeSameAddress);
380 $this->set('mergeSameHousehold', $mergeSameHousehold);
382 // instead of increasing the number of arguments to exportComponents function, we
383 // will send $exportParams as another argument, which is an array and suppose to contain
384 // all submitted options or any other argument
385 $exportParams = $params;
387 if (!empty($this->_greetingOptions
)) {
388 foreach ($this->_greetingOptions
as $key => $value) {
389 if ($option = CRM_Utils_Array
::value($key, $exportParams)) {
390 if ($this->_greetingOptions
[$key][$option] == ts('Other')) {
391 $exportParams[$key] = $exportParams["{$key}_other"];
393 elseif ($this->_greetingOptions
[$key][$option] == ts('List of names')) {
394 $exportParams[$key] = '';
397 $exportParams[$key] = $this->_greetingOptions
[$key][$option];
403 $mappingId = CRM_Utils_Array
::value('mapping', $params);
405 $this->set('mappingId', $mappingId);
408 $this->set('mappingId', NULL);
412 if ($exportOption == self
::EXPORT_ALL
) {
413 CRM_Export_BAO_Export
::exportComponents($this->_selectAll
,
414 $this->_componentIds
,
415 $this->get('queryParams'),
416 $this->get(CRM_Utils_Sort
::SORT_ORDER
),
418 $this->get('returnProperties'),
420 $this->_componentClause
,
421 $this->_componentTable
,
425 $this->get('queryOperator')
430 $this->controller
->resetPage('Map');
434 * Return a descriptive name for the page, used in wizard header
439 public function getTitle() {
440 return ts('Export All or Selected Fields');
444 * Function to build mapping form element
447 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 $mappingTypeId = CRM_Core_OptionGroup
::getValue('mapping_type', $exportType, 'name');
483 $this->set('mappingTypeId', $mappingTypeId);
485 $mappings = CRM_Core_BAO_Mapping
::getMappings($mappingTypeId);
486 if (!empty($mappings)) {
487 $this->add('select', 'mapping', ts('Use Saved Field Mapping'), array('' => '-select-') +
$mappings);
491 static function getGreetingOptions() {
494 'postal_greeting' => 'postal_greeting_other',
495 'addressee' => 'addressee_other',
498 foreach ($greetings as $key => $value) {
500 $optionGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
502 CRM_Core_DAO
::commonRetrieveAll('CRM_Core_DAO_OptionValue', 'option_group_id', $optionGroupId,
503 $params, array('label', 'filter')
507 $options[$key] = array("$greetingCount" => ts('List of names'));
509 foreach ($params as $id => $field) {
510 if (CRM_Utils_Array
::value('filter', $field) == 4) {
511 $options[$key][++
$greetingCount] = $field['label'];
515 $options[$key][++
$greetingCount] = ts('Other');