CRM-12595 fix formatting in CRM/P* files
[civicrm-core.git] / CRM / Export / Form / Select.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36/**
37 * This class gets the name of the file to upload
38 */
39class CRM_Export_Form_Select extends CRM_Core_Form {
40
41 /**
42 * various Contact types
43 */
44 CONST EXPORT_ALL = 1, EXPORT_SELECTED = 2, EXPORT_MERGE_DO_NOT_MERGE = 0, EXPORT_MERGE_SAME_ADDRESS = 1, EXPORT_MERGE_HOUSEHOLD = 2;
45
46 /**
47 * export modes
48 */
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;
50
51 /**
52 * current export mode
53 *
54 * @var int
55 */
56 public $_exportMode;
57
58 public $_componentTable;
59
60 /**
61 * build all the data structures needed to build the form
62 *
63 * @param
64 *
65 * @return void
66 * @access public
67 */
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)
75 );
76 }
77
78 $this->_selectAll = FALSE;
79 $this->_exportMode = self::CONTACT_EXPORT;
80 $this->_componentIds = array();
81 $this->_componentClause = NULL;
82
83 // get the submitted values based on search
84 if ($this->_action == CRM_Core_Action::ADVANCED) {
85 $values = $this->controller->exportValues('Advanced');
86 }
87 elseif ($this->_action == CRM_Core_Action::PROFILE) {
88 $values = $this->controller->exportValues('Builder');
89 }
90 elseif ($this->_action == CRM_Core_Action::COPY) {
91 $values = $this->controller->exportValues('Custom');
92 }
93 else {
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');
99
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');
104 }
105 else {
106 $values = $this->controller->exportValues('Basic');
107 }
108 }
109
110 $count = 0;
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')) {
115 $count++;
116 }
117 if ($count > 2) {
118 $this->_matchingContacts = TRUE;
119 break;
120 }
121 }
122 }
123
124 $componentMode = $this->get('component_mode');
125 switch ($componentMode) {
126 case 2:
127 CRM_Contribute_Form_Task::preProcessCommon($this, TRUE);
128 $this->_exportMode = self::CONTRIBUTE_EXPORT;
129 $componentName = array('', 'Contribute');
130 break;
131
132 case 3:
133 CRM_Event_Form_Task::preProcessCommon($this, TRUE);
134 $this->_exportMode = self::EVENT_EXPORT;
135 $componentName = array('', 'Event');
136 break;
137
138 case 4:
139 CRM_Activity_Form_Task::preProcessCommon($this, TRUE);
140 $this->_exportMode = self::ACTIVITY_EXPORT;
141 $componentName = array('', 'Activity');
142 break;
143 case 5:
144 CRM_Member_Form_Task::preProcessCommon($this, TRUE);
145 $this->_exportMode = self::MEMBER_EXPORT;
146 $componentName = array('', 'Member');
147 break;
148 case 6:
149 CRM_Case_Form_Task::preProcessCommon($this, TRUE);
150 $this->_exportMode = self::CASE_EXPORT;
151 $componentName = array('', 'Case');
152 break;
153 }
154
155 $this->_task = $values['task'];
156 if ($this->_exportMode == self::CONTACT_EXPORT) {
157 $contactTasks = CRM_Contact_Task::taskTitles();
158 $taskName = $contactTasks[$this->_task];
159 $component = FALSE;
160 CRM_Contact_Form_Task::preProcessCommon($this, TRUE);
161 }
162 else {
163 $this->assign('taskName', "Export $componentName[1]");
164 eval('$componentTasks = CRM_' . $componentName[1] . '_Task::tasks();');
165 $taskName = $componentTasks[$this->_task];
166 $component = TRUE;
167 }
168
169 if ($this->_componentTable) {
170 $query = "
171SELECT count(*)
172FROM {$this->_componentTable}
173";
174 $totalSelectedRecords = CRM_Core_DAO::singleValueQuery($query);
175 }
176 else {
177 $totalSelectedRecords = count($this->_componentIds);
178 }
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');
186 if ($rowCount > 2) {
187 $this->_matchingContacts = TRUE;
188 }
189 $this->assign('totalSelectedRecords', $rowCount);
190 }
191
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);
198 }
199
200 /**
201 * Function to actually build the form
202 *
203 * @return void
204 * @access public
205 */
206 public function buildQuickForm() {
207 //export option
208 $exportOptions = $mergeOptions = $postalMailing = array();
209 $exportOptions[] = $this->createElement('radio',
210 NULL, NULL,
211 ts('Export PRIMARY fields'),
212 self::EXPORT_ALL,
213 array('onClick' => 'showMappingOption( );')
214 );
215 $exportOptions[] = $this->createElement('radio',
216 NULL, NULL,
217 ts('Select fields for export'),
218 self::EXPORT_SELECTED,
219 array('onClick' => 'showMappingOption( );')
220 );
221
222 $mergeOptions[] = $this->createElement('radio',
223 NULL, NULL,
224 ts('Do not merge'),
225 self::EXPORT_MERGE_DO_NOT_MERGE,
226 array('onclick' => 'showGreetingOptions( );')
227 );
228 $mergeOptions[] = $this->createElement('radio',
229 NULL, NULL,
230 ts('Merge All Contacts with the Same Address'),
231 self::EXPORT_MERGE_SAME_ADDRESS,
232 array('onclick' => 'showGreetingOptions( );')
233 );
234 $mergeOptions[] = $this->createElement('radio',
235 NULL, NULL,
236 ts('Merge Household Members into their Households'),
237 self::EXPORT_MERGE_HOUSEHOLD,
238 array('onclick' => 'showGreetingOptions( );')
239 );
240
241 $postalMailing[] = $this->createElement('advcheckbox',
242 'postal_mailing_export',
243 NULL,
244 NULL
245 );
246
247 $this->addGroup($exportOptions, 'exportOption', ts('Export Type'), '<br/>');
248
249 if ($this->_matchingContacts) {
250 $this->_greetingOptions = self::getGreetingOptions();
251
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);")
256 );
257 $this->addElement('text', "{$key}_other", '');
258 }
259 }
260
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/>');
264
265 $this->addElement('select', 'additional_group', ts('Additional Group for Export'),
266 array('' => ts('- select group -')) + CRM_Core_PseudoConstant::group()
267 );
268 }
269
270 $this->buildMapping();
271
272 $this->setDefaults(array(
273 'exportOption' => self::EXPORT_ALL,
274 'mergeOption' => self::EXPORT_MERGE_DO_NOT_MERGE,
275 ));
276
277 $this->addButtons(array(
278 array(
279 'type' => 'next',
280 'name' => ts('Continue >>'),
281 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
282 'isDefault' => TRUE,
283 ),
284 array(
285 'type' => 'cancel',
286 'name' => ts('Cancel'),
287 ),
288 )
289 );
290
291 $this->addFormRule(array('CRM_Export_Form_Select', 'formRule'), $this);
292 }
293
294 /**
295 * Function for validation
296 *
297 * @param array $params (ref.) an assoc array of name/value pairs
298 *
299 * @return mixed true or array of errors
300 * @access public
301 * @static
302 */
303 public function formRule($params, $files, $self) {
304 $errors = array();
305
306 if (CRM_Utils_Array::value('mergeOption', $params) == self::EXPORT_MERGE_SAME_ADDRESS &&
307 $self->_matchingContacts
308 ) {
309 $greetings = array(
310 'postal_greeting' => 'postal_greeting_other',
311 'addressee' => 'addressee_other',
312 );
313
314 foreach ($greetings as $key => $value) {
315 $otherOption = CRM_Utils_Array::value($key, $params);
316
317 if ((CRM_Utils_Array::value($otherOption, $self->_greetingOptions[$key]) == ts('Other')) &&
318 !CRM_Utils_Array::value($value, $params)
319 ) {
320
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));
323 }
324 }
325 }
326
327 return empty($errors) ? TRUE : $errors;
328 }
329
330 /**
331 * Process the uploaded file
332 *
333 * @return void
334 * @access public
335 */
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;
341
342 $this->set('mergeSameAddress', $mergeSameAddress);
343 $this->set('mergeSameHousehold', $mergeSameHousehold);
344
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;
349
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"];
355 }
356 elseif ($this->_greetingOptions[$key][$option] == ts('List of names')) {
357 $exportParams[$key] = '';
358 }
359 else {
360 $exportParams[$key] = $this->_greetingOptions[$key][$option];
361 }
362 }
363 }
364 }
365
366 $mappingId = CRM_Utils_Array::value('mapping', $params);
367 if ($mappingId) {
368 $this->set('mappingId', $mappingId);
369 }
370 else {
371 $this->set('mappingId', NULL);
372 }
373
374
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),
380 NULL,
381 $this->get('returnProperties'),
382 $this->_exportMode,
383 $this->_componentClause,
384 $this->_componentTable,
385 $mergeSameAddress,
386 $mergeSameHousehold,
387 $exportParams,
388 $this->get('queryOperator')
389 );
390 }
391
392 //reset map page
393 $this->controller->resetPage('Map');
394 }
395
396 /**
397 * Return a descriptive name for the page, used in wizard header
398 *
399 * @return string
400 * @access public
401 */
402 public function getTitle() {
403 return ts('Export All or Selected Fields');
404 }
405
406 /**
407 * Function to build mapping form element
408 *
409 */
410 function buildMapping() {
411 switch ($this->_exportMode) {
412 case CRM_Export_Form_Select::CONTACT_EXPORT:
413 $exportType = 'Export Contact';
414 break;
415
416 case CRM_Export_Form_Select::CONTRIBUTE_EXPORT:
417 $exportType = 'Export Contribution';
418 break;
419
420 case CRM_Export_Form_Select::MEMBER_EXPORT:
421 $exportType = 'Export Membership';
422 break;
423
424 case CRM_Export_Form_Select::EVENT_EXPORT:
425 $exportType = 'Export Participant';
426 break;
427
428 case CRM_Export_Form_Select::PLEDGE_EXPORT:
429 $exportType = 'Export Pledge';
430 break;
431
432 case CRM_Export_Form_Select::CASE_EXPORT:
433 $exportType = 'Export Case';
434 break;
435
436 case CRM_Export_Form_Select::GRANT_EXPORT:
437 $exportType = 'Export Grant';
438 break;
439
440 case CRM_Export_Form_Select::ACTIVITY_EXPORT:
441 $exportType = 'Export Activity';
442 break;
443 }
444
445 $mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', $exportType, 'name');
446 $this->set('mappingTypeId', $mappingTypeId);
447
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);
451 }
452 }
453
454 static function getGreetingOptions() {
455 $options = array();
456 $greetings = array(
457 'postal_greeting' => 'postal_greeting_other',
458 'addressee' => 'addressee_other',
459 );
460
461 foreach ($greetings as $key => $value) {
462 $params = array();
463 $optionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
464
465 CRM_Core_DAO::commonRetrieveAll('CRM_Core_DAO_OptionValue', 'option_group_id', $optionGroupId,
466 $params, array('label', 'filter')
467 );
468
469 $greetingCount = 1;
470 $options[$key] = array("$greetingCount" => ts('List of names'));
471
472 foreach ($params as $id => $field) {
473 if (CRM_Utils_Array::value('filter', $field) == 4) {
474 $options[$key][++$greetingCount] = $field['label'];
475 }
476 }
477
478 $options[$key][++$greetingCount] = ts('Other');
479 }
480
481 return $options;
482 }
483}
484