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