Merge pull request #23172 from eileenmcnaughton/import_most
[civicrm-core.git] / CRM / Import / Form / DataSource.php
CommitLineData
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 20abstract 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);
81c3812a
CW
35 }
36
37 /**
38 * Common form elements.
81c3812a
CW
39 */
40 public function buildQuickForm() {
41 $config = CRM_Core_Config::singleton();
42
2e966dd5 43 $uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE);
ebcf0a88
JP
44
45 //Fetch uploadFileSize from php_ini when $config->maxFileSize is set to "no limit".
46 if (empty($uploadFileSize)) {
47 $uploadFileSize = CRM_Utils_Number::formatUnitSize(ini_get('upload_max_filesize'), TRUE);
48 }
81c3812a
CW
49 $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
50
51 $this->assign('uploadSize', $uploadSize);
52
8307337b 53 $this->add('File', 'uploadFile', ts('Import Data File'), NULL, TRUE);
81c3812a 54 $this->setMaxFileSize($uploadFileSize);
be2fb01f 55 $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', [
81c3812a
CW
56 1 => $uploadSize,
57 2 => $uploadFileSize,
be2fb01f 58 ]), 'maxfilesize', $uploadFileSize);
81c3812a
CW
59 $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
60 $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
61
62 $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
63
be2fb01f
CW
64 $this->add('text', 'fieldSeparator', ts('Import Field Separator'), ['size' => 2], TRUE);
65 $this->setDefaults(['fieldSeparator' => $config->fieldSeparator]);
412585fb 66 $mappingArray = CRM_Core_BAO_Mapping::getCreateMappingValues('Import ' . static::IMPORT_ENTITY);
19a68bae 67
81c3812a 68 $this->assign('savedMapping', $mappingArray);
5658157f 69 $this->add('select', 'savedMapping', ts('Saved Field Mapping'), ['' => ts('- select -')] + $mappingArray);
81c3812a
CW
70
71 if ($loadedMapping = $this->get('loadedMapping')) {
be2fb01f 72 $this->setDefaults(['savedMapping' => $loadedMapping]);
81c3812a
CW
73 }
74
75 //build date formats
76 CRM_Core_Form_Date::buildAllowedDateFormats($this);
77
be2fb01f
CW
78 $this->addButtons([
79 [
81c3812a
CW
80 'type' => 'upload',
81 'name' => ts('Continue'),
82 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
83 'isDefault' => TRUE,
be2fb01f
CW
84 ],
85 [
81c3812a
CW
86 'type' => 'cancel',
87 'name' => ts('Cancel'),
be2fb01f 88 ],
971e129b 89 ]);
81c3812a
CW
90 }
91
92 /**
2b4bc760 93 * A long-winded way to add one radio element to the form.
81c3812a
CW
94 */
95 protected function addContactTypeSelector() {
96 //contact types option
39405208 97 $contactTypeOptions = [];
81c3812a 98 if (CRM_Contact_BAO_ContactType::isActive('Individual')) {
39405208 99 $contactTypeOptions[CRM_Import_Parser::CONTACT_INDIVIDUAL] = ts('Individual');
81c3812a
CW
100 }
101 if (CRM_Contact_BAO_ContactType::isActive('Household')) {
39405208 102 $contactTypeOptions[CRM_Import_Parser::CONTACT_HOUSEHOLD] = ts('Household');
81c3812a
CW
103 }
104 if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
39405208 105 $contactTypeOptions[CRM_Import_Parser::CONTACT_ORGANIZATION] = ts('Organization');
81c3812a 106 }
39405208 107 $this->addRadio('contactType', ts('Contact Type'), $contactTypeOptions);
81c3812a 108
be2fb01f 109 $this->setDefaults([
81c3812a 110 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
be2fb01f 111 ]);
81c3812a
CW
112 }
113
114 /**
2b4bc760 115 * Store form values.
116 *
81c3812a
CW
117 * @param array $names
118 */
119 protected function storeFormValues($names) {
120 foreach ($names as $name) {
121 $this->set($name, $this->controller->exportValue($this->_name, $name));
122 }
123 }
124
125 /**
2b4bc760 126 * Common form postProcess.
81c3812a
CW
127 *
128 * @param string $parserClassName
cef29526
SL
129 *
130 * @param string|null $entity
131 * Entity to set for paraser currently only for custom import
81c3812a 132 */
cef29526 133 protected function submitFileForMapping($parserClassName, $entity = NULL) {
81c3812a 134 $this->controller->resetPage('MapField');
5003f9ab 135 CRM_Core_Session::singleton()->set('dateTypes', $this->getSubmittedValue('dateFormats'));
81c3812a 136
be2fb01f 137 $mapper = [];
81c3812a
CW
138
139 $parser = new $parserClassName($mapper);
cef29526
SL
140 if ($entity) {
141 $parser->setEntity($this->get($entity));
142 }
81c3812a 143 $parser->setMaxLinesToProcess(100);
5e8faabc
EM
144 $parser->run(
145 $this->getSubmittedValue('uploadFile'),
146 $this->getSubmittedValue('fieldSeparator'),
6d283ebd 147 [],
5e8faabc 148 $this->getSubmittedValue('skipColumnHeader'),
81c3812a 149 CRM_Import_Parser::MODE_MAPFIELD,
5003f9ab 150 $this->getSubmittedValue('contactType')
81c3812a
CW
151 );
152
153 // add all the necessary variables to the form
154 $parser->set($this);
155 }
156
157 /**
2b4bc760 158 * Return a descriptive name for the page, used in wizard header.
81c3812a
CW
159 *
160 * @return string
161 */
162 public function getTitle() {
163 return ts('Upload Data');
164 }
165
166}