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();
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 $fieldName = strtoupper($componentName[1]) . '_EXPORT';
116 $this->_exportMode
= self
::$fieldName;
117 $className = "CRM_{$componentName[1]}_Form_Task";
118 $className::preProcessCommon( $this, true );
119 $values = $this->controller
->exportValues('Search');
122 $values = $this->controller
->exportValues('Basic');
127 $this->_matchingContacts
= FALSE;
128 if (CRM_Utils_Array
::value('radio_ts', $values) == 'ts_sel') {
129 foreach ($values as $key => $value) {
130 if (strstr($key, 'mark_x')) {
134 $this->_matchingContacts
= TRUE;
140 $componentMode = $this->get('component_mode');
141 switch ($componentMode) {
143 CRM_Contribute_Form_Task
::preProcessCommon($this, TRUE);
144 $this->_exportMode
= self
::CONTRIBUTE_EXPORT
;
145 $componentName = array('', 'Contribute');
149 CRM_Event_Form_Task
::preProcessCommon($this, TRUE);
150 $this->_exportMode
= self
::EVENT_EXPORT
;
151 $componentName = array('', 'Event');
155 CRM_Activity_Form_Task
::preProcessCommon($this, TRUE);
156 $this->_exportMode
= self
::ACTIVITY_EXPORT
;
157 $componentName = array('', 'Activity');
160 CRM_Member_Form_Task
::preProcessCommon($this, TRUE);
161 $this->_exportMode
= self
::MEMBER_EXPORT
;
162 $componentName = array('', 'Member');
165 CRM_Case_Form_Task
::preProcessCommon($this, TRUE);
166 $this->_exportMode
= self
::CASE_EXPORT
;
167 $componentName = array('', 'Case');
171 $this->_task
= $values['task'];
172 if ($this->_exportMode
== self
::CONTACT_EXPORT
) {
173 $contactTasks = CRM_Contact_Task
::taskTitles();
174 $taskName = $contactTasks[$this->_task
];
176 CRM_Contact_Form_Task
::preProcessCommon($this, TRUE);
179 $this->assign('taskName', "Export $componentName[1]");
180 $className = "CRM_{$componentName[1]}_Task";
181 $componentTasks = $className::tasks();
182 $taskName = $componentTasks[$this->_task
];
186 if ($this->_componentTable
) {
189 FROM {$this->_componentTable}
191 $totalSelectedRecords = CRM_Core_DAO
::singleValueQuery($query);
194 $totalSelectedRecords = count($this->_componentIds
);
196 $this->assign('totalSelectedRecords', $totalSelectedRecords);
197 $this->assign('taskName', $taskName);
198 $this->assign('component', $component);
199 // all records actions = save a search
200 if (($values['radio_ts'] == 'ts_all') ||
($this->_task
== CRM_Contact_Task
::SAVE_SEARCH
)) {
201 $this->_selectAll
= TRUE;
202 $rowCount = $this->get('rowCount');
204 $this->_matchingContacts
= TRUE;
206 $this->assign('totalSelectedRecords', $rowCount);
209 $this->assign('matchingContacts', $this->_matchingContacts
);
210 $this->set('componentIds', $this->_componentIds
);
211 $this->set('selectAll', $this->_selectAll
);
212 $this->set('exportMode', $this->_exportMode
);
213 $this->set('componentClause', $this->_componentClause
);
214 $this->set('componentTable', $this->_componentTable
);
218 * Function to actually build the form
223 public function buildQuickForm() {
225 $exportOptions = $mergeOptions = $postalMailing = array();
226 $exportOptions[] = $this->createElement('radio',
228 ts('Export PRIMARY fields'),
230 array('onClick' => 'showMappingOption( );')
232 $exportOptions[] = $this->createElement('radio',
234 ts('Select fields for export'),
235 self
::EXPORT_SELECTED
,
236 array('onClick' => 'showMappingOption( );')
239 $mergeOptions[] = $this->createElement('radio',
242 self
::EXPORT_MERGE_DO_NOT_MERGE
,
243 array('onclick' => 'showGreetingOptions( );')
245 $mergeOptions[] = $this->createElement('radio',
247 ts('Merge All Contacts with the Same Address'),
248 self
::EXPORT_MERGE_SAME_ADDRESS
,
249 array('onclick' => 'showGreetingOptions( );')
251 $mergeOptions[] = $this->createElement('radio',
253 ts('Merge Household Members into their Households'),
254 self
::EXPORT_MERGE_HOUSEHOLD
,
255 array('onclick' => 'showGreetingOptions( );')
258 $postalMailing[] = $this->createElement('advcheckbox',
259 'postal_mailing_export',
264 $this->addGroup($exportOptions, 'exportOption', ts('Export Type'), '<br/>');
266 if ($this->_matchingContacts
) {
267 $this->_greetingOptions
= self
::getGreetingOptions();
269 foreach ($this->_greetingOptions
as $key => $value) {
270 $fieldLabel = ts('%1 (merging > 2 contacts)', array(1 => ucwords(str_replace('_', ' ', $key))));
271 $this->addElement('select', $key, $fieldLabel,
272 $value, array('onchange' => "showOther(this);")
274 $this->addElement('text', "{$key}_other", '');
278 if ($this->_exportMode
== self
::CONTACT_EXPORT
) {
279 $this->addGroup($mergeOptions, 'mergeOption', ts('Merge Options'), '<br/>');
280 $this->addGroup($postalMailing, 'postal_mailing_export', ts('Postal Mailing Export'), '<br/>');
282 $this->addElement('select', 'additional_group', ts('Additional Group for Export'),
283 array('' => ts('- select group -')) + CRM_Core_PseudoConstant
::group()
287 $this->buildMapping();
289 $this->setDefaults(array(
290 'exportOption' => self
::EXPORT_ALL
,
291 'mergeOption' => self
::EXPORT_MERGE_DO_NOT_MERGE
,
294 $this->addButtons(array(
297 'name' => ts('Continue >>'),
298 'spacing' => ' ',
303 'name' => ts('Cancel'),
308 $this->addFormRule(array('CRM_Export_Form_Select', 'formRule'), $this);
312 * Function for validation
314 * @param array $params (ref.) an assoc array of name/value pairs
316 * @return mixed true or array of errors
320 static public function formRule($params, $files, $self) {
323 if (CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_SAME_ADDRESS
&&
324 $self->_matchingContacts
327 'postal_greeting' => 'postal_greeting_other',
328 'addressee' => 'addressee_other',
331 foreach ($greetings as $key => $value) {
332 $otherOption = CRM_Utils_Array
::value($key, $params);
334 if ((CRM_Utils_Array
::value($otherOption, $self->_greetingOptions
[$key]) == ts('Other')) &&
335 !CRM_Utils_Array
::value($value, $params)
338 $label = ucwords(str_replace('_', ' ', $key));
339 $errors[$value] = ts('Please enter a value for %1 (merging > 2 contacts), or select a pre-configured option from the list.', array(1 => $label));
344 return empty($errors) ?
TRUE : $errors;
348 * Process the uploaded file
353 public function postProcess() {
354 $params = $this->controller
->exportValues($this->_name
);
355 $exportOption = $params['exportOption'];
356 $mergeSameAddress = CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_SAME_ADDRESS ?
1 : 0;
357 $mergeSameHousehold = CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_HOUSEHOLD ?
1 : 0;
359 $this->set('mergeSameAddress', $mergeSameAddress);
360 $this->set('mergeSameHousehold', $mergeSameHousehold);
362 // instead of increasing the number of arguments to exportComponents function, we
363 // will send $exportParams as another argument, which is an array and suppose to contain
364 // all submitted options or any other argument
365 $exportParams = $params;
367 if (!empty($this->_greetingOptions
)) {
368 foreach ($this->_greetingOptions
as $key => $value) {
369 if ($option = CRM_Utils_Array
::value($key, $exportParams)) {
370 if ($this->_greetingOptions
[$key][$option] == ts('Other')) {
371 $exportParams[$key] = $exportParams["{$key}_other"];
373 elseif ($this->_greetingOptions
[$key][$option] == ts('List of names')) {
374 $exportParams[$key] = '';
377 $exportParams[$key] = $this->_greetingOptions
[$key][$option];
383 $mappingId = CRM_Utils_Array
::value('mapping', $params);
385 $this->set('mappingId', $mappingId);
388 $this->set('mappingId', NULL);
392 if ($exportOption == self
::EXPORT_ALL
) {
393 CRM_Export_BAO_Export
::exportComponents($this->_selectAll
,
394 $this->_componentIds
,
395 $this->get('queryParams'),
396 $this->get(CRM_Utils_Sort
::SORT_ORDER
),
398 $this->get('returnProperties'),
400 $this->_componentClause
,
401 $this->_componentTable
,
405 $this->get('queryOperator')
410 $this->controller
->resetPage('Map');
414 * Return a descriptive name for the page, used in wizard header
419 public function getTitle() {
420 return ts('Export All or Selected Fields');
424 * Function to build mapping form element
427 function buildMapping() {
428 switch ($this->_exportMode
) {
429 case CRM_Export_Form_Select
::CONTACT_EXPORT
:
430 $exportType = 'Export Contact';
433 case CRM_Export_Form_Select
::CONTRIBUTE_EXPORT
:
434 $exportType = 'Export Contribution';
437 case CRM_Export_Form_Select
::MEMBER_EXPORT
:
438 $exportType = 'Export Membership';
441 case CRM_Export_Form_Select
::EVENT_EXPORT
:
442 $exportType = 'Export Participant';
445 case CRM_Export_Form_Select
::PLEDGE_EXPORT
:
446 $exportType = 'Export Pledge';
449 case CRM_Export_Form_Select
::CASE_EXPORT
:
450 $exportType = 'Export Case';
453 case CRM_Export_Form_Select
::GRANT_EXPORT
:
454 $exportType = 'Export Grant';
457 case CRM_Export_Form_Select
::ACTIVITY_EXPORT
:
458 $exportType = 'Export Activity';
462 $mappingTypeId = CRM_Core_OptionGroup
::getValue('mapping_type', $exportType, 'name');
463 $this->set('mappingTypeId', $mappingTypeId);
465 $mappings = CRM_Core_BAO_Mapping
::getMappings($mappingTypeId);
466 if (!empty($mappings)) {
467 $this->add('select', 'mapping', ts('Use Saved Field Mapping'), array('' => '-select-') +
$mappings);
471 static function getGreetingOptions() {
474 'postal_greeting' => 'postal_greeting_other',
475 'addressee' => 'addressee_other',
478 foreach ($greetings as $key => $value) {
480 $optionGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
482 CRM_Core_DAO
::commonRetrieveAll('CRM_Core_DAO_OptionValue', 'option_group_id', $optionGroupId,
483 $params, array('label', 'filter')
487 $options[$key] = array("$greetingCount" => ts('List of names'));
489 foreach ($params as $id => $field) {
490 if (CRM_Utils_Array
::value('filter', $field) == 4) {
491 $options[$key][++
$greetingCount] = $field['label'];
495 $options[$key][++
$greetingCount] = ts('Other');