Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
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 | ||
6a488035 | 23 | /** |
100fef9d | 24 | * Loaded mapping ID |
6a488035 TO |
25 | * |
26 | * @var int | |
6a488035 TO |
27 | */ |
28 | protected $_mappingId; | |
29 | ||
e2710fee | 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 | ||
6a488035 | 39 | /** |
fe482240 | 40 | * Build the form object. |
6a488035 TO |
41 | */ |
42 | public function preProcess() { | |
eac832c5 CW |
43 | $this->_mappingId = $this->get('mappingId'); |
44 | ||
33044927 CW |
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 | ]; | |
6a488035 | 52 | } |
33044927 CW |
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 | ||
b1510da4 CW |
72 | // Add exportui app |
73 | Civi::service('angularjs.loader') | |
74 | ->addModules('exportui'); | |
6a488035 TO |
75 | } |
76 | ||
77 | public function buildQuickForm() { | |
33044927 | 78 | $this->add('hidden', 'export_field_map'); |
6a488035 | 79 | |
be2fb01f | 80 | $this->addButtons([ |
7b966967 SL |
81 | [ |
82 | 'type' => 'back', | |
83 | 'name' => ts('Previous'), | |
84 | ], | |
85 | [ | |
33044927 | 86 | 'type' => 'done', |
0016fc7d | 87 | 'icon' => 'fa-times', |
33044927 | 88 | 'name' => ts('Return to Search'), |
7b966967 SL |
89 | ], |
90 | [ | |
33044927 CW |
91 | 'type' => 'next', |
92 | 'icon' => 'fa-download', | |
93 | 'name' => ts('Download File'), | |
7b966967 SL |
94 | ], |
95 | ]); | |
6a488035 TO |
96 | } |
97 | ||
33044927 CW |
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'])); | |
6a488035 | 103 | } |
33044927 | 104 | return $defaults; |
6a488035 TO |
105 | } |
106 | ||
107 | /** | |
33044927 | 108 | * Process the form submission. |
6a488035 TO |
109 | */ |
110 | public function postProcess() { | |
111 | $params = $this->controller->exportValues($this->_name); | |
112 | $exportParams = $this->controller->exportValues('Select'); | |
113 | ||
33044927 CW |
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 | } | |
6a488035 TO |
122 | $this->controller->resetPage($this->_name); |
123 | return CRM_Utils_System::redirect(CRM_Utils_System::url($currentPath, 'force=1' . $urlParams)); | |
124 | } | |
125 | ||
33044927 | 126 | $selectedFields = json_decode($params['export_field_map'], TRUE); |
6a488035 TO |
127 | |
128 | //get the csv file | |
129 | CRM_Export_BAO_Export::exportComponents($this->get('selectAll'), | |
130 | $this->get('componentIds'), | |
78b22ce9 | 131 | (array) $this->get('queryParams'), |
6a488035 | 132 | $this->get(CRM_Utils_Sort::SORT_ORDER), |
33044927 | 133 | $selectedFields, |
6a488035 TO |
134 | $this->get('returnProperties'), |
135 | $this->get('exportMode'), | |
136 | $this->get('componentClause'), | |
137 | $this->get('componentTable'), | |
138 | $this->get('mergeSameAddress'), | |
139 | $this->get('mergeSameHousehold'), | |
9a8044dd | 140 | $exportParams, |
141 | $this->get('queryOperator') | |
6a488035 TO |
142 | ); |
143 | } | |
144 | ||
33044927 CW |
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')); | |
726c1918 SL |
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); | |
33044927 CW |
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 | ||
6a488035 TO |
185 | /** |
186 | * Return a descriptive name for the page, used in wizard header | |
187 | * | |
188 | * @return string | |
6a488035 TO |
189 | */ |
190 | public function getTitle() { | |
191 | return ts('Select Fields to Export'); | |
192 | } | |
96025800 | 193 | |
6a488035 | 194 | } |