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