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