Convert remaining Authorize.net test to use guzzle
[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 * $Id$
17 *
18 */
19
20 /**
21 * This class gets the name of the file to upload
22 */
23 class CRM_Export_Form_Map extends CRM_Core_Form {
24
25 /**
26 * Loaded mapping ID
27 *
28 * @var int
29 */
30 protected $_mappingId;
31
32 /**
33 * Build the form object.
34 */
35 public function preProcess() {
36 $this->_mappingId = $this->get('mappingId');
37
38 $contactTypes = array_column(CRM_Utils_Array::makeNonAssociative(CRM_Contact_BAO_ContactType::basicTypePairs(), 'id', 'text'), NULL, 'id');
39 foreach (CRM_Contact_BAO_ContactType::subTypeInfo() as $subType) {
40 $contactTypes[$subType['parent']]['children'][] = [
41 'id' => $subType['name'],
42 'text' => $subType['label'],
43 'description' => $subType['description'] ?? NULL,
44 ];
45 }
46 $mappingTypeId = $this->get('mappingTypeId');
47 $mappings = civicrm_api3('Mapping', 'get', ['return' => ['name', 'description'], 'mapping_type_id' => $mappingTypeId, 'options' => ['limit' => 0]]);
48
49 Civi::resources()->addVars('exportUi', [
50 'fields' => CRM_Export_Utils::getExportFields($this->get('exportMode')),
51 'contact_types' => array_values($contactTypes),
52 'location_type_id' => CRM_Utils_Array::makeNonAssociative(CRM_Core_BAO_Address::buildOptions('location_type_id'), 'id', 'text'),
53 'preview_data' => $this->getPreviewData(),
54 'mapping_id' => $this->_mappingId,
55 'mapping_description' => $mappings['values'][$this->_mappingId]['description'] ?? '',
56 'mapping_type_id' => $mappingTypeId,
57 'mapping_names' => CRM_Utils_Array::collect('name', $mappings['values']),
58 'option_list' => [
59 'phone_type_id' => CRM_Utils_Array::makeNonAssociative(CRM_Core_BAO_Phone::buildOptions('phone_type_id'), 'id', 'text'),
60 'website_type_id' => CRM_Utils_Array::makeNonAssociative(CRM_Core_BAO_Website::buildOptions('website_type_id'), 'id', 'text'),
61 'im_provider_id' => CRM_Utils_Array::makeNonAssociative(CRM_Core_BAO_IM::buildOptions('provider_id'), 'id', 'text'),
62 ],
63 ]);
64
65 // Bootstrap angular and load exportui app
66 $loader = new Civi\Angular\AngularLoader();
67 $loader->setModules(['exportui']);
68 $loader->load();
69 }
70
71 public function buildQuickForm() {
72 $this->add('hidden', 'export_field_map');
73
74 $this->addButtons([
75 [
76 'type' => 'back',
77 'name' => ts('Previous'),
78 ],
79 [
80 'type' => 'done',
81 'icon' => 'fa-times',
82 'name' => ts('Return to Search'),
83 ],
84 [
85 'type' => 'next',
86 'icon' => 'fa-download',
87 'name' => ts('Download File'),
88 ],
89 ]);
90 }
91
92 public function setDefaultValues() {
93 $defaults = [];
94 if ($this->_mappingId) {
95 $mappingFields = civicrm_api3('mappingField', 'get', ['mapping_id' => $this->_mappingId, 'options' => ['limit' => 0, 'sort' => 'column_number']]);
96 $defaults['export_field_map'] = json_encode(array_values($mappingFields['values']));
97 }
98 return $defaults;
99 }
100
101 /**
102 * Process the form submission.
103 */
104 public function postProcess() {
105 $params = $this->controller->exportValues($this->_name);
106 $exportParams = $this->controller->exportValues('Select');
107
108 // Redirect back to search if "done" button pressed
109 if ($this->controller->getButtonName('done') == '_qf_Map_done') {
110 $currentPath = CRM_Utils_System::currentPath();
111 $urlParams = NULL;
112 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
113 if (CRM_Utils_Rule::qfKey($qfKey)) {
114 $urlParams = "&qfKey=$qfKey";
115 }
116 $this->controller->resetPage($this->_name);
117 return CRM_Utils_System::redirect(CRM_Utils_System::url($currentPath, 'force=1' . $urlParams));
118 }
119
120 $selectedFields = json_decode($params['export_field_map'], TRUE);
121
122 //get the csv file
123 CRM_Export_BAO_Export::exportComponents($this->get('selectAll'),
124 $this->get('componentIds'),
125 (array) $this->get('queryParams'),
126 $this->get(CRM_Utils_Sort::SORT_ORDER),
127 $selectedFields,
128 $this->get('returnProperties'),
129 $this->get('exportMode'),
130 $this->get('componentClause'),
131 $this->get('componentTable'),
132 $this->get('mergeSameAddress'),
133 $this->get('mergeSameHousehold'),
134 $exportParams,
135 $this->get('queryOperator')
136 );
137 }
138
139 /**
140 * @return array
141 */
142 protected function getPreviewData() {
143 $exportParams = $this->controller->exportValues('Select');
144 $isPostalOnly = (
145 isset($exportParams['postal_mailing_export']['postal_mailing_export']) &&
146 $exportParams['postal_mailing_export']['postal_mailing_export'] == 1
147 );
148 $processor = new CRM_Export_BAO_ExportProcessor($this->get('exportMode'), NULL, $this->get('queryOperator'), $this->get('mergeSameHousehold'), $isPostalOnly, $this->get('mergeSameAddress'));
149 $processor->setComponentTable($this->get('componentTable'));
150 $processor->setComponentClause($this->get('componentClause'));
151 $data = $processor->getPreview(4);
152 $ids = CRM_Utils_Array::collect('id', $data);
153 $data = array_pad($data, 4, []);
154
155 // Add location-type-specific data
156 if ($ids) {
157 foreach (['address', 'phone', 'email'] as $ent) {
158 foreach (civicrm_api3($ent, 'get', ['options' => ['limit' => 0], 'contact_id' => ['IN' => $ids]])['values'] as $loc) {
159 $row = array_search($loc['contact_id'], $ids);
160 $suffix = '_' . $loc['location_type_id'] . ($ent == 'phone' ? '_' . $loc['phone_type_id'] : '');
161 CRM_Utils_Array::remove($loc, 'id', 'contact_id', 'location_type_id', 'phone_type_id');
162 foreach ($loc as $name => $val) {
163 $data[$row][$name . $suffix] = $val;
164 }
165 }
166 }
167 }
168 return $data;
169 }
170
171 /**
172 * Return a descriptive name for the page, used in wizard header
173 *
174 * @return string
175 */
176 public function getTitle() {
177 return ts('Select Fields to Export');
178 }
179
180 }