Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Import_DataSource_CSV extends CRM_Import_DataSource { | |
7da04cde | 18 | const |
b0b2638a DL |
19 | NUM_ROWS_TO_INSERT = 100; |
20 | ||
e0ef6999 | 21 | /** |
fe482240 | 22 | * Provides information about the data source. |
e0ef6999 | 23 | * |
a6c01b45 CW |
24 | * @return array |
25 | * collection of info about this data source | |
e0ef6999 | 26 | */ |
00be9182 | 27 | public function getInfo() { |
be2fb01f | 28 | return ['title' => ts('Comma-Separated Values (CSV)')]; |
6a488035 TO |
29 | } |
30 | ||
e0ef6999 | 31 | /** |
fe482240 | 32 | * Set variables up before form is built. |
3bdf1f3a | 33 | * |
34 | * @param CRM_Core_Form $form | |
e0ef6999 | 35 | */ |
3a05d67e TO |
36 | public function preProcess(&$form) { |
37 | } | |
6a488035 | 38 | |
e0ef6999 | 39 | /** |
b8c71ffa | 40 | * This is function is called by the form object to get the DataSource's form snippet. |
41 | * | |
42 | * It should add all fields necessary to get the data | |
e0ef6999 EM |
43 | * uploaded to the temporary table in the DB. |
44 | * | |
c490a46a | 45 | * @param CRM_Core_Form $form |
e0ef6999 | 46 | */ |
00be9182 | 47 | public function buildQuickForm(&$form) { |
6a488035 TO |
48 | $form->add('hidden', 'hidden_dataSource', 'CRM_Import_DataSource_CSV'); |
49 | ||
50 | $config = CRM_Core_Config::singleton(); | |
51 | ||
2e966dd5 | 52 | $uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE); |
ebcf0a88 JP |
53 | //Fetch uploadFileSize from php_ini when $config->maxFileSize is set to "no limit". |
54 | if (empty($uploadFileSize)) { | |
55 | $uploadFileSize = CRM_Utils_Number::formatUnitSize(ini_get('upload_max_filesize'), TRUE); | |
56 | } | |
6a488035 TO |
57 | $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2); |
58 | $form->assign('uploadSize', $uploadSize); | |
66dc6009 | 59 | $form->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE); |
6a488035 | 60 | $form->setMaxFileSize($uploadFileSize); |
be2fb01f | 61 | $form->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', [ |
971e129b SL |
62 | 1 => $uploadSize, |
63 | 2 => $uploadFileSize, | |
64 | ]), 'maxfilesize', $uploadFileSize); | |
6a488035 TO |
65 | $form->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File'); |
66 | $form->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile'); | |
67 | ||
68 | $form->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers')); | |
69 | } | |
70 | ||
e0ef6999 | 71 | /** |
fe482240 | 72 | * Process the form submission. |
54957108 | 73 | * |
74 | * @param array $params | |
75 | * @param string $db | |
76 | * @param \CRM_Core_Form $form | |
e0ef6999 | 77 | */ |
00be9182 | 78 | public function postProcess(&$params, &$db, &$form) { |
6a488035 | 79 | $file = $params['uploadFile']['name']; |
6a488035 TO |
80 | $result = self::_CsvToTable($db, |
81 | $file, | |
82 | CRM_Utils_Array::value('skipColumnHeader', $params, FALSE), | |
83 | CRM_Utils_Array::value('import_table_name', $params), | |
84 | CRM_Utils_Array::value('fieldSeparator', $params, ',') | |
85 | ); | |
86 | ||
87 | $form->set('originalColHeader', CRM_Utils_Array::value('original_col_header', $result)); | |
88 | ||
89 | $table = $result['import_table_name']; | |
719a6fec | 90 | $importJob = new CRM_Contact_Import_ImportJob($table); |
6a488035 TO |
91 | $form->set('importTableName', $importJob->getTableName()); |
92 | } | |
93 | ||
94 | /** | |
95 | * Create a table that matches the CSV file and populate it with the file's contents | |
96 | * | |
6f69cc11 TO |
97 | * @param object $db |
98 | * Handle to the database connection. | |
99 | * @param string $file | |
100 | * File name to load. | |
101 | * @param bool $headers | |
102 | * Whether the first row contains headers. | |
103 | * @param string $table | |
104 | * Name of table from which data imported. | |
105 | * @param string $fieldSeparator | |
b44e3f84 | 106 | * Character that separates the various columns in the file. |
6a488035 | 107 | * |
a6c01b45 CW |
108 | * @return string |
109 | * name of the created table | |
6a488035 | 110 | */ |
bd5d7c2b TO |
111 | private static function _CsvToTable( |
112 | &$db, | |
6a488035 | 113 | $file, |
3a05d67e TO |
114 | $headers = FALSE, |
115 | $table = NULL, | |
6a488035 TO |
116 | $fieldSeparator = ',' |
117 | ) { | |
be2fb01f | 118 | $result = []; |
6a488035 TO |
119 | $fd = fopen($file, 'r'); |
120 | if (!$fd) { | |
121 | CRM_Core_Error::fatal("Could not read $file"); | |
122 | } | |
3bf4c8a0 SB |
123 | if (filesize($file) == 0) { |
124 | CRM_Core_Error::fatal("$file is empty. Please upload a valid file."); | |
125 | } | |
6a488035 TO |
126 | |
127 | $config = CRM_Core_Config::singleton(); | |
128 | // support tab separated | |
129 | if (strtolower($fieldSeparator) == 'tab' || | |
130 | strtolower($fieldSeparator) == '\t' | |
131 | ) { | |
132 | $fieldSeparator = "\t"; | |
133 | } | |
134 | ||
135 | $firstrow = fgetcsv($fd, 0, $fieldSeparator); | |
136 | ||
137 | // create the column names from the CSV header or as col_0, col_1, etc. | |
138 | if ($headers) { | |
139 | //need to get original headers. | |
140 | $result['original_col_header'] = $firstrow; | |
141 | ||
142 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; | |
353ffa53 TO |
143 | $columns = array_map($strtolower, $firstrow); |
144 | $columns = str_replace(' ', '_', $columns); | |
145 | $columns = preg_replace('/[^a-z_]/', '', $columns); | |
6a488035 TO |
146 | |
147 | // need to take care of null as well as duplicate col names. | |
148 | $duplicateColName = FALSE; | |
149 | if (count($columns) != count(array_unique($columns))) { | |
150 | $duplicateColName = TRUE; | |
151 | } | |
152 | ||
6a488035 TO |
153 | // need to truncate values per mysql field name length limits |
154 | // mysql allows 64, but we need to account for appending colKey | |
155 | // CRM-9079 | |
156 | foreach ($columns as $colKey => & $colName) { | |
157 | if (strlen($colName) > 58) { | |
158 | $colName = substr($colName, 0, 58); | |
159 | } | |
160 | } | |
161 | ||
162 | if (in_array('', $columns) || $duplicateColName) { | |
163 | foreach ($columns as $colKey => & $colName) { | |
164 | if (!$colName) { | |
165 | $colName = "col_$colKey"; | |
166 | } | |
167 | elseif ($duplicateColName) { | |
168 | $colName .= "_$colKey"; | |
169 | } | |
170 | } | |
171 | } | |
172 | ||
173 | // CRM-4881: we need to quote column names, as they may be MySQL reserved words | |
bd5d7c2b TO |
174 | foreach ($columns as & $column) { |
175 | $column = "`$column`"; | |
3a05d67e | 176 | } |
6a488035 TO |
177 | } |
178 | else { | |
be2fb01f | 179 | $columns = []; |
bd5d7c2b TO |
180 | foreach ($firstrow as $i => $_) { |
181 | $columns[] = "col_$i"; | |
3a05d67e | 182 | } |
6a488035 TO |
183 | } |
184 | ||
185 | // FIXME: we should regen this table's name if it exists rather than drop it | |
186 | if (!$table) { | |
187 | $table = 'civicrm_import_job_' . md5(uniqid(rand(), TRUE)); | |
188 | } | |
189 | ||
190 | $db->query("DROP TABLE IF EXISTS $table"); | |
191 | ||
192 | $numColumns = count($columns); | |
193 | $create = "CREATE TABLE $table (" . implode(' text, ', $columns) . " text) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"; | |
194 | $db->query($create); | |
195 | ||
196 | // the proper approach, but some MySQL installs do not have this enabled | |
197 | // $load = "LOAD DATA LOCAL INFILE '$file' INTO TABLE $table FIELDS TERMINATED BY '$fieldSeparator' OPTIONALLY ENCLOSED BY '\"'"; | |
198 | // if ($headers) { $load .= ' IGNORE 1 LINES'; } | |
199 | // $db->query($load); | |
200 | ||
201 | // parse the CSV line by line and build one big INSERT (while MySQL-escaping the CSV contents) | |
202 | if (!$headers) { | |
203 | rewind($fd); | |
204 | } | |
205 | ||
353ffa53 | 206 | $sql = NULL; |
6a488035 TO |
207 | $first = TRUE; |
208 | $count = 0; | |
209 | while ($row = fgetcsv($fd, 0, $fieldSeparator)) { | |
210 | // skip rows that dont match column count, else we get a sql error | |
211 | if (count($row) != $numColumns) { | |
212 | continue; | |
213 | } | |
214 | ||
215 | if (!$first) { | |
216 | $sql .= ', '; | |
217 | } | |
218 | ||
219 | $first = FALSE; | |
5dcdc4d6 SB |
220 | |
221 | // CRM-17859 Trim non-breaking spaces from columns. | |
222 | $row = array_map( | |
223 | function($string) { | |
224 | return trim($string, chr(0xC2) . chr(0xA0)); | |
225 | }, $row); | |
be2fb01f | 226 | $row = array_map(['CRM_Core_DAO', 'escapeString'], $row); |
6a488035 TO |
227 | $sql .= "('" . implode("', '", $row) . "')"; |
228 | $count++; | |
229 | ||
230 | if ($count >= self::NUM_ROWS_TO_INSERT && !empty($sql)) { | |
231 | $sql = "INSERT IGNORE INTO $table VALUES $sql"; | |
232 | $db->query($sql); | |
233 | ||
353ffa53 | 234 | $sql = NULL; |
6a488035 TO |
235 | $first = TRUE; |
236 | $count = 0; | |
237 | } | |
238 | } | |
239 | ||
240 | if (!empty($sql)) { | |
241 | $sql = "INSERT IGNORE INTO $table VALUES $sql"; | |
242 | $db->query($sql); | |
243 | } | |
244 | ||
245 | fclose($fd); | |
246 | ||
247 | //get the import tmp table name. | |
248 | $result['import_table_name'] = $table; | |
249 | ||
250 | return $result; | |
251 | } | |
96025800 | 252 | |
6a488035 | 253 | } |