3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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
44 CONST EXPORT_ALL
= 1, EXPORT_SELECTED
= 2, EXPORT_MERGE_DO_NOT_MERGE
= 0, EXPORT_MERGE_SAME_ADDRESS
= 1, EXPORT_MERGE_HOUSEHOLD
= 2;
49 CONST CONTACT_EXPORT
= 1, CONTRIBUTE_EXPORT
= 2, MEMBER_EXPORT
= 3, EVENT_EXPORT
= 4, PLEDGE_EXPORT
= 5, CASE_EXPORT
= 6, GRANT_EXPORT
= 7, ACTIVITY_EXPORT
= 8;
58 public $_componentTable;
61 * build all the data structures needed to build the form
68 function preProcess() {
69 //special case for custom search, directly give option to download csv file
70 $customSearchID = $this->get('customSearchID');
71 if ($customSearchID) {
72 CRM_Export_BAO_Export
::exportCustom($this->get('customSearchClass'),
73 $this->get('formValues'),
74 $this->get(CRM_Utils_Sort
::SORT_ORDER
)
78 $this->_selectAll
= FALSE;
79 $this->_exportMode
= self
::CONTACT_EXPORT
;
80 $this->_componentIds
= array();
81 $this->_componentClause
= NULL;
83 // get the submitted values based on search
84 if ($this->_action
== CRM_Core_Action
::ADVANCED
) {
85 $values = $this->controller
->exportValues('Advanced');
87 elseif ($this->_action
== CRM_Core_Action
::PROFILE
) {
88 $values = $this->controller
->exportValues('Builder');
90 elseif ($this->_action
== CRM_Core_Action
::COPY
) {
91 $values = $this->controller
->exportValues('Custom');
94 // we need to determine component export
95 $stateMachine = &$this->controller
->getStateMachine();
96 $formName = CRM_Utils_System
::getClassName($stateMachine);
97 $componentName = explode('_', $formName);
98 $components = array('Contribute', 'Member', 'Event', 'Pledge', 'Case', 'Grant', 'Activity');
100 if (in_array($componentName[1], $components)) {
101 eval('$this->_exportMode = self::' . strtoupper($componentName[1]) . '_EXPORT;');
102 eval('CRM_' . $componentName[1] . '_Form_Task::preProcessCommon( $this, true );');
103 $values = $this->controller
->exportValues('Search');
106 $values = $this->controller
->exportValues('Basic');
111 $this->_matchingContacts
= FALSE;
112 if (CRM_Utils_Array
::value('radio_ts', $values) == 'ts_sel') {
113 foreach ($values as $key => $value) {
114 if (strstr($key, 'mark_x')) {
118 $this->_matchingContacts
= TRUE;
124 $componentMode = $this->get('component_mode');
125 switch ($componentMode) {
127 CRM_Contribute_Form_Task
::preProcessCommon($this, TRUE);
128 $this->_exportMode
= self
::CONTRIBUTE_EXPORT
;
129 $componentName = array('', 'Contribute');
133 CRM_Event_Form_Task
::preProcessCommon($this, TRUE);
134 $this->_exportMode
= self
::EVENT_EXPORT
;
135 $componentName = array('', 'Event');
139 CRM_Activity_Form_Task
::preProcessCommon($this, TRUE);
140 $this->_exportMode
= self
::ACTIVITY_EXPORT
;
141 $componentName = array('', 'Activity');
144 CRM_Member_Form_Task
::preProcessCommon($this, TRUE);
145 $this->_exportMode
= self
::MEMBER_EXPORT
;
146 $componentName = array('', 'Member');
149 CRM_Case_Form_Task
::preProcessCommon($this, TRUE);
150 $this->_exportMode
= self
::CASE_EXPORT
;
151 $componentName = array('', 'Case');
155 $this->_task
= $values['task'];
156 if ($this->_exportMode
== self
::CONTACT_EXPORT
) {
157 $contactTasks = CRM_Contact_Task
::taskTitles();
158 $taskName = $contactTasks[$this->_task
];
160 CRM_Contact_Form_Task
::preProcessCommon($this, TRUE);
163 $this->assign('taskName', "Export $componentName[1]");
164 eval('$componentTasks = CRM_' . $componentName[1] . '_Task::tasks();');
165 $taskName = $componentTasks[$this->_task
];
169 if ($this->_componentTable
) {
172 FROM {$this->_componentTable}
174 $totalSelectedRecords = CRM_Core_DAO
::singleValueQuery($query);
177 $totalSelectedRecords = count($this->_componentIds
);
179 $this->assign('totalSelectedRecords', $totalSelectedRecords);
180 $this->assign('taskName', $taskName);
181 $this->assign('component', $component);
182 // all records actions = save a search
183 if (($values['radio_ts'] == 'ts_all') ||
($this->_task
== CRM_Contact_Task
::SAVE_SEARCH
)) {
184 $this->_selectAll
= TRUE;
185 $rowCount = $this->get('rowCount');
187 $this->_matchingContacts
= TRUE;
189 $this->assign('totalSelectedRecords', $rowCount);
192 $this->assign('matchingContacts', $this->_matchingContacts
);
193 $this->set('componentIds', $this->_componentIds
);
194 $this->set('selectAll', $this->_selectAll
);
195 $this->set('exportMode', $this->_exportMode
);
196 $this->set('componentClause', $this->_componentClause
);
197 $this->set('componentTable', $this->_componentTable
);
201 * Function to actually build the form
206 public function buildQuickForm() {
208 $exportOptions = $mergeOptions = $postalMailing = array();
209 $exportOptions[] = $this->createElement('radio',
211 ts('Export PRIMARY fields'),
213 array('onClick' => 'showMappingOption( );')
215 $exportOptions[] = $this->createElement('radio',
217 ts('Select fields for export'),
218 self
::EXPORT_SELECTED
,
219 array('onClick' => 'showMappingOption( );')
222 $mergeOptions[] = $this->createElement('radio',
225 self
::EXPORT_MERGE_DO_NOT_MERGE
,
226 array('onclick' => 'showGreetingOptions( );')
228 $mergeOptions[] = $this->createElement('radio',
230 ts('Merge All Contacts with the Same Address'),
231 self
::EXPORT_MERGE_SAME_ADDRESS
,
232 array('onclick' => 'showGreetingOptions( );')
234 $mergeOptions[] = $this->createElement('radio',
236 ts('Merge Household Members into their Households'),
237 self
::EXPORT_MERGE_HOUSEHOLD
,
238 array('onclick' => 'showGreetingOptions( );')
241 $postalMailing[] = $this->createElement('advcheckbox',
242 'postal_mailing_export',
247 $this->addGroup($exportOptions, 'exportOption', ts('Export Type'), '<br/>');
249 if ($this->_matchingContacts
) {
250 $this->_greetingOptions
= self
::getGreetingOptions();
252 foreach ($this->_greetingOptions
as $key => $value) {
253 $fieldLabel = ts('%1 (merging > 2 contacts)', array(1 => ucwords(str_replace('_', ' ', $key))));
254 $this->addElement('select', $key, $fieldLabel,
255 $value, array('onchange' => "showOther(this);")
257 $this->addElement('text', "{$key}_other", '');
261 if ($this->_exportMode
== self
::CONTACT_EXPORT
) {
262 $this->addGroup($mergeOptions, 'mergeOption', ts('Merge Options'), '<br/>');
263 $this->addGroup($postalMailing, 'postal_mailing_export', ts('Postal Mailing Export'), '<br/>');
265 $this->addElement('select', 'additional_group', ts('Additional Group for Export'),
266 array('' => ts('- select group -')) + CRM_Core_PseudoConstant
::group()
270 $this->buildMapping();
272 $this->setDefaults(array(
273 'exportOption' => self
::EXPORT_ALL
,
274 'mergeOption' => self
::EXPORT_MERGE_DO_NOT_MERGE
,
277 $this->addButtons(array(
280 'name' => ts('Continue >>'),
281 'spacing' => ' ',
286 'name' => ts('Cancel'),
291 $this->addFormRule(array('CRM_Export_Form_Select', 'formRule'), $this);
295 * Function for validation
297 * @param array $params (ref.) an assoc array of name/value pairs
299 * @return mixed true or array of errors
303 public function formRule($params, $files, $self) {
306 if (CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_SAME_ADDRESS
&&
307 $self->_matchingContacts
310 'postal_greeting' => 'postal_greeting_other',
311 'addressee' => 'addressee_other',
314 foreach ($greetings as $key => $value) {
315 $otherOption = CRM_Utils_Array
::value($key, $params);
317 if ((CRM_Utils_Array
::value($otherOption, $self->_greetingOptions
[$key]) == ts('Other')) &&
318 !CRM_Utils_Array
::value($value, $params)
321 $label = ucwords(str_replace('_', ' ', $key));
322 $errors[$value] = ts('Please enter a value for %1 (merging > 2 contacts), or select a pre-configured option from the list.', array(1 => $label));
327 return empty($errors) ?
TRUE : $errors;
331 * Process the uploaded file
336 public function postProcess() {
337 $params = $this->controller
->exportValues($this->_name
);
338 $exportOption = $params['exportOption'];
339 $mergeSameAddress = CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_SAME_ADDRESS ?
1 : 0;
340 $mergeSameHousehold = CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_HOUSEHOLD ?
1 : 0;
342 $this->set('mergeSameAddress', $mergeSameAddress);
343 $this->set('mergeSameHousehold', $mergeSameHousehold);
345 // instead of increasing the number of arguments to exportComponents function, we
346 // will send $exportParams as another argument, which is an array and suppose to contain
347 // all submitted options or any other argument
348 $exportParams = $params;
350 if (!empty($this->_greetingOptions
)) {
351 foreach ($this->_greetingOptions
as $key => $value) {
352 if ($option = CRM_Utils_Array
::value($key, $exportParams)) {
353 if ($this->_greetingOptions
[$key][$option] == ts('Other')) {
354 $exportParams[$key] = $exportParams["{$key}_other"];
356 elseif ($this->_greetingOptions
[$key][$option] == ts('List of names')) {
357 $exportParams[$key] = '';
360 $exportParams[$key] = $this->_greetingOptions
[$key][$option];
366 $mappingId = CRM_Utils_Array
::value('mapping', $params);
368 $this->set('mappingId', $mappingId);
371 $this->set('mappingId', NULL);
375 if ($exportOption == self
::EXPORT_ALL
) {
376 CRM_Export_BAO_Export
::exportComponents($this->_selectAll
,
377 $this->_componentIds
,
378 $this->get('queryParams'),
379 $this->get(CRM_Utils_Sort
::SORT_ORDER
),
381 $this->get('returnProperties'),
383 $this->_componentClause
,
384 $this->_componentTable
,
388 $this->get('queryOperator')
393 $this->controller
->resetPage('Map');
397 * Return a descriptive name for the page, used in wizard header
402 public function getTitle() {
403 return ts('Export All or Selected Fields');
407 * Function to build mapping form element
410 function buildMapping() {
411 switch ($this->_exportMode
) {
412 case CRM_Export_Form_Select
::CONTACT_EXPORT
:
413 $exportType = 'Export Contact';
416 case CRM_Export_Form_Select
::CONTRIBUTE_EXPORT
:
417 $exportType = 'Export Contribution';
420 case CRM_Export_Form_Select
::MEMBER_EXPORT
:
421 $exportType = 'Export Membership';
424 case CRM_Export_Form_Select
::EVENT_EXPORT
:
425 $exportType = 'Export Participant';
428 case CRM_Export_Form_Select
::PLEDGE_EXPORT
:
429 $exportType = 'Export Pledge';
432 case CRM_Export_Form_Select
::CASE_EXPORT
:
433 $exportType = 'Export Case';
436 case CRM_Export_Form_Select
::GRANT_EXPORT
:
437 $exportType = 'Export Grant';
440 case CRM_Export_Form_Select
::ACTIVITY_EXPORT
:
441 $exportType = 'Export Activity';
445 $mappingTypeId = CRM_Core_OptionGroup
::getValue('mapping_type', $exportType, 'name');
446 $this->set('mappingTypeId', $mappingTypeId);
448 $mappings = CRM_Core_BAO_Mapping
::getMappings($mappingTypeId);
449 if (!empty($mappings)) {
450 $this->add('select', 'mapping', ts('Use Saved Field Mapping'), array('' => '-select-') +
$mappings);
454 static function getGreetingOptions() {
457 'postal_greeting' => 'postal_greeting_other',
458 'addressee' => 'addressee_other',
461 foreach ($greetings as $key => $value) {
463 $optionGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
465 CRM_Core_DAO
::commonRetrieveAll('CRM_Core_DAO_OptionValue', 'option_group_id', $optionGroupId,
466 $params, array('label', 'filter')
470 $options[$key] = array("$greetingCount" => ts('List of names'));
472 foreach ($params as $id => $field) {
473 if (CRM_Utils_Array
::value('filter', $field) == 4) {
474 $options[$key][++
$greetingCount] = $field['label'];
478 $options[$key][++
$greetingCount] = ts('Other');