CRM-17383 fix - Smart group form values broken in 4.7
authormonishdeb <monish.deb@webaccessglobal.com>
Tue, 13 Oct 2015 13:11:12 +0000 (18:41 +0530)
committermonishdeb <monish.deb@webaccessglobal.com>
Wed, 14 Oct 2015 16:18:03 +0000 (21:48 +0530)
https://issues.civicrm.org/jira/browse/CRM-17383

CRM/Contact/BAO/Query.php
CRM/Contact/BAO/SavedSearch.php
CRM/Contact/Form/Search/Advanced.php
CRM/Contact/Form/Search/Custom/Group.php
CRM/Contact/Form/Search/Custom/MultipleValues.php
CRM/Contact/Form/Search/Custom/PriceSet.php
CRM/Contact/Form/Search/Custom/Proximity.php
CRM/Contact/Form/Search/Custom/Sample.php
CRM/Contact/Form/Search/Custom/ZipCodeRange.php
CRM/Contact/Selector.php

index baaf0b147935fe2f3362d365bdef2655f5ff7237..f024f09b1f6e4f8e0256b0d98af93274eec8a924 100644 (file)
@@ -1507,12 +1507,13 @@ class CRM_Contact_BAO_Query {
 
     foreach ($formValues as $id => $values) {
 
-      self::legacyConvertFormValues($id, $values);
-
       if (self::isAlreadyProcessedForQueryFormat($values)) {
         $params[] = $values;
         continue;
       }
+
+      self::legacyConvertFormValues($id, $values);
+
       // The form uses 1 field to represent two db fields
       if ($id == 'contact_type' && $values && (!is_array($values) || !array_intersect(array_keys($values), CRM_Core_DAO::acceptedSQLOperators()))) {
         $contactType = array();
@@ -1611,12 +1612,25 @@ class CRM_Contact_BAO_Query {
    *
    */
   public static function legacyConvertFormValues($id, &$values) {
-    if (in_array($id, array('group', 'tag')) && is_array($values)) {
+    $legacyElements = array(
+      'group',
+      'tag',
+      'contact_tags',
+      'contact_type',
+      'membership_type_id',
+      'membership_status_id',
+    );
+    if (in_array($id, $legacyElements) && is_array($values)) {
       // prior to 4.7, formValues for some attributes (e.g. group, tag) are stored in array(id1 => 1, id2 => 1),
       // as per the recent Search fixes $values need to be in standard array(id1, id2) format
       $ids = array_keys($values, 1);
       if (count($ids) > 1 ||
-        (count($ids) == 1 && (key($values) > 1 || (key($values) == 1 && $values[1] == 1))) // handle (0 => 4), (1 => 1)
+        (count($ids) == 1 &&
+          (key($values) > 1 ||
+            is_string(key($values)) ||
+            (key($values) == 1 && $values[1] == 1) // handle (0 => 4), (1 => 1)
+          )
+        )
       ) {
         $values = $ids;
       }
@@ -4492,7 +4506,7 @@ civicrm_relationship.is_permission_a_b = 0
    *
    * @return bool;
    */
-  protected static function isAlreadyProcessedForQueryFormat($values) {
+  public static function isAlreadyProcessedForQueryFormat($values) {
     if (!is_array($values)) {
       return FALSE;
     }
index d3b0307a35ab730c46187652d4d04753668c04be..ccfe4f917be219c76ed09a750d297878ad9ce205 100644 (file)
@@ -99,27 +99,66 @@ class CRM_Contact_BAO_SavedSearch extends CRM_Contact_DAO_SavedSearch {
       $result = unserialize($fv);
     }
 
-    // check to see if we need to convert the old privacy array
-    // CRM-9180
-    if (isset($result['privacy'])) {
-      if (is_array($result['privacy'])) {
-        $result['privacy_operator'] = 'AND';
-        $result['privacy_toggle'] = 1;
-        if (isset($result['privacy']['do_not_toggle'])) {
-          if ($result['privacy']['do_not_toggle']) {
-            $result['privacy_toggle'] = 2;
+    $specialFields = array('contact_type', 'group', 'contact_tags', 'member_membership_type_id', 'member_status_id');
+    foreach ($result as $element => $value) {
+      if (CRM_Contact_BAO_Query::isAlreadyProcessedForQueryFormat($value)) {
+        $id = CRM_Utils_Array::value(0, $value);
+        $value = CRM_Utils_Array::value(2, $value);
+        if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
+          $value = CRM_Utils_Array::value(key($value), $value);
+        }
+        $result[$id] = $value;
+        unset($result[$element]);
+        continue;
+      }
+      if (!empty($value) && is_array($value)) {
+        if (in_array($element, $specialFields)) {
+          $element = str_replace('member_membership_type_id', 'membership_type_id', $element);
+          $element = str_replace('member_status_id', 'membership_status_id', $element);
+          CRM_Contact_BAO_Query::legacyConvertFormValues($element, $value);
+          $result[$element] = $value;
+        }
+        // As per the OK (Operator as Key) value format, value array may contain key
+        // as an operator so to ensure the default is always set actual value
+        elseif (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
+          $result[$element] = CRM_Utils_Array::value(key($value), $value);
+          if (is_string($result[$element])) {
+            $result[$element] = str_replace("%", '', $result[$element]);
           }
-          unset($result['privacy']['do_not_toggle']);
         }
+      }
+      if (substr($element, 0, 7) == 'custom_' &&
+        (substr($element, -5, 5) == '_from' || substr($element, -3, 3) == '_to')
+      ) {
+        // Ensure the _relative field is set if from or to are set to ensure custom date
+        // fields with 'from' or 'to' values are displayed when the are set in the smart group
+        // being loaded. (CRM-17116)
+        if (!isset($result[CRM_Contact_BAO_Query::getCustomFieldName($element) . '_relative'])) {
+          $result[CRM_Contact_BAO_Query::getCustomFieldName($element) . '_relative'] = 0;
+        }
+      }
+      // check to see if we need to convert the old privacy array
+      // CRM-9180
+      if (!empty($result['privacy'])) {
+        if (is_array($result['privacy'])) {
+          $result['privacy_operator'] = 'AND';
+          $result['privacy_toggle'] = 1;
+          if (isset($result['privacy']['do_not_toggle'])) {
+            if ($result['privacy']['do_not_toggle']) {
+              $result['privacy_toggle'] = 2;
+            }
+            unset($result['privacy']['do_not_toggle']);
+          }
 
-        $result['privacy_options'] = array();
-        foreach ($result['privacy'] as $name => $value) {
-          if ($value) {
-            $result['privacy_options'][] = $name;
+          $result['privacy_options'] = array();
+          foreach ($result['privacy'] as $name => $value) {
+            if ($value) {
+              $result['privacy_options'][] = $name;
+            }
           }
         }
+        unset($result['privacy']);
       }
-      unset($result['privacy']);
     }
 
     return $result;
index 9f6b2e294cab2b4a738a1fad8e62560000f64705..0dd5e95ae8269d66fc1dcf5a1f16368b402c175d 100644 (file)
@@ -392,35 +392,7 @@ class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search {
     }
 
     if ($this->_ssID && empty($_POST)) {
-      $specialFields = array('contact_type', 'group', 'contact_tags', 'member_membership_type_id', 'member_status_id');
-
-      foreach ($defaults as $element => $value) {
-        if (!empty($value) && is_array($value)) {
-          if (in_array($element, $specialFields)) {
-            $element = str_replace('member_membership_type_id', 'membership_type_id', $element);
-            $element = str_replace('member_status_id', 'membership_status_id', $element);
-            $defaults[$element] = array_keys($value);
-          }
-          // As per the OK (Operator as Key) value format, value array may contain key
-          // as an operator so to ensure the default is always set actual value
-          elseif (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
-            $defaults[$element] = CRM_Utils_Array::value(key($value), $value);
-            if (is_string($defaults[$element])) {
-              $defaults[$element] = str_replace("%", '', $defaults[$element]);
-            }
-          }
-        }
-        if (substr($element, 0, 7) == 'custom_' &&
-          (substr($element, -5, 5) == '_from' || substr($element, -3, 3) == '_to')
-          ) {
-          // Ensure the _relative field is set if from or to are set to ensure custom date
-          // fields with 'from' or 'to' values are displayed when the are set in the smart group
-          // being loaded. (CRM-17116)
-          if (!isset($defaults[CRM_Contact_BAO_Query::getCustomFieldName($element) . '_relative'])) {
-            $defaults[CRM_Contact_BAO_Query::getCustomFieldName($element) . '_relative'] = 0;
-          }
-        }
-      }
+      $defaults = array_merge($defaults, CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID));
     }
     return $defaults;
   }
index 2900468923f9a4990416f1350157c8dd4b0c29eb..dc8968f0a36d959b82b869ffc508e8e51c0c2ffc 100644 (file)
@@ -150,26 +150,6 @@ class CRM_Contact_Form_Search_Custom_Group extends CRM_Contact_Form_Search_Custo
     $form->assign('elements', array('includeGroups', 'excludeGroups', 'andOr', 'includeTags', 'excludeTags'));
   }
 
-  /**
-   * Set search form field defaults here.
-   * @return array
-   */
-  public function setDefaultValues() {
-    $defaults = array('andOr' => '1');
-
-    if (!empty($this->_formValues)) {
-      $defaults['andOr'] = CRM_Utils_Array::value('andOr', $this->_formValues, '1');
-
-      $defaults['includeGroups'] = CRM_Utils_Array::value('includeGroups', $this->_formValues);
-      $defaults['excludeGroups'] = CRM_Utils_Array::value('excludeGroups', $this->_formValues);
-
-      $defaults['includeTags'] = CRM_Utils_Array::value('includeTags', $this->_formValues);
-      $defaults['excludeTags'] = CRM_Utils_Array::value('excludeTags', $this->_formValues);
-    }
-
-    return $defaults;
-  }
-
   /**
    * @param int $offset
    * @param int $rowcount
index 829dd5f4766a3a3b10f7fba487fb239a923ce782..0fdd01a5aad0d7e69318fecd4d815c6946ce1c04 100644 (file)
@@ -288,13 +288,6 @@ contact_a.sort_name    as sort_name,
     return 'CRM/Contact/Form/Search/Custom/MultipleValues.tpl';
   }
 
-  /**
-   * @return array
-   */
-  public function setDefaultValues() {
-    return array();
-  }
-
   /**
    * @param $row
    */
index bde740e950332295d619e2c694c05a02600b4bad..eca744c22a50af852d98f10e8737291b9d509e6e 100644 (file)
@@ -338,13 +338,6 @@ INNER JOIN {$this->_tableName} tempTable ON ( tempTable.contact_id = contact_a.i
     return 'CRM/Contact/Form/Search/Custom.tpl';
   }
 
-  /**
-   * @return array
-   */
-  public function setDefaultValues() {
-    return array();
-  }
-
   /**
    * @param $row
    */
index d854c078e6a869e4393338a3390adc4a52e3c09a..bf53ab8c2ae25799be5823b93ba1a910abf04220 100644 (file)
@@ -271,6 +271,9 @@ AND cgc.group_id = {$this->_group}
    * @return array|null
    */
   public function setDefaultValues() {
+    if (!empty($this->_formValues)) {
+      return $this->_formValues;
+    }
     $config = CRM_Core_Config::singleton();
     $countryDefault = $config->defaultContactCountry;
     $stateprovinceDefault = $config->defaultContactStateProvince;
index 32b9cdc1bbe76bd8e6a24462e927dc6fbc3ded57..b79bfce55b78169081ad1a04eb8446632133a745 100644 (file)
@@ -213,9 +213,7 @@ LEFT JOIN civicrm_state_province state_province ON state_province.id = address.s
    * @return array
    */
   public function setDefaultValues() {
-    return array(
-      'household_name' => '',
-    );
+    return array_merge(array('household_name' => ''), $this->_formValues);
   }
 
   /**
index b7331a666497bf52f3f83fdb06f4bb4472e8ae8e..12f07d948297bdf11201323f6e65e5346e968e3a 100644 (file)
@@ -178,13 +178,6 @@ LEFT JOIN civicrm_email   email   ON ( email.contact_id = contact_a.id AND
     return $this->whereClause($where, $params);
   }
 
-  /**
-   * @return array
-   */
-  public function setDefaultValues() {
-    return array();
-  }
-
   /**
    * @return string
    */
index 45354aa4a2e3d54723913be648737ed9811f54a9..629de9456000cff224d697ceabb162dc47ce3f3a 100644 (file)
@@ -627,7 +627,7 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se
     $links = self::links($this->_context, $this->_contextMenu, $this->_key);
 
     //check explicitly added contact to a Smart Group.
-    $groupID = CRM_Utils_Array::key('1', $this->_formValues['group']);
+    $groupID = CRM_Utils_Array::value('group', $this->_formValues);
 
     $pseudoconstants = array();
     // for CRM-3157 purposes