| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class gets the name of the file to upload |
| 38 | */ |
| 39 | class CRM_Export_Form_Select extends CRM_Core_Form { |
| 40 | |
| 41 | /** |
| 42 | * Various Contact types |
| 43 | */ |
| 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; |
| 50 | |
| 51 | /** |
| 52 | * Export modes |
| 53 | */ |
| 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; |
| 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 | |
| 110 | $formName = CRM_Utils_System::getClassName($stateMachine); |
| 111 | $componentName = explode('_', $formName); |
| 112 | $components = array('Contribute', 'Member', 'Event', 'Pledge', 'Case', 'Grant', 'Activity'); |
| 113 | |
| 114 | if (in_array($componentName[1], $components)) { |
| 115 | switch ($componentName[1]) { |
| 116 | case 'Contribute': |
| 117 | $this->_exportMode = self::CONTRIBUTE_EXPORT; |
| 118 | break; |
| 119 | case 'Member': |
| 120 | $this->_exportMode = self::MEMBER_EXPORT; |
| 121 | break; |
| 122 | case 'Event': |
| 123 | $this->_exportMode = self::EVENT_EXPORT; |
| 124 | break; |
| 125 | case 'Pledge': |
| 126 | $this->_exportMode = self::PLEDGE_EXPORT; |
| 127 | break; |
| 128 | case 'Case': |
| 129 | $this->_exportMode = self::CASE_EXPORT; |
| 130 | break; |
| 131 | case 'Grant': |
| 132 | $this->_exportMode = self::GRANT_EXPORT; |
| 133 | break; |
| 134 | case 'Activity': |
| 135 | $this->_exportMode = self::ACTIVITY_EXPORT; |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | $className = "CRM_{$componentName[1]}_Form_Task"; |
| 140 | $className::preProcessCommon( $this, true ); |
| 141 | $values = $this->controller->exportValues('Search'); |
| 142 | } |
| 143 | else { |
| 144 | $values = $this->controller->exportValues('Basic'); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | $count = 0; |
| 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')) { |
| 153 | $count++; |
| 154 | } |
| 155 | if ($count > 2) { |
| 156 | $this->_matchingContacts = TRUE; |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | $componentMode = $this->get('component_mode'); |
| 163 | switch ($componentMode) { |
| 164 | case 2: |
| 165 | CRM_Contribute_Form_Task::preProcessCommon($this, TRUE); |
| 166 | $this->_exportMode = self::CONTRIBUTE_EXPORT; |
| 167 | $componentName = array('', 'Contribute'); |
| 168 | break; |
| 169 | |
| 170 | case 3: |
| 171 | CRM_Event_Form_Task::preProcessCommon($this, TRUE); |
| 172 | $this->_exportMode = self::EVENT_EXPORT; |
| 173 | $componentName = array('', 'Event'); |
| 174 | break; |
| 175 | |
| 176 | case 4: |
| 177 | CRM_Activity_Form_Task::preProcessCommon($this, TRUE); |
| 178 | $this->_exportMode = self::ACTIVITY_EXPORT; |
| 179 | $componentName = array('', 'Activity'); |
| 180 | break; |
| 181 | case 5: |
| 182 | CRM_Member_Form_Task::preProcessCommon($this, TRUE); |
| 183 | $this->_exportMode = self::MEMBER_EXPORT; |
| 184 | $componentName = array('', 'Member'); |
| 185 | break; |
| 186 | case 6: |
| 187 | CRM_Case_Form_Task::preProcessCommon($this, TRUE); |
| 188 | $this->_exportMode = self::CASE_EXPORT; |
| 189 | $componentName = array('', 'Case'); |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | $this->_task = $values['task']; |
| 194 | if ($this->_exportMode == self::CONTACT_EXPORT) { |
| 195 | $contactTasks = CRM_Contact_Task::taskTitles(); |
| 196 | $taskName = $contactTasks[$this->_task]; |
| 197 | $component = FALSE; |
| 198 | CRM_Contact_Form_Task::preProcessCommon($this, TRUE); |
| 199 | } |
| 200 | else { |
| 201 | $this->assign('taskName', "Export $componentName[1]"); |
| 202 | $className = "CRM_{$componentName[1]}_Task"; |
| 203 | $componentTasks = $className::tasks(); |
| 204 | $taskName = $componentTasks[$this->_task]; |
| 205 | $component = TRUE; |
| 206 | } |
| 207 | |
| 208 | if ($this->_componentTable) { |
| 209 | $query = " |
| 210 | SELECT count(*) |
| 211 | FROM {$this->_componentTable} |
| 212 | "; |
| 213 | $totalSelectedRecords = CRM_Core_DAO::singleValueQuery($query); |
| 214 | } |
| 215 | else { |
| 216 | $totalSelectedRecords = count($this->_componentIds); |
| 217 | } |
| 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'); |
| 225 | if ($rowCount > 2) { |
| 226 | $this->_matchingContacts = TRUE; |
| 227 | } |
| 228 | $this->assign('totalSelectedRecords', $rowCount); |
| 229 | } |
| 230 | |
| 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); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Build the form object |
| 241 | * |
| 242 | * @return void |
| 243 | * @access public |
| 244 | */ |
| 245 | public function buildQuickForm() { |
| 246 | //export option |
| 247 | $exportOptions = $mergeOptions = $postalMailing = array(); |
| 248 | $exportOptions[] = $this->createElement('radio', |
| 249 | NULL, NULL, |
| 250 | ts('Export PRIMARY fields'), |
| 251 | self::EXPORT_ALL, |
| 252 | array('onClick' => 'showMappingOption( );') |
| 253 | ); |
| 254 | $exportOptions[] = $this->createElement('radio', |
| 255 | NULL, NULL, |
| 256 | ts('Select fields for export'), |
| 257 | self::EXPORT_SELECTED, |
| 258 | array('onClick' => 'showMappingOption( );') |
| 259 | ); |
| 260 | |
| 261 | $mergeOptions[] = $this->createElement('radio', |
| 262 | NULL, NULL, |
| 263 | ts('Do not merge'), |
| 264 | self::EXPORT_MERGE_DO_NOT_MERGE, |
| 265 | array('onclick' => 'showGreetingOptions( );') |
| 266 | ); |
| 267 | $mergeOptions[] = $this->createElement('radio', |
| 268 | NULL, NULL, |
| 269 | ts('Merge All Contacts with the Same Address'), |
| 270 | self::EXPORT_MERGE_SAME_ADDRESS, |
| 271 | array('onclick' => 'showGreetingOptions( );') |
| 272 | ); |
| 273 | $mergeOptions[] = $this->createElement('radio', |
| 274 | NULL, NULL, |
| 275 | ts('Merge Household Members into their Households'), |
| 276 | self::EXPORT_MERGE_HOUSEHOLD, |
| 277 | array('onclick' => 'showGreetingOptions( );') |
| 278 | ); |
| 279 | |
| 280 | $postalMailing[] = $this->createElement('advcheckbox', |
| 281 | 'postal_mailing_export', |
| 282 | NULL, |
| 283 | NULL |
| 284 | ); |
| 285 | |
| 286 | $this->addGroup($exportOptions, 'exportOption', ts('Export Type'), '<br/>'); |
| 287 | |
| 288 | if ($this->_matchingContacts) { |
| 289 | $this->_greetingOptions = self::getGreetingOptions(); |
| 290 | |
| 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);") |
| 295 | ); |
| 296 | $this->addElement('text', "{$key}_other", ''); |
| 297 | } |
| 298 | } |
| 299 | |
| 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/>'); |
| 303 | |
| 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') |
| 307 | ); |
| 308 | } |
| 309 | |
| 310 | $this->buildMapping(); |
| 311 | |
| 312 | $this->setDefaults(array( |
| 313 | 'exportOption' => self::EXPORT_ALL, |
| 314 | 'mergeOption' => self::EXPORT_MERGE_DO_NOT_MERGE, |
| 315 | )); |
| 316 | |
| 317 | $this->addButtons(array( |
| 318 | array( |
| 319 | 'type' => 'next', |
| 320 | 'name' => ts('Continue >>'), |
| 321 | 'spacing' => ' ', |
| 322 | 'isDefault' => TRUE, |
| 323 | ), |
| 324 | array( |
| 325 | 'type' => 'cancel', |
| 326 | 'name' => ts('Cancel'), |
| 327 | ), |
| 328 | ) |
| 329 | ); |
| 330 | |
| 331 | $this->addFormRule(array('CRM_Export_Form_Select', 'formRule'), $this); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Validation |
| 336 | * |
| 337 | * @param array $params (ref.) an assoc array of name/value pairs |
| 338 | * |
| 339 | * @param $files |
| 340 | * @param $self |
| 341 | * |
| 342 | * @return mixed true or array of errors |
| 343 | * @access public |
| 344 | * @static |
| 345 | */ |
| 346 | static public function formRule($params, $files, $self) { |
| 347 | $errors = array(); |
| 348 | |
| 349 | if (CRM_Utils_Array::value('mergeOption', $params) == self::EXPORT_MERGE_SAME_ADDRESS && |
| 350 | $self->_matchingContacts |
| 351 | ) { |
| 352 | $greetings = array( |
| 353 | 'postal_greeting' => 'postal_greeting_other', |
| 354 | 'addressee' => 'addressee_other', |
| 355 | ); |
| 356 | |
| 357 | foreach ($greetings as $key => $value) { |
| 358 | $otherOption = CRM_Utils_Array::value($key, $params); |
| 359 | |
| 360 | if ((CRM_Utils_Array::value($otherOption, $self->_greetingOptions[$key]) == ts('Other')) && empty($params[$value])) { |
| 361 | |
| 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)); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return empty($errors) ? TRUE : $errors; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Process the uploaded file |
| 373 | * |
| 374 | * @return void |
| 375 | * @access public |
| 376 | */ |
| 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; |
| 382 | |
| 383 | $this->set('mergeSameAddress', $mergeSameAddress); |
| 384 | $this->set('mergeSameHousehold', $mergeSameHousehold); |
| 385 | |
| 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; |
| 390 | |
| 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"]; |
| 396 | } |
| 397 | elseif ($this->_greetingOptions[$key][$option] == ts('List of names')) { |
| 398 | $exportParams[$key] = ''; |
| 399 | } |
| 400 | else { |
| 401 | $exportParams[$key] = $this->_greetingOptions[$key][$option]; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | $mappingId = CRM_Utils_Array::value('mapping', $params); |
| 408 | if ($mappingId) { |
| 409 | $this->set('mappingId', $mappingId); |
| 410 | } |
| 411 | else { |
| 412 | $this->set('mappingId', NULL); |
| 413 | } |
| 414 | |
| 415 | |
| 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), |
| 421 | NULL, |
| 422 | $this->get('returnProperties'), |
| 423 | $this->_exportMode, |
| 424 | $this->_componentClause, |
| 425 | $this->_componentTable, |
| 426 | $mergeSameAddress, |
| 427 | $mergeSameHousehold, |
| 428 | $exportParams, |
| 429 | $this->get('queryOperator') |
| 430 | ); |
| 431 | } |
| 432 | |
| 433 | //reset map page |
| 434 | $this->controller->resetPage('Map'); |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Return a descriptive name for the page, used in wizard header |
| 439 | * |
| 440 | * @return string |
| 441 | * @access public |
| 442 | */ |
| 443 | public function getTitle() { |
| 444 | return ts('Export All or Selected Fields'); |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Build mapping form element |
| 449 | * |
| 450 | */ |
| 451 | function buildMapping() { |
| 452 | switch ($this->_exportMode) { |
| 453 | case CRM_Export_Form_Select::CONTACT_EXPORT: |
| 454 | $exportType = 'Export Contact'; |
| 455 | break; |
| 456 | |
| 457 | case CRM_Export_Form_Select::CONTRIBUTE_EXPORT: |
| 458 | $exportType = 'Export Contribution'; |
| 459 | break; |
| 460 | |
| 461 | case CRM_Export_Form_Select::MEMBER_EXPORT: |
| 462 | $exportType = 'Export Membership'; |
| 463 | break; |
| 464 | |
| 465 | case CRM_Export_Form_Select::EVENT_EXPORT: |
| 466 | $exportType = 'Export Participant'; |
| 467 | break; |
| 468 | |
| 469 | case CRM_Export_Form_Select::PLEDGE_EXPORT: |
| 470 | $exportType = 'Export Pledge'; |
| 471 | break; |
| 472 | |
| 473 | case CRM_Export_Form_Select::CASE_EXPORT: |
| 474 | $exportType = 'Export Case'; |
| 475 | break; |
| 476 | |
| 477 | case CRM_Export_Form_Select::GRANT_EXPORT: |
| 478 | $exportType = 'Export Grant'; |
| 479 | break; |
| 480 | |
| 481 | case CRM_Export_Form_Select::ACTIVITY_EXPORT: |
| 482 | $exportType = 'Export Activity'; |
| 483 | break; |
| 484 | } |
| 485 | |
| 486 | $mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', $exportType, 'name'); |
| 487 | $this->set('mappingTypeId', $mappingTypeId); |
| 488 | |
| 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); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * @return array |
| 497 | */ |
| 498 | static function getGreetingOptions() { |
| 499 | $options = array(); |
| 500 | $greetings = array( |
| 501 | 'postal_greeting' => 'postal_greeting_other', |
| 502 | 'addressee' => 'addressee_other', |
| 503 | ); |
| 504 | |
| 505 | foreach ($greetings as $key => $value) { |
| 506 | $params = array(); |
| 507 | $optionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name'); |
| 508 | |
| 509 | CRM_Core_DAO::commonRetrieveAll('CRM_Core_DAO_OptionValue', 'option_group_id', $optionGroupId, |
| 510 | $params, array('label', 'filter') |
| 511 | ); |
| 512 | |
| 513 | $greetingCount = 1; |
| 514 | $options[$key] = array("$greetingCount" => ts('List of names')); |
| 515 | |
| 516 | foreach ($params as $id => $field) { |
| 517 | if (CRM_Utils_Array::value('filter', $field) == 4) { |
| 518 | $options[$key][++$greetingCount] = $field['label']; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | $options[$key][++$greetingCount] = ts('Other'); |
| 523 | } |
| 524 | |
| 525 | return $options; |
| 526 | } |
| 527 | } |
| 528 | |