| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | abstract class CRM_Event_Import_Parser extends CRM_Import_Parser { |
| 36 | |
| 37 | protected $_fileName; |
| 38 | |
| 39 | /**#@+ |
| 40 | * @var integer |
| 41 | */ |
| 42 | |
| 43 | /** |
| 44 | * Imported file size |
| 45 | */ |
| 46 | protected $_fileSize; |
| 47 | |
| 48 | /** |
| 49 | * Seperator being used |
| 50 | */ |
| 51 | protected $_seperator; |
| 52 | |
| 53 | /** |
| 54 | * Total number of lines in file |
| 55 | */ |
| 56 | protected $_lineCount; |
| 57 | |
| 58 | /** |
| 59 | * Whether the file has a column header or not |
| 60 | * |
| 61 | * @var boolean |
| 62 | */ |
| 63 | protected $_haveColumnHeader; |
| 64 | |
| 65 | /** |
| 66 | * @param string $fileName |
| 67 | * @param string $seperator |
| 68 | * @param $mapper |
| 69 | * @param bool $skipColumnHeader |
| 70 | * @param int $mode |
| 71 | * @param int $contactType |
| 72 | * @param int $onDuplicate |
| 73 | * |
| 74 | * @return mixed |
| 75 | * @throws Exception |
| 76 | */ |
| 77 | public function run( |
| 78 | $fileName, |
| 79 | $seperator = ',', |
| 80 | &$mapper, |
| 81 | $skipColumnHeader = FALSE, |
| 82 | $mode = self::MODE_PREVIEW, |
| 83 | $contactType = self::CONTACT_INDIVIDUAL, |
| 84 | $onDuplicate = self::DUPLICATE_SKIP |
| 85 | ) { |
| 86 | if (!is_array($fileName)) { |
| 87 | CRM_Core_Error::fatal(); |
| 88 | } |
| 89 | $fileName = $fileName['name']; |
| 90 | |
| 91 | switch ($contactType) { |
| 92 | case self::CONTACT_INDIVIDUAL: |
| 93 | $this->_contactType = 'Individual'; |
| 94 | break; |
| 95 | |
| 96 | case self::CONTACT_HOUSEHOLD: |
| 97 | $this->_contactType = 'Household'; |
| 98 | break; |
| 99 | |
| 100 | case self::CONTACT_ORGANIZATION: |
| 101 | $this->_contactType = 'Organization'; |
| 102 | } |
| 103 | |
| 104 | $this->init(); |
| 105 | |
| 106 | $this->_haveColumnHeader = $skipColumnHeader; |
| 107 | |
| 108 | $this->_seperator = $seperator; |
| 109 | |
| 110 | $fd = fopen($fileName, "r"); |
| 111 | if (!$fd) { |
| 112 | return FALSE; |
| 113 | } |
| 114 | |
| 115 | $this->_lineCount = $this->_warningCount = 0; |
| 116 | $this->_invalidRowCount = $this->_validCount = 0; |
| 117 | $this->_totalCount = $this->_conflictCount = 0; |
| 118 | |
| 119 | $this->_errors = array(); |
| 120 | $this->_warnings = array(); |
| 121 | $this->_conflicts = array(); |
| 122 | |
| 123 | $this->_fileSize = number_format(filesize($fileName) / 1024.0, 2); |
| 124 | |
| 125 | if ($mode == self::MODE_MAPFIELD) { |
| 126 | $this->_rows = array(); |
| 127 | } |
| 128 | else { |
| 129 | $this->_activeFieldCount = count($this->_activeFields); |
| 130 | } |
| 131 | |
| 132 | while (!feof($fd)) { |
| 133 | $this->_lineCount++; |
| 134 | |
| 135 | $values = fgetcsv($fd, 8192, $seperator); |
| 136 | if (!$values) { |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | self::encloseScrub($values); |
| 141 | |
| 142 | // skip column header if we're not in mapfield mode |
| 143 | if ($mode != self::MODE_MAPFIELD && $skipColumnHeader) { |
| 144 | $skipColumnHeader = FALSE; |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | /* trim whitespace around the values */ |
| 149 | |
| 150 | $empty = TRUE; |
| 151 | foreach ($values as $k => $v) { |
| 152 | $values[$k] = trim($v, " \t\r\n"); |
| 153 | } |
| 154 | |
| 155 | if (CRM_Utils_System::isNull($values)) { |
| 156 | continue; |
| 157 | } |
| 158 | |
| 159 | $this->_totalCount++; |
| 160 | |
| 161 | if ($mode == self::MODE_MAPFIELD) { |
| 162 | $returnCode = $this->mapField($values); |
| 163 | } |
| 164 | elseif ($mode == self::MODE_PREVIEW) { |
| 165 | $returnCode = $this->preview($values); |
| 166 | } |
| 167 | elseif ($mode == self::MODE_SUMMARY) { |
| 168 | $returnCode = $this->summary($values); |
| 169 | } |
| 170 | elseif ($mode == self::MODE_IMPORT) { |
| 171 | $returnCode = $this->import($onDuplicate, $values); |
| 172 | } |
| 173 | else { |
| 174 | $returnCode = self::ERROR; |
| 175 | } |
| 176 | |
| 177 | // note that a line could be valid but still produce a warning |
| 178 | if ($returnCode & self::VALID) { |
| 179 | $this->_validCount++; |
| 180 | if ($mode == self::MODE_MAPFIELD) { |
| 181 | $this->_rows[] = $values; |
| 182 | $this->_activeFieldCount = max($this->_activeFieldCount, count($values)); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if ($returnCode & self::WARNING) { |
| 187 | $this->_warningCount++; |
| 188 | if ($this->_warningCount < $this->_maxWarningCount) { |
| 189 | $this->_warningCount[] = $line; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | if ($returnCode & self::ERROR) { |
| 194 | $this->_invalidRowCount++; |
| 195 | if ($this->_invalidRowCount < $this->_maxErrorCount) { |
| 196 | $recordNumber = $this->_lineCount; |
| 197 | if ($this->_haveColumnHeader) { |
| 198 | $recordNumber--; |
| 199 | } |
| 200 | array_unshift($values, $recordNumber); |
| 201 | $this->_errors[] = $values; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if ($returnCode & self::CONFLICT) { |
| 206 | $this->_conflictCount++; |
| 207 | $recordNumber = $this->_lineCount; |
| 208 | if ($this->_haveColumnHeader) { |
| 209 | $recordNumber--; |
| 210 | } |
| 211 | array_unshift($values, $recordNumber); |
| 212 | $this->_conflicts[] = $values; |
| 213 | } |
| 214 | |
| 215 | if ($returnCode & self::DUPLICATE) { |
| 216 | if ($returnCode & self::MULTIPLE_DUPE) { |
| 217 | /* TODO: multi-dupes should be counted apart from singles |
| 218 | * on non-skip action */ |
| 219 | } |
| 220 | $this->_duplicateCount++; |
| 221 | $recordNumber = $this->_lineCount; |
| 222 | if ($this->_haveColumnHeader) { |
| 223 | $recordNumber--; |
| 224 | } |
| 225 | array_unshift($values, $recordNumber); |
| 226 | $this->_duplicates[] = $values; |
| 227 | if ($onDuplicate != self::DUPLICATE_SKIP) { |
| 228 | $this->_validCount++; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // we give the derived class a way of aborting the process |
| 233 | // note that the return code could be multiple code or'ed together |
| 234 | if ($returnCode & self::STOP) { |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | // if we are done processing the maxNumber of lines, break |
| 239 | if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) { |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | fclose($fd); |
| 245 | |
| 246 | if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) { |
| 247 | $customHeaders = $mapper; |
| 248 | |
| 249 | $customfields = CRM_Core_BAO_CustomField::getFields('Participant'); |
| 250 | foreach ($customHeaders as $key => $value) { |
| 251 | if ($id = CRM_Core_BAO_CustomField::getKeyID($value)) { |
| 252 | $customHeaders[$key] = $customfields[$id][0]; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if ($this->_invalidRowCount) { |
| 257 | // removed view url for invlaid contacts |
| 258 | $headers = array_merge(array( |
| 259 | ts('Line Number'), |
| 260 | ts('Reason'), |
| 261 | ), |
| 262 | $customHeaders |
| 263 | ); |
| 264 | $this->_errorFileName = self::errorFileName(self::ERROR); |
| 265 | self::exportCSV($this->_errorFileName, $headers, $this->_errors); |
| 266 | } |
| 267 | if ($this->_conflictCount) { |
| 268 | $headers = array_merge(array( |
| 269 | ts('Line Number'), |
| 270 | ts('Reason'), |
| 271 | ), |
| 272 | $customHeaders |
| 273 | ); |
| 274 | $this->_conflictFileName = self::errorFileName(self::CONFLICT); |
| 275 | self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts); |
| 276 | } |
| 277 | if ($this->_duplicateCount) { |
| 278 | $headers = array_merge(array( |
| 279 | ts('Line Number'), |
| 280 | ts('View Participant URL'), |
| 281 | ), |
| 282 | $customHeaders |
| 283 | ); |
| 284 | |
| 285 | $this->_duplicateFileName = self::errorFileName(self::DUPLICATE); |
| 286 | self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates); |
| 287 | } |
| 288 | } |
| 289 | return $this->fini(); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Given a list of the importable field keys that the user has selected |
| 294 | * set the active fields array to this list |
| 295 | * |
| 296 | * @param $fieldKeys array mapped array of values |
| 297 | * |
| 298 | * @return void |
| 299 | */ |
| 300 | public function setActiveFields($fieldKeys) { |
| 301 | $this->_activeFieldCount = count($fieldKeys); |
| 302 | foreach ($fieldKeys as $key) { |
| 303 | if (empty($this->_fields[$key])) { |
| 304 | $this->_activeFields[] = new CRM_Event_Import_Field('', ts('- do not import -')); |
| 305 | } |
| 306 | else { |
| 307 | $this->_activeFields[] = clone($this->_fields[$key]); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Format the field values for input to the api. |
| 314 | * |
| 315 | * @return array |
| 316 | * (reference ) associative array of name/value pairs |
| 317 | */ |
| 318 | public function &getActiveFieldParams() { |
| 319 | $params = array(); |
| 320 | for ($i = 0; $i < $this->_activeFieldCount; $i++) { |
| 321 | if (isset($this->_activeFields[$i]->_value) |
| 322 | && !isset($params[$this->_activeFields[$i]->_name]) |
| 323 | && !isset($this->_activeFields[$i]->_related) |
| 324 | ) { |
| 325 | |
| 326 | $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value; |
| 327 | } |
| 328 | } |
| 329 | return $params; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * @param string $name |
| 334 | * @param $title |
| 335 | * @param int $type |
| 336 | * @param string $headerPattern |
| 337 | * @param string $dataPattern |
| 338 | */ |
| 339 | public function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') { |
| 340 | if (empty($name)) { |
| 341 | $this->_fields['doNotImport'] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern); |
| 342 | } |
| 343 | else { |
| 344 | |
| 345 | //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null ); |
| 346 | $tempField = CRM_Contact_BAO_Contact::importableFields('All', NULL); |
| 347 | if (!array_key_exists($name, $tempField)) { |
| 348 | $this->_fields[$name] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern); |
| 349 | } |
| 350 | else { |
| 351 | $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, |
| 352 | CRM_Utils_Array::value('hasLocationType', $tempField[$name]) |
| 353 | ); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Store parser values. |
| 360 | * |
| 361 | * @param CRM_Core_Session $store |
| 362 | * |
| 363 | * @param int $mode |
| 364 | * |
| 365 | * @return void |
| 366 | */ |
| 367 | public function set($store, $mode = self::MODE_SUMMARY) { |
| 368 | $store->set('fileSize', $this->_fileSize); |
| 369 | $store->set('lineCount', $this->_lineCount); |
| 370 | $store->set('seperator', $this->_seperator); |
| 371 | $store->set('fields', $this->getSelectValues()); |
| 372 | $store->set('fieldTypes', $this->getSelectTypes()); |
| 373 | |
| 374 | $store->set('headerPatterns', $this->getHeaderPatterns()); |
| 375 | $store->set('dataPatterns', $this->getDataPatterns()); |
| 376 | $store->set('columnCount', $this->_activeFieldCount); |
| 377 | |
| 378 | $store->set('totalRowCount', $this->_totalCount); |
| 379 | $store->set('validRowCount', $this->_validCount); |
| 380 | $store->set('invalidRowCount', $this->_invalidRowCount); |
| 381 | $store->set('conflictRowCount', $this->_conflictCount); |
| 382 | |
| 383 | switch ($this->_contactType) { |
| 384 | case 'Individual': |
| 385 | $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL); |
| 386 | break; |
| 387 | |
| 388 | case 'Household': |
| 389 | $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD); |
| 390 | break; |
| 391 | |
| 392 | case 'Organization': |
| 393 | $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION); |
| 394 | } |
| 395 | |
| 396 | if ($this->_invalidRowCount) { |
| 397 | $store->set('errorsFileName', $this->_errorFileName); |
| 398 | } |
| 399 | if ($this->_conflictCount) { |
| 400 | $store->set('conflictsFileName', $this->_conflictFileName); |
| 401 | } |
| 402 | if (isset($this->_rows) && !empty($this->_rows)) { |
| 403 | $store->set('dataValues', $this->_rows); |
| 404 | } |
| 405 | |
| 406 | if ($mode == self::MODE_IMPORT) { |
| 407 | $store->set('duplicateRowCount', $this->_duplicateCount); |
| 408 | if ($this->_duplicateCount) { |
| 409 | $store->set('duplicatesFileName', $this->_duplicateFileName); |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Export data to a CSV file. |
| 416 | * |
| 417 | * @param string $fileName |
| 418 | * @param array $header |
| 419 | * @param array $data |
| 420 | * |
| 421 | * @return void |
| 422 | */ |
| 423 | public static function exportCSV($fileName, $header, $data) { |
| 424 | $output = array(); |
| 425 | $fd = fopen($fileName, 'w'); |
| 426 | |
| 427 | foreach ($header as $key => $value) { |
| 428 | $header[$key] = "\"$value\""; |
| 429 | } |
| 430 | $config = CRM_Core_Config::singleton(); |
| 431 | $output[] = implode($config->fieldSeparator, $header); |
| 432 | |
| 433 | foreach ($data as $datum) { |
| 434 | foreach ($datum as $key => $value) { |
| 435 | if (is_array($value)) { |
| 436 | foreach ($value[0] as $k1 => $v1) { |
| 437 | if ($k1 == 'location_type_id') { |
| 438 | continue; |
| 439 | } |
| 440 | $datum[$k1] = $v1; |
| 441 | } |
| 442 | } |
| 443 | else { |
| 444 | $datum[$key] = "\"$value\""; |
| 445 | } |
| 446 | } |
| 447 | $output[] = implode($config->fieldSeparator, $datum); |
| 448 | } |
| 449 | fwrite($fd, implode("\n", $output)); |
| 450 | fclose($fd); |
| 451 | } |
| 452 | |
| 453 | } |