CRM-12743 - goodbye eval
[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 */
0e6e8724
DL
44 CONST
45 EXPORT_ALL = 1,
46 EXPORT_SELECTED = 2,
47 EXPORT_MERGE_DO_NOT_MERGE = 0,
48 EXPORT_MERGE_SAME_ADDRESS = 1,
49 EXPORT_MERGE_HOUSEHOLD = 2;
6a488035
TO
50
51 /**
52 * export modes
53 */
0e6e8724
DL
54 CONST
55 CONTACT_EXPORT = 1,
56 CONTRIBUTE_EXPORT = 2,
57 MEMBER_EXPORT = 3,
58 EVENT_EXPORT = 4,
59 PLEDGE_EXPORT = 5,
60 CASE_EXPORT = 6,
61 GRANT_EXPORT = 7,
62 ACTIVITY_EXPORT = 8;
6a488035
TO
63
64 /**
65 * current export mode
66 *
67 * @var int
68 */
69 public $_exportMode;
70
71 public $_componentTable;
72
73 /**
74 * build all the data structures needed to build the form
75 *
76 * @param
77 *
78 * @return void
79 * @access public
80 */
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)
88 );
89 }
90
91 $this->_selectAll = FALSE;
92 $this->_exportMode = self::CONTACT_EXPORT;
93 $this->_componentIds = array();
94 $this->_componentClause = NULL;
95
96 // get the submitted values based on search
97 if ($this->_action == CRM_Core_Action::ADVANCED) {
98 $values = $this->controller->exportValues('Advanced');
99 }
100 elseif ($this->_action == CRM_Core_Action::PROFILE) {
101 $values = $this->controller->exportValues('Builder');
102 }
103 elseif ($this->_action == CRM_Core_Action::COPY) {
104 $values = $this->controller->exportValues('Custom');
105 }
106 else {
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');
112
113 if (in_array($componentName[1], $components)) {
0e6e8724
DL
114 $fieldName = strtoupper($componentName[1]) . '_EXPORT';
115 $this->_exportMode = self::$fieldName;
116 $className = "CRM_{$componentName[1]}_Form_Task";
117 $className::preProcessCommon( $this, true );
6a488035
TO
118 $values = $this->controller->exportValues('Search');
119 }
120 else {
121 $values = $this->controller->exportValues('Basic');
122 }
123 }
124
125 $count = 0;
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')) {
130 $count++;
131 }
132 if ($count > 2) {
133 $this->_matchingContacts = TRUE;
134 break;
135 }
136 }
137 }
138
139 $componentMode = $this->get('component_mode');
140 switch ($componentMode) {
141 case 2:
142 CRM_Contribute_Form_Task::preProcessCommon($this, TRUE);
143 $this->_exportMode = self::CONTRIBUTE_EXPORT;
144 $componentName = array('', 'Contribute');
145 break;
146
147 case 3:
148 CRM_Event_Form_Task::preProcessCommon($this, TRUE);
149 $this->_exportMode = self::EVENT_EXPORT;
150 $componentName = array('', 'Event');
151 break;
152
153 case 4:
154 CRM_Activity_Form_Task::preProcessCommon($this, TRUE);
155 $this->_exportMode = self::ACTIVITY_EXPORT;
156 $componentName = array('', 'Activity');
157 break;
158 case 5:
159 CRM_Member_Form_Task::preProcessCommon($this, TRUE);
160 $this->_exportMode = self::MEMBER_EXPORT;
161 $componentName = array('', 'Member');
162 break;
163 case 6:
164 CRM_Case_Form_Task::preProcessCommon($this, TRUE);
165 $this->_exportMode = self::CASE_EXPORT;
166 $componentName = array('', 'Case');
03e04002 167 break;
6a488035
TO
168 }
169
170 $this->_task = $values['task'];
171 if ($this->_exportMode == self::CONTACT_EXPORT) {
172 $contactTasks = CRM_Contact_Task::taskTitles();
173 $taskName = $contactTasks[$this->_task];
174 $component = FALSE;
175 CRM_Contact_Form_Task::preProcessCommon($this, TRUE);
176 }
177 else {
178 $this->assign('taskName', "Export $componentName[1]");
0e6e8724
DL
179 $className = "CRM_{$componentName[1]}_Task";
180 $componentTasks = $className::tasks();
6a488035
TO
181 $taskName = $componentTasks[$this->_task];
182 $component = TRUE;
183 }
184
185 if ($this->_componentTable) {
186 $query = "
187SELECT count(*)
188FROM {$this->_componentTable}
189";
190 $totalSelectedRecords = CRM_Core_DAO::singleValueQuery($query);
191 }
192 else {
193 $totalSelectedRecords = count($this->_componentIds);
194 }
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');
202 if ($rowCount > 2) {
203 $this->_matchingContacts = TRUE;
204 }
205 $this->assign('totalSelectedRecords', $rowCount);
206 }
207
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);
214 }
215
216 /**
217 * Function to actually build the form
218 *
219 * @return void
220 * @access public
221 */
222 public function buildQuickForm() {
223 //export option
224 $exportOptions = $mergeOptions = $postalMailing = array();
225 $exportOptions[] = $this->createElement('radio',
226 NULL, NULL,
227 ts('Export PRIMARY fields'),
228 self::EXPORT_ALL,
229 array('onClick' => 'showMappingOption( );')
230 );
231 $exportOptions[] = $this->createElement('radio',
232 NULL, NULL,
233 ts('Select fields for export'),
234 self::EXPORT_SELECTED,
235 array('onClick' => 'showMappingOption( );')
236 );
237
238 $mergeOptions[] = $this->createElement('radio',
239 NULL, NULL,
240 ts('Do not merge'),
241 self::EXPORT_MERGE_DO_NOT_MERGE,
242 array('onclick' => 'showGreetingOptions( );')
243 );
244 $mergeOptions[] = $this->createElement('radio',
245 NULL, NULL,
246 ts('Merge All Contacts with the Same Address'),
247 self::EXPORT_MERGE_SAME_ADDRESS,
248 array('onclick' => 'showGreetingOptions( );')
249 );
250 $mergeOptions[] = $this->createElement('radio',
251 NULL, NULL,
252 ts('Merge Household Members into their Households'),
253 self::EXPORT_MERGE_HOUSEHOLD,
254 array('onclick' => 'showGreetingOptions( );')
255 );
256
257 $postalMailing[] = $this->createElement('advcheckbox',
258 'postal_mailing_export',
259 NULL,
260 NULL
261 );
262
263 $this->addGroup($exportOptions, 'exportOption', ts('Export Type'), '<br/>');
264
265 if ($this->_matchingContacts) {
266 $this->_greetingOptions = self::getGreetingOptions();
267
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);")
272 );
273 $this->addElement('text', "{$key}_other", '');
274 }
275 }
276
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/>');
280
281 $this->addElement('select', 'additional_group', ts('Additional Group for Export'),
282 array('' => ts('- select group -')) + CRM_Core_PseudoConstant::group()
283 );
284 }
285
286 $this->buildMapping();
287
288 $this->setDefaults(array(
289 'exportOption' => self::EXPORT_ALL,
290 'mergeOption' => self::EXPORT_MERGE_DO_NOT_MERGE,
291 ));
292
293 $this->addButtons(array(
294 array(
295 'type' => 'next',
296 'name' => ts('Continue >>'),
297 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
298 'isDefault' => TRUE,
299 ),
300 array(
301 'type' => 'cancel',
302 'name' => ts('Cancel'),
303 ),
304 )
305 );
306
307 $this->addFormRule(array('CRM_Export_Form_Select', 'formRule'), $this);
308 }
309
310 /**
311 * Function for validation
312 *
313 * @param array $params (ref.) an assoc array of name/value pairs
314 *
315 * @return mixed true or array of errors
316 * @access public
317 * @static
318 */
319 public function formRule($params, $files, $self) {
320 $errors = array();
321
322 if (CRM_Utils_Array::value('mergeOption', $params) == self::EXPORT_MERGE_SAME_ADDRESS &&
323 $self->_matchingContacts
324 ) {
325 $greetings = array(
326 'postal_greeting' => 'postal_greeting_other',
327 'addressee' => 'addressee_other',
328 );
329
330 foreach ($greetings as $key => $value) {
331 $otherOption = CRM_Utils_Array::value($key, $params);
332
333 if ((CRM_Utils_Array::value($otherOption, $self->_greetingOptions[$key]) == ts('Other')) &&
334 !CRM_Utils_Array::value($value, $params)
335 ) {
336
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));
339 }
340 }
341 }
342
343 return empty($errors) ? TRUE : $errors;
344 }
345
346 /**
347 * Process the uploaded file
348 *
349 * @return void
350 * @access public
351 */
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;
357
358 $this->set('mergeSameAddress', $mergeSameAddress);
359 $this->set('mergeSameHousehold', $mergeSameHousehold);
360
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;
365
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"];
371 }
372 elseif ($this->_greetingOptions[$key][$option] == ts('List of names')) {
373 $exportParams[$key] = '';
374 }
375 else {
376 $exportParams[$key] = $this->_greetingOptions[$key][$option];
377 }
378 }
379 }
380 }
381
382 $mappingId = CRM_Utils_Array::value('mapping', $params);
383 if ($mappingId) {
384 $this->set('mappingId', $mappingId);
385 }
386 else {
387 $this->set('mappingId', NULL);
388 }
389
390
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),
396 NULL,
397 $this->get('returnProperties'),
398 $this->_exportMode,
399 $this->_componentClause,
400 $this->_componentTable,
401 $mergeSameAddress,
402 $mergeSameHousehold,
403 $exportParams,
404 $this->get('queryOperator')
405 );
406 }
407
408 //reset map page
409 $this->controller->resetPage('Map');
410 }
411
412 /**
413 * Return a descriptive name for the page, used in wizard header
414 *
415 * @return string
416 * @access public
417 */
418 public function getTitle() {
419 return ts('Export All or Selected Fields');
420 }
421
422 /**
423 * Function to build mapping form element
424 *
425 */
426 function buildMapping() {
427 switch ($this->_exportMode) {
428 case CRM_Export_Form_Select::CONTACT_EXPORT:
429 $exportType = 'Export Contact';
430 break;
431
432 case CRM_Export_Form_Select::CONTRIBUTE_EXPORT:
433 $exportType = 'Export Contribution';
434 break;
435
436 case CRM_Export_Form_Select::MEMBER_EXPORT:
437 $exportType = 'Export Membership';
438 break;
439
440 case CRM_Export_Form_Select::EVENT_EXPORT:
441 $exportType = 'Export Participant';
442 break;
443
444 case CRM_Export_Form_Select::PLEDGE_EXPORT:
445 $exportType = 'Export Pledge';
446 break;
447
448 case CRM_Export_Form_Select::CASE_EXPORT:
449 $exportType = 'Export Case';
450 break;
451
452 case CRM_Export_Form_Select::GRANT_EXPORT:
453 $exportType = 'Export Grant';
454 break;
455
456 case CRM_Export_Form_Select::ACTIVITY_EXPORT:
457 $exportType = 'Export Activity';
458 break;
459 }
460
461 $mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', $exportType, 'name');
462 $this->set('mappingTypeId', $mappingTypeId);
463
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);
467 }
468 }
469
470 static function getGreetingOptions() {
471 $options = array();
472 $greetings = array(
473 'postal_greeting' => 'postal_greeting_other',
474 'addressee' => 'addressee_other',
475 );
476
477 foreach ($greetings as $key => $value) {
478 $params = array();
479 $optionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
480
481 CRM_Core_DAO::commonRetrieveAll('CRM_Core_DAO_OptionValue', 'option_group_id', $optionGroupId,
482 $params, array('label', 'filter')
483 );
484
485 $greetingCount = 1;
486 $options[$key] = array("$greetingCount" => ts('List of names'));
487
488 foreach ($params as $id => $field) {
489 if (CRM_Utils_Array::value('filter', $field) == 4) {
490 $options[$key][++$greetingCount] = $field['label'];
491 }
492 }
493
494 $options[$key][++$greetingCount] = ts('Other');
495 }
496
497 return $options;
498 }
499}
500