3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class gets the name of the file to upload
21 class CRM_Export_Form_Map
extends CRM_Core_Form
{
28 protected $_mappingId;
31 * Use the form name to create the tpl file name.
35 public function getTemplateFileName() {
36 return 'CRM/Export/Form/Map.tpl';
40 * Build the form object.
42 public function preProcess() {
43 $this->_mappingId
= $this->get('mappingId');
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,
53 $mappingTypeId = $this->get('mappingTypeId');
54 $mappings = civicrm_api3('Mapping', 'get', ['return' => ['name', 'description'], 'mapping_type_id' => $mappingTypeId, 'options' => ['limit' => 0]]);
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']),
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'),
73 Civi
::service('angularjs.loader')
74 ->addModules('exportui');
77 public function buildQuickForm() {
78 $this->add('hidden', 'export_field_map');
83 'name' => ts('Previous'),
88 'name' => ts('Return to Search'),
92 'icon' => 'fa-download',
93 'name' => ts('Download File'),
98 public function setDefaultValues() {
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']));
108 * Process the form submission.
110 public function postProcess() {
111 $params = $this->controller
->exportValues($this->_name
);
112 $exportParams = $this->controller
->exportValues('Select');
114 // Redirect back to search if "done" button pressed
115 if ($this->controller
->getButtonName('done') == '_qf_Map_done') {
116 $currentPath = CRM_Utils_System
::currentPath();
118 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
119 if (CRM_Utils_Rule
::qfKey($qfKey)) {
120 $urlParams = "&qfKey=$qfKey";
122 $this->controller
->resetPage($this->_name
);
123 return CRM_Utils_System
::redirect(CRM_Utils_System
::url($currentPath, 'force=1' . $urlParams));
126 $selectedFields = json_decode($params['export_field_map'], TRUE);
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
),
134 $this->get('returnProperties'),
135 $this->get('exportMode'),
136 $this->get('componentClause'),
137 $this->get('componentTable'),
138 $this->get('mergeSameAddress'),
139 $this->get('mergeSameHousehold'),
141 $this->get('queryOperator')
148 protected function getPreviewData() {
149 $exportParams = $this->controller
->exportValues('Select');
151 isset($exportParams['postal_mailing_export']['postal_mailing_export']) &&
152 $exportParams['postal_mailing_export']['postal_mailing_export'] == 1
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]);
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, []);
169 // Add location-type-specific data
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;
186 * Return a descriptive name for the page, used in wizard header
190 public function getTitle() {
191 return ts('Select Fields to Export');