Merge pull request #22316 from braders/core-3003-preserve-tab-between-pageloads
[civicrm-core.git] / CRM / Export / Form / Map.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class gets the name of the file to upload
20 */
21 class CRM_Export_Form_Map extends CRM_Core_Form {
22
23 /**
24 * Loaded mapping ID
25 *
26 * @var int
27 */
28 protected $_mappingId;
29
30 /**
31 * Use the form name to create the tpl file name.
32 *
33 * @return string
34 */
35 public function getTemplateFileName() {
36 return 'CRM/Export/Form/Map.tpl';
37 }
38
39 /**
40 * Build the form object.
41 */
42 public function preProcess() {
43 $this->_mappingId = $this->get('mappingId');
44
45 $contactTypes = array_column(CRM_Utils_Array::makeNonAssociative(CRM_Contact_BAO_ContactType::basicTypePairs(), 'id', 'text'), NULL, 'id');
46 foreach (CRM_Contact_BAO_ContactType::subTypeInfo() as $subType) {
47 $contactTypes[$subType['parent']]['children'][] = [
48 'id' => $subType['name'],
49 'text' => $subType['label'],
50 'description' => $subType['description'] ?? NULL,
51 ];
52 }
53 $mappingTypeId = $this->get('mappingTypeId');
54 $mappings = civicrm_api3('Mapping', 'get', ['return' => ['name', 'description'], 'mapping_type_id' => $mappingTypeId, 'options' => ['limit' => 0]]);
55
56 Civi::resources()->addVars('exportUi', [
57 'fields' => CRM_Export_Utils::getExportFields($this->get('exportMode')),
58 'contact_types' => array_values($contactTypes),
59 'location_type_id' => CRM_Utils_Array::makeNonAssociative(CRM_Core_BAO_Address::buildOptions('location_type_id'), 'id', 'text'),
60 'preview_data' => $this->getPreviewData(),
61 'mapping_id' => $this->_mappingId,
62 'mapping_description' => $mappings['values'][$this->_mappingId]['description'] ?? '',
63 'mapping_type_id' => $mappingTypeId,
64 'mapping_names' => CRM_Utils_Array::collect('name', $mappings['values']),
65 'option_list' => [
66 'phone_type_id' => CRM_Utils_Array::makeNonAssociative(CRM_Core_BAO_Phone::buildOptions('phone_type_id'), 'id', 'text'),
67 'website_type_id' => CRM_Utils_Array::makeNonAssociative(CRM_Core_BAO_Website::buildOptions('website_type_id'), 'id', 'text'),
68 'im_provider_id' => CRM_Utils_Array::makeNonAssociative(CRM_Core_BAO_IM::buildOptions('provider_id'), 'id', 'text'),
69 ],
70 ]);
71
72 // Add exportui app
73 Civi::service('angularjs.loader')
74 ->addModules('exportui');
75 }
76
77 public function buildQuickForm() {
78 $this->add('hidden', 'export_field_map');
79
80 $this->addButtons([
81 [
82 'type' => 'back',
83 'name' => ts('Previous'),
84 ],
85 [
86 'type' => 'done',
87 'icon' => 'fa-times',
88 'name' => ts('Return to Search'),
89 ],
90 [
91 'type' => 'next',
92 'icon' => 'fa-download',
93 'name' => ts('Download File'),
94 ],
95 ]);
96 }
97
98 public function setDefaultValues() {
99 $defaults = [];
100 if ($this->_mappingId) {
101 $mappingFields = civicrm_api3('mappingField', 'get', ['mapping_id' => $this->_mappingId, 'options' => ['limit' => 0, 'sort' => 'column_number']]);
102 $defaults['export_field_map'] = json_encode(array_values($mappingFields['values']));
103 }
104 return $defaults;
105 }
106
107 /**
108 * Process the form submission.
109 */
110 public function postProcess() {
111 $params = $this->controller->exportValues($this->_name);
112 $exportParams = $this->controller->exportValues('Select');
113
114 // Redirect back to search if "done" button pressed
115 if ($this->controller->getButtonName('done') == '_qf_Map_done') {
116 $currentPath = CRM_Utils_System::currentPath();
117 $urlParams = NULL;
118 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
119 if (CRM_Utils_Rule::qfKey($qfKey)) {
120 $urlParams = "&qfKey=$qfKey";
121 }
122 $this->controller->resetPage($this->_name);
123 return CRM_Utils_System::redirect(CRM_Utils_System::url($currentPath, 'force=1' . $urlParams));
124 }
125
126 $selectedFields = json_decode($params['export_field_map'], TRUE);
127
128 //get the csv file
129 CRM_Export_BAO_Export::exportComponents($this->get('selectAll'),
130 $this->get('componentIds'),
131 (array) $this->get('queryParams'),
132 $this->get(CRM_Utils_Sort::SORT_ORDER),
133 $selectedFields,
134 $this->get('returnProperties'),
135 $this->get('exportMode'),
136 $this->get('componentClause'),
137 $this->get('componentTable'),
138 $this->get('mergeSameAddress'),
139 $this->get('mergeSameHousehold'),
140 $exportParams,
141 $this->get('queryOperator')
142 );
143 }
144
145 /**
146 * @return array
147 */
148 protected function getPreviewData() {
149 $exportParams = $this->controller->exportValues('Select');
150 $isPostalOnly = (
151 isset($exportParams['postal_mailing_export']['postal_mailing_export']) &&
152 $exportParams['postal_mailing_export']['postal_mailing_export'] == 1
153 );
154 $processor = new CRM_Export_BAO_ExportProcessor($this->get('exportMode'), NULL, $this->get('queryOperator'), $this->get('mergeSameHousehold'), $isPostalOnly, $this->get('mergeSameAddress'));
155 // dev/core#1775 Remove notes,groups and tags from the preview data to speed up export
156 $defaultProperties = $processor->getDefaultReturnProperties();
157 foreach ($defaultProperties as $key => $var) {
158 if (in_array($key, ['groups', 'tags', 'notes'])) {
159 unset($defaultProperties[$key]);
160 }
161 }
162 $processor->setReturnProperties($defaultProperties);
163 $processor->setComponentTable($this->get('componentTable'));
164 $processor->setComponentClause($this->get('componentClause'));
165 $data = $processor->getPreview(4);
166 $ids = CRM_Utils_Array::collect('id', $data);
167 $data = array_pad($data, 4, []);
168
169 // Add location-type-specific data
170 if ($ids) {
171 foreach (['address', 'phone', 'email'] as $ent) {
172 foreach (civicrm_api3($ent, 'get', ['options' => ['limit' => 0], 'contact_id' => ['IN' => $ids]])['values'] as $loc) {
173 $row = array_search($loc['contact_id'], $ids);
174 $suffix = '_' . $loc['location_type_id'] . ($ent == 'phone' ? '_' . $loc['phone_type_id'] : '');
175 CRM_Utils_Array::remove($loc, 'id', 'contact_id', 'location_type_id', 'phone_type_id');
176 foreach ($loc as $name => $val) {
177 $data[$row][$name . $suffix] = $val;
178 }
179 }
180 }
181 }
182 return $data;
183 }
184
185 /**
186 * Return a descriptive name for the page, used in wizard header
187 *
188 * @return string
189 */
190 public function getTitle() {
191 return ts('Select Fields to Export');
192 }
193
194 }