[REF] Remove handling for non-existent 'savedMapping' field
[civicrm-core.git] / CRM / Import / Form / DataSource.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 * @package CRM
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
15 */
16
17 /**
18 * Base class for upload-only import forms (all but Contact import).
19 */
20 abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms {
21
22 /**
23 * Set variables up before form is built.
24 */
25 public function preProcess() {
26 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
27 $params = "reset=1";
28 if ($this->_id) {
29 $params .= "&id={$this->_id}";
30 }
31 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url(static::PATH, $params));
32
33 // check for post max size
34 CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE);
35 $this->assign('importEntity', $this->getTranslatedEntity());
36 $this->assign('importEntities', $this->getTranslatedEntities());
37 }
38
39 /**
40 * Get the import entity (translated).
41 *
42 * Used for template layer text.
43 *
44 * @return string
45 */
46 protected function getTranslatedEntity(): string {
47 return (string) Civi\Api4\Utils\CoreUtil::getInfoItem($this::IMPORT_ENTITY, 'title');
48 }
49
50 /**
51 * Get the import entity plural (translated).
52 *
53 * Used for template layer text.
54 *
55 * @return string
56 */
57 protected function getTranslatedEntities(): string {
58 return (string) Civi\Api4\Utils\CoreUtil::getInfoItem($this::IMPORT_ENTITY, 'title_plural');
59 }
60
61 /**
62 * Common form elements.
63 */
64 public function buildQuickForm() {
65 $config = CRM_Core_Config::singleton();
66
67 $uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE);
68
69 //Fetch uploadFileSize from php_ini when $config->maxFileSize is set to "no limit".
70 if (empty($uploadFileSize)) {
71 $uploadFileSize = CRM_Utils_Number::formatUnitSize(ini_get('upload_max_filesize'), TRUE);
72 }
73 $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
74
75 $this->assign('uploadSize', $uploadSize);
76
77 $this->add('File', 'uploadFile', ts('Import Data File'), NULL, TRUE);
78 $this->setMaxFileSize($uploadFileSize);
79 $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', [
80 1 => $uploadSize,
81 2 => $uploadFileSize,
82 ]), 'maxfilesize', $uploadFileSize);
83 $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
84 $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
85
86 $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
87
88 $this->add('text', 'fieldSeparator', ts('Import Field Separator'), ['size' => 2], TRUE);
89 $this->setDefaults(['fieldSeparator' => $config->fieldSeparator]);
90 $mappingArray = CRM_Core_BAO_Mapping::getCreateMappingValues('Import ' . static::IMPORT_ENTITY);
91
92 $this->assign('savedMapping', $mappingArray);
93 $this->add('select', 'savedMapping', ts('Saved Field Mapping'), ['' => ts('- select -')] + $mappingArray);
94
95 if ($loadedMapping = $this->get('loadedMapping')) {
96 $this->setDefaults(['savedMapping' => $loadedMapping]);
97 }
98
99 //build date formats
100 CRM_Core_Form_Date::buildAllowedDateFormats($this);
101
102 $this->addButtons([
103 [
104 'type' => 'upload',
105 'name' => ts('Continue'),
106 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
107 'isDefault' => TRUE,
108 ],
109 [
110 'type' => 'cancel',
111 'name' => ts('Cancel'),
112 ],
113 ]);
114 }
115
116 /**
117 * A long-winded way to add one radio element to the form.
118 */
119 protected function addContactTypeSelector() {
120 //contact types option
121 $contactTypeOptions = [];
122 if (CRM_Contact_BAO_ContactType::isActive('Individual')) {
123 $contactTypeOptions[CRM_Import_Parser::CONTACT_INDIVIDUAL] = ts('Individual');
124 }
125 if (CRM_Contact_BAO_ContactType::isActive('Household')) {
126 $contactTypeOptions[CRM_Import_Parser::CONTACT_HOUSEHOLD] = ts('Household');
127 }
128 if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
129 $contactTypeOptions[CRM_Import_Parser::CONTACT_ORGANIZATION] = ts('Organization');
130 }
131 $this->addRadio('contactType', ts('Contact Type'), $contactTypeOptions);
132
133 $this->setDefaults([
134 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
135 ]);
136 }
137
138 /**
139 * Store form values.
140 *
141 * @param array $names
142 */
143 protected function storeFormValues($names) {
144 foreach ($names as $name) {
145 $this->set($name, $this->controller->exportValue($this->_name, $name));
146 }
147 }
148
149 /**
150 * Common form postProcess.
151 *
152 * @param string $parserClassName
153 *
154 * @param string|null $entity
155 * Entity to set for paraser currently only for custom import
156 */
157 protected function submitFileForMapping($parserClassName, $entity = NULL) {
158 $this->controller->resetPage('MapField');
159 CRM_Core_Session::singleton()->set('dateTypes', $this->getSubmittedValue('dateFormats'));
160 if (!$this->getUserJobID()) {
161 $this->createUserJob();
162 }
163 else {
164 $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues());
165 }
166
167 $mapper = [];
168
169 $parser = new $parserClassName($mapper);
170 if ($entity) {
171 $parser->setEntity($this->get($entity));
172 }
173 $parser->setMaxLinesToProcess(100);
174 $parser->setUserJobID($this->getUserJobID());
175 $parser->run(
176 $this->getSubmittedValue('uploadFile'),
177 $this->getSubmittedValue('fieldSeparator'),
178 [],
179 $this->getSubmittedValue('skipColumnHeader'),
180 CRM_Import_Parser::MODE_MAPFIELD,
181 $this->getSubmittedValue('contactType')
182 );
183
184 // add all the necessary variables to the form
185 $parser->set($this);
186 }
187
188 /**
189 * Return a descriptive name for the page, used in wizard header.
190 *
191 * @return string
192 */
193 public function getTitle() {
194 return ts('Upload Data');
195 }
196
197 }