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();
67 $uploadFileSize = CRM_Utils_Number
::formatUnitSize($config->maxFileSize
. 'm', TRUE);
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);
73 $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
75 $this->assign('uploadSize', $uploadSize);
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)', [
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');
86 $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
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
);
92 $this->assign('savedMapping', $mappingArray);
93 $this->add('select', 'savedMapping', ts('Saved Field Mapping'), ['' => ts('- select -')] +
$mappingArray);
95 if ($loadedMapping = $this->get('loadedMapping')) {
96 $this->setDefaults(['savedMapping' => $loadedMapping]);
100 CRM_Core_Form_Date
::buildAllowedDateFormats($this);
105 'name' => ts('Continue'),
106 'spacing' => ' ',
111 'name' => ts('Cancel'),
117 * A long-winded way to add one radio element to the form.
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');
125 if (CRM_Contact_BAO_ContactType
::isActive('Household')) {
126 $contactTypeOptions[CRM_Import_Parser
::CONTACT_HOUSEHOLD
] = ts('Household');
128 if (CRM_Contact_BAO_ContactType
::isActive('Organization')) {
129 $contactTypeOptions[CRM_Import_Parser
::CONTACT_ORGANIZATION
] = ts('Organization');
131 $this->addRadio('contactType', ts('Contact Type'), $contactTypeOptions);
134 'contactType' => CRM_Import_Parser
::CONTACT_INDIVIDUAL
,
141 * @param array $names
143 protected function storeFormValues($names) {
144 foreach ($names as $name) {
145 $this->set($name, $this->controller
->exportValue($this->_name
, $name));
150 * Common form postProcess.
152 * @param string $parserClassName
154 * @param string|null $entity
155 * Entity to set for paraser currently only for custom import
157 protected function submitFileForMapping($parserClassName, $entity = NULL) {
158 $this->controller
->resetPage('MapField');
159 CRM_Core_Session
::singleton()->set('dateTypes', $this->getSubmittedValue('dateFormats'));
163 $parser = new $parserClassName($mapper);
165 $parser->setEntity($this->get($entity));
167 $parser->setMaxLinesToProcess(100);
169 $this->getSubmittedValue('uploadFile'),
170 $this->getSubmittedValue('fieldSeparator'),
172 $this->getSubmittedValue('skipColumnHeader'),
173 CRM_Import_Parser
::MODE_MAPFIELD
,
174 $this->getSubmittedValue('contactType')
177 // add all the necessary variables to the form
182 * Return a descriptive name for the page, used in wizard header.
186 public function getTitle() {
187 return ts('Upload Data');