From 69a4c20a4a07313a08b8a34af2f8c48a1edbc9d2 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 8 Jun 2013 21:13:21 -0700 Subject: [PATCH] Move redundant parser code into parent CRM-11254 ---------------------------------------- * CRM-11254: Create parent class for import http://issues.civicrm.org/jira/browse/CRM-11254 --- CRM/Activity/Import/Parser.php | 242 ------------------ CRM/Contact/Import/Parser.php | 260 -------------------- CRM/Contact/Import/Parser/Contact.php | 3 +- CRM/Contribute/Import/Parser.php | 278 +-------------------- CRM/Event/Import/Parser.php | 251 ------------------- CRM/Import/Parser.php | 339 ++++++++++++++++++++++++++ CRM/Member/Import/Parser.php | 249 ------------------- 7 files changed, 349 insertions(+), 1273 deletions(-) diff --git a/CRM/Activity/Import/Parser.php b/CRM/Activity/Import/Parser.php index 3563e973d5..32460b7f74 100644 --- a/CRM/Activity/Import/Parser.php +++ b/CRM/Activity/Import/Parser.php @@ -58,123 +58,6 @@ abstract class CRM_Activity_Import_Parser extends CRM_Import_Parser { */ protected $_lineCount; - /** - * total number of non empty lines - */ - protected $_totalCount; - - /** - * running total number of valid lines - */ - protected $_validCount; - - /** - * running total number of invalid rows - */ - protected $_invalidRowCount; - - /** - * maximum number of invalid rows to store - */ - protected $_maxErrorCount; - - /** - * array of error lines, bounded by MAX_ERROR - */ - protected $_errors; - - /** - * total number of conflict lines - */ - protected $_conflictCount; - - /** - * array of conflict lines - */ - protected $_conflicts; - - /** - * total number of duplicate (from database) lines - */ - protected $_duplicateCount; - - /** - * array of duplicate lines - */ - protected $_duplicates; - - /** - * running total number of warnings - */ - protected $_warningCount; - - /** - * maximum number of warnings to store - */ - protected $_maxWarningCount = self::MAX_WARNINGS; - - /** - * array of warning lines, bounded by MAX_WARNING - */ - protected $_warnings; - - /** - * array of all the fields that could potentially be part - * of this import process - * @var array - */ - protected $_fields; - - /** - * array of the fields that are actually part of the import process - * the position in the array also dictates their position in the import - * file - * @var array - */ - protected $_activeFields; - - /** - * cache the count of active fields - * - * @var int - */ - protected $_activeFieldCount; - - /** - * maximum number of non-empty/comment lines to process - * - * @var int - */ - protected $_maxLinesToProcess; - - /** - * cache of preview rows - * - * @var array - */ - protected $_rows; - - /** - * filename of error data - * - * @var string - */ - protected $_errorFileName; - - /** - * filename of conflict data - * - * @var string - */ - protected $_conflictFileName; - - /** - * filename of duplicate data - * - * @var string - */ - protected $_duplicateFileName; - /** * whether the file has a column header or not * @@ -182,12 +65,6 @@ abstract class CRM_Activity_Import_Parser extends CRM_Import_Parser { */ protected $_haveColumnHeader; - function __construct() { - $this->_maxLinesToProcess = 0; - $this->_maxErrorCount = self::MAX_ERRORS; - } - - abstract function init(); function run($fileName, $seperator = ',', &$mapper, @@ -386,13 +263,6 @@ abstract class CRM_Activity_Import_Parser extends CRM_Import_Parser { return $this->fini(); } - abstract function mapField(&$values); - abstract function preview(&$values); - abstract function summary(&$values); - abstract function import($onDuplicate, &$values); - - abstract function fini(); - /** * Given a list of the importable field keys that the user has selected * set the active fields array to this list @@ -414,43 +284,6 @@ abstract class CRM_Activity_Import_Parser extends CRM_Import_Parser { } } - /*function setActiveFieldLocationTypes( $elements ) - { - for ($i = 0; $i < count( $elements ); $i++) { - $this->_activeFields[$i]->_hasLocationType = $elements[$i]; - } - } - - function setActiveFieldPhoneTypes( $elements ) - { - for ($i = 0; $i < count( $elements ); $i++) { - $this->_activeFields[$i]->_phoneType = $elements[$i]; - } - }*/ - function setActiveFieldValues($elements, &$erroneousField) { - $maxCount = count($elements) < $this->_activeFieldCount ? count($elements) : $this->_activeFieldCount; - for ($i = 0; $i < $maxCount; $i++) { - $this->_activeFields[$i]->setValue($elements[$i]); - } - - // reset all the values that we did not have an equivalent import element - for (; $i < $this->_activeFieldCount; $i++) { - $this->_activeFields[$i]->resetValue(); - } - - // now validate the fields and return false if error - $valid = self::VALID; - for ($i = 0; $i < $this->_activeFieldCount; $i++) { - if (!$this->_activeFields[$i]->validate()) { - // no need to do any more validation - $erroneousField = $i; - $valid = self::ERROR; - break; - } - } - return $valid; - } - /** * function to format the field values for input to the api * @@ -471,40 +304,6 @@ abstract class CRM_Activity_Import_Parser extends CRM_Import_Parser { return $params; } - function getSelectValues() { - $values = array(); - foreach ($this->_fields as $name => $field) { - $values[$name] = $field->_title; - } - return $values; - } - - function getSelectTypes() { - $values = array(); - foreach ($this->_fields as $name => $field) { - if (isset($field->_hasLocationType)) { - $values[$name] = $field->_hasLocationType; - } - } - return $values; - } - - function getHeaderPatterns() { - $values = array(); - foreach ($this->_fields as $name => $field) { - $values[$name] = $field->_headerPattern; - } - return $values; - } - - function getDataPatterns() { - $values = array(); - foreach ($this->_fields as $name => $field) { - $values[$name] = $field->_dataPattern; - } - return $values; - } - function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') { if (empty($name)) { $this->_fields['doNotImport'] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern); @@ -521,18 +320,6 @@ abstract class CRM_Activity_Import_Parser extends CRM_Import_Parser { } } - /** - * setter function - * - * @param int $max - * - * @return void - * @access public - */ - function setMaxLinesToProcess($max) { - $this->_maxLinesToProcess = $max; - } - /** * Store parser values * @@ -606,34 +393,5 @@ abstract class CRM_Activity_Import_Parser extends CRM_Import_Parser { fclose($fd); } - /** - * Remove single-quote enclosures from a value array (row) - * - * @param array $values - * @param string $enclosure - * - * @return void - * @static - * @access public - */ - static function encloseScrub(&$values, $enclosure = "'") { - if (empty($values)) { - return; - } - - foreach ($values as $k => $v) { - $values[$k] = preg_replace("/^$enclosure(.*)$enclosure$/", '$1', $v); - } - } - - function errorFileName($type) { - $fileName = CRM_Contact_Import_Parser::errorFileName($type); - return $fileName; - } - - function saveFileName($type) { - $fileName = CRM_Contact_Import_Parser::saveFileName($type); - return $fileName; - } } diff --git a/CRM/Contact/Import/Parser.php b/CRM/Contact/Import/Parser.php index 39d1d58f07..6546d9a8f6 100644 --- a/CRM/Contact/Import/Parser.php +++ b/CRM/Contact/Import/Parser.php @@ -48,56 +48,6 @@ abstract class CRM_Contact_Import_Parser extends CRM_Import_Parser { */ protected $_rowCount; - /** - * total number of non empty lines - */ - protected $_totalCount; - - /** - * running total number of valid lines - */ - protected $_validCount; - - /** - * running total number of invalid rows - */ - protected $_invalidRowCount; - - /** - * maximum number of invalid rows to store - */ - protected $_maxErrorCount; - - /** - * array of error lines, bounded by MAX_ERROR - */ - protected $_errors; - - /** - * total number of conflict lines - */ - protected $_conflictCount; - - /** - * array of conflict lines - */ - protected $_conflicts; - - /** - * total number of duplicate (from database) lines - */ - protected $_duplicateCount; - - /** - * array of duplicate lines - */ - protected $_duplicates; - - /** - * running total number of warnings - */ - protected $_warningCount; - /** * running total number of un matched Conact */ @@ -108,78 +58,11 @@ abstract class CRM_Contact_Import_Parser extends CRM_Import_Parser { */ protected $_unMatch; - /** - * maximum number of warnings to store - */ - protected $_maxWarningCount = self::MAX_WARNINGS; - /** * total number of contacts with unparsed addresses */ protected $_unparsedAddressCount; - /** - * array of warning lines, bounded by MAX_WARNING - */ - protected $_warnings; - - /** - * array of all the fields that could potentially be part - * of this import process - * @var array - */ - protected $_fields; - - /** - * array of the fields that are actually part of the import process - * the position in the array also dictates their position in the import - * file - * @var array - */ - protected $_activeFields; - - /** - * cache the count of active fields - * - * @var int - */ - protected $_activeFieldCount; - - /** - * maximum number of non-empty/comment lines to process - * - * @var int - */ - protected $_maxLinesToProcess; - - /** - * cache of preview rows - * - * @var array - */ - protected $_rows; - - /** - * filename of error data - * - * @var string - */ - protected $_errorFileName; - - /** - * filename of conflict data - * - * @var string - */ - protected $_conflictFileName; - - /** - * filename of duplicate data - * - * @var string - */ - protected $_duplicateFileName; - /** * filename of mismatch data * @@ -190,14 +73,6 @@ abstract class CRM_Contact_Import_Parser extends CRM_Import_Parser { protected $_primaryKeyName; protected $_statusFieldName; - /** - * contact type - * - * @var int - */ - - public $_contactType; - /** * on duplicate * @@ -212,13 +87,6 @@ abstract class CRM_Contact_Import_Parser extends CRM_Import_Parser { */ public $_dedupeRuleGroupID = NULL; - function __construct() { - $this->_maxLinesToProcess = 0; - $this->_maxErrorCount = self::MAX_ERRORS; - } - - abstract function init(); - function run($tableName, &$mapper, $mode = self::MODE_PREVIEW, @@ -514,13 +382,6 @@ abstract class CRM_Contact_Import_Parser extends CRM_Import_Parser { return $this->fini(); } - abstract function mapField(&$values); - abstract function preview(&$values); - abstract function summary(&$values); - abstract function import($onDuplicate, &$values); - - abstract function fini(); - /** * Given a list of the importable field keys that the user has selected * set the active fields array to this list @@ -542,29 +403,6 @@ abstract class CRM_Contact_Import_Parser extends CRM_Import_Parser { } } - function setActiveFieldValues($elements) { - $maxCount = count($elements) < $this->_activeFieldCount ? count($elements) : $this->_activeFieldCount; - for ($i = 0; $i < $maxCount; $i++) { - $this->_activeFields[$i]->setValue($elements[$i]); - } - - // reset all the values that we did not have an equivalent import element - for (; $i < $this->_activeFieldCount; $i++) { - $this->_activeFields[$i]->resetValue(); - } - - // now validate the fields and return false if error - $valid = self::VALID; - for ($i = 0; $i < $this->_activeFieldCount; $i++) { - if (!$this->_activeFields[$i]->validate()) { - // no need to do any more validation - $valid = self::ERROR; - break; - } - } - return $valid; - } - function setActiveFieldLocationTypes($elements) { for ($i = 0; $i < count($elements); $i++) { $this->_activeFields[$i]->_hasLocationType = $elements[$i]; @@ -747,22 +585,6 @@ abstract class CRM_Contact_Import_Parser extends CRM_Import_Parser { return $params; } - function getSelectValues() { - $values = array(); - foreach ($this->_fields as $name => $field) { - $values[$name] = $field->_title; - } - return $values; - } - - function getSelectTypes() { - $values = array(); - foreach ($this->_fields as $name => $field) { - $values[$name] = $field->_hasLocationType; - } - return $values; - } - function getColumnPatterns() { $values = array(); foreach ($this->_fields as $name => $field) { @@ -771,14 +593,6 @@ abstract class CRM_Contact_Import_Parser extends CRM_Import_Parser { return $values; } - function getDataPatterns() { - $values = array(); - foreach ($this->_fields as $name => $field) { - $values[$name] = $field->_dataPattern; - } - return $values; - } - function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//', $hasLocationType = FALSE @@ -789,18 +603,6 @@ abstract class CRM_Contact_Import_Parser extends CRM_Import_Parser { } } - /** - * setter function - * - * @param int $max - * - * @return void - * @access public - */ - function setMaxLinesToProcess($max) { - $this->_maxLinesToProcess = $max; - } - /** * Store parser values * @@ -946,67 +748,5 @@ abstract class CRM_Contact_Import_Parser extends CRM_Import_Parser { } } - function errorFileName($type) { - $fileName = NULL; - if (empty($type)) { - return $fileName; - } - - $config = CRM_Core_Config::singleton(); - $fileName = $config->uploadDir . "sqlImport"; - switch ($type) { - case CRM_Import_Parser::ERROR: - $fileName .= '.errors'; - break; - - case CRM_Import_Parser::CONFLICT: - $fileName .= '.conflicts'; - break; - - case CRM_Import_Parser::DUPLICATE: - $fileName .= '.duplicates'; - break; - - case CRM_Import_Parser::NO_MATCH: - $fileName .= '.mismatch'; - break; - - case CRM_Import_Parser::UNPARSED_ADDRESS_WARNING: - $fileName .= '.unparsedAddress'; - break; - } - - return $fileName; - } - - function saveFileName($type) { - $fileName = NULL; - if (empty($type)) { - return $fileName; - } - switch ($type) { - case CRM_Import_Parser::ERROR: - $fileName = 'Import_Errors.csv'; - break; - - case CRM_Import_Parser::CONFLICT: - $fileName = 'Import_Conflicts.csv'; - break; - - case CRM_Import_Parser::DUPLICATE: - $fileName = 'Import_Duplicates.csv'; - break; - - case CRM_Import_Parser::NO_MATCH: - $fileName = 'Import_Mismatch.csv'; - break; - - case CRM_Import_Parser::UNPARSED_ADDRESS_WARNING: - $fileName = 'Import_Unparsed_Address.csv'; - break; - } - - return $fileName; - } } diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index a2456192c4..9d9209b324 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -276,7 +276,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { * @access public */ function summary(&$values) { - $response = $this->setActiveFieldValues($values); + $erroneousField = NULL; + $response = $this->setActiveFieldValues($values, $erroneousField); $errorMessage = NULL; $errorRequired = FALSE; diff --git a/CRM/Contribute/Import/Parser.php b/CRM/Contribute/Import/Parser.php index 04fa45fb7e..907eaec1ce 100644 --- a/CRM/Contribute/Import/Parser.php +++ b/CRM/Contribute/Import/Parser.php @@ -64,21 +64,6 @@ abstract class CRM_Contribute_Import_Parser extends CRM_Import_Parser { */ protected $_lineCount; - /** - * total number of non empty lines - */ - protected $_totalCount; - - /** - * running total number of valid lines - */ - protected $_validCount; - - /** - * running total number of invalid rows - */ - protected $_invalidRowCount; - /** * running total number of valid soft credit rows */ @@ -99,16 +84,6 @@ abstract class CRM_Contribute_Import_Parser extends CRM_Import_Parser { */ protected $_invalidPledgePaymentRowCount; - /** - * maximum number of invalid rows to store - */ - protected $_maxErrorCount; - - /** - * array of error lines, bounded by MAX_ERROR - */ - protected $_errors; - /** * array of pledge payment error lines, bounded by MAX_ERROR */ @@ -119,84 +94,6 @@ abstract class CRM_Contribute_Import_Parser extends CRM_Import_Parser { */ protected $_softCreditErrors; - /** - * total number of conflict lines - */ - protected $_conflictCount; - - /** - * array of conflict lines - */ - protected $_conflicts; - - /** - * total number of duplicate (from database) lines - */ - protected $_duplicateCount; - - /** - * array of duplicate lines - */ - protected $_duplicates; - - /** - * running total number of warnings - */ - protected $_warningCount; - - /** - * maximum number of warnings to store - */ - protected $_maxWarningCount = self::MAX_WARNINGS; - - /** - * array of warning lines, bounded by MAX_WARNING - */ - protected $_warnings; - - /** - * array of all the fields that could potentially be part - * of this import process - * @var array - */ - protected $_fields; - - /** - * array of the fields that are actually part of the import process - * the position in the array also dictates their position in the import - * file - * @var array - */ - protected $_activeFields; - - /** - * cache the count of active fields - * - * @var int - */ - protected $_activeFieldCount; - - /** - * maximum number of non-empty/comment lines to process - * - * @var int - */ - protected $_maxLinesToProcess; - - /** - * cache of preview rows - * - * @var array - */ - protected $_rows; - - /** - * filename of error data - * - * @var string - */ - protected $_errorFileName; - /** * filename of pledge payment error data * @@ -211,20 +108,6 @@ abstract class CRM_Contribute_Import_Parser extends CRM_Import_Parser { */ protected $_softCreditErrorsFileName; - /** - * filename of conflict data - * - * @var string - */ - protected $_conflictFileName; - - /** - * filename of duplicate data - * - * @var string - */ - protected $_duplicateFileName; - /** * whether the file has a column header or not * @@ -232,20 +115,6 @@ abstract class CRM_Contribute_Import_Parser extends CRM_Import_Parser { */ protected $_haveColumnHeader; - /** - * contact type - * - * @var int - */ - - public $_contactType; - - function __construct() { - $this->_maxLinesToProcess = 0; - $this->_maxErrorCount = self::MAX_ERRORS; - } - - abstract function init(); function run($fileName, $seperator = ',', &$mapper, @@ -524,13 +393,6 @@ abstract class CRM_Contribute_Import_Parser extends CRM_Import_Parser { return $this->fini(); } - abstract function mapField(&$values); - abstract function preview(&$values); - abstract function summary(&$values); - abstract function import($onDuplicate, &$values); - - abstract function fini(); - /** * Given a list of the importable field keys that the user has selected * set the active fields array to this list @@ -558,30 +420,6 @@ pppp * @return void } } - function setActiveFieldValues($elements, &$erroneousField) { - $maxCount = count($elements) < $this->_activeFieldCount ? count($elements) : $this->_activeFieldCount; - for ($i = 0; $i < $maxCount; $i++) { - $this->_activeFields[$i]->setValue($elements[$i]); - } - - // reset all the values that we did not have an equivalent import element - for (; $i < $this->_activeFieldCount; $i++) { - $this->_activeFields[$i]->resetValue(); - } - - // now validate the fields and return false if error - $valid = self::VALID; - for ($i = 0; $i < $this->_activeFieldCount; $i++) { - if (!$this->_activeFields[$i]->validate()) { - // no need to do any more validation - $erroneousField = $i; - $valid = self::ERROR; - break; - } - } - return $valid; - } - /** * function to format the field values for input to the api * @@ -609,42 +447,6 @@ pppp * @return void return $params; } - function getSelectValues() { - $values = array(); - foreach ($this->_fields as $name => $field) { - $values[$name] = $field->_title; - } - return $values; - } - - function getSelectTypes() { - $values = array(); - foreach ($this->_fields as $name => $field) { - if (isset($field->_hasLocationType)) { - $values[$name] = $field->_hasLocationType; - } - } - return $values; - } - - function getHeaderPatterns() { - $values = array(); - foreach ($this->_fields as $name => $field) { - if (isset($field->_headerPattern)) { - $values[$name] = $field->_headerPattern; - } - } - return $values; - } - - function getDataPatterns() { - $values = array(); - foreach ($this->_fields as $name => $field) { - $values[$name] = $field->_dataPattern; - } - return $values; - } - function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') { if (empty($name)) { $this->_fields['doNotImport'] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern); @@ -662,18 +464,6 @@ pppp * @return void } } - /** - * setter function - * - * @param int $max - * - * @return void - * @access public - */ - function setMaxLinesToProcess($max) { - $this->_maxLinesToProcess = $max; - } - /** * Store parser values * @@ -782,26 +572,6 @@ pppp * @return void fclose($fd); } - /** - * Remove single-quote enclosures from a value array (row) - * - * @param array $values - * @param string $enclosure - * - * @return void - * @static - * @access public - */ - static function encloseScrub(&$values, $enclosure = "'") { - if (empty($values)) { - return; - } - - foreach ($values as $k => $v) { - $values[$k] = preg_replace("/^$enclosure(.*)$enclosure$/", '$1', $v); - } - } - function errorFileName($type) { $fileName = NULL; if (empty($type)) { @@ -812,26 +582,6 @@ pppp * @return void $fileName = $config->uploadDir . "sqlImport"; switch ($type) { - case CRM_Import_Parser::ERROR: - case CRM_Import_Parser::NO_MATCH: - case CRM_Import_Parser::CONFLICT: - case CRM_Import_Parser::DUPLICATE: - //here constants get collides. - if ($type == CRM_Import_Parser::ERROR) { - $type = CRM_Import_Parser::ERROR; - } - elseif ($type == CRM_Import_Parser::NO_MATCH) { - $type = CRM_Import_Parser::NO_MATCH; - } - elseif ($type == CRM_Import_Parser::CONFLICT) { - $type = CRM_Import_Parser::CONFLICT; - } - else { - $type = CRM_Import_Parser::DUPLICATE; - } - $fileName = CRM_Contact_Import_Parser::errorFileName($type); - break; - case CRM_Contribute_Import_Parser::SOFT_CREDIT_ERROR: $fileName .= '.softCreditErrors'; break; @@ -839,6 +589,10 @@ pppp * @return void case CRM_Contribute_Import_Parser::PLEDGE_PAYMENT_ERROR: $fileName .= '.pledgePaymentErrors'; break; + + default: + $fileName = parent::errorFileName($type); + break; } return $fileName; @@ -851,26 +605,6 @@ pppp * @return void } switch ($type) { - case CRM_Import_Parser::ERROR: - case CRM_Import_Parser::NO_MATCH: - case CRM_Import_Parser::CONFLICT: - case CRM_Import_Parser::DUPLICATE: - //here constants get collides. - if ($type == CRM_Import_Parser::ERROR) { - $type = CRM_Import_Parser::ERROR; - } - elseif ($type == CRM_Import_Parser::NO_MATCH) { - $type = CRM_Import_Parser::NO_MATCH; - } - elseif ($type == CRM_Import_Parser::CONFLICT) { - $type = CRM_Import_Parser::CONFLICT; - } - else { - $type = CRM_Import_Parser::DUPLICATE; - } - $fileName = CRM_Contact_Import_Parser::saveFileName($type); - break; - case CRM_Contribute_Import_Parser::SOFT_CREDIT_ERROR: $fileName = 'Import_Soft_Credit_Errors.csv'; break; @@ -878,6 +612,10 @@ pppp * @return void case CRM_Contribute_Import_Parser::PLEDGE_PAYMENT_ERROR: $fileName = 'Import_Pledge_Payment_Errors.csv'; break; + + default: + $fileName = parent::saveFileName($type); + break; } return $fileName; diff --git a/CRM/Event/Import/Parser.php b/CRM/Event/Import/Parser.php index 06813afcc4..2ba6883b9d 100644 --- a/CRM/Event/Import/Parser.php +++ b/CRM/Event/Import/Parser.php @@ -58,123 +58,6 @@ abstract class CRM_Event_Import_Parser extends CRM_Import_Parser { */ protected $_lineCount; - /** - * total number of non empty lines - */ - protected $_totalCount; - - /** - * running total number of valid lines - */ - protected $_validCount; - - /** - * running total number of invalid rows - */ - protected $_invalidRowCount; - - /** - * maximum number of invalid rows to store - */ - protected $_maxErrorCount; - - /** - * array of error lines, bounded by MAX_ERROR - */ - protected $_errors; - - /** - * total number of conflict lines - */ - protected $_conflictCount; - - /** - * array of conflict lines - */ - protected $_conflicts; - - /** - * total number of duplicate (from database) lines - */ - protected $_duplicateCount; - - /** - * array of duplicate lines - */ - protected $_duplicates; - - /** - * running total number of warnings - */ - protected $_warningCount; - - /** - * maximum number of warnings to store - */ - protected $_maxWarningCount = self::MAX_WARNINGS; - - /** - * array of warning lines, bounded by MAX_WARNING - */ - protected $_warnings; - - /** - * array of all the fields that could potentially be part - * of this import process - * @var array - */ - protected $_fields; - - /** - * array of the fields that are actually part of the import process - * the position in the array also dictates their position in the import - * file - * @var array - */ - protected $_activeFields; - - /** - * cache the count of active fields - * - * @var int - */ - protected $_activeFieldCount; - - /** - * maximum number of non-empty/comment lines to process - * - * @var int - */ - protected $_maxLinesToProcess; - - /** - * cache of preview rows - * - * @var array - */ - protected $_rows; - - /** - * filename of error data - * - * @var string - */ - protected $_errorFileName; - - /** - * filename of conflict data - * - * @var string - */ - protected $_conflictFileName; - - /** - * filename of duplicate data - * - * @var string - */ - protected $_duplicateFileName; - /** * whether the file has a column header or not * @@ -182,19 +65,6 @@ abstract class CRM_Event_Import_Parser extends CRM_Import_Parser { */ protected $_haveColumnHeader; - /** - * contact type - * - * @var int - */ - - public $_contactType; - function __construct() { - $this->_maxLinesToProcess = 0; - $this->_maxErrorCount = self::MAX_ERRORS; - } - - abstract function init(); function run($fileName, $seperator = ',', &$mapper, @@ -406,13 +276,6 @@ abstract class CRM_Event_Import_Parser extends CRM_Import_Parser { return $this->fini(); } - abstract function mapField(&$values); - abstract function preview(&$values); - abstract function summary(&$values); - abstract function import($onDuplicate, &$values); - - abstract function fini(); - /** * Given a list of the importable field keys that the user has selected * set the active fields array to this list @@ -434,43 +297,6 @@ abstract class CRM_Event_Import_Parser extends CRM_Import_Parser { } } - /*function setActiveFieldLocationTypes( $elements ) - { - for ($i = 0; $i < count( $elements ); $i++) { - $this->_activeFields[$i]->_hasLocationType = $elements[$i]; - } - } - - function setActiveFieldPhoneTypes( $elements ) - { - for ($i = 0; $i < count( $elements ); $i++) { - $this->_activeFields[$i]->_phoneType = $elements[$i]; - } - }*/ - function setActiveFieldValues($elements, &$erroneousField) { - $maxCount = count($elements) < $this->_activeFieldCount ? count($elements) : $this->_activeFieldCount; - for ($i = 0; $i < $maxCount; $i++) { - $this->_activeFields[$i]->setValue($elements[$i]); - } - - // reset all the values that we did not have an equivalent import element - for (; $i < $this->_activeFieldCount; $i++) { - $this->_activeFields[$i]->resetValue(); - } - - // now validate the fields and return false if error - $valid = self::VALID; - for ($i = 0; $i < $this->_activeFieldCount; $i++) { - if (!$this->_activeFields[$i]->validate()) { - // no need to do any more validation - $erroneousField = $i; - $valid = self::ERROR; - break; - } - } - return $valid; - } - /** * function to format the field values for input to the api * @@ -491,42 +317,6 @@ abstract class CRM_Event_Import_Parser extends CRM_Import_Parser { return $params; } - function getSelectValues() { - $values = array(); - foreach ($this->_fields as $name => $field) { - $values[$name] = $field->_title; - } - return $values; - } - - function getSelectTypes() { - $values = array(); - foreach ($this->_fields as $name => $field) { - if (isset($field->_hasLocationType)) { - $values[$name] = $field->_hasLocationType; - } - } - return $values; - } - - function getHeaderPatterns() { - $values = array(); - foreach ($this->_fields as $name => $field) { - if (isset($field->_headerPattern)) { - $values[$name] = $field->_headerPattern; - } - } - return $values; - } - - function getDataPatterns() { - $values = array(); - foreach ($this->_fields as $name => $field) { - $values[$name] = $field->_dataPattern; - } - return $values; - } - function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') { if (empty($name)) { $this->_fields['doNotImport'] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern); @@ -546,18 +336,6 @@ abstract class CRM_Event_Import_Parser extends CRM_Import_Parser { } } - /** - * setter function - * - * @param int $max - * - * @return void - * @access public - */ - function setMaxLinesToProcess($max) { - $this->_maxLinesToProcess = $max; - } - /** * Store parser values * @@ -653,34 +431,5 @@ abstract class CRM_Event_Import_Parser extends CRM_Import_Parser { fclose($fd); } - /** - * Remove single-quote enclosures from a value array (row) - * - * @param array $values - * @param string $enclosure - * - * @return void - * @static - * @access public - */ - static function encloseScrub(&$values, $enclosure = "'") { - if (empty($values)) { - return; - } - - foreach ($values as $k => $v) { - $values[$k] = preg_replace("/^$enclosure(.*) $enclosure$/", '$1', $v); - } - } - - function errorFileName($type) { - $fileName = CRM_Contact_Import_Parser::errorFileName($type); - return $fileName; - } - - function saveFileName($type) { - $fileName = CRM_Contact_Import_Parser::saveFileName($type); - return $fileName; - } } diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index d5ccb4c37a..b16b4386cb 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -59,4 +59,343 @@ abstract class CRM_Import_Parser { * Contact types */ CONST CONTACT_INDIVIDUAL = 1, CONTACT_HOUSEHOLD = 2, CONTACT_ORGANIZATION = 4; + + + /** + * total number of non empty lines + */ + protected $_totalCount; + + /** + * running total number of valid lines + */ + protected $_validCount; + + /** + * running total number of invalid rows + */ + protected $_invalidRowCount; + + /** + * maximum number of non-empty/comment lines to process + * + * @var int + */ + protected $_maxLinesToProcess; + + /** + * maximum number of invalid rows to store + */ + protected $_maxErrorCount; + + /** + * array of error lines, bounded by MAX_ERROR + */ + protected $_errors; + + /** + * total number of conflict lines + */ + protected $_conflictCount; + + /** + * array of conflict lines + */ + protected $_conflicts; + + /** + * total number of duplicate (from database) lines + */ + protected $_duplicateCount; + + /** + * array of duplicate lines + */ + protected $_duplicates; + + /** + * running total number of warnings + */ + protected $_warningCount; + + /** + * maximum number of warnings to store + */ + protected $_maxWarningCount = self::MAX_WARNINGS; + + /** + * array of warning lines, bounded by MAX_WARNING + */ + protected $_warnings; + + /** + * array of all the fields that could potentially be part + * of this import process + * @var array + */ + protected $_fields; + + /** + * array of the fields that are actually part of the import process + * the position in the array also dictates their position in the import + * file + * @var array + */ + protected $_activeFields; + + /** + * cache the count of active fields + * + * @var int + */ + protected $_activeFieldCount; + + /** + * cache of preview rows + * + * @var array + */ + protected $_rows; + + /** + * filename of error data + * + * @var string + */ + protected $_errorFileName; + + /** + * filename of conflict data + * + * @var string + */ + protected $_conflictFileName; + + /** + * filename of duplicate data + * + * @var string + */ + protected $_duplicateFileName; + + /** + * contact type + * + * @var int + */ + public $_contactType; + + /** + * Class constructor + */ + function __construct() { + $this->_maxLinesToProcess = 0; + $this->_maxErrorCount = self::MAX_ERRORS; + } + + /** + * Abstract function definitions + */ + abstract function init(); + abstract function fini(); + abstract function mapField(&$values); + abstract function preview(&$values); + abstract function summary(&$values); + abstract function import($onDuplicate, &$values); + + /** + * Set and validate field values + * + * @param $elements: array + * @param $erroneousField: reference + */ + function setActiveFieldValues($elements, &$erroneousField) { + $maxCount = count($elements) < $this->_activeFieldCount ? count($elements) : $this->_activeFieldCount; + for ($i = 0; $i < $maxCount; $i++) { + $this->_activeFields[$i]->setValue($elements[$i]); + } + + // reset all the values that we did not have an equivalent import element + for (; $i < $this->_activeFieldCount; $i++) { + $this->_activeFields[$i]->resetValue(); + } + + // now validate the fields and return false if error + $valid = self::VALID; + for ($i = 0; $i < $this->_activeFieldCount; $i++) { + if (!$this->_activeFields[$i]->validate()) { + // no need to do any more validation + $erroneousField = $i; + $valid = self::ERROR; + break; + } + } + return $valid; + } + + /** + * Format the field values for input to the api + * + * @return array (reference) associative array of name/value pairs + * @access public + */ + function &getActiveFieldParams() { + $params = array(); + for ($i = 0; $i < $this->_activeFieldCount; $i++) { + if (isset($this->_activeFields[$i]->_value) + && !isset($params[$this->_activeFields[$i]->_name]) + && !isset($this->_activeFields[$i]->_related) + ) { + + $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value; + } + } + return $params; + } + + function getSelectValues() { + $values = array(); + foreach ($this->_fields as $name => $field) { + $values[$name] = $field->_title; + } + return $values; + } + + function getSelectTypes() { + $values = array(); + foreach ($this->_fields as $name => $field) { + if (isset($field->_hasLocationType)) { + $values[$name] = $field->_hasLocationType; + } + } + return $values; + } + + function getHeaderPatterns() { + $values = array(); + foreach ($this->_fields as $name => $field) { + if (isset($field->_headerPattern)) { + $values[$name] = $field->_headerPattern; + } + } + return $values; + } + + function getDataPatterns() { + $values = array(); + foreach ($this->_fields as $name => $field) { + $values[$name] = $field->_dataPattern; + } + return $values; + } + + /** + * Remove single-quote enclosures from a value array (row) + * + * @param array $values + * @param string $enclosure + * + * @return void + * @static + * @access public + */ + static function encloseScrub(&$values, $enclosure = "'") { + if (empty($values)) { + return; + } + + foreach ($values as $k => $v) { + $values[$k] = preg_replace("/^$enclosure(.*)$enclosure$/", '$1', $v); + } + } + + /** + * setter function + * + * @param int $max + * + * @return void + * @access public + */ + function setMaxLinesToProcess($max) { + $this->_maxLinesToProcess = $max; + } + + /** + * Determines the file extension based on error code + * + * @var $type error code constant + * @return string + * @static + */ + static function errorFileName($type) { + $fileName = NULL; + if (empty($type)) { + return $fileName; + } + + $config = CRM_Core_Config::singleton(); + $fileName = $config->uploadDir . "sqlImport"; + switch ($type) { + case self::ERROR: + $fileName .= '.errors'; + break; + + case self::CONFLICT: + $fileName .= '.conflicts'; + break; + + case self::DUPLICATE: + $fileName .= '.duplicates'; + break; + + case self::NO_MATCH: + $fileName .= '.mismatch'; + break; + + case self::UNPARSED_ADDRESS_WARNING: + $fileName .= '.unparsedAddress'; + break; + } + + return $fileName; + } + + /** + * Determines the file name based on error code + * + * @var $type error code constant + * @return string + * @static + */ + static function saveFileName($type) { + $fileName = NULL; + if (empty($type)) { + return $fileName; + } + switch ($type) { + case self::ERROR: + $fileName = 'Import_Errors.csv'; + break; + + case self::CONFLICT: + $fileName = 'Import_Conflicts.csv'; + break; + + case self::DUPLICATE: + $fileName = 'Import_Duplicates.csv'; + break; + + case self::NO_MATCH: + $fileName = 'Import_Mismatch.csv'; + break; + + case self::UNPARSED_ADDRESS_WARNING: + $fileName = 'Import_Unparsed_Address.csv'; + break; + } + + return $fileName; + } + } diff --git a/CRM/Member/Import/Parser.php b/CRM/Member/Import/Parser.php index 4b733e6906..70338e2800 100644 --- a/CRM/Member/Import/Parser.php +++ b/CRM/Member/Import/Parser.php @@ -58,123 +58,6 @@ abstract class CRM_Member_Import_Parser extends CRM_Import_Parser { */ protected $_lineCount; - /** - * total number of non empty lines - */ - protected $_totalCount; - - /** - * running total number of valid lines - */ - protected $_validCount; - - /** - * running total number of invalid rows - */ - protected $_invalidRowCount; - - /** - * maximum number of invalid rows to store - */ - protected $_maxErrorCount; - - /** - * array of error lines, bounded by MAX_ERROR - */ - protected $_errors; - - /** - * total number of conflict lines - */ - protected $_conflictCount; - - /** - * array of conflict lines - */ - protected $_conflicts; - - /** - * total number of duplicate (from database) lines - */ - protected $_duplicateCount; - - /** - * array of duplicate lines - */ - protected $_duplicates; - - /** - * running total number of warnings - */ - protected $_warningCount; - - /** - * maximum number of warnings to store - */ - protected $_maxWarningCount = self::MAX_WARNINGS; - - /** - * array of warning lines, bounded by MAX_WARNING - */ - protected $_warnings; - - /** - * array of all the fields that could potentially be part - * of this import process - * @var array - */ - protected $_fields; - - /** - * array of the fields that are actually part of the import process - * the position in the array also dictates their position in the import - * file - * @var array - */ - protected $_activeFields; - - /** - * cache the count of active fields - * - * @var int - */ - protected $_activeFieldCount; - - /** - * maximum number of non-empty/comment lines to process - * - * @var int - */ - protected $_maxLinesToProcess; - - /** - * cache of preview rows - * - * @var array - */ - protected $_rows; - - /** - * filename of error data - * - * @var string - */ - protected $_errorFileName; - - /** - * filename of conflict data - * - * @var string - */ - protected $_conflictFileName; - - /** - * filename of duplicate data - * - * @var string - */ - protected $_duplicateFileName; - /** * whether the file has a column header or not * @@ -182,19 +65,6 @@ abstract class CRM_Member_Import_Parser extends CRM_Import_Parser { */ protected $_haveColumnHeader; - /** - * contact type - * - * @var int - */ - - public $_contactType; - function __construct() { - $this->_maxLinesToProcess = 0; - $this->_maxErrorCount = self::MAX_ERRORS; - } - - abstract function init(); function run($fileName, $seperator = ',', &$mapper, @@ -396,13 +266,6 @@ abstract class CRM_Member_Import_Parser extends CRM_Import_Parser { return $this->fini(); } - abstract function mapField(&$values); - abstract function preview(&$values); - abstract function summary(&$values); - abstract function import($onDuplicate, &$values); - - abstract function fini(); - /** * Given a list of the importable field keys that the user has selected * set the active fields array to this list @@ -424,41 +287,6 @@ abstract class CRM_Member_Import_Parser extends CRM_Import_Parser { } } - /*function setActiveFieldLocationTypes( $elements ) { - for ($i = 0; $i < count( $elements ); $i++) { - $this->_activeFields[$i]->_hasLocationType = $elements[$i]; - } - } - - function setActiveFieldPhoneTypes( $elements ) { - for ($i = 0; $i < count( $elements ); $i++) { - $this->_activeFields[$i]->_phoneType = $elements[$i]; - } - }*/ - function setActiveFieldValues($elements, &$erroneousField) { - $maxCount = count($elements) < $this->_activeFieldCount ? count($elements) : $this->_activeFieldCount; - for ($i = 0; $i < $maxCount; $i++) { - $this->_activeFields[$i]->setValue($elements[$i]); - } - - // reset all the values that we did not have an equivalent import element - for (; $i < $this->_activeFieldCount; $i++) { - $this->_activeFields[$i]->resetValue(); - } - - // now validate the fields and return false if error - $valid = self::VALID; - for ($i = 0; $i < $this->_activeFieldCount; $i++) { - if (!$this->_activeFields[$i]->validate()) { - // no need to do any more validation - $erroneousField = $i; - $valid = self::ERROR; - break; - } - } - return $valid; - } - /** * function to format the field values for input to the api * @@ -479,42 +307,6 @@ abstract class CRM_Member_Import_Parser extends CRM_Import_Parser { return $params; } - function getSelectValues() { - $values = array(); - foreach ($this->_fields as $name => $field) { - $values[$name] = $field->_title; - } - return $values; - } - - function getSelectTypes() { - $values = array(); - foreach ($this->_fields as $name => $field) { - if (isset($field->_hasLocationType)) { - $values[$name] = $field->_hasLocationType; - } - } - return $values; - } - - function getHeaderPatterns() { - $values = array(); - foreach ($this->_fields as $name => $field) { - if (isset($field->_headerPattern)) { - $values[$name] = $field->_headerPattern; - } - } - return $values; - } - - function getDataPatterns() { - $values = array(); - foreach ($this->_fields as $name => $field) { - $values[$name] = $field->_dataPattern; - } - return $values; - } - function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') { if (empty($name)) { $this->_fields['doNotImport'] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern); @@ -534,18 +326,6 @@ abstract class CRM_Member_Import_Parser extends CRM_Import_Parser { } } - /** - * setter function - * - * @param int $max - * - * @return void - * @access public - */ - function setMaxLinesToProcess($max) { - $this->_maxLinesToProcess = $max; - } - /** * Store parser values * @@ -641,34 +421,5 @@ abstract class CRM_Member_Import_Parser extends CRM_Import_Parser { fclose($fd); } - /** - * Remove single-quote enclosures from a value array (row) - * - * @param array $values - * @param string $enclosure - * - * @return void - * @static - * @access public - */ - static function encloseScrub(&$values, $enclosure = "'") { - if (empty($values)) { - return; - } - - foreach ($values as $k => $v) { - $values[$k] = preg_replace("/^$enclosure(.*)$enclosure$/", '$1', $v); - } - } - - function errorFileName($type) { - $fileName = CRM_Contact_Import_Parser::errorFileName($type); - return $fileName; - } - - function saveFileName($type) { - $fileName = CRM_Contact_Import_Parser::saveFileName($type); - return $fileName; - } } -- 2.25.1