Merge pull request #18772 from eileenmcnaughton/ids
[civicrm-core.git] / CRM / Import / Form / DataSource.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * @package CRM
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
15 */
16
17 /**
18 * Base class for upload-only import forms (all but Contact import).
19 */
20 abstract class CRM_Import_Form_DataSource extends CRM_Core_Form {
21
22 /**
23 * Set variables up before form is built.
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
34 CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE);
35 }
36
37 /**
38 * Common form elements.
39 */
40 public function buildQuickForm() {
41 $config = CRM_Core_Config::singleton();
42
43 $uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE);
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 }
49 $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
50
51 $this->assign('uploadSize', $uploadSize);
52
53 $this->add('File', 'uploadFile', ts('Import Data File'), NULL, TRUE);
54 $this->setMaxFileSize($uploadFileSize);
55 $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', [
56 1 => $uploadSize,
57 2 => $uploadFileSize,
58 ]), 'maxfilesize', $uploadFileSize);
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
64 $this->add('text', 'fieldSeparator', ts('Import Field Separator'), ['size' => 2], TRUE);
65 $this->setDefaults(['fieldSeparator' => $config->fieldSeparator]);
66 $mappingArray = CRM_Core_BAO_Mapping::getCreateMappingValues('Import ' . static::IMPORT_ENTITY);
67
68 $this->assign('savedMapping', $mappingArray);
69 $this->add('select', 'savedMapping', ts('Mapping Option'), ['' => ts('- select -')] + $mappingArray);
70
71 if ($loadedMapping = $this->get('loadedMapping')) {
72 $this->assign('loadedMapping', $loadedMapping);
73 $this->setDefaults(['savedMapping' => $loadedMapping]);
74 }
75
76 //build date formats
77 CRM_Core_Form_Date::buildAllowedDateFormats($this);
78
79 $this->addButtons([
80 [
81 'type' => 'upload',
82 'name' => ts('Continue'),
83 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
84 'isDefault' => TRUE,
85 ],
86 [
87 'type' => 'cancel',
88 'name' => ts('Cancel'),
89 ],
90 ]);
91 }
92
93 /**
94 * A long-winded way to add one radio element to the form.
95 */
96 protected function addContactTypeSelector() {
97 //contact types option
98 $contactTypeOptions = [];
99 if (CRM_Contact_BAO_ContactType::isActive('Individual')) {
100 $contactTypeOptions[CRM_Import_Parser::CONTACT_INDIVIDUAL] = ts('Individual');
101 }
102 if (CRM_Contact_BAO_ContactType::isActive('Household')) {
103 $contactTypeOptions[CRM_Import_Parser::CONTACT_HOUSEHOLD] = ts('Household');
104 }
105 if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
106 $contactTypeOptions[CRM_Import_Parser::CONTACT_ORGANIZATION] = ts('Organization');
107 }
108 $this->addRadio('contactType', ts('Contact Type'), $contactTypeOptions);
109
110 $this->setDefaults([
111 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
112 ]);
113 }
114
115 /**
116 * Store form values.
117 *
118 * @param array $names
119 */
120 protected function storeFormValues($names) {
121 foreach ($names as $name) {
122 $this->set($name, $this->controller->exportValue($this->_name, $name));
123 }
124 }
125
126 /**
127 * Common form postProcess.
128 *
129 * @param string $parserClassName
130 *
131 * @param string|null $entity
132 * Entity to set for paraser currently only for custom import
133 */
134 protected function submitFileForMapping($parserClassName, $entity = NULL) {
135 $this->controller->resetPage('MapField');
136
137 $fileName = $this->controller->exportValue($this->_name, 'uploadFile');
138 $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader');
139
140 $session = CRM_Core_Session::singleton();
141 $session->set("dateTypes", $this->get('dateFormats'));
142
143 $separator = $this->controller->exportValue($this->_name, 'fieldSeparator');
144
145 $mapper = [];
146
147 $parser = new $parserClassName($mapper);
148 if ($entity) {
149 $parser->setEntity($this->get($entity));
150 }
151 $parser->setMaxLinesToProcess(100);
152 $parser->run($fileName,
153 $separator,
154 $mapper,
155 $skipColumnHeader,
156 CRM_Import_Parser::MODE_MAPFIELD,
157 $this->get('contactType')
158 );
159
160 // add all the necessary variables to the form
161 $parser->set($this);
162 }
163
164 /**
165 * Return a descriptive name for the page, used in wizard header.
166 *
167 * @return string
168 */
169 public function getTitle() {
170 return ts('Upload Data');
171 }
172
173 }