INFRA-132 - CRM/Contact - Misc
[civicrm-core.git] / CRM / Contact / Import / Parser / Contact.php
index 1aa47bb11f5699ff7ec6871b67f58029b65054ee..6ba3beb7a47e12395c0c42959aaab3d663c48916 100644 (file)
@@ -102,7 +102,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
   /**
    * Class constructor
    */
-  function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL, $mapperImProvider = NULL, $mapperRelated = NULL, $mapperRelatedContactType = NULL, $mapperRelatedContactDetails = NULL, $mapperRelatedContactLocType = NULL, $mapperRelatedContactPhoneType = NULL, $mapperRelatedContactImProvider = NULL,
+  function __construct(
+    &$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL, $mapperImProvider = NULL, $mapperRelated = NULL, $mapperRelatedContactType = NULL, $mapperRelatedContactDetails = NULL, $mapperRelatedContactLocType = NULL, $mapperRelatedContactPhoneType = NULL, $mapperRelatedContactImProvider = NULL,
     $mapperWebsiteType = NULL, $mapperRelatedContactWebsiteType = NULL
   ) {
     parent::__construct();
@@ -166,7 +167,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
     if (!empty($relationshipType)) {
       $fields = array_merge($fields, array(
         'related' => array(
-           'title' => ts('- related contact info -'),
+          'title' => ts('- related contact info -'),
         ),
       ), $relationshipType);
     }
@@ -233,9 +234,10 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
 
     $this->_updateWithId = FALSE;
     if (in_array('id', $this->_mapperKeys) || ($this->_externalIdentifierIndex >= 0 && in_array($this->_onDuplicate, array(
-      CRM_Import_Parser::DUPLICATE_UPDATE,
-      CRM_Import_Parser::DUPLICATE_FILL,
-      )))) {
+          CRM_Import_Parser::DUPLICATE_UPDATE,
+          CRM_Import_Parser::DUPLICATE_FILL,
+        )))
+    ) {
       $this->_updateWithId = TRUE;
     }
 
@@ -481,9 +483,10 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
 
     //check if external identifier exists in database
     if (!empty($params['external_identifier']) && (!empty($params['id']) || in_array($onDuplicate, array(
-            CRM_Import_Parser::DUPLICATE_SKIP,
-            CRM_Import_Parser::DUPLICATE_NOCHECK,
-          )))) {
+          CRM_Import_Parser::DUPLICATE_SKIP,
+          CRM_Import_Parser::DUPLICATE_NOCHECK,
+        )))
+    ) {
 
       if ($internalCid = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['external_identifier'], 'id', 'external_identifier')) {
         if ($internalCid != CRM_Utils_Array::value('id', $params)) {
@@ -562,8 +565,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
                   $this->_retCode = CRM_Import_Parser::NO_MATCH;
                 }
                 else {
-                  $newContact     = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId, FALSE, $this->_dedupeRuleGroupID);
-                  $updateflag     = FALSE;
+                  $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId, FALSE, $this->_dedupeRuleGroupID);
+                  $updateflag = FALSE;
                   $this->_retCode = CRM_Import_Parser::VALID;
                 }
               }
@@ -684,7 +687,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
 
       $relationship = TRUE;
       # see CRM-10433 - might return comma separate list of all dupes
-      $dupeContactIDs = explode(',',$newContact['error_message']['params'][0]);
+      $dupeContactIDs = explode(',', $newContact['error_message']['params'][0]);
       $dupeCount = count($dupeContactIDs);
       $contactID = array_pop($dupeContactIDs);
       // check to see if we had more than one duplicate contact id.
@@ -764,9 +767,10 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
           }
           // check for valid related contact id in update/fill mode, CRM-4424
           if (in_array($onDuplicate, array(
-                CRM_Import_Parser::DUPLICATE_UPDATE,
-                CRM_Import_Parser::DUPLICATE_FILL,
-              )) && !empty($params[$key]['id'])) {
+              CRM_Import_Parser::DUPLICATE_UPDATE,
+              CRM_Import_Parser::DUPLICATE_FILL,
+            )) && !empty($params[$key]['id'])
+          ) {
             $relatedContactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_type');
             if (!$relatedContactType) {
               $errorMessage = ts("No contact found for this related contact ID: %1", array(1 => $params[$key]['id']));
@@ -782,7 +786,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
               }
 
               if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType::isAllowEdit($params[$key]['id'], $relatedCsType) &&
-                $relatedCsType != CRM_Utils_Array::value('contact_sub_type', $formatting))) {
+                  $relatedCsType != CRM_Utils_Array::value('contact_sub_type', $formatting))
+              ) {
                 $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.") . ' ' . ts("ID: %1", array(1 => $params[$key]['id']));
                 array_unshift($values, $errorMessage);
                 return CRM_Import_Parser::NO_MATCH;
@@ -846,9 +851,9 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
           }
           // update/fill related contact after getting matching Contact Ids, CRM-4424
           if (in_array($onDuplicate, array(
-                CRM_Import_Parser::DUPLICATE_UPDATE,
-                CRM_Import_Parser::DUPLICATE_FILL,
-              ))) {
+            CRM_Import_Parser::DUPLICATE_UPDATE,
+            CRM_Import_Parser::DUPLICATE_FILL,
+          ))) {
             //validation of related contact subtype for update mode
             //CRM-5125
             $relatedCsType = NULL;
@@ -922,12 +927,12 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
               if ($valid) {
                 $allRelationships = CRM_Core_PseudoConstant::relationshipType('name');
                 $relationshipTypeId = str_replace(array(
-                    '_a_b',
-                    '_b_a',
-                  ), array(
-                    '',
-                    '',
-                  ), $key);
+                  '_a_b',
+                  '_b_a',
+                ), array(
+                  '',
+                  '',
+                ), $key);
                 $relationshipType = str_replace($relationshipTypeId . '_', '', $key);
                 $orgId = $individualId = NULL;
                 if ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employee of') {
@@ -1090,7 +1095,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
    *
    * @return void
    */
-  public function fini() {}
+  public function fini() {
+  }
 
   /**
    * Check if an error in custom data
@@ -1126,8 +1132,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
           self::addToErrorMsg(ts('field ID'), $errorMessage);
         }
         // validate null values for required custom fields of type boolean
-        if (!empty($customFields[$customFieldID]['is_required']) && (empty($params['custom_'.$customFieldID]) && !is_numeric($params['custom_'.$customFieldID])) && $customFields[$customFieldID]['data_type'] == 'Boolean') {
-          self::addToErrorMsg($customFields[$customFieldID]['label'].'::'.$customFields[$customFieldID]['groupTitle'], $errorMessage);
+        if (!empty($customFields[$customFieldID]['is_required']) && (empty($params['custom_' . $customFieldID]) && !is_numeric($params['custom_' . $customFieldID])) && $customFields[$customFieldID]['data_type'] == 'Boolean') {
+          self::addToErrorMsg($customFields[$customFieldID]['label'] . '::' . $customFields[$customFieldID]['groupTitle'], $errorMessage);
         }
 
         //For address custom fields, we do get actual custom field value as an inner array of
@@ -1151,7 +1157,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
           }
           elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
             if (CRM_Utils_String::strtoboolstr($value) === FALSE) {
-              self::addToErrorMsg($customFields[$customFieldID]['label'].'::'.$customFields[$customFieldID]['groupTitle'], $errorMessage);
+              self::addToErrorMsg($customFields[$customFieldID]['label'] . '::' . $customFields[$customFieldID]['groupTitle'], $errorMessage);
             }
           }
           // need not check for label filed import
@@ -1173,9 +1179,9 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
 
           // check for values for custom fields for checkboxes and multiselect
           if ($customFields[$customFieldID]['html_type'] == 'CheckBox' || $customFields[$customFieldID]['html_type'] == 'AdvMulti-Select' || $customFields[$customFieldID]['html_type'] == 'Multi-Select') {
-            $value        = trim($value);
-            $value        = str_replace('|', ',', $value);
-            $mulValues    = explode(',', $value);
+            $value = trim($value);
+            $value = str_replace('|', ',', $value);
+            $mulValues = explode(',', $value);
             $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
             foreach ($mulValues as $v1) {
               if (strlen($v1) == 0) {
@@ -1230,10 +1236,10 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
 
                 $error = TRUE;
                 foreach (array(
-                    $countryNames,
-                    $countryIsoCodes,
-                    $limitCodes,
-                  ) as $values) {
+                           $countryNames,
+                           $countryIsoCodes,
+                           $limitCodes,
+                         ) as $values) {
                   if (in_array(trim($countryValue), $values)) {
                     $error = FALSE;
                     break;
@@ -1398,7 +1404,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
               foreach ($value as $stateValue) {
                 if ($stateValue['state_province']) {
                   if (self::in_value($stateValue['state_province'], CRM_Core_PseudoConstant::stateProvinceAbbreviation()) ||
-                      self::in_value($stateValue['state_province'], CRM_Core_PseudoConstant::stateProvince())) {
+                    self::in_value($stateValue['state_province'], CRM_Core_PseudoConstant::stateProvince())
+                  ) {
                     continue;
                   }
                   else {
@@ -1633,7 +1640,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
     $newContact = NULL;
 
     if (is_null($contactId) && ($onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK)) {
-      $dupeCheck = (bool)($onDuplicate);
+      $dupeCheck = (bool) ($onDuplicate);
     }
 
     //get the prefix id etc if exists
@@ -1739,10 +1746,10 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
         continue;
       }
       elseif (in_array($key, array(
-            'email_greeting',
-            'postal_greeting',
-            'addressee',
-          ))) {
+        'email_greeting',
+        'postal_greeting',
+        'addressee',
+      ))) {
         // CRM-4575, need to null custom
         if ($params["{$key}_id"] != 4) {
           $params["{$key}_custom"] = 'null';
@@ -1864,7 +1871,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
     foreach ($params as $key => $val) {
       $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key);
       if ($customFieldID &&
-          !array_key_exists($customFieldID, $addressCustomFields)) {
+        !array_key_exists($customFieldID, $addressCustomFields)
+      ) {
         //we should not update Date to null, CRM-4062
         if ($val && ($customFields[$customFieldID]['data_type'] == 'Date')) {
           self::formatCustomDate($params, $formatted, $dateType, $key);
@@ -1879,21 +1887,21 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
           }
         }
 
-      if ($key == 'birth_date' && $val) {
-        CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key);
-      }
-      elseif ($key == 'deceased_date' && $val) {
-        CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key);
-      }
-      elseif ($key == 'is_deceased' && $val) {
-        $params[$key] = CRM_Utils_String::strtoboolstr($val);
-      }
-      elseif ($key == 'gender') {
-        //CRM-4360
-        $params[$key] = $this->checkGender($val);
+        if ($key == 'birth_date' && $val) {
+          CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key);
+        }
+        elseif ($key == 'deceased_date' && $val) {
+          CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key);
+        }
+        elseif ($key == 'is_deceased' && $val) {
+          $params[$key] = CRM_Utils_String::strtoboolstr($val);
+        }
+        elseif ($key == 'gender') {
+          //CRM-4360
+          $params[$key] = $this->checkGender($val);
+        }
       }
     }
-   }
 
     //now format custom data.
     foreach ($params as $key => $field) {
@@ -1948,57 +1956,61 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
       //Handling Custom Data
       // note: Address custom fields will be handled separately inside _civicrm_api3_deprecated_add_formatted_param
       if (($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) &&
-          array_key_exists($customFieldID, $customFields) &&
-          !array_key_exists($customFieldID, $addressCustomFields)) {
+        array_key_exists($customFieldID, $customFields) &&
+        !array_key_exists($customFieldID, $addressCustomFields)
+      ) {
 
         $extends = CRM_Utils_Array::value('extends', $customFields[$customFieldID]);
-        $htmlType = CRM_Utils_Array::value( 'html_type', $customFields[$customFieldID] );
-        switch ( $htmlType ) {
-        case 'Select':
-        case 'Radio':
-        case 'Autocomplete-Select':
-          if ($customFields[$customFieldID]['data_type'] == 'String') {
-            $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
-            foreach ($customOption as $customFldID => $customValue) {
-              $val = CRM_Utils_Array::value('value', $customValue);
-              $label = CRM_Utils_Array::value('label', $customValue);
-              $label = strtolower($label);
-              $value = strtolower(trim($formatted[$key]));
-              if (($value == $label) || ($value == strtolower($val))) {
-                $params[$key] = $formatted[$key] = $val;
+        $htmlType = CRM_Utils_Array::value('html_type', $customFields[$customFieldID]);
+        switch ($htmlType) {
+          case 'Select':
+          case 'Radio':
+          case 'Autocomplete-Select':
+            if ($customFields[$customFieldID]['data_type'] == 'String') {
+              $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
+              foreach ($customOption as $customFldID => $customValue) {
+                $val = CRM_Utils_Array::value('value', $customValue);
+                $label = CRM_Utils_Array::value('label', $customValue);
+                $label = strtolower($label);
+                $value = strtolower(trim($formatted[$key]));
+                if (($value == $label) || ($value == strtolower($val))) {
+                  $params[$key] = $formatted[$key] = $val;
+                }
               }
             }
-          }
-          break;
-        case 'CheckBox':
-        case 'AdvMulti-Select':
-        case 'Multi-Select':
-
-          if (!empty($formatted[$key]) && !empty($params[$key])) {
-            $mulValues       = explode( ',', $formatted[$key] );
-            $customOption    = CRM_Core_BAO_CustomOption::getCustomOption( $customFieldID, true );
-            $formatted[$key] = array( );
-            $params[$key]    = array( );
-            foreach ( $mulValues as $v1 ) {
-              foreach ( $customOption as $v2 ) {
-                if ( ( strtolower( $v2['label'] ) == strtolower( trim( $v1 ) ) ) ||
-                     ( strtolower( $v2['value'] ) == strtolower( trim( $v1 ) ) ) ) {
-                  if ( $htmlType == 'CheckBox' ) {
-                    $params[$key][$v2['value']] = $formatted[$key][$v2['value']] = 1;
-                  } else {
-                    $params[$key][] = $formatted[$key][] = $v2['value'];
+            break;
+          case 'CheckBox':
+          case 'AdvMulti-Select':
+          case 'Multi-Select':
+
+            if (!empty($formatted[$key]) && !empty($params[$key])) {
+              $mulValues = explode(',', $formatted[$key]);
+              $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
+              $formatted[$key] = array();
+              $params[$key] = array();
+              foreach ($mulValues as $v1) {
+                foreach ($customOption as $v2) {
+                  if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
+                    (strtolower($v2['value']) == strtolower(trim($v1)))
+                  ) {
+                    if ($htmlType == 'CheckBox') {
+                      $params[$key][$v2['value']] = $formatted[$key][$v2['value']] = 1;
+                    }
+                    else {
+                      $params[$key][] = $formatted[$key][] = $v2['value'];
+                    }
                   }
                 }
               }
             }
-          }
-          break;
+            break;
         }
       }
     }
 
     if (($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) && array_key_exists($customFieldID, $customFields) &&
-      !array_key_exists($customFieldID, $addressCustomFields)) {
+      !array_key_exists($customFieldID, $addressCustomFields)
+    ) {
       // @todo calling api functions directly is not supported
       _civicrm_api3_custom_format_params($params, $formatted, $extends);
     }
@@ -2006,10 +2018,10 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
     // to check if not update mode and unset the fields with empty value.
     if (!$this->_updateWithId && array_key_exists('custom', $formatted)) {
       foreach ($formatted['custom'] as $customKey => $customvalue) {
-        if (empty($formatted['custom'][$customKey][- 1]['is_required'])) {
+        if (empty($formatted['custom'][$customKey][-1]['is_required'])) {
           $formatted['custom'][$customKey][-1]['is_required'] = $customFields[$customKey]['is_required'];
         }
-        $emptyValue = CRM_Utils_Array::value('value', $customvalue[ - 1]);
+        $emptyValue = CRM_Utils_Array::value('value', $customvalue[-1]);
         if (!isset($emptyValue)) {
           unset($formatted['custom'][$customKey]);
         }