Commit | Line | Data |
---|---|---|
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 CW |
19 | */ |
20 | abstract class CRM_Import_Form_DataSource extends CRM_Core_Form { | |
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 | ||
53 | $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE); | |
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); |
be2fb01f | 69 | $this->add('select', 'savedMapping', ts('Mapping Option'), ['' => ts('- select -')] + $mappingArray); |
81c3812a CW |
70 | |
71 | if ($loadedMapping = $this->get('loadedMapping')) { | |
72 | $this->assign('loadedMapping', $loadedMapping); | |
be2fb01f | 73 | $this->setDefaults(['savedMapping' => $loadedMapping]); |
81c3812a CW |
74 | } |
75 | ||
76 | //build date formats | |
77 | CRM_Core_Form_Date::buildAllowedDateFormats($this); | |
78 | ||
be2fb01f CW |
79 | $this->addButtons([ |
80 | [ | |
81c3812a CW |
81 | 'type' => 'upload', |
82 | 'name' => ts('Continue'), | |
83 | 'spacing' => ' ', | |
84 | 'isDefault' => TRUE, | |
be2fb01f CW |
85 | ], |
86 | [ | |
81c3812a CW |
87 | 'type' => 'cancel', |
88 | 'name' => ts('Cancel'), | |
be2fb01f | 89 | ], |
971e129b | 90 | ]); |
81c3812a CW |
91 | } |
92 | ||
93 | /** | |
2b4bc760 | 94 | * A long-winded way to add one radio element to the form. |
81c3812a CW |
95 | */ |
96 | protected function addContactTypeSelector() { | |
97 | //contact types option | |
be2fb01f | 98 | $contactOptions = []; |
81c3812a CW |
99 | if (CRM_Contact_BAO_ContactType::isActive('Individual')) { |
100 | $contactOptions[] = $this->createElement('radio', | |
101 | NULL, NULL, ts('Individual'), CRM_Import_Parser::CONTACT_INDIVIDUAL | |
102 | ); | |
103 | } | |
104 | if (CRM_Contact_BAO_ContactType::isActive('Household')) { | |
105 | $contactOptions[] = $this->createElement('radio', | |
106 | NULL, NULL, ts('Household'), CRM_Import_Parser::CONTACT_HOUSEHOLD | |
107 | ); | |
108 | } | |
109 | if (CRM_Contact_BAO_ContactType::isActive('Organization')) { | |
110 | $contactOptions[] = $this->createElement('radio', | |
111 | NULL, NULL, ts('Organization'), CRM_Import_Parser::CONTACT_ORGANIZATION | |
112 | ); | |
113 | } | |
114 | ||
115 | $this->addGroup($contactOptions, 'contactType', | |
116 | ts('Contact Type') | |
117 | ); | |
118 | ||
be2fb01f | 119 | $this->setDefaults([ |
81c3812a | 120 | 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL, |
be2fb01f | 121 | ]); |
81c3812a CW |
122 | } |
123 | ||
124 | /** | |
2b4bc760 | 125 | * Store form values. |
126 | * | |
81c3812a CW |
127 | * @param array $names |
128 | */ | |
129 | protected function storeFormValues($names) { | |
130 | foreach ($names as $name) { | |
131 | $this->set($name, $this->controller->exportValue($this->_name, $name)); | |
132 | } | |
133 | } | |
134 | ||
135 | /** | |
2b4bc760 | 136 | * Common form postProcess. |
81c3812a CW |
137 | * |
138 | * @param string $parserClassName | |
cef29526 SL |
139 | * |
140 | * @param string|null $entity | |
141 | * Entity to set for paraser currently only for custom import | |
81c3812a | 142 | */ |
cef29526 | 143 | protected function submitFileForMapping($parserClassName, $entity = NULL) { |
81c3812a CW |
144 | $this->controller->resetPage('MapField'); |
145 | ||
146 | $fileName = $this->controller->exportValue($this->_name, 'uploadFile'); | |
147 | $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader'); | |
148 | ||
149 | $session = CRM_Core_Session::singleton(); | |
150 | $session->set("dateTypes", $this->get('dateFormats')); | |
151 | ||
19a68bae | 152 | $separator = $this->controller->exportValue($this->_name, 'fieldSeparator'); |
81c3812a | 153 | |
be2fb01f | 154 | $mapper = []; |
81c3812a CW |
155 | |
156 | $parser = new $parserClassName($mapper); | |
cef29526 SL |
157 | if ($entity) { |
158 | $parser->setEntity($this->get($entity)); | |
159 | } | |
81c3812a CW |
160 | $parser->setMaxLinesToProcess(100); |
161 | $parser->run($fileName, | |
19a68bae | 162 | $separator, |
81c3812a CW |
163 | $mapper, |
164 | $skipColumnHeader, | |
165 | CRM_Import_Parser::MODE_MAPFIELD, | |
166 | $this->get('contactType') | |
167 | ); | |
168 | ||
169 | // add all the necessary variables to the form | |
170 | $parser->set($this); | |
171 | } | |
172 | ||
173 | /** | |
2b4bc760 | 174 | * Return a descriptive name for the page, used in wizard header. |
81c3812a CW |
175 | * |
176 | * @return string | |
177 | */ | |
178 | public function getTitle() { | |
179 | return ts('Upload Data'); | |
180 | } | |
181 | ||
182 | } |