Merge pull request #2033 from JoeMurray/master
[civicrm-core.git] / CRM / Contribute / Import / Parser.php
index 2d7b5cc8a96c7eab5fe96f917ffcc56e907865b8..64aabcff19998c81d5407ef46706b63c6b7957bc 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
  */
 
 
-
-abstract class CRM_Contribute_Import_Parser {
-  CONST MAX_ERRORS = 250, MAX_WARNINGS = 25, VALID = 1, WARNING = 2, ERROR = 3, CONFLICT = 4, STOP = 5, DUPLICATE = 6, MULTIPLE_DUPE = 7, NO_MATCH = 8, SOFT_CREDIT = 9, SOFT_CREDIT_ERROR = 10, PLEDGE_PAYMENT = 11, PLEDGE_PAYMENT_ERROR = 12;
-
-  /**
-   * various parser modes
-   */
-  CONST MODE_MAPFIELD = 1, MODE_PREVIEW = 2, MODE_SUMMARY = 4, MODE_IMPORT = 8;
-
-  /**
-   * codes for duplicate record handling
-   */
-  CONST DUPLICATE_SKIP = 1, DUPLICATE_REPLACE = 2, DUPLICATE_UPDATE = 4, DUPLICATE_FILL = 8, DUPLICATE_NOCHECK = 16;
+abstract class CRM_Contribute_Import_Parser extends CRM_Import_Parser {
 
   /**
-   * various Contact types
+   * Contribution-specific result codes
+   * @see CRM_Import_Parser result code constants
    */
-  CONST CONTACT_INDIVIDUAL = 1, CONTACT_HOUSEHOLD = 2, CONTACT_ORGANIZATION = 4;
+  CONST SOFT_CREDIT = 512, SOFT_CREDIT_ERROR = 1024, PLEDGE_PAYMENT = 2048, PLEDGE_PAYMENT_ERROR = 4096;
 
   protected $_fileName;
 
@@ -75,21 +64,6 @@ abstract class CRM_Contribute_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
    */
@@ -110,16 +84,6 @@ abstract class CRM_Contribute_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
    */
@@ -130,84 +94,6 @@ abstract class CRM_Contribute_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
    *
@@ -222,20 +108,6 @@ abstract class CRM_Contribute_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
    *
@@ -243,27 +115,13 @@ abstract class CRM_Contribute_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,
     $skipColumnHeader = FALSE,
-    $mode             = self::MODE_PREVIEW,
-    $contactType      = self::CONTACT_INDIVIDUAL,
-    $onDuplicate      = self::DUPLICATE_SKIP
+    $mode = self::MODE_PREVIEW,
+    $contactType = self::CONTACT_INDIVIDUAL,
+    $onDuplicate = self::DUPLICATE_SKIP
   ) {
     if (!is_array($fileName)) {
       CRM_Core_Error::fatal();
@@ -535,13 +393,6 @@ abstract class CRM_Contribute_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
@@ -569,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
    *
@@ -620,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);
@@ -673,18 +464,6 @@ pppp   * @return void
     }
   }
 
-  /**
-   * setter function
-   *
-   * @param int $max
-   *
-   * @return void
-   * @access public
-   */
-  function setMaxLinesToProcess($max) {
-    $this->_maxLinesToProcess = $max;
-  }
-
   /**
    * Store parser values
    *
@@ -715,15 +494,15 @@ pppp   * @return void
 
     switch ($this->_contactType) {
       case 'Individual':
-        $store->set('contactType', CRM_Contribute_Import_Parser::CONTACT_INDIVIDUAL);
+        $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
         break;
 
       case 'Household':
-        $store->set('contactType', CRM_Contribute_Import_Parser::CONTACT_HOUSEHOLD);
+        $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD);
         break;
 
       case 'Organization':
-        $store->set('contactType', CRM_Contribute_Import_Parser::CONTACT_ORGANIZATION);
+        $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION);
     }
 
     if ($this->_invalidRowCount) {
@@ -793,27 +572,7 @@ 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) {
+  static function errorFileName($type) {
     $fileName = NULL;
     if (empty($type)) {
       return $fileName;
@@ -823,26 +582,6 @@ pppp   * @return void
     $fileName = $config->uploadDir . "sqlImport";
 
     switch ($type) {
-      case CRM_Contribute_Import_Parser::ERROR:
-      case CRM_Contribute_Import_Parser::NO_MATCH:
-      case CRM_Contribute_Import_Parser::CONFLICT:
-      case CRM_Contribute_Import_Parser::DUPLICATE:
-        //here constants get collides.
-        if ($type == CRM_Contribute_Import_Parser::ERROR) {
-          $type = CRM_Contact_Import_Parser::ERROR;
-        }
-        elseif ($type == CRM_Contribute_Import_Parser::NO_MATCH) {
-          $type = CRM_Contact_Import_Parser::NO_MATCH;
-        }
-        elseif ($type == CRM_Contribute_Import_Parser::CONFLICT) {
-          $type = CRM_Contact_Import_Parser::CONFLICT;
-        }
-        else {
-          $type = CRM_Contact_Import_Parser::DUPLICATE;
-        }
-        $fileName = CRM_Contact_Import_Parser::errorFileName($type);
-        break;
-
       case CRM_Contribute_Import_Parser::SOFT_CREDIT_ERROR:
         $fileName .= '.softCreditErrors';
         break;
@@ -850,38 +589,22 @@ pppp   * @return void
       case CRM_Contribute_Import_Parser::PLEDGE_PAYMENT_ERROR:
         $fileName .= '.pledgePaymentErrors';
         break;
+
+      default:
+        $fileName = parent::errorFileName($type);
+        break;
     }
 
     return $fileName;
   }
 
-  function saveFileName($type) {
+  static function saveFileName($type) {
     $fileName = NULL;
     if (empty($type)) {
       return $fileName;
     }
 
     switch ($type) {
-      case CRM_Contribute_Import_Parser::ERROR:
-      case CRM_Contribute_Import_Parser::NO_MATCH:
-      case CRM_Contribute_Import_Parser::CONFLICT:
-      case CRM_Contribute_Import_Parser::DUPLICATE:
-        //here constants get collides.
-        if ($type == CRM_Contribute_Import_Parser::ERROR) {
-          $type = CRM_Contact_Import_Parser::ERROR;
-        }
-        elseif ($type == CRM_Contribute_Import_Parser::NO_MATCH) {
-          $type = CRM_Contact_Import_Parser::NO_MATCH;
-        }
-        elseif ($type == CRM_Contribute_Import_Parser::CONFLICT) {
-          $type = CRM_Contact_Import_Parser::CONFLICT;
-        }
-        else {
-          $type = CRM_Contact_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;
@@ -889,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;