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
21 * This class gets the name of the file to upload
23 class CRM_Export_Form_Map
extends CRM_Core_Form
{
30 protected $_mappingId;
33 * Build the form object.
35 public function preProcess() {
36 $this->_mappingId
= $this->get('mappingId');
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,
46 $mappingTypeId = $this->get('mappingTypeId');
47 $mappings = civicrm_api3('Mapping', 'get', ['return' => ['name', 'description'], 'mapping_type_id' => $mappingTypeId, 'options' => ['limit' => 0]]);
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']),
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'),
65 // Bootstrap angular and load exportui app
66 $loader = new Civi\Angular\
AngularLoader();
67 $loader->setModules(['exportui']);
71 public function buildQuickForm() {
72 $this->add('hidden', 'export_field_map');
77 'name' => ts('Previous'),
82 'name' => ts('Return to Search'),
86 'icon' => 'fa-download',
87 'name' => ts('Download File'),
92 public function setDefaultValues() {
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']));
102 * Process the form submission.
104 public function postProcess() {
105 $params = $this->controller
->exportValues($this->_name
);
106 $exportParams = $this->controller
->exportValues('Select');
108 // Redirect back to search if "done" button pressed
109 if ($this->controller
->getButtonName('done') == '_qf_Map_done') {
110 $currentPath = CRM_Utils_System
::currentPath();
112 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
113 if (CRM_Utils_Rule
::qfKey($qfKey)) {
114 $urlParams = "&qfKey=$qfKey";
116 $this->controller
->resetPage($this->_name
);
117 return CRM_Utils_System
::redirect(CRM_Utils_System
::url($currentPath, 'force=1' . $urlParams));
120 $selectedFields = json_decode($params['export_field_map'], TRUE);
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
),
128 $this->get('returnProperties'),
129 $this->get('exportMode'),
130 $this->get('componentClause'),
131 $this->get('componentTable'),
132 $this->get('mergeSameAddress'),
133 $this->get('mergeSameHousehold'),
135 $this->get('queryOperator')
142 protected function getPreviewData() {
143 $exportParams = $this->controller
->exportValues('Select');
145 isset($exportParams['postal_mailing_export']['postal_mailing_export']) &&
146 $exportParams['postal_mailing_export']['postal_mailing_export'] == 1
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, []);
155 // Add location-type-specific data
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;
172 * Return a descriptive name for the page, used in wizard header
176 public function getTitle() {
177 return ts('Select Fields to Export');