Commit | Line | Data |
---|---|---|
81c3812a CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
81c3812a | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
81c3812a CW |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
81c3812a | 29 | * @package CRM |
6b83d5bd | 30 | * @copyright CiviCRM LLC (c) 2004-2019 |
81c3812a CW |
31 | */ |
32 | ||
33 | /** | |
2b4bc760 | 34 | * Base class for upload-only import forms (all but Contact import). |
81c3812a CW |
35 | */ |
36 | abstract class CRM_Import_Form_DataSource extends CRM_Core_Form { | |
37 | ||
38 | /** | |
39 | * Set variables up before form is built. | |
81c3812a CW |
40 | */ |
41 | public function preProcess() { | |
42 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE); | |
43 | $params = "reset=1"; | |
44 | if ($this->_id) { | |
45 | $params .= "&id={$this->_id}"; | |
46 | } | |
47 | CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url(static::PATH, $params)); | |
48 | ||
49 | // check for post max size | |
2e966dd5 | 50 | CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE); |
81c3812a CW |
51 | } |
52 | ||
53 | /** | |
54 | * Common form elements. | |
81c3812a CW |
55 | */ |
56 | public function buildQuickForm() { | |
57 | $config = CRM_Core_Config::singleton(); | |
58 | ||
2e966dd5 | 59 | $uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE); |
ebcf0a88 JP |
60 | |
61 | //Fetch uploadFileSize from php_ini when $config->maxFileSize is set to "no limit". | |
62 | if (empty($uploadFileSize)) { | |
63 | $uploadFileSize = CRM_Utils_Number::formatUnitSize(ini_get('upload_max_filesize'), TRUE); | |
64 | } | |
81c3812a CW |
65 | $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2); |
66 | ||
67 | $this->assign('uploadSize', $uploadSize); | |
68 | ||
69 | $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE); | |
70 | $this->setMaxFileSize($uploadFileSize); | |
be2fb01f | 71 | $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', [ |
81c3812a CW |
72 | 1 => $uploadSize, |
73 | 2 => $uploadFileSize, | |
be2fb01f | 74 | ]), 'maxfilesize', $uploadFileSize); |
81c3812a CW |
75 | $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile'); |
76 | $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File'); | |
77 | ||
78 | $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers')); | |
79 | ||
be2fb01f CW |
80 | $this->add('text', 'fieldSeparator', ts('Import Field Separator'), ['size' => 2], TRUE); |
81 | $this->setDefaults(['fieldSeparator' => $config->fieldSeparator]); | |
412585fb | 82 | $mappingArray = CRM_Core_BAO_Mapping::getCreateMappingValues('Import ' . static::IMPORT_ENTITY); |
19a68bae | 83 | |
81c3812a | 84 | $this->assign('savedMapping', $mappingArray); |
be2fb01f | 85 | $this->add('select', 'savedMapping', ts('Mapping Option'), ['' => ts('- select -')] + $mappingArray); |
81c3812a CW |
86 | |
87 | if ($loadedMapping = $this->get('loadedMapping')) { | |
88 | $this->assign('loadedMapping', $loadedMapping); | |
be2fb01f | 89 | $this->setDefaults(['savedMapping' => $loadedMapping]); |
81c3812a CW |
90 | } |
91 | ||
92 | //build date formats | |
93 | CRM_Core_Form_Date::buildAllowedDateFormats($this); | |
94 | ||
be2fb01f CW |
95 | $this->addButtons([ |
96 | [ | |
81c3812a CW |
97 | 'type' => 'upload', |
98 | 'name' => ts('Continue'), | |
99 | 'spacing' => ' ', | |
100 | 'isDefault' => TRUE, | |
be2fb01f CW |
101 | ], |
102 | [ | |
81c3812a CW |
103 | 'type' => 'cancel', |
104 | 'name' => ts('Cancel'), | |
be2fb01f | 105 | ], |
971e129b | 106 | ]); |
81c3812a CW |
107 | } |
108 | ||
109 | /** | |
2b4bc760 | 110 | * A long-winded way to add one radio element to the form. |
81c3812a CW |
111 | */ |
112 | protected function addContactTypeSelector() { | |
113 | //contact types option | |
be2fb01f | 114 | $contactOptions = []; |
81c3812a CW |
115 | if (CRM_Contact_BAO_ContactType::isActive('Individual')) { |
116 | $contactOptions[] = $this->createElement('radio', | |
117 | NULL, NULL, ts('Individual'), CRM_Import_Parser::CONTACT_INDIVIDUAL | |
118 | ); | |
119 | } | |
120 | if (CRM_Contact_BAO_ContactType::isActive('Household')) { | |
121 | $contactOptions[] = $this->createElement('radio', | |
122 | NULL, NULL, ts('Household'), CRM_Import_Parser::CONTACT_HOUSEHOLD | |
123 | ); | |
124 | } | |
125 | if (CRM_Contact_BAO_ContactType::isActive('Organization')) { | |
126 | $contactOptions[] = $this->createElement('radio', | |
127 | NULL, NULL, ts('Organization'), CRM_Import_Parser::CONTACT_ORGANIZATION | |
128 | ); | |
129 | } | |
130 | ||
131 | $this->addGroup($contactOptions, 'contactType', | |
132 | ts('Contact Type') | |
133 | ); | |
134 | ||
be2fb01f | 135 | $this->setDefaults([ |
81c3812a | 136 | 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL, |
be2fb01f | 137 | ]); |
81c3812a CW |
138 | } |
139 | ||
140 | /** | |
2b4bc760 | 141 | * Store form values. |
142 | * | |
81c3812a CW |
143 | * @param array $names |
144 | */ | |
145 | protected function storeFormValues($names) { | |
146 | foreach ($names as $name) { | |
147 | $this->set($name, $this->controller->exportValue($this->_name, $name)); | |
148 | } | |
149 | } | |
150 | ||
151 | /** | |
2b4bc760 | 152 | * Common form postProcess. |
81c3812a CW |
153 | * |
154 | * @param string $parserClassName | |
cef29526 SL |
155 | * |
156 | * @param string|null $entity | |
157 | * Entity to set for paraser currently only for custom import | |
81c3812a | 158 | */ |
cef29526 | 159 | protected function submitFileForMapping($parserClassName, $entity = NULL) { |
81c3812a CW |
160 | $this->controller->resetPage('MapField'); |
161 | ||
162 | $fileName = $this->controller->exportValue($this->_name, 'uploadFile'); | |
163 | $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader'); | |
164 | ||
165 | $session = CRM_Core_Session::singleton(); | |
166 | $session->set("dateTypes", $this->get('dateFormats')); | |
167 | ||
19a68bae | 168 | $separator = $this->controller->exportValue($this->_name, 'fieldSeparator'); |
81c3812a | 169 | |
be2fb01f | 170 | $mapper = []; |
81c3812a CW |
171 | |
172 | $parser = new $parserClassName($mapper); | |
cef29526 SL |
173 | if ($entity) { |
174 | $parser->setEntity($this->get($entity)); | |
175 | } | |
81c3812a CW |
176 | $parser->setMaxLinesToProcess(100); |
177 | $parser->run($fileName, | |
19a68bae | 178 | $separator, |
81c3812a CW |
179 | $mapper, |
180 | $skipColumnHeader, | |
181 | CRM_Import_Parser::MODE_MAPFIELD, | |
182 | $this->get('contactType') | |
183 | ); | |
184 | ||
185 | // add all the necessary variables to the form | |
186 | $parser->set($this); | |
187 | } | |
188 | ||
189 | /** | |
2b4bc760 | 190 | * Return a descriptive name for the page, used in wizard header. |
81c3812a CW |
191 | * |
192 | * @return string | |
193 | */ | |
194 | public function getTitle() { | |
195 | return ts('Upload Data'); | |
196 | } | |
197 | ||
198 | } |