| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 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_Map extends CRM_Core_Form { |
| 40 | |
| 41 | /** |
| 42 | * mapper fields |
| 43 | * |
| 44 | * @var array |
| 45 | * @access protected |
| 46 | */ |
| 47 | protected $_mapperFields; |
| 48 | |
| 49 | /** |
| 50 | * number of columns in import file |
| 51 | * |
| 52 | * @var int |
| 53 | * @access protected |
| 54 | */ |
| 55 | protected $_exportColumnCount; |
| 56 | |
| 57 | /** |
| 58 | * loaded mapping ID |
| 59 | * |
| 60 | * @var int |
| 61 | * @access protected |
| 62 | */ |
| 63 | protected $_mappingId; |
| 64 | |
| 65 | /** |
| 66 | * Function to actually build the form |
| 67 | * |
| 68 | * @return void |
| 69 | * @access public |
| 70 | */ |
| 71 | public function preProcess() { |
| 72 | $this->_exportColumnCount = $this->get('exportColumnCount'); |
| 73 | if (!$this->_exportColumnCount) { |
| 74 | $this->_exportColumnCount = 10; |
| 75 | } |
| 76 | else { |
| 77 | $this->_exportColumnCount = $this->_exportColumnCount + 10; |
| 78 | } |
| 79 | |
| 80 | $this->_mappingId = $this->get('mappingId'); |
| 81 | } |
| 82 | |
| 83 | public function buildQuickForm() { |
| 84 | CRM_Core_BAO_Mapping::buildMappingForm($this, |
| 85 | 'Export', |
| 86 | $this->_mappingId, |
| 87 | $this->_exportColumnCount, |
| 88 | $blockCnt = 2, |
| 89 | $this->get('exportMode') |
| 90 | ); |
| 91 | |
| 92 | $this->addButtons(array( |
| 93 | array( |
| 94 | 'type' => 'back', |
| 95 | 'name' => ts('<< Previous'), |
| 96 | ), |
| 97 | array( |
| 98 | 'type' => 'next', |
| 99 | 'name' => ts('Export >>'), |
| 100 | 'spacing' => ' ', |
| 101 | ), |
| 102 | array( |
| 103 | 'type' => 'done', |
| 104 | 'name' => ts('Done'), |
| 105 | ), |
| 106 | ) |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * global validation rules for the form |
| 112 | * |
| 113 | * @param array $fields posted values of the form |
| 114 | * |
| 115 | * @param $values |
| 116 | * @param $mappingTypeId |
| 117 | * |
| 118 | * @return array list of errors to be posted back to the form |
| 119 | * @static |
| 120 | * @access public |
| 121 | */ |
| 122 | static function formRule($fields, $values, $mappingTypeId) { |
| 123 | $errors = array(); |
| 124 | |
| 125 | if (!empty($fields['saveMapping']) && !empty($fields['_qf_Map_next'])) { |
| 126 | $nameField = CRM_Utils_Array::value('saveMappingName', $fields); |
| 127 | if (empty($nameField)) { |
| 128 | $errors['saveMappingName'] = ts('Name is required to save Export Mapping'); |
| 129 | } |
| 130 | else { |
| 131 | //check for Duplicate mappingName |
| 132 | if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) { |
| 133 | $errors['saveMappingName'] = ts('Duplicate Export Mapping Name'); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (!empty($errors)) { |
| 139 | $_flag = 1; |
| 140 | $assignError = new CRM_Core_Page(); |
| 141 | $assignError->assign('mappingDetailsError', $_flag); |
| 142 | return $errors; |
| 143 | } |
| 144 | else { |
| 145 | return TRUE; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Process the uploaded file |
| 151 | * |
| 152 | * @return void |
| 153 | * @access public |
| 154 | */ |
| 155 | public function postProcess() { |
| 156 | $params = $this->controller->exportValues($this->_name); |
| 157 | $exportParams = $this->controller->exportValues('Select'); |
| 158 | |
| 159 | $greetingOptions = CRM_Export_Form_Select::getGreetingOptions(); |
| 160 | |
| 161 | if (!empty($greetingOptions)) { |
| 162 | foreach ($greetingOptions as $key => $value) { |
| 163 | if ($option = CRM_Utils_Array::value($key, $exportParams)) { |
| 164 | if ($greetingOptions[$key][$option] == ts('Other')) { |
| 165 | $exportParams[$key] = $exportParams["{$key}_other"]; |
| 166 | } |
| 167 | elseif ($greetingOptions[$key][$option] == ts('List of names')) { |
| 168 | $exportParams[$key] = ''; |
| 169 | } |
| 170 | else { |
| 171 | $exportParams[$key] = $greetingOptions[$key][$option]; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | $currentPath = CRM_Utils_System::currentPath(); |
| 178 | |
| 179 | $urlParams = NULL; |
| 180 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this); |
| 181 | if (CRM_Utils_Rule::qfKey($qfKey)) { |
| 182 | $urlParams = "&qfKey=$qfKey"; |
| 183 | } |
| 184 | |
| 185 | //get the button name |
| 186 | $buttonName = $this->controller->getButtonName('done'); |
| 187 | $buttonName1 = $this->controller->getButtonName('next'); |
| 188 | if ($buttonName == '_qf_Map_done') { |
| 189 | $this->set('exportColumnCount', NULL); |
| 190 | $this->controller->resetPage($this->_name); |
| 191 | return CRM_Utils_System::redirect(CRM_Utils_System::url($currentPath, 'force=1' . $urlParams)); |
| 192 | } |
| 193 | |
| 194 | if ($this->controller->exportValue($this->_name, 'addMore')) { |
| 195 | $this->set('exportColumnCount', $this->_exportColumnCount); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | $mapperKeys = $params['mapper'][1]; |
| 200 | |
| 201 | $checkEmpty = 0; |
| 202 | foreach ($mapperKeys as $value) { |
| 203 | if ($value[0]) { |
| 204 | $checkEmpty++; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | if (!$checkEmpty) { |
| 209 | $this->set('mappingId', NULL); |
| 210 | CRM_Utils_System::redirect(CRM_Utils_System::url($currentPath, '_qf_Map_display=true' . $urlParams)); |
| 211 | } |
| 212 | |
| 213 | if ($buttonName1 == '_qf_Map_next') { |
| 214 | if (!empty($params['updateMapping'])) { |
| 215 | //save mapping fields |
| 216 | CRM_Core_BAO_Mapping::saveMappingFields($params, $params['mappingId']); |
| 217 | } |
| 218 | |
| 219 | if (!empty($params['saveMapping'])) { |
| 220 | $mappingParams = array( |
| 221 | 'name' => $params['saveMappingName'], |
| 222 | 'description' => $params['saveMappingDesc'], |
| 223 | 'mapping_type_id' => $this->get('mappingTypeId'), |
| 224 | ); |
| 225 | |
| 226 | $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams); |
| 227 | |
| 228 | //save mapping fields |
| 229 | CRM_Core_BAO_Mapping::saveMappingFields($params, $saveMapping->id); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | //get the csv file |
| 234 | CRM_Export_BAO_Export::exportComponents($this->get('selectAll'), |
| 235 | $this->get('componentIds'), |
| 236 | $this->get('queryParams'), |
| 237 | $this->get(CRM_Utils_Sort::SORT_ORDER), |
| 238 | $mapperKeys, |
| 239 | $this->get('returnProperties'), |
| 240 | $this->get('exportMode'), |
| 241 | $this->get('componentClause'), |
| 242 | $this->get('componentTable'), |
| 243 | $this->get('mergeSameAddress'), |
| 244 | $this->get('mergeSameHousehold'), |
| 245 | $exportParams |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Return a descriptive name for the page, used in wizard header |
| 251 | * |
| 252 | * @return string |
| 253 | * @access public |
| 254 | */ |
| 255 | public function getTitle() { |
| 256 | return ts('Select Fields to Export'); |
| 257 | } |
| 258 | } |
| 259 | |