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
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();
109 $formName = CRM_Utils_System
::getClassName($stateMachine);
110 $componentName = explode('_', $formName);
111 $components = array('Contribute', 'Member', 'Event', 'Pledge', 'Case', 'Grant', 'Activity');
113 if (in_array($componentName[1], $components)) {
114 $fieldName = strtoupper($componentName[1]) . '_EXPORT';
115 $this->_exportMode
= self
::$fieldName;
116 $className = "CRM_{$componentName[1]}_Form_Task";
117 $className::preProcessCommon( $this, true );
118 $values = $this->controller
->exportValues('Search');
121 $values = $this->controller
->exportValues('Basic');
126 $this->_matchingContacts
= FALSE;
127 if (CRM_Utils_Array
::value('radio_ts', $values) == 'ts_sel') {
128 foreach ($values as $key => $value) {
129 if (strstr($key, 'mark_x')) {
133 $this->_matchingContacts
= TRUE;
139 $componentMode = $this->get('component_mode');
140 switch ($componentMode) {
142 CRM_Contribute_Form_Task
::preProcessCommon($this, TRUE);
143 $this->_exportMode
= self
::CONTRIBUTE_EXPORT
;
144 $componentName = array('', 'Contribute');
148 CRM_Event_Form_Task
::preProcessCommon($this, TRUE);
149 $this->_exportMode
= self
::EVENT_EXPORT
;
150 $componentName = array('', 'Event');
154 CRM_Activity_Form_Task
::preProcessCommon($this, TRUE);
155 $this->_exportMode
= self
::ACTIVITY_EXPORT
;
156 $componentName = array('', 'Activity');
159 CRM_Member_Form_Task
::preProcessCommon($this, TRUE);
160 $this->_exportMode
= self
::MEMBER_EXPORT
;
161 $componentName = array('', 'Member');
164 CRM_Case_Form_Task
::preProcessCommon($this, TRUE);
165 $this->_exportMode
= self
::CASE_EXPORT
;
166 $componentName = array('', 'Case');
170 $this->_task
= $values['task'];
171 if ($this->_exportMode
== self
::CONTACT_EXPORT
) {
172 $contactTasks = CRM_Contact_Task
::taskTitles();
173 $taskName = $contactTasks[$this->_task
];
175 CRM_Contact_Form_Task
::preProcessCommon($this, TRUE);
178 $this->assign('taskName', "Export $componentName[1]");
179 $className = "CRM_{$componentName[1]}_Task";
180 $componentTasks = $className::tasks();
181 $taskName = $componentTasks[$this->_task
];
185 if ($this->_componentTable
) {
188 FROM {$this->_componentTable}
190 $totalSelectedRecords = CRM_Core_DAO
::singleValueQuery($query);
193 $totalSelectedRecords = count($this->_componentIds
);
195 $this->assign('totalSelectedRecords', $totalSelectedRecords);
196 $this->assign('taskName', $taskName);
197 $this->assign('component', $component);
198 // all records actions = save a search
199 if (($values['radio_ts'] == 'ts_all') ||
($this->_task
== CRM_Contact_Task
::SAVE_SEARCH
)) {
200 $this->_selectAll
= TRUE;
201 $rowCount = $this->get('rowCount');
203 $this->_matchingContacts
= TRUE;
205 $this->assign('totalSelectedRecords', $rowCount);
208 $this->assign('matchingContacts', $this->_matchingContacts
);
209 $this->set('componentIds', $this->_componentIds
);
210 $this->set('selectAll', $this->_selectAll
);
211 $this->set('exportMode', $this->_exportMode
);
212 $this->set('componentClause', $this->_componentClause
);
213 $this->set('componentTable', $this->_componentTable
);
217 * Function to actually build the form
222 public function buildQuickForm() {
224 $exportOptions = $mergeOptions = $postalMailing = array();
225 $exportOptions[] = $this->createElement('radio',
227 ts('Export PRIMARY fields'),
229 array('onClick' => 'showMappingOption( );')
231 $exportOptions[] = $this->createElement('radio',
233 ts('Select fields for export'),
234 self
::EXPORT_SELECTED
,
235 array('onClick' => 'showMappingOption( );')
238 $mergeOptions[] = $this->createElement('radio',
241 self
::EXPORT_MERGE_DO_NOT_MERGE
,
242 array('onclick' => 'showGreetingOptions( );')
244 $mergeOptions[] = $this->createElement('radio',
246 ts('Merge All Contacts with the Same Address'),
247 self
::EXPORT_MERGE_SAME_ADDRESS
,
248 array('onclick' => 'showGreetingOptions( );')
250 $mergeOptions[] = $this->createElement('radio',
252 ts('Merge Household Members into their Households'),
253 self
::EXPORT_MERGE_HOUSEHOLD
,
254 array('onclick' => 'showGreetingOptions( );')
257 $postalMailing[] = $this->createElement('advcheckbox',
258 'postal_mailing_export',
263 $this->addGroup($exportOptions, 'exportOption', ts('Export Type'), '<br/>');
265 if ($this->_matchingContacts
) {
266 $this->_greetingOptions
= self
::getGreetingOptions();
268 foreach ($this->_greetingOptions
as $key => $value) {
269 $fieldLabel = ts('%1 (merging > 2 contacts)', array(1 => ucwords(str_replace('_', ' ', $key))));
270 $this->addElement('select', $key, $fieldLabel,
271 $value, array('onchange' => "showOther(this);")
273 $this->addElement('text', "{$key}_other", '');
277 if ($this->_exportMode
== self
::CONTACT_EXPORT
) {
278 $this->addGroup($mergeOptions, 'mergeOption', ts('Merge Options'), '<br/>');
279 $this->addGroup($postalMailing, 'postal_mailing_export', ts('Postal Mailing Export'), '<br/>');
281 $this->addElement('select', 'additional_group', ts('Additional Group for Export'),
282 array('' => ts('- select group -')) + CRM_Core_PseudoConstant
::group()
286 $this->buildMapping();
288 $this->setDefaults(array(
289 'exportOption' => self
::EXPORT_ALL
,
290 'mergeOption' => self
::EXPORT_MERGE_DO_NOT_MERGE
,
293 $this->addButtons(array(
296 'name' => ts('Continue >>'),
297 'spacing' => ' ',
302 'name' => ts('Cancel'),
307 $this->addFormRule(array('CRM_Export_Form_Select', 'formRule'), $this);
311 * Function for validation
313 * @param array $params (ref.) an assoc array of name/value pairs
315 * @return mixed true or array of errors
319 public function formRule($params, $files, $self) {
322 if (CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_SAME_ADDRESS
&&
323 $self->_matchingContacts
326 'postal_greeting' => 'postal_greeting_other',
327 'addressee' => 'addressee_other',
330 foreach ($greetings as $key => $value) {
331 $otherOption = CRM_Utils_Array
::value($key, $params);
333 if ((CRM_Utils_Array
::value($otherOption, $self->_greetingOptions
[$key]) == ts('Other')) &&
334 !CRM_Utils_Array
::value($value, $params)
337 $label = ucwords(str_replace('_', ' ', $key));
338 $errors[$value] = ts('Please enter a value for %1 (merging > 2 contacts), or select a pre-configured option from the list.', array(1 => $label));
343 return empty($errors) ?
TRUE : $errors;
347 * Process the uploaded file
352 public function postProcess() {
353 $params = $this->controller
->exportValues($this->_name
);
354 $exportOption = $params['exportOption'];
355 $mergeSameAddress = CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_SAME_ADDRESS ?
1 : 0;
356 $mergeSameHousehold = CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_HOUSEHOLD ?
1 : 0;
358 $this->set('mergeSameAddress', $mergeSameAddress);
359 $this->set('mergeSameHousehold', $mergeSameHousehold);
361 // instead of increasing the number of arguments to exportComponents function, we
362 // will send $exportParams as another argument, which is an array and suppose to contain
363 // all submitted options or any other argument
364 $exportParams = $params;
366 if (!empty($this->_greetingOptions
)) {
367 foreach ($this->_greetingOptions
as $key => $value) {
368 if ($option = CRM_Utils_Array
::value($key, $exportParams)) {
369 if ($this->_greetingOptions
[$key][$option] == ts('Other')) {
370 $exportParams[$key] = $exportParams["{$key}_other"];
372 elseif ($this->_greetingOptions
[$key][$option] == ts('List of names')) {
373 $exportParams[$key] = '';
376 $exportParams[$key] = $this->_greetingOptions
[$key][$option];
382 $mappingId = CRM_Utils_Array
::value('mapping', $params);
384 $this->set('mappingId', $mappingId);
387 $this->set('mappingId', NULL);
391 if ($exportOption == self
::EXPORT_ALL
) {
392 CRM_Export_BAO_Export
::exportComponents($this->_selectAll
,
393 $this->_componentIds
,
394 $this->get('queryParams'),
395 $this->get(CRM_Utils_Sort
::SORT_ORDER
),
397 $this->get('returnProperties'),
399 $this->_componentClause
,
400 $this->_componentTable
,
404 $this->get('queryOperator')
409 $this->controller
->resetPage('Map');
413 * Return a descriptive name for the page, used in wizard header
418 public function getTitle() {
419 return ts('Export All or Selected Fields');
423 * Function to build mapping form element
426 function buildMapping() {
427 switch ($this->_exportMode
) {
428 case CRM_Export_Form_Select
::CONTACT_EXPORT
:
429 $exportType = 'Export Contact';
432 case CRM_Export_Form_Select
::CONTRIBUTE_EXPORT
:
433 $exportType = 'Export Contribution';
436 case CRM_Export_Form_Select
::MEMBER_EXPORT
:
437 $exportType = 'Export Membership';
440 case CRM_Export_Form_Select
::EVENT_EXPORT
:
441 $exportType = 'Export Participant';
444 case CRM_Export_Form_Select
::PLEDGE_EXPORT
:
445 $exportType = 'Export Pledge';
448 case CRM_Export_Form_Select
::CASE_EXPORT
:
449 $exportType = 'Export Case';
452 case CRM_Export_Form_Select
::GRANT_EXPORT
:
453 $exportType = 'Export Grant';
456 case CRM_Export_Form_Select
::ACTIVITY_EXPORT
:
457 $exportType = 'Export Activity';
461 $mappingTypeId = CRM_Core_OptionGroup
::getValue('mapping_type', $exportType, 'name');
462 $this->set('mappingTypeId', $mappingTypeId);
464 $mappings = CRM_Core_BAO_Mapping
::getMappings($mappingTypeId);
465 if (!empty($mappings)) {
466 $this->add('select', 'mapping', ts('Use Saved Field Mapping'), array('' => '-select-') +
$mappings);
470 static function getGreetingOptions() {
473 'postal_greeting' => 'postal_greeting_other',
474 'addressee' => 'addressee_other',
477 foreach ($greetings as $key => $value) {
479 $optionGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
481 CRM_Core_DAO
::commonRetrieveAll('CRM_Core_DAO_OptionValue', 'option_group_id', $optionGroupId,
482 $params, array('label', 'filter')
486 $options[$key] = array("$greetingCount" => ts('List of names'));
488 foreach ($params as $id => $field) {
489 if (CRM_Utils_Array
::value('filter', $field) == 4) {
490 $options[$key][++
$greetingCount] = $field['label'];
494 $options[$key][++
$greetingCount] = ts('Other');