Added the current uncommited changes to production code, and rebased to 4.6.8
[civicrm-core.git] / CRM / Contact / BAO / Contact / Utils.php
index a4aff68fa82c6ceb357bd16a45951982e1fdb50f..1a468fda15b8f06670ad475c7cb972a5789b9790 100644 (file)
@@ -47,16 +47,16 @@ class CRM_Contact_BAO_Contact_Utils {
    * @return string
    */
   public static function getImage($contactType, $urlOnly = FALSE, $contactId = NULL, $addProfileOverlay = TRUE) {
-    static $imageInfo = array();
+    static $imageInfo = [];
 
     $contactType = CRM_Utils_Array::explodePadded($contactType);
     $contactType = $contactType[0];
 
     if (!array_key_exists($contactType, $imageInfo)) {
-      $imageInfo[$contactType] = array();
+      $imageInfo[$contactType] = [];
 
-      $typeInfo = array();
-      $params = array('name' => $contactType);
+      $typeInfo = [];
+      $params = ['name' => $contactType];
       CRM_Contact_BAO_ContactType::retrieve($params, $typeInfo);
 
       if (!empty($typeInfo['image_URL'])) {
@@ -253,7 +253,7 @@ WHERE  id IN ( $idString )
    *   max locations for the contact
    */
   public static function maxLocations($contactId) {
-    $contactLocations = array();
+    $contactLocations = [];
 
     // find number of location blocks for this contact and adjust value accordinly
     // get location type from email
@@ -284,7 +284,7 @@ UNION
   public static function createCurrentEmployerRelationship($contactID, $organization, $previousEmployerID = NULL, $newContact = FALSE) {
     //if organization name is passed. CRM-15368,CRM-15547
     if ($organization && !is_numeric($organization)) {
-      $dupeIDs = CRM_Contact_BAO_Contact::getDuplicateContacts(array('organization_name' => $organization), 'Organization', 'Unsupervised', array(), FALSE);
+      $dupeIDs = CRM_Contact_BAO_Contact::getDuplicateContacts(['organization_name' => $organization], 'Organization', 'Unsupervised', [], FALSE);
 
       if (is_array($dupeIDs) && !empty($dupeIDs)) {
         // we should create relationship only w/ first org CRM-4193
@@ -295,17 +295,17 @@ UNION
       }
       else {
         //create new organization
-        $newOrg = array(
+        $newOrg = [
           'contact_type' => 'Organization',
           'organization_name' => $organization,
-        );
+        ];
         $org = CRM_Contact_BAO_Contact::create($newOrg);
         $organization = $org->id;
       }
     }
 
     if ($organization && is_numeric($organization)) {
-      $cid = array('contact' => $contactID);
+      $cid = ['contact' => $contactID];
 
       // get the relationship type id of "Employee of"
       $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b');
@@ -314,11 +314,11 @@ UNION
       }
 
       // create employee of relationship
-      $relationshipParams = array(
+      $relationshipParams = [
         'is_active' => TRUE,
         'relationship_type_id' => $relTypeId . '_a_b',
-        'contact_check' => array($organization => TRUE),
-      );
+        'contact_check' => [$organization => TRUE],
+      ];
       list($valid, $invalid, $duplicate, $saved, $relationshipIds)
         = CRM_Contact_BAO_Relationship::legacyCreateMultiple($relationshipParams, $cid);
 
@@ -333,7 +333,7 @@ UNION
       }
 
       // set current employer
-      self::setCurrentEmployer(array($contactID => $organization));
+      self::setCurrentEmployer([$contactID => $organization]);
 
       $relationshipParams['relationship_ids'] = $relationshipIds;
       // Handle related memberships. CRM-3792
@@ -358,7 +358,7 @@ UNION
    * @throws CiviCRM_API3_Exception
    */
   public static function currentEmployerRelatedMembership($contactID, $employerID, $relationshipParams, $duplicate = FALSE, $previousEmpID = NULL) {
-    $ids = array();
+    $ids = [];
     $action = CRM_Core_Action::ADD;
 
     //we do not know that triggered relationship record is active.
@@ -453,7 +453,7 @@ WHERE id={$contactId}; ";
         if ($relationship->find(TRUE)) {
           CRM_Contact_BAO_Relationship::setIsActive($relationship->id, FALSE);
           CRM_Contact_BAO_Relationship::relatedMemberships($contactId, $relMembershipParams,
-            $ids = array(),
+            $ids = [],
             CRM_Core_Action::DELETE
           );
         }
@@ -499,7 +499,7 @@ WHERE id={$contactId}; ";
       default:
         // individual
         $form->addElement('select', 'prefix_id', ts('Prefix'),
-          array('' => ts('- prefix -')) + CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id')
+          ['' => ts('- prefix -')] + CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id')
         );
         $form->addElement('text', 'first_name', ts('First Name'),
           $attributes['first_name']
@@ -511,7 +511,7 @@ WHERE id={$contactId}; ";
           $attributes['last_name']
         );
         $form->addElement('select', 'suffix_id', ts('Suffix'),
-          array('' => ts('- suffix -')) + CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id')
+          ['' => ts('- suffix -')] + CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id')
         );
     }
 
@@ -569,19 +569,19 @@ UPDATE civicrm_contact
    *   returns array with links to contact view
    */
   public static function formatContactIDSToLinks($contactIDs, $addViewLink = TRUE, $addEditLink = TRUE, $originalId = NULL) {
-    $contactLinks = array();
+    $contactLinks = [];
     if (!is_array($contactIDs) || empty($contactIDs)) {
       return $contactLinks;
     }
 
     // does contact has sufficient permissions.
-    $permissions = array(
+    $permissions = [
       'view' => 'view all contacts',
       'edit' => 'edit all contacts',
       'merge' => 'merge duplicate contacts',
-    );
+    ];
 
-    $permissionedContactIds = array();
+    $permissionedContactIds = [];
     foreach ($permissions as $task => $permission) {
       // give permission.
       if (CRM_Core_Permission::check($permission)) {
@@ -592,10 +592,10 @@ UPDATE civicrm_contact
       }
 
       // check permission on acl basis.
-      if (in_array($task, array(
+      if (in_array($task, [
         'view',
         'edit',
-      ))) {
+      ])) {
         $aclPermission = CRM_Core_Permission::VIEW;
         if ($task == 'edit') {
           $aclPermission = CRM_Core_Permission::EDIT;
@@ -676,10 +676,10 @@ LEFT JOIN  civicrm_email ce ON ( ce.contact_id=c.id AND ce.is_primary = 1 )
    * @return array
    *   array of contact info.
    */
-  public static function contactDetails($componentIds, $componentName, $returnProperties = array()) {
-    $contactDetails = array();
+  public static function contactDetails($componentIds, $componentName, $returnProperties = []) {
+    $contactDetails = [];
     if (empty($componentIds) ||
-      !in_array($componentName, array('CiviContribute', 'CiviMember', 'CiviEvent', 'Activity', 'CiviCase'))
+      !in_array($componentName, ['CiviContribute', 'CiviMember', 'CiviEvent', 'Activity', 'CiviCase'])
     ) {
       return $contactDetails;
     }
@@ -688,7 +688,7 @@ LEFT JOIN  civicrm_email ce ON ( ce.contact_id=c.id AND ce.is_primary = 1 )
       $autocompleteContactSearch = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
         'contact_autocomplete_options'
       );
-      $returnProperties = array_fill_keys(array_merge(array('sort_name'),
+      $returnProperties = array_fill_keys(array_merge(['sort_name'],
         array_keys($autocompleteContactSearch)
       ), 1);
     }
@@ -711,13 +711,13 @@ LEFT JOIN  civicrm_email ce ON ( ce.contact_id=c.id AND ce.is_primary = 1 )
       $compTable = 'civicrm_participant';
     }
 
-    $select = $from = array();
+    $select = $from = [];
     foreach ($returnProperties as $property => $ignore) {
-      $value = (in_array($property, array(
+      $value = (in_array($property, [
         'city',
         'street_address',
         'postal_code',
-      ))) ? 'address' : $property;
+      ])) ? 'address' : $property;
       switch ($property) {
         case 'sort_name':
           if ($componentName == 'Activity') {
@@ -784,7 +784,7 @@ INNER JOIN civicrm_contact contact_target ON ( contact_target.id = act.contact_i
       $fromClause = implode(' ', $from);
       $selectClause = implode(', ', $select);
       $whereClause = "{$compTable}.id IN (" . implode(',', $componentIds) . ')';
-      $groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($select, array("{$compTable}.id", 'contact.id'));
+      $groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($select, ["{$compTable}.id", 'contact.id']);
 
       $query = "
   SELECT  contact.id as contactId, $compTable.id as componentId, $selectClause
@@ -825,7 +825,7 @@ INNER JOIN civicrm_contact contact_target ON ( contact_target.id = act.contact_i
     // Normal update process will automatically create new address with submitted values
 
     // 1. loop through entire submitted address array
-    $skipFields = array('is_primary', 'location_type_id', 'is_billing', 'master_id', 'update_current_employer');
+    $skipFields = ['is_primary', 'location_type_id', 'is_billing', 'master_id', 'update_current_employer'];
     foreach ($address as & $values) {
       // 2. check if "Use another contact's address" is checked, if not continue
       // Additionally, if master_id is set (address was shared), set master_id to empty value.
@@ -876,9 +876,9 @@ INNER JOIN civicrm_contact contact_target ON ( contact_target.id = act.contact_i
    *   associated array of contact names
    */
   public static function getAddressShareContactNames(&$addresses) {
-    $contactNames = array();
+    $contactNames = [];
     // get the list of master id's for address
-    $masterAddressIds = array();
+    $masterAddressIds = [];
     foreach ($addresses as $key => $addressValue) {
       if (!empty($addressValue['master_id'])) {
         $masterAddressIds[] = $addressValue['master_id'];
@@ -894,11 +894,11 @@ INNER JOIN civicrm_contact contact_target ON ( contact_target.id = act.contact_i
 
       while ($dao->fetch()) {
         $contactViewUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$dao->cid}");
-        $contactNames[$dao->id] = array(
+        $contactNames[$dao->id] = [
           'name' => "<a href='{$contactViewUrl}'>{$dao->display_name}</a>",
           'is_deleted' => $dao->is_deleted,
           'contact_id' => $dao->cid,
-        );
+        ];
       }
     }
     return $contactNames;
@@ -947,21 +947,21 @@ INNER JOIN civicrm_contact contact_target ON ( contact_target.id = act.contact_i
       $valueID = $id = self::defaultGreeting($contactType, $greeting);
     }
 
-    $filter = array(
+    $filter = [
       'contact_type' => $contactType,
       'greeting_type' => $greeting,
-    );
+    ];
 
     $allGreetings = CRM_Core_PseudoConstant::greeting($filter);
     $originalGreetingString = $greetingString = CRM_Utils_Array::value($valueID, $allGreetings);
     if (!$greetingString) {
-      CRM_Core_Error::fatal(ts('Incorrect greeting value id %1, or no default greeting for this contact type and greeting type.', array(1 => $valueID)));
+      CRM_Core_Error::fatal(ts('Incorrect greeting value id %1, or no default greeting for this contact type and greeting type.', [1 => $valueID]));
     }
 
     // build return properties based on tokens
     $greetingTokens = CRM_Utils_Token::getTokens($greetingString);
     $tokens = CRM_Utils_Array::value('contact', $greetingTokens);
-    $greetingsReturnProperties = array();
+    $greetingsReturnProperties = [];
     if (is_array($tokens)) {
       $greetingsReturnProperties = array_fill_keys(array_values($tokens), 1);
     }
@@ -976,7 +976,7 @@ INNER JOIN civicrm_contact contact_target ON ( contact_target.id = act.contact_i
     }
 
     //FIXME : apiQuery should handle these clause.
-    $filterContactFldIds = $filterIds = array();
+    $filterContactFldIds = $filterIds = [];
     $idFldName = $displayFldName = NULL;
     if (in_array($greeting, CRM_Contact_BAO_Contact::$_greetingTypes)) {
       $idFldName = $greeting . '_id';
@@ -984,7 +984,7 @@ INNER JOIN civicrm_contact contact_target ON ( contact_target.id = act.contact_i
     }
 
     if ($idFldName) {
-      $queryParams = array(1 => array($contactType, 'String'));
+      $queryParams = [1 => [$contactType, 'String']];
 
       // if $force == 1 then update all contacts else only
       // those with NULL greeting or addressee value CRM-9476
@@ -997,12 +997,12 @@ INNER JOIN civicrm_contact contact_target ON ( contact_target.id = act.contact_i
           FROM civicrm_contact
           WHERE contact_type = %1
           AND ({$idFldName} IS NULL
-          OR ( {$idFldName} IS NOT NULL AND ({$displayFldName} IS NULL OR {$displayFldName} = '')) )";
+          OR ( {$idFldName} IS NOT NULL AND ({$displayFldName} IS NULL OR {$displayFldName} = '')) ) LIMIT 800";
       }
 
       if ($limit) {
         $sql .= " LIMIT 0, %2";
-        $queryParams += array(2 => array($limit, 'Integer'));
+        $queryParams += [2 => [$limit, 'Integer']];
       }
 
       $dao = CRM_Core_DAO::executeQuery($sql, $queryParams);
@@ -1020,14 +1020,14 @@ INNER JOIN civicrm_contact contact_target ON ( contact_target.id = act.contact_i
     }
 
     // retrieve only required contact information
-    $extraParams[] = array('contact_type', '=', $contactType, 0, 0);
+    $extraParams[] = ['contact_type', '=', $contactType, 0, 0];
     // we do token replacement in the replaceGreetingTokens hook
     list($greetingDetails) = CRM_Utils_Token::getTokenDetails(array_keys($filterContactFldIds),
       $greetingsReturnProperties,
       FALSE, FALSE, $extraParams
     );
     // perform token replacement and build update SQL
-    $contactIds = array();
+    $contactIds = [];
     $cacheFieldQuery = "UPDATE civicrm_contact SET {$greeting}_display = CASE id ";
     foreach ($greetingDetails as $contactID => $contactDetails) {
       if (!$processAll &&
@@ -1092,11 +1092,11 @@ WHERE id IN (" . implode(',', $contactIds) . ")";
    * @return int|NULL
    */
   public static function defaultGreeting($contactType, $greetingType) {
-    $contactTypeFilters = array(
+    $contactTypeFilters = [
       'Individual' => 1,
       'Household' => 2,
       'Organization' => 3,
-    );
+    ];
     if (!isset($contactTypeFilters[$contactType])) {
       return NULL;
     }
@@ -1120,15 +1120,15 @@ WHERE id IN (" . implode(',', $contactIds) . ")";
    *   Array of tokens. The ALL ke
    */
   public static function getTokensRequiredForContactGreetings($contactParams) {
-    $tokens = array();
-    foreach (array('addressee', 'email_greeting', 'postal_greeting') as $greeting) {
+    $tokens = [];
+    foreach (['addressee', 'email_greeting', 'postal_greeting'] as $greeting) {
       $string = '';
       if (!empty($contactParams[$greeting . '_id'])) {
         $string = CRM_Core_PseudoConstant::getLabel('CRM_Contact_BAO_Contact', $greeting . '_id', $contactParams[$greeting . '_id']);
       }
       $string = isset($contactParams[$greeting . '_custom']) ? $contactParams[$greeting . '_custom'] : $string;
       if (empty($string)) {
-        $tokens[$greeting] = array();
+        $tokens[$greeting] = [];
       }
       else {
         $tokens[$greeting] = CRM_Utils_Token::getTokens($string);