CRM-16055 migrate single create call away from multiple create and use create api
[civicrm-core.git] / CRM / Core / BAO / Address.php
index c74aa7a073f687e68db93ec80756a49e05d56db2..14aaa5664e0338478f8aba32e98972dced9d7a29 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -39,7 +39,7 @@
 class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
 
   /**
-   * Takes an associative array and creates a address
+   * Takes an associative array and creates a address.
    *
    * @param array $params
    *   (reference ) an assoc array of name/value pairs.
@@ -49,12 +49,12 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
    *
    * @param null $entity
    *
-   * @return array $blocks array of created address
-   * @static
+   * @return array|NULL
+   *   array of created address
    */
   public static function create(&$params, $fixAddress = TRUE, $entity = NULL) {
     if (!isset($params['address']) || !is_array($params['address'])) {
-      return;
+      return NULL;
     }
     CRM_Core_BAO_Block::sortPrimaryFirst($params['address']);
     $addresses = array();
@@ -133,7 +133,7 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
   }
 
   /**
-   * Takes an associative array and adds address
+   * Takes an associative array and adds address.
    *
    * @param array $params
    *   (reference ) an assoc array of name/value pairs.
@@ -141,8 +141,7 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
    *   True if you need to fix (format) address values.
    *                               before inserting in db
    *
-   * @return object       CRM_Core_BAO_Address object on success, null otherwise
-   * @static
+   * @return CRM_Core_BAO_Address|null
    */
   public static function add(&$params, $fixAddress) {
     static $customFields = NULL;
@@ -198,13 +197,12 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
   }
 
   /**
-   * Format the address params to have reasonable values
+   * Format the address params to have reasonable values.
    *
    * @param array $params
    *   (reference ) an assoc array of name/value pairs.
    *
    * @return void
-   * @static
    */
   public static function fixAddress(&$params) {
     if (!empty($params['billing_street_address'])) {
@@ -366,19 +364,22 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
       CRM_Utils_Address_USPS::checkAddress($params);
 
       // do street parsing again if enabled, since street address might have changed
-      $parseStreetAddress =
-        CRM_Utils_Array::value(
-          'street_address_parsing',
-          CRM_Core_BAO_Setting::valueOptions(
-            CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
-            'address_options'
-          ),
-          FALSE
-        );
+      $parseStreetAddress = CRM_Utils_Array::value(
+        'street_address_parsing',
+        CRM_Core_BAO_Setting::valueOptions(
+          CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
+          'address_options'
+        ),
+        FALSE
+      );
 
       if ($parseStreetAddress && !empty($params['street_address'])) {
         foreach (array(
-          'street_number', 'street_name', 'street_unit', 'street_number_suffix') as $fld) {
+                   'street_number',
+                   'street_name',
+                   'street_unit',
+                   'street_number_suffix',
+                 ) as $fld) {
           unset($params[$fld]);
         }
         // main parse string.
@@ -398,14 +399,12 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
   }
 
   /**
-   * Check if there is data to create the object
+   * Check if there is data to create the object.
    *
    * @param array $params
    *   (reference ) an assoc array of name/value pairs.
    *
-   * @return boolean
-   *
-   * @static
+   * @return bool
    */
   public static function dataExists(&$params) {
     //check if location type is set if not return false
@@ -416,7 +415,14 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
     $config = CRM_Core_Config::singleton();
     foreach ($params as $name => $value) {
       if (in_array($name, array(
-        'is_primary', 'location_type_id', 'id', 'contact_id', 'is_billing', 'display', 'master_id'))) {
+        'is_primary',
+        'location_type_id',
+        'id',
+        'contact_id',
+        'is_billing',
+        'display',
+        'master_id',
+      ))) {
         continue;
       }
       elseif (!CRM_Utils_System::isNull($value)) {
@@ -463,8 +469,8 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
    *   If microformat output is required.
    * @param int|string $fieldName conditional field name
    *
-   * @return array  $addresses     array with address fields
-   * @static
+   * @return array
+   *   array with address fields
    */
   public static function &getValues($entityBlock, $microformat = FALSE, $fieldName = 'contact_id') {
     if (empty($entityBlock)) {
@@ -492,12 +498,17 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
 
     $address->find();
 
+    $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
     $count = 1;
     while ($address->fetch()) {
       // deprecate reference.
       if ($count > 1) {
         foreach (array(
-          'state', 'state_name', 'country', 'world_region') as $fld) {
+                   'state',
+                   'state_name',
+                   'country',
+                   'world_region',
+                 ) as $fld) {
           if (isset($address->$fld)) {
             unset($address->$fld);
           }
@@ -508,6 +519,9 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
       CRM_Core_DAO::storeValues($address, $values);
 
       // add state and country information: CRM-369
+      if (!empty($address->location_type_id)) {
+        $values['location_type'] = CRM_Utils_Array::value($address->location_type_id, $locationTypes);
+      }
       if (!empty($address->state_province_id)) {
         $address->state = CRM_Core_PseudoConstant::stateProvinceAbbreviation($address->state_province_id, FALSE);
         $address->state_name = CRM_Core_PseudoConstant::stateProvince($address->state_province_id, FALSE);
@@ -589,8 +603,8 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
    *
    * @param bool $updateBlankLocInfo
    *
-   * @return array  the array of adrress data
-   * @static
+   * @return array
+   *   the array of adrress data
    */
   public static function allAddress($id, $updateBlankLocInfo = FALSE) {
     if (!$id) {
@@ -623,10 +637,10 @@ ORDER BY civicrm_address.is_primary DESC, address_id ASC";
    *
    * @param array $entityElements
    *   The array containing entity_id and.
-   * entity_table name
+   *   entity_table name
    *
-   * @return array  the array of adrress data
-   * @static
+   * @return array
+   *   the array of adrress data
    */
   public static function allEntityAddress(&$entityElements) {
     $addresses = array();
@@ -657,9 +671,10 @@ ORDER BY civicrm_address.is_primary DESC, civicrm_address.location_type_id DESC,
   }
 
   /**
-   * Get address sequence
+   * Get address sequence.
    *
-   * @return  array of address sequence.
+   * @return array
+   *   Array of address sequence.
    */
   public static function addressSequence() {
     $config = CRM_Core_Config::singleton();
@@ -682,7 +697,7 @@ ORDER BY civicrm_address.is_primary DESC, civicrm_address.location_type_id DESC,
         $addressSequence[$key] = 'city_postal_code';
       }
       elseif (
-        in_array($field, array('country', 'state_province', 'city', 'postal_code'))
+      in_array($field, array('country', 'state_province', 'city', 'postal_code'))
       ) {
         unset($addressSequence[$key]);
       }
@@ -699,11 +714,13 @@ ORDER BY civicrm_address.is_primary DESC, civicrm_address.location_type_id DESC,
    * NB: civic street formats for en_CA and fr_CA used by default if those locales are active
    *     otherwise en_US format is default action
    *
-   * @param string Street address including number and apt
-   * @param string Locale - to set locale used to parse address
+   * @param string $streetAddress
+   *   Street address including number and apt.
+   * @param string $locale
+   *   Locale used to parse address.
    *
-   * @return array    $parseFields    parsed fields values.
-   * @static
+   * @return array
+   *   parsed fields values.
    */
   public static function parseStreetAddress($streetAddress, $locale = NULL) {
     $config = CRM_Core_Config::singleton();
@@ -740,7 +757,10 @@ ORDER BY civicrm_address.is_primary DESC, civicrm_address.location_type_id DESC,
 
     $matches = array();
     if (in_array($locale, array(
-      'en_CA', 'fr_CA')) && preg_match('/^([A-Za-z0-9]+)[ ]*\-[ ]*/', $streetAddress, $matches)) {
+        'en_CA',
+        'fr_CA',
+      )) && preg_match('/^([A-Za-z0-9]+)[ ]*\-[ ]*/', $streetAddress, $matches)
+    ) {
       $parseFields['street_unit'] = $matches[1];
       // unset from rest of street address
       $streetAddress = preg_replace('/^([A-Za-z0-9]+)[ ]*\-[ ]*/', '', $streetAddress);
@@ -787,18 +807,53 @@ ORDER BY civicrm_address.is_primary DESC, civicrm_address.location_type_id DESC,
     // now get the street unit.
     // supportable street unit formats.
     $streetUnitFormats = array(
-      'APT', 'APARTMENT', 'BSMT', 'BASEMENT', 'BLDG', 'BUILDING',
-      'DEPT', 'DEPARTMENT', 'FL', 'FLOOR', 'FRNT', 'FRONT',
-      'HNGR', 'HANGER', 'LBBY', 'LOBBY', 'LOWR', 'LOWER',
-      'OFC', 'OFFICE', 'PH', 'PENTHOUSE', 'TRLR', 'TRAILER',
-      'UPPR', 'RM', 'ROOM', 'SIDE', 'SLIP', 'KEY',
-      'LOT', 'PIER', 'REAR', 'SPC', 'SPACE',
-      'STOP', 'STE', 'SUITE', 'UNIT', '#',
+      'APT',
+      'APARTMENT',
+      'BSMT',
+      'BASEMENT',
+      'BLDG',
+      'BUILDING',
+      'DEPT',
+      'DEPARTMENT',
+      'FL',
+      'FLOOR',
+      'FRNT',
+      'FRONT',
+      'HNGR',
+      'HANGER',
+      'LBBY',
+      'LOBBY',
+      'LOWR',
+      'LOWER',
+      'OFC',
+      'OFFICE',
+      'PH',
+      'PENTHOUSE',
+      'TRLR',
+      'TRAILER',
+      'UPPR',
+      'RM',
+      'ROOM',
+      'SIDE',
+      'SLIP',
+      'KEY',
+      'LOT',
+      'PIER',
+      'REAR',
+      'SPC',
+      'SPACE',
+      'STOP',
+      'STE',
+      'SUITE',
+      'UNIT',
+      '#',
     );
 
     // overwriting $streetUnitFormats for 'en_CA' and 'fr_CA' locale
     if (in_array($locale, array(
-      'en_CA', 'fr_CA'))) {
+      'en_CA',
+      'fr_CA',
+    ))) {
       $streetUnitFormats = array('APT', 'APP', 'SUITE', 'BUREAU', 'UNIT');
     }
     //@todo per CRM-14459 this regex picks up words with the string in them - e.g APT picks up
@@ -831,23 +886,22 @@ ORDER BY civicrm_address.is_primary DESC, civicrm_address.location_type_id DESC,
   }
 
   /**
-   * Validate the address fields based on the address options enabled
+   * Validate the address fields based on the address options enabled.
    * in the Address Settings
    *
    * @param array $fields
    *   An array of importable/exportable contact fields.
    *
-   * @return array   $fields an array of contact fields and only the enabled address options
-   * @static
+   * @return array
+   *   an array of contact fields and only the enabled address options
    */
   public static function validateAddressOptions($fields) {
     static $addressOptions = NULL;
     if (!$addressOptions) {
-      $addressOptions =
-        CRM_Core_BAO_Setting::valueOptions(
-          CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
-          'address_options'
-        );
+      $addressOptions = CRM_Core_BAO_Setting::valueOptions(
+        CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
+        'address_options'
+      );
     }
 
     if (is_array($fields) && !empty($fields)) {
@@ -861,13 +915,13 @@ ORDER BY civicrm_address.is_primary DESC, civicrm_address.location_type_id DESC,
   }
 
   /**
-   * Check if current address is used by any other contacts
+   * Check if current address is used by any other contacts.
    *
    * @param int $addressId
    *   Address id.
    *
-   * @return count of contacts that use this shared address
-   * @static
+   * @return int
+   *   count of contacts that use this shared address
    */
   public static function checkContactSharedAddress($addressId) {
     $query = 'SELECT count(id) FROM civicrm_address WHERE master_id = %1';
@@ -875,14 +929,13 @@ ORDER BY civicrm_address.is_primary DESC, civicrm_address.location_type_id DESC,
   }
 
   /**
-   * Check if current address fields are shared with any other address
+   * Check if current address fields are shared with any other address.
    *
    * @param array $fields
    *   Address fields in profile.
    * @param int $contactId
    *   Contact id.
    *
-   * @static
    */
   public static function checkContactSharedAddressFields(&$fields, $contactId) {
     if (!$contactId || !is_array($fields) || empty($fields)) {
@@ -947,7 +1000,7 @@ SELECT is_primary,
   }
 
   /**
-   * Update the shared addresses if master address is modified
+   * Update the shared addresses if master address is modified.
    *
    * @param int $addressId
    *   Address id.
@@ -955,7 +1008,6 @@ SELECT is_primary,
    *   Associated array of address params.
    *
    * @return void
-   * @static
    */
   public static function processSharedAddress($addressId, $params) {
     $query = 'SELECT id FROM civicrm_address WHERE master_id = %1';
@@ -977,7 +1029,7 @@ SELECT is_primary,
   }
 
   /**
-   * Merge contacts with the Same address to get one shared label
+   * Merge contacts with the Same address to get one shared label.
    * @param array $rows
    *   Array[contact_id][contactDetails].
    */
@@ -985,8 +1037,11 @@ SELECT is_primary,
     $uniqueAddress = array();
     foreach (array_keys($rows) as $rowID) {
       // load complete address as array key
-      $address =
-      trim($rows[$rowID]['street_address']) . trim($rows[$rowID]['city']) . trim($rows[$rowID]['state_province']) . trim($rows[$rowID]['postal_code']) . trim($rows[$rowID]['country']);
+      $address = trim($rows[$rowID]['street_address'])
+        . trim($rows[$rowID]['city'])
+        . trim($rows[$rowID]['state_province'])
+        . trim($rows[$rowID]['postal_code'])
+        . trim($rows[$rowID]['country']);
       if (isset($rows[$rowID]['last_name'])) {
         $name = $rows[$rowID]['last_name'];
       }
@@ -997,7 +1052,7 @@ SELECT is_primary,
       // CRM-15120
       $formatted = array(
         'first_name' => $rows[$rowID]['first_name'],
-        'individual_prefix' => $rows[$rowID]['individual_prefix']
+        'individual_prefix' => $rows[$rowID]['individual_prefix'],
       );
       $format = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'display_name_format');
       $firstNameWithPrefix = CRM_Utils_Address::format($formatted, $format, FALSE, FALSE, TRUE);
@@ -1047,7 +1102,7 @@ SELECT is_primary,
   }
 
   /**
-   * Create relationship between contacts who share an address
+   * Create relationship between contacts who share an address.
    *
    * Note that currently we create relationship only for Individual contacts
    * Individual + Household and Individual + Orgnization
@@ -1058,7 +1113,6 @@ SELECT is_primary,
    *   Associated array of submitted values.
    *
    * @return void
-   * @static
    */
   public static function processSharedAddressRelationship($masterAddressId, $params) {
     if (!$masterAddressId) {
@@ -1109,20 +1163,23 @@ SELECT is_primary,
       CRM_Core_Error::fatal(ts("You seem to have deleted the relationship type '%1'", array(1 => $relationshipType)));
     }
 
-    // create relationship
-    $relationshipParams = array(
-      'is_active' => TRUE,
-      'relationship_type_id' => $relTypeId . '_a_b',
-      'contact_check' => array($sharedContactId => TRUE),
-    );
-
-    list($valid, $invalid, $duplicate,
-      $saved, $relationshipIds
-    ) = CRM_Contact_BAO_Relationship::createMultiple($relationshipParams, $cid);
+    try {
+      // create relationship
+      civicrm_api3('relationship', 'create', array(
+        'is_active' => TRUE,
+        'relationship_type_id' => $relTypeId,
+        'contact_id_a' => $cid,
+        'contact_id_b' => $sharedContactId,
+      ));
+    }
+    catch (CiviCRM_API3_Exception $e) {
+      // We catch and ignore here because this has historically been a best-effort relationship create call.
+      // presumably it could refuse due to duplication or similar and we would ignore that.
+    }
   }
 
   /**
-   * Check and set the status for shared address delete
+   * Check and set the status for shared address delete.
    *
    * @param int $addressId
    *   Address id.
@@ -1131,8 +1188,7 @@ SELECT is_primary,
    * @param bool $returnStatus
    *   By default false.
    *
-   * @return string $statusMessage
-   * @static
+   * @return string
    */
   public static function setSharedAddressDeleteStatus($addressId = NULL, $contactId = NULL, $returnStatus = FALSE) {
     // check if address that is being deleted has any shared
@@ -1185,7 +1241,7 @@ SELECT is_primary,
   }
 
   /**
-   * Call common delete function
+   * Call common delete function.
    */
   public static function del($id) {
     return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Address', $id);
@@ -1200,11 +1256,11 @@ SELECT is_primary,
    *
    * @param string $fieldName
    * @param string $context
-   *   : @see CRM_Core_DAO::buildOptionsContext.
+   * @see CRM_Core_DAO::buildOptionsContext
    * @param array $props
-   *   whatever is known about this dao object.
+   *   whatever is known about this dao object.
    *
-   * @return Array|bool
+   * @return array|bool
    */
   public static function buildOptions($fieldName, $context = NULL, $props = array()) {
     $params = array();
@@ -1246,9 +1302,8 @@ SELECT is_primary,
       // Not a real field in this entity
       case 'world_region':
         return CRM_Core_PseudoConstant::worldRegion();
-
-      break;
     }
     return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
   }
+
 }