Merge pull request #23258 from civicrm/5.49
[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 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 Civi\Api4\Utils\CoreUtil::getInfoItem($this::IMPORT_ENTITY, 'title_plural');
59
60 }
61
62 /**
63 * Common form elements.
64 */
65 public function buildQuickForm() {
66 $config = CRM_Core_Config::singleton();
67
68 $uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE);
69
70 //Fetch uploadFileSize from php_ini when $config->maxFileSize is set to "no limit".
71 if (empty($uploadFileSize)) {
72 $uploadFileSize = CRM_Utils_Number::formatUnitSize(ini_get('upload_max_filesize'), TRUE);
73 }
74 $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
75
76 $this->assign('uploadSize', $uploadSize);
77
78 $this->add('File', 'uploadFile', ts('Import Data File'), NULL, TRUE);
79 $this->setMaxFileSize($uploadFileSize);
80 $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', [
81 1 => $uploadSize,
82 2 => $uploadFileSize,
83 ]), 'maxfilesize', $uploadFileSize);
84 $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
85 $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
86
87 $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
88
89 $this->add('text', 'fieldSeparator', ts('Import Field Separator'), ['size' => 2], TRUE);
90 $this->setDefaults(['fieldSeparator' => $config->fieldSeparator]);
91 $mappingArray = CRM_Core_BAO_Mapping::getCreateMappingValues('Import ' . static::IMPORT_ENTITY);
92
93 $this->assign('savedMapping', $mappingArray);
94 $this->add('select', 'savedMapping', ts('Saved Field Mapping'), ['' => ts('- select -')] + $mappingArray);
95
96 if ($loadedMapping = $this->get('loadedMapping')) {
97 $this->setDefaults(['savedMapping' => $loadedMapping]);
98 }
99
100 //build date formats
101 CRM_Core_Form_Date::buildAllowedDateFormats($this);
102
103 $this->addButtons([
104 [
105 'type' => 'upload',
106 'name' => ts('Continue'),
107 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
108 'isDefault' => TRUE,
109 ],
110 [
111 'type' => 'cancel',
112 'name' => ts('Cancel'),
113 ],
114 ]);
115 }
116
117 /**
118 * A long-winded way to add one radio element to the form.
119 */
120 protected function addContactTypeSelector() {
121 //contact types option
122 $contactTypeOptions = [];
123 if (CRM_Contact_BAO_ContactType::isActive('Individual')) {
124 $contactTypeOptions[CRM_Import_Parser::CONTACT_INDIVIDUAL] = ts('Individual');
125 }
126 if (CRM_Contact_BAO_ContactType::isActive('Household')) {
127 $contactTypeOptions[CRM_Import_Parser::CONTACT_HOUSEHOLD] = ts('Household');
128 }
129 if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
130 $contactTypeOptions[CRM_Import_Parser::CONTACT_ORGANIZATION] = ts('Organization');
131 }
132 $this->addRadio('contactType', ts('Contact Type'), $contactTypeOptions);
133
134 $this->setDefaults([
135 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
136 ]);
137 }
138
139 /**
140 * Store form values.
141 *
142 * @param array $names
143 */
144 protected function storeFormValues($names) {
145 foreach ($names as $name) {
146 $this->set($name, $this->controller->exportValue($this->_name, $name));
147 }
148 }
149
150 /**
151 * Common form postProcess.
152 *
153 * @param string $parserClassName
154 *
155 * @param string|null $entity
156 * Entity to set for paraser currently only for custom import
157 */
158 protected function submitFileForMapping($parserClassName, $entity = NULL) {
159 $this->controller->resetPage('MapField');
160 CRM_Core_Session::singleton()->set('dateTypes', $this->getSubmittedValue('dateFormats'));
161
162 $mapper = [];
163
164 $parser = new $parserClassName($mapper);
165 if ($entity) {
166 $parser->setEntity($this->get($entity));
167 }
168 $parser->setMaxLinesToProcess(100);
169 $parser->run(
170 $this->getSubmittedValue('uploadFile'),
171 $this->getSubmittedValue('fieldSeparator'),
172 [],
173 $this->getSubmittedValue('skipColumnHeader'),
174 CRM_Import_Parser::MODE_MAPFIELD,
175 $this->getSubmittedValue('contactType')
176 );
177
178 // add all the necessary variables to the form
179 $parser->set($this);
180 }
181
182 /**
183 * Return a descriptive name for the page, used in wizard header.
184 *
185 * @return string
186 */
187 public function getTitle() {
188 return ts('Upload Data');
189 }
190
191 }