Merge pull request #6037 from colemanw/CRM-16512
[civicrm-core.git] / CRM / Export / Form / Map.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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 /**
fe482240 63 * Build the form object.
6a488035 64 *
355ba699 65 * @return void
6a488035
TO
66 */
67 public function preProcess() {
68 $this->_exportColumnCount = $this->get('exportColumnCount');
eac832c5
CW
69 $this->_mappingId = $this->get('mappingId');
70
6a488035 71 if (!$this->_exportColumnCount) {
eac832c5
CW
72 // Set default from saved mapping
73 if ($this->_mappingId) {
74 $mapping = new CRM_Core_DAO_MappingField();
75 $mapping->mapping_id = $this->_mappingId;
76 $this->_exportColumnCount = $mapping->count();
77 }
78 else {
79 $this->_exportColumnCount = 10;
80 }
6a488035
TO
81 }
82 else {
eac832c5 83 $this->_exportColumnCount += 10;
6a488035 84 }
6a488035
TO
85 }
86
87 public function buildQuickForm() {
88 CRM_Core_BAO_Mapping::buildMappingForm($this,
89 'Export',
90 $this->_mappingId,
91 $this->_exportColumnCount,
92 $blockCnt = 2,
93 $this->get('exportMode')
94 );
95
96 $this->addButtons(array(
97 array(
98 'type' => 'back',
f212d37d 99 'name' => ts('Previous'),
6a488035
TO
100 ),
101 array(
102 'type' => 'next',
f212d37d 103 'name' => ts('Export'),
6a488035
TO
104 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
105 ),
106 array(
107 'type' => 'done',
7ec3896b 108 'icon' => 'close',
6a488035
TO
109 'name' => ts('Done'),
110 ),
111 )
112 );
113 }
114
115 /**
fe482240 116 * Global validation rules for the form.
6a488035 117 *
b9add4b3
TO
118 * @param array $fields
119 * Posted values of the form.
6a488035 120 *
dd244018 121 * @param $values
100fef9d 122 * @param int $mappingTypeId
dd244018 123 *
a6c01b45
CW
124 * @return array
125 * list of errors to be posted back to the form
6a488035 126 */
00be9182 127 public static function formRule($fields, $values, $mappingTypeId) {
6a488035
TO
128 $errors = array();
129
8cc574cf 130 if (!empty($fields['saveMapping']) && !empty($fields['_qf_Map_next'])) {
6a488035
TO
131 $nameField = CRM_Utils_Array::value('saveMappingName', $fields);
132 if (empty($nameField)) {
133 $errors['saveMappingName'] = ts('Name is required to save Export Mapping');
134 }
135 else {
136 //check for Duplicate mappingName
137 if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) {
138 $errors['saveMappingName'] = ts('Duplicate Export Mapping Name');
139 }
140 }
141 }
142
143 if (!empty($errors)) {
144 $_flag = 1;
145 $assignError = new CRM_Core_Page();
146 $assignError->assign('mappingDetailsError', $_flag);
147 return $errors;
148 }
149 else {
150 return TRUE;
151 }
152 }
153
154 /**
fe482240 155 * Process the uploaded file.
6a488035
TO
156 *
157 * @return void
6a488035
TO
158 */
159 public function postProcess() {
160 $params = $this->controller->exportValues($this->_name);
161 $exportParams = $this->controller->exportValues('Select');
162
163 $greetingOptions = CRM_Export_Form_Select::getGreetingOptions();
164
165 if (!empty($greetingOptions)) {
166 foreach ($greetingOptions as $key => $value) {
167 if ($option = CRM_Utils_Array::value($key, $exportParams)) {
168 if ($greetingOptions[$key][$option] == ts('Other')) {
169 $exportParams[$key] = $exportParams["{$key}_other"];
170 }
171 elseif ($greetingOptions[$key][$option] == ts('List of names')) {
172 $exportParams[$key] = '';
173 }
174 else {
175 $exportParams[$key] = $greetingOptions[$key][$option];
176 }
177 }
178 }
179 }
180
181 $currentPath = CRM_Utils_System::currentPath();
182
183 $urlParams = NULL;
184 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
185 if (CRM_Utils_Rule::qfKey($qfKey)) {
186 $urlParams = "&qfKey=$qfKey";
187 }
188
189 //get the button name
190 $buttonName = $this->controller->getButtonName('done');
191 $buttonName1 = $this->controller->getButtonName('next');
192 if ($buttonName == '_qf_Map_done') {
193 $this->set('exportColumnCount', NULL);
194 $this->controller->resetPage($this->_name);
195 return CRM_Utils_System::redirect(CRM_Utils_System::url($currentPath, 'force=1' . $urlParams));
196 }
197
198 if ($this->controller->exportValue($this->_name, 'addMore')) {
199 $this->set('exportColumnCount', $this->_exportColumnCount);
200 return;
201 }
202
203 $mapperKeys = $params['mapper'][1];
204
205 $checkEmpty = 0;
206 foreach ($mapperKeys as $value) {
207 if ($value[0]) {
208 $checkEmpty++;
209 }
210 }
211
212 if (!$checkEmpty) {
213 $this->set('mappingId', NULL);
214 CRM_Utils_System::redirect(CRM_Utils_System::url($currentPath, '_qf_Map_display=true' . $urlParams));
215 }
216
217 if ($buttonName1 == '_qf_Map_next') {
a7488080 218 if (!empty($params['updateMapping'])) {
6a488035
TO
219 //save mapping fields
220 CRM_Core_BAO_Mapping::saveMappingFields($params, $params['mappingId']);
221 }
222
a7488080 223 if (!empty($params['saveMapping'])) {
6a488035
TO
224 $mappingParams = array(
225 'name' => $params['saveMappingName'],
226 'description' => $params['saveMappingDesc'],
227 'mapping_type_id' => $this->get('mappingTypeId'),
228 );
229
230 $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);
231
232 //save mapping fields
233 CRM_Core_BAO_Mapping::saveMappingFields($params, $saveMapping->id);
234 }
235 }
236
237 //get the csv file
238 CRM_Export_BAO_Export::exportComponents($this->get('selectAll'),
239 $this->get('componentIds'),
240 $this->get('queryParams'),
241 $this->get(CRM_Utils_Sort::SORT_ORDER),
242 $mapperKeys,
243 $this->get('returnProperties'),
244 $this->get('exportMode'),
245 $this->get('componentClause'),
246 $this->get('componentTable'),
247 $this->get('mergeSameAddress'),
248 $this->get('mergeSameHousehold'),
249 $exportParams
250 );
251 }
252
253 /**
254 * Return a descriptive name for the page, used in wizard header
255 *
256 * @return string
6a488035
TO
257 */
258 public function getTitle() {
259 return ts('Select Fields to Export');
260 }
96025800 261
6a488035 262}