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