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 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 * Base class for upload-only import forms (all but Contact import).
20 abstract class CRM_Import_Form_DataSource
extends CRM_Import_Forms
{
23 * Set variables up before form is built.
25 public function preProcess() {
26 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, FALSE);
29 $params .= "&id={$this->_id}";
31 CRM_Core_Session
::singleton()->pushUserContext(CRM_Utils_System
::url(static::PATH
, $params));
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());
40 * Get the import entity (translated).
42 * Used for template layer text.
46 protected function getTranslatedEntity(): string {
47 return (string) Civi\Api4\Utils\CoreUtil
::getInfoItem($this::IMPORT_ENTITY
, 'title');
51 * Get the import entity plural (translated).
53 * Used for template layer text.
57 protected function getTranslatedEntities(): string {
58 return (string) Civi\Api4\Utils\CoreUtil
::getInfoItem($this::IMPORT_ENTITY
, 'title_plural');
62 * Common form elements.
64 public function buildQuickForm() {
65 $config = CRM_Core_Config
::singleton();
66 // When we switch to using the DataSource.tpl used by Contact we can remove this in
67 // favour of the one used by Contact - I was trying to consolidate
68 // first & got stuck on https://github.com/civicrm/civicrm-core/pull/23458
69 $this->add('hidden', 'hidden_dataSource', 'CRM_Import_DataSource_CSV');
70 $uploadFileSize = CRM_Utils_Number
::formatUnitSize($config->maxFileSize
. 'm', TRUE);
72 //Fetch uploadFileSize from php_ini when $config->maxFileSize is set to "no limit".
73 if (empty($uploadFileSize)) {
74 $uploadFileSize = CRM_Utils_Number
::formatUnitSize(ini_get('upload_max_filesize'), TRUE);
76 $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
78 $this->assign('uploadSize', $uploadSize);
80 $this->add('File', 'uploadFile', ts('Import Data File'), NULL, TRUE);
81 $this->setMaxFileSize($uploadFileSize);
82 $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', [
85 ]), 'maxfilesize', $uploadFileSize);
86 $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
87 $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
89 $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
91 $this->add('text', 'fieldSeparator', ts('Import Field Separator'), ['size' => 2], TRUE);
92 $this->setDefaults(['fieldSeparator' => $config->fieldSeparator
]);
93 $mappingArray = CRM_Core_BAO_Mapping
::getCreateMappingValues('Import ' . static::IMPORT_ENTITY
);
95 $this->assign('savedMapping', $mappingArray);
96 $this->add('select', 'savedMapping', ts('Saved Field Mapping'), ['' => ts('- select -')] +
$mappingArray);
98 if ($loadedMapping = $this->get('loadedMapping')) {
99 $this->setDefaults(['savedMapping' => $loadedMapping]);
103 CRM_Core_Form_Date
::buildAllowedDateFormats($this);
108 'name' => ts('Continue'),
109 'spacing' => ' ',
114 'name' => ts('Cancel'),
120 * A long-winded way to add one radio element to the form.
122 protected function addContactTypeSelector() {
123 //contact types option
124 $contactTypeOptions = [];
125 if (CRM_Contact_BAO_ContactType
::isActive('Individual')) {
126 $contactTypeOptions[CRM_Import_Parser
::CONTACT_INDIVIDUAL
] = ts('Individual');
128 if (CRM_Contact_BAO_ContactType
::isActive('Household')) {
129 $contactTypeOptions[CRM_Import_Parser
::CONTACT_HOUSEHOLD
] = ts('Household');
131 if (CRM_Contact_BAO_ContactType
::isActive('Organization')) {
132 $contactTypeOptions[CRM_Import_Parser
::CONTACT_ORGANIZATION
] = ts('Organization');
134 $this->addRadio('contactType', ts('Contact Type'), $contactTypeOptions);
137 'contactType' => CRM_Import_Parser
::CONTACT_INDIVIDUAL
,
144 * @param array $names
146 protected function storeFormValues($names) {
147 foreach ($names as $name) {
148 $this->set($name, $this->controller
->exportValue($this->_name
, $name));
153 * Common form postProcess.
155 * @param string $parserClassName
157 * @param string|null $entity
158 * Entity to set for paraser currently only for custom import
160 protected function submitFileForMapping($parserClassName, $entity = NULL) {
161 CRM_Core_Session
::singleton()->set('dateTypes', $this->getSubmittedValue('dateFormats'));
162 $this->processDatasource();
166 $parser = new $parserClassName($mapper);
168 $parser->setEntity($this->get($entity));
170 $parser->setMaxLinesToProcess(100);
171 $parser->setUserJobID($this->getUserJobID());
173 $this->getSubmittedValue('uploadFile'),
174 $this->getSubmittedValue('fieldSeparator'),
176 $this->getSubmittedValue('skipColumnHeader'),
177 CRM_Import_Parser
::MODE_MAPFIELD
,
178 $this->getSubmittedValue('contactType')
181 // add all the necessary variables to the form
183 $this->controller
->resetPage('MapField');
187 * Return a descriptive name for the page, used in wizard header.
191 public function getTitle() {
192 return ts('Upload Data');
196 * Process the datasource submission - setting up the job and data source.
198 * @throws \API_Exception
199 * @throws \CRM_Core_Exception
201 protected function processDatasource(): void
{
202 if (!$this->getUserJobID()) {
203 $this->createUserJob();
206 $this->flushDataSource();
207 $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues());
209 $this->instantiateDataSource();
213 * Instantiate the datasource.
215 * This gives the datasource a chance to do any table creation etc.
217 * @throws \API_Exception
218 * @throws \CRM_Core_Exception
220 private function instantiateDataSource(): void
{
221 $this->getDataSourceObject()->initialize();