3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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,
65 * Current export mode.
71 public $_componentTable;
74 * Build all the data structures needed to build the form.
80 public function preProcess() {
81 //special case for custom search, directly give option to download csv file
82 $customSearchID = $this->get('customSearchID');
83 if ($customSearchID) {
84 CRM_Export_BAO_Export
::exportCustom($this->get('customSearchClass'),
85 $this->get('formValues'),
86 $this->get(CRM_Utils_Sort
::SORT_ORDER
)
90 $this->_selectAll
= FALSE;
91 $this->_exportMode
= self
::CONTACT_EXPORT
;
92 $this->_componentIds
= array();
93 $this->_componentClause
= NULL;
95 // get the submitted values based on search
96 if ($this->_action
== CRM_Core_Action
::ADVANCED
) {
97 $values = $this->controller
->exportValues('Advanced');
99 elseif ($this->_action
== CRM_Core_Action
::PROFILE
) {
100 $values = $this->controller
->exportValues('Builder');
102 elseif ($this->_action
== CRM_Core_Action
::COPY
) {
103 $values = $this->controller
->exportValues('Custom');
106 // we need to determine component export
107 $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 switch ($componentName[1]) {
116 $this->_exportMode
= self
::CONTRIBUTE_EXPORT
;
120 $this->_exportMode
= self
::MEMBER_EXPORT
;
124 $this->_exportMode
= self
::EVENT_EXPORT
;
128 $this->_exportMode
= self
::PLEDGE_EXPORT
;
132 $this->_exportMode
= self
::CASE_EXPORT
;
136 $this->_exportMode
= self
::GRANT_EXPORT
;
140 $this->_exportMode
= self
::ACTIVITY_EXPORT
;
144 $className = "CRM_{$componentName[1]}_Form_Task";
145 $className::preProcessCommon($this, TRUE);
146 $values = $this->controller
->exportValues('Search');
149 $values = $this->controller
->exportValues('Basic');
154 $this->_matchingContacts
= FALSE;
155 if (CRM_Utils_Array
::value('radio_ts', $values) == 'ts_sel') {
156 foreach ($values as $key => $value) {
157 if (strstr($key, 'mark_x')) {
161 $this->_matchingContacts
= TRUE;
167 $componentMode = $this->get('component_mode');
168 switch ($componentMode) {
170 CRM_Contribute_Form_Task
::preProcessCommon($this, TRUE);
171 $this->_exportMode
= self
::CONTRIBUTE_EXPORT
;
172 $componentName = array('', 'Contribute');
176 CRM_Event_Form_Task
::preProcessCommon($this, TRUE);
177 $this->_exportMode
= self
::EVENT_EXPORT
;
178 $componentName = array('', 'Event');
182 CRM_Activity_Form_Task
::preProcessCommon($this, TRUE);
183 $this->_exportMode
= self
::ACTIVITY_EXPORT
;
184 $componentName = array('', 'Activity');
188 CRM_Member_Form_Task
::preProcessCommon($this, TRUE);
189 $this->_exportMode
= self
::MEMBER_EXPORT
;
190 $componentName = array('', 'Member');
194 CRM_Case_Form_Task
::preProcessCommon($this, TRUE);
195 $this->_exportMode
= self
::CASE_EXPORT
;
196 $componentName = array('', 'Case');
200 $this->_task
= $values['task'];
201 if ($this->_exportMode
== self
::CONTACT_EXPORT
) {
202 $contactTasks = CRM_Contact_Task
::taskTitles();
203 $taskName = $contactTasks[$this->_task
];
205 CRM_Contact_Form_Task
::preProcessCommon($this, TRUE);
208 $this->assign('taskName', "Export $componentName[1]");
209 $className = "CRM_{$componentName[1]}_Task";
210 $componentTasks = $className::tasks();
211 $taskName = $componentTasks[$this->_task
];
215 if ($this->_componentTable
) {
218 FROM {$this->_componentTable}
220 $totalSelectedRecords = CRM_Core_DAO
::singleValueQuery($query);
223 $totalSelectedRecords = count($this->_componentIds
);
225 $this->assign('totalSelectedRecords', $totalSelectedRecords);
226 $this->assign('taskName', $taskName);
227 $this->assign('component', $component);
228 // all records actions = save a search
229 if (($values['radio_ts'] == 'ts_all') ||
($this->_task
== CRM_Contact_Task
::SAVE_SEARCH
)) {
230 $this->_selectAll
= TRUE;
231 $rowCount = $this->get('rowCount');
233 $this->_matchingContacts
= TRUE;
235 $this->assign('totalSelectedRecords', $rowCount);
238 $this->assign('matchingContacts', $this->_matchingContacts
);
239 $this->set('componentIds', $this->_componentIds
);
240 $this->set('selectAll', $this->_selectAll
);
241 $this->set('exportMode', $this->_exportMode
);
242 $this->set('componentClause', $this->_componentClause
);
243 $this->set('componentTable', $this->_componentTable
);
247 * Build the form object.
251 public function buildQuickForm() {
253 $exportOptions = $mergeOptions = $postalMailing = array();
254 $exportOptions[] = $this->createElement('radio',
256 ts('Export PRIMARY fields'),
258 array('onClick' => 'showMappingOption( );')
260 $exportOptions[] = $this->createElement('radio',
262 ts('Select fields for export'),
263 self
::EXPORT_SELECTED
,
264 array('onClick' => 'showMappingOption( );')
267 $mergeOptions[] = $this->createElement('radio',
270 self
::EXPORT_MERGE_DO_NOT_MERGE
,
271 array('onclick' => 'showGreetingOptions( );')
273 $mergeOptions[] = $this->createElement('radio',
275 ts('Merge All Contacts with the Same Address'),
276 self
::EXPORT_MERGE_SAME_ADDRESS
,
277 array('onclick' => 'showGreetingOptions( );')
279 $mergeOptions[] = $this->createElement('radio',
281 ts('Merge Household Members into their Households'),
282 self
::EXPORT_MERGE_HOUSEHOLD
,
283 array('onclick' => 'showGreetingOptions( );')
286 $postalMailing[] = $this->createElement('advcheckbox',
287 'postal_mailing_export',
292 $this->addGroup($exportOptions, 'exportOption', ts('Export Type'), '<br/>');
294 if ($this->_matchingContacts
) {
295 $this->_greetingOptions
= self
::getGreetingOptions();
297 foreach ($this->_greetingOptions
as $key => $value) {
298 $fieldLabel = ts('%1 (merging > 2 contacts)', array(1 => ucwords(str_replace('_', ' ', $key))));
299 $this->addElement('select', $key, $fieldLabel,
300 $value, array('onchange' => "showOther(this);")
302 $this->addElement('text', "{$key}_other", '');
306 if ($this->_exportMode
== self
::CONTACT_EXPORT
) {
307 $this->addGroup($mergeOptions, 'mergeOption', ts('Merge Options'), '<br/>');
308 $this->addGroup($postalMailing, 'postal_mailing_export', ts('Postal Mailing Export'), '<br/>');
310 $this->addElement('select', 'additional_group', ts('Additional Group for Export'),
311 array('' => ts('- select group -')) + CRM_Core_PseudoConstant
::nestedGroup(),
312 array('class' => 'crm-select2 huge')
316 $this->buildMapping();
318 $this->setDefaults(array(
319 'exportOption' => self
::EXPORT_ALL
,
320 'mergeOption' => self
::EXPORT_MERGE_DO_NOT_MERGE
,
323 $this->addButtons(array(
326 'name' => ts('Continue'),
327 'spacing' => ' ',
332 'name' => ts('Cancel'),
337 $this->addFormRule(array('CRM_Export_Form_Select', 'formRule'), $this);
343 * @param array $params
344 * (ref.) an assoc array of name/value pairs.
350 * mixed true or array of errors
352 static public function formRule($params, $files, $self) {
355 if (CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_SAME_ADDRESS
&&
356 $self->_matchingContacts
359 'postal_greeting' => 'postal_greeting_other',
360 'addressee' => 'addressee_other',
363 foreach ($greetings as $key => $value) {
364 $otherOption = CRM_Utils_Array
::value($key, $params);
366 if ((CRM_Utils_Array
::value($otherOption, $self->_greetingOptions
[$key]) == ts('Other')) && empty($params[$value])) {
368 $label = ucwords(str_replace('_', ' ', $key));
369 $errors[$value] = ts('Please enter a value for %1 (merging > 2 contacts), or select a pre-configured option from the list.', array(1 => $label));
374 return empty($errors) ?
TRUE : $errors;
378 * Process the uploaded file.
382 public function postProcess() {
383 $params = $this->controller
->exportValues($this->_name
);
384 $exportOption = $params['exportOption'];
385 $mergeSameAddress = CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_SAME_ADDRESS ?
1 : 0;
386 $mergeSameHousehold = CRM_Utils_Array
::value('mergeOption', $params) == self
::EXPORT_MERGE_HOUSEHOLD ?
1 : 0;
388 $this->set('mergeSameAddress', $mergeSameAddress);
389 $this->set('mergeSameHousehold', $mergeSameHousehold);
391 // instead of increasing the number of arguments to exportComponents function, we
392 // will send $exportParams as another argument, which is an array and suppose to contain
393 // all submitted options or any other argument
394 $exportParams = $params;
396 if (!empty($this->_greetingOptions
)) {
397 foreach ($this->_greetingOptions
as $key => $value) {
398 if ($option = CRM_Utils_Array
::value($key, $exportParams)) {
399 if ($this->_greetingOptions
[$key][$option] == ts('Other')) {
400 $exportParams[$key] = $exportParams["{$key}_other"];
402 elseif ($this->_greetingOptions
[$key][$option] == ts('List of names')) {
403 $exportParams[$key] = '';
406 $exportParams[$key] = $this->_greetingOptions
[$key][$option];
412 $mappingId = CRM_Utils_Array
::value('mapping', $params);
414 $this->set('mappingId', $mappingId);
417 $this->set('mappingId', NULL);
420 if ($exportOption == self
::EXPORT_ALL
) {
421 CRM_Export_BAO_Export
::exportComponents($this->_selectAll
,
422 $this->_componentIds
,
423 $this->get('queryParams'),
424 $this->get(CRM_Utils_Sort
::SORT_ORDER
),
426 $this->get('returnProperties'),
428 $this->_componentClause
,
429 $this->_componentTable
,
433 $this->get('queryOperator')
438 $this->controller
->resetPage('Map');
442 * Return a descriptive name for the page, used in wizard header
446 public function getTitle() {
447 return ts('Export All or Selected Fields');
451 * Build mapping form element.
453 public function buildMapping() {
454 switch ($this->_exportMode
) {
455 case CRM_Export_Form_Select
::CONTACT_EXPORT
:
456 $exportType = 'Export Contact';
459 case CRM_Export_Form_Select
::CONTRIBUTE_EXPORT
:
460 $exportType = 'Export Contribution';
463 case CRM_Export_Form_Select
::MEMBER_EXPORT
:
464 $exportType = 'Export Membership';
467 case CRM_Export_Form_Select
::EVENT_EXPORT
:
468 $exportType = 'Export Participant';
471 case CRM_Export_Form_Select
::PLEDGE_EXPORT
:
472 $exportType = 'Export Pledge';
475 case CRM_Export_Form_Select
::CASE_EXPORT
:
476 $exportType = 'Export Case';
479 case CRM_Export_Form_Select
::GRANT_EXPORT
:
480 $exportType = 'Export Grant';
483 case CRM_Export_Form_Select
::ACTIVITY_EXPORT
:
484 $exportType = 'Export Activity';
488 $mappingTypeId = CRM_Core_OptionGroup
::getValue('mapping_type', $exportType, 'name');
489 $this->set('mappingTypeId', $mappingTypeId);
491 $mappings = CRM_Core_BAO_Mapping
::getMappings($mappingTypeId);
492 if (!empty($mappings)) {
493 $this->add('select', 'mapping', ts('Use Saved Field Mapping'), array('' => '-select-') +
$mappings);
500 public static function getGreetingOptions() {
503 'postal_greeting' => 'postal_greeting_other',
504 'addressee' => 'addressee_other',
507 foreach ($greetings as $key => $value) {
509 $optionGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
511 CRM_Core_DAO
::commonRetrieveAll('CRM_Core_DAO_OptionValue', 'option_group_id', $optionGroupId,
512 $params, array('label', 'filter')
516 $options[$key] = array("$greetingCount" => ts('List of names'));
518 foreach ($params as $id => $field) {
519 if (CRM_Utils_Array
::value('filter', $field) == 4) {
520 $options[$key][++
$greetingCount] = $field['label'];
524 $options[$key][++
$greetingCount] = ts('Other');