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 * 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 (ref.) an assoc array of name/value pairs
342 * @return mixed true or array of errors
346 static public 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
377 public function postProcess() {
378 $params = $this->controller
->exportValues($this->_name
);
379 $exportOption = $params['exportOption'];
380 $mergeSameAddress = CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_SAME_ADDRESS ?
1 : 0;
381 $mergeSameHousehold = CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_HOUSEHOLD ?
1 : 0;
383 $this->set('mergeSameAddress', $mergeSameAddress);
384 $this->set('mergeSameHousehold', $mergeSameHousehold);
386 // instead of increasing the number of arguments to exportComponents function, we
387 // will send $exportParams as another argument, which is an array and suppose to contain
388 // all submitted options or any other argument
389 $exportParams = $params;
391 if (!empty($this->_greetingOptions
)) {
392 foreach ($this->_greetingOptions
as $key => $value) {
393 if ($option = CRM_Utils_Array
::value($key, $exportParams)) {
394 if ($this->_greetingOptions
[$key][$option] == ts('Other')) {
395 $exportParams[$key] = $exportParams["{$key}_other"];
397 elseif ($this->_greetingOptions
[$key][$option] == ts('List of names')) {
398 $exportParams[$key] = '';
401 $exportParams[$key] = $this->_greetingOptions
[$key][$option];
407 $mappingId = CRM_Utils_Array
::value('mapping', $params);
409 $this->set('mappingId', $mappingId);
412 $this->set('mappingId', NULL);
416 if ($exportOption == self
::EXPORT_ALL
) {
417 CRM_Export_BAO_Export
::exportComponents($this->_selectAll
,
418 $this->_componentIds
,
419 $this->get('queryParams'),
420 $this->get(CRM_Utils_Sort
::SORT_ORDER
),
422 $this->get('returnProperties'),
424 $this->_componentClause
,
425 $this->_componentTable
,
429 $this->get('queryOperator')
434 $this->controller
->resetPage('Map');
438 * Return a descriptive name for the page, used in wizard header
443 public function getTitle() {
444 return ts('Export All or Selected Fields');
448 * Build mapping form element
451 function buildMapping() {
452 switch ($this->_exportMode
) {
453 case CRM_Export_Form_Select
::CONTACT_EXPORT
:
454 $exportType = 'Export Contact';
457 case CRM_Export_Form_Select
::CONTRIBUTE_EXPORT
:
458 $exportType = 'Export Contribution';
461 case CRM_Export_Form_Select
::MEMBER_EXPORT
:
462 $exportType = 'Export Membership';
465 case CRM_Export_Form_Select
::EVENT_EXPORT
:
466 $exportType = 'Export Participant';
469 case CRM_Export_Form_Select
::PLEDGE_EXPORT
:
470 $exportType = 'Export Pledge';
473 case CRM_Export_Form_Select
::CASE_EXPORT
:
474 $exportType = 'Export Case';
477 case CRM_Export_Form_Select
::GRANT_EXPORT
:
478 $exportType = 'Export Grant';
481 case CRM_Export_Form_Select
::ACTIVITY_EXPORT
:
482 $exportType = 'Export Activity';
486 $mappingTypeId = CRM_Core_OptionGroup
::getValue('mapping_type', $exportType, 'name');
487 $this->set('mappingTypeId', $mappingTypeId);
489 $mappings = CRM_Core_BAO_Mapping
::getMappings($mappingTypeId);
490 if (!empty($mappings)) {
491 $this->add('select', 'mapping', ts('Use Saved Field Mapping'), array('' => '-select-') +
$mappings);
498 static function getGreetingOptions() {
501 'postal_greeting' => 'postal_greeting_other',
502 'addressee' => 'addressee_other',
505 foreach ($greetings as $key => $value) {
507 $optionGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
509 CRM_Core_DAO
::commonRetrieveAll('CRM_Core_DAO_OptionValue', 'option_group_id', $optionGroupId,
510 $params, array('label', 'filter')
514 $options[$key] = array("$greetingCount" => ts('List of names'));
516 foreach ($params as $id => $field) {
517 if (CRM_Utils_Array
::value('filter', $field) == 4) {
518 $options[$key][++
$greetingCount] = $field['label'];
522 $options[$key][++
$greetingCount] = ts('Other');