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