Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-01-19-15-14-40
[civicrm-core.git] / CRM / Export / Form / Map.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class gets the name of the file to upload
38 */
39class CRM_Export_Form_Map extends CRM_Core_Form {
40
41 /**
100fef9d 42 * Mapper fields
6a488035
TO
43 *
44 * @var array
6a488035
TO
45 */
46 protected $_mapperFields;
47
48 /**
100fef9d 49 * Number of columns in import file
6a488035
TO
50 *
51 * @var int
6a488035
TO
52 */
53 protected $_exportColumnCount;
54
55 /**
100fef9d 56 * Loaded mapping ID
6a488035
TO
57 *
58 * @var int
6a488035
TO
59 */
60 protected $_mappingId;
61
62 /**
c490a46a 63 * Build the form object
6a488035 64 *
355ba699 65 * @return void
6a488035
TO
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',
f212d37d 91 'name' => ts('Previous'),
6a488035
TO
92 ),
93 array(
94 'type' => 'next',
f212d37d 95 'name' => ts('Export'),
6a488035
TO
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 /**
100fef9d 107 * Global validation rules for the form
6a488035 108 *
b9add4b3
TO
109 * @param array $fields
110 * Posted values of the form.
6a488035 111 *
dd244018 112 * @param $values
100fef9d 113 * @param int $mappingTypeId
dd244018 114 *
a6c01b45
CW
115 * @return array
116 * list of errors to be posted back to the form
6a488035 117 */
00be9182 118 public static function formRule($fields, $values, $mappingTypeId) {
6a488035
TO
119 $errors = array();
120
8cc574cf 121 if (!empty($fields['saveMapping']) && !empty($fields['_qf_Map_next'])) {
6a488035
TO
122 $nameField = CRM_Utils_Array::value('saveMappingName', $fields);
123 if (empty($nameField)) {
124 $errors['saveMappingName'] = ts('Name is required to save Export Mapping');
125 }
126 else {
127 //check for Duplicate mappingName
128 if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) {
129 $errors['saveMappingName'] = ts('Duplicate Export Mapping Name');
130 }
131 }
132 }
133
134 if (!empty($errors)) {
135 $_flag = 1;
136 $assignError = new CRM_Core_Page();
137 $assignError->assign('mappingDetailsError', $_flag);
138 return $errors;
139 }
140 else {
141 return TRUE;
142 }
143 }
144
145 /**
146 * Process the uploaded file
147 *
148 * @return void
6a488035
TO
149 */
150 public function postProcess() {
151 $params = $this->controller->exportValues($this->_name);
152 $exportParams = $this->controller->exportValues('Select');
153
154 $greetingOptions = CRM_Export_Form_Select::getGreetingOptions();
155
156 if (!empty($greetingOptions)) {
157 foreach ($greetingOptions as $key => $value) {
158 if ($option = CRM_Utils_Array::value($key, $exportParams)) {
159 if ($greetingOptions[$key][$option] == ts('Other')) {
160 $exportParams[$key] = $exportParams["{$key}_other"];
161 }
162 elseif ($greetingOptions[$key][$option] == ts('List of names')) {
163 $exportParams[$key] = '';
164 }
165 else {
166 $exportParams[$key] = $greetingOptions[$key][$option];
167 }
168 }
169 }
170 }
171
172 $currentPath = CRM_Utils_System::currentPath();
173
174 $urlParams = NULL;
175 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
176 if (CRM_Utils_Rule::qfKey($qfKey)) {
177 $urlParams = "&qfKey=$qfKey";
178 }
179
180 //get the button name
181 $buttonName = $this->controller->getButtonName('done');
182 $buttonName1 = $this->controller->getButtonName('next');
183 if ($buttonName == '_qf_Map_done') {
184 $this->set('exportColumnCount', NULL);
185 $this->controller->resetPage($this->_name);
186 return CRM_Utils_System::redirect(CRM_Utils_System::url($currentPath, 'force=1' . $urlParams));
187 }
188
189 if ($this->controller->exportValue($this->_name, 'addMore')) {
190 $this->set('exportColumnCount', $this->_exportColumnCount);
191 return;
192 }
193
194 $mapperKeys = $params['mapper'][1];
195
196 $checkEmpty = 0;
197 foreach ($mapperKeys as $value) {
198 if ($value[0]) {
199 $checkEmpty++;
200 }
201 }
202
203 if (!$checkEmpty) {
204 $this->set('mappingId', NULL);
205 CRM_Utils_System::redirect(CRM_Utils_System::url($currentPath, '_qf_Map_display=true' . $urlParams));
206 }
207
208 if ($buttonName1 == '_qf_Map_next') {
a7488080 209 if (!empty($params['updateMapping'])) {
6a488035
TO
210 //save mapping fields
211 CRM_Core_BAO_Mapping::saveMappingFields($params, $params['mappingId']);
212 }
213
a7488080 214 if (!empty($params['saveMapping'])) {
6a488035
TO
215 $mappingParams = array(
216 'name' => $params['saveMappingName'],
217 'description' => $params['saveMappingDesc'],
218 'mapping_type_id' => $this->get('mappingTypeId'),
219 );
220
221 $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);
222
223 //save mapping fields
224 CRM_Core_BAO_Mapping::saveMappingFields($params, $saveMapping->id);
225 }
226 }
227
228 //get the csv file
229 CRM_Export_BAO_Export::exportComponents($this->get('selectAll'),
230 $this->get('componentIds'),
231 $this->get('queryParams'),
232 $this->get(CRM_Utils_Sort::SORT_ORDER),
233 $mapperKeys,
234 $this->get('returnProperties'),
235 $this->get('exportMode'),
236 $this->get('componentClause'),
237 $this->get('componentTable'),
238 $this->get('mergeSameAddress'),
239 $this->get('mergeSameHousehold'),
240 $exportParams
241 );
242 }
243
244 /**
245 * Return a descriptive name for the page, used in wizard header
246 *
247 * @return string
6a488035
TO
248 */
249 public function getTitle() {
250 return ts('Select Fields to Export');
251 }
252}