Commit | Line | Data |
---|---|---|
81c3812a CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
81c3812a | 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 | | |
81c3812a CW |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
81c3812a | 13 | * @package CRM |
ca5cec67 | 14 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
81c3812a CW |
15 | */ |
16 | ||
17 | /** | |
2b4bc760 | 18 | * Base class for upload-only import forms (all but Contact import). |
81c3812a | 19 | */ |
5e8faabc | 20 | abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms { |
81c3812a CW |
21 | |
22 | /** | |
23 | * Set variables up before form is built. | |
81c3812a CW |
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 | |
2e966dd5 | 34 | CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE); |
4009ea99 EM |
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 { | |
20ad3ea3 | 47 | return (string) Civi\Api4\Utils\CoreUtil::getInfoItem($this::IMPORT_ENTITY, 'title'); |
4009ea99 EM |
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 { | |
20ad3ea3 | 58 | return (string) Civi\Api4\Utils\CoreUtil::getInfoItem($this::IMPORT_ENTITY, 'title_plural'); |
81c3812a CW |
59 | } |
60 | ||
61 | /** | |
62 | * Common form elements. | |
81c3812a CW |
63 | */ |
64 | public function buildQuickForm() { | |
65 | $config = CRM_Core_Config::singleton(); | |
66 | ||
2e966dd5 | 67 | $uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE); |
ebcf0a88 JP |
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 | } | |
81c3812a CW |
73 | $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2); |
74 | ||
75 | $this->assign('uploadSize', $uploadSize); | |
76 | ||
8307337b | 77 | $this->add('File', 'uploadFile', ts('Import Data File'), NULL, TRUE); |
81c3812a | 78 | $this->setMaxFileSize($uploadFileSize); |
be2fb01f | 79 | $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', [ |
81c3812a CW |
80 | 1 => $uploadSize, |
81 | 2 => $uploadFileSize, | |
be2fb01f | 82 | ]), 'maxfilesize', $uploadFileSize); |
81c3812a CW |
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 | ||
be2fb01f CW |
88 | $this->add('text', 'fieldSeparator', ts('Import Field Separator'), ['size' => 2], TRUE); |
89 | $this->setDefaults(['fieldSeparator' => $config->fieldSeparator]); | |
412585fb | 90 | $mappingArray = CRM_Core_BAO_Mapping::getCreateMappingValues('Import ' . static::IMPORT_ENTITY); |
19a68bae | 91 | |
81c3812a | 92 | $this->assign('savedMapping', $mappingArray); |
5658157f | 93 | $this->add('select', 'savedMapping', ts('Saved Field Mapping'), ['' => ts('- select -')] + $mappingArray); |
81c3812a CW |
94 | |
95 | if ($loadedMapping = $this->get('loadedMapping')) { | |
be2fb01f | 96 | $this->setDefaults(['savedMapping' => $loadedMapping]); |
81c3812a CW |
97 | } |
98 | ||
99 | //build date formats | |
100 | CRM_Core_Form_Date::buildAllowedDateFormats($this); | |
101 | ||
be2fb01f CW |
102 | $this->addButtons([ |
103 | [ | |
81c3812a CW |
104 | 'type' => 'upload', |
105 | 'name' => ts('Continue'), | |
106 | 'spacing' => ' ', | |
107 | 'isDefault' => TRUE, | |
be2fb01f CW |
108 | ], |
109 | [ | |
81c3812a CW |
110 | 'type' => 'cancel', |
111 | 'name' => ts('Cancel'), | |
be2fb01f | 112 | ], |
971e129b | 113 | ]); |
81c3812a CW |
114 | } |
115 | ||
116 | /** | |
2b4bc760 | 117 | * A long-winded way to add one radio element to the form. |
81c3812a CW |
118 | */ |
119 | protected function addContactTypeSelector() { | |
120 | //contact types option | |
39405208 | 121 | $contactTypeOptions = []; |
81c3812a | 122 | if (CRM_Contact_BAO_ContactType::isActive('Individual')) { |
39405208 | 123 | $contactTypeOptions[CRM_Import_Parser::CONTACT_INDIVIDUAL] = ts('Individual'); |
81c3812a CW |
124 | } |
125 | if (CRM_Contact_BAO_ContactType::isActive('Household')) { | |
39405208 | 126 | $contactTypeOptions[CRM_Import_Parser::CONTACT_HOUSEHOLD] = ts('Household'); |
81c3812a CW |
127 | } |
128 | if (CRM_Contact_BAO_ContactType::isActive('Organization')) { | |
39405208 | 129 | $contactTypeOptions[CRM_Import_Parser::CONTACT_ORGANIZATION] = ts('Organization'); |
81c3812a | 130 | } |
39405208 | 131 | $this->addRadio('contactType', ts('Contact Type'), $contactTypeOptions); |
81c3812a | 132 | |
be2fb01f | 133 | $this->setDefaults([ |
81c3812a | 134 | 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL, |
be2fb01f | 135 | ]); |
81c3812a CW |
136 | } |
137 | ||
138 | /** | |
2b4bc760 | 139 | * Store form values. |
140 | * | |
81c3812a CW |
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 | /** | |
2b4bc760 | 150 | * Common form postProcess. |
81c3812a CW |
151 | * |
152 | * @param string $parserClassName | |
cef29526 SL |
153 | * |
154 | * @param string|null $entity | |
155 | * Entity to set for paraser currently only for custom import | |
81c3812a | 156 | */ |
cef29526 | 157 | protected function submitFileForMapping($parserClassName, $entity = NULL) { |
81c3812a | 158 | $this->controller->resetPage('MapField'); |
5003f9ab | 159 | CRM_Core_Session::singleton()->set('dateTypes', $this->getSubmittedValue('dateFormats')); |
81c3812a | 160 | |
be2fb01f | 161 | $mapper = []; |
81c3812a CW |
162 | |
163 | $parser = new $parserClassName($mapper); | |
cef29526 SL |
164 | if ($entity) { |
165 | $parser->setEntity($this->get($entity)); | |
166 | } | |
81c3812a | 167 | $parser->setMaxLinesToProcess(100); |
5e8faabc EM |
168 | $parser->run( |
169 | $this->getSubmittedValue('uploadFile'), | |
170 | $this->getSubmittedValue('fieldSeparator'), | |
6d283ebd | 171 | [], |
5e8faabc | 172 | $this->getSubmittedValue('skipColumnHeader'), |
81c3812a | 173 | CRM_Import_Parser::MODE_MAPFIELD, |
5003f9ab | 174 | $this->getSubmittedValue('contactType') |
81c3812a CW |
175 | ); |
176 | ||
177 | // add all the necessary variables to the form | |
178 | $parser->set($this); | |
179 | } | |
180 | ||
181 | /** | |
2b4bc760 | 182 | * Return a descriptive name for the page, used in wizard header. |
81c3812a CW |
183 | * |
184 | * @return string | |
185 | */ | |
186 | public function getTitle() { | |
187 | return ts('Upload Data'); | |
188 | } | |
189 | ||
190 | } |