CRM-16927 fix - Notice Search membership with custom fields
authormonishdeb <monish.deb@webaccessglobal.com>
Wed, 29 Jul 2015 17:21:28 +0000 (22:51 +0530)
committermonishdeb <monish.deb@webaccessglobal.com>
Fri, 31 Jul 2015 17:20:08 +0000 (22:50 +0530)
https://issues.civicrm.org/jira/browse/CRM-16927

CRM/Activity/Form/Search.php
CRM/Case/Form/Search.php
CRM/Contact/Form/Search/Advanced.php
CRM/Contribute/Form/Search.php
CRM/Core/BAO/CustomField.php
CRM/Core/BAO/CustomQuery.php
CRM/Core/BAO/CustomValue.php
CRM/Event/Form/Search.php
CRM/Member/Form/Search.php
CRM/Pledge/Form/Search.php

index 8e2a4ba5d978351d7aefc1ac14c9b5270c080f6f..c58751dfa4a6f7b7996339c3802c6a6db362747c 100644 (file)
@@ -234,7 +234,7 @@ class CRM_Activity_Form_Search extends CRM_Core_Form_Search {
       $this->_formValues["activity_test"] = 0;
     }
 
-    CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
+    CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
 
     $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
 
index 4c33c675de452b4d22549d51a0098029b017ff0e..de684a1f5e34a540421c4b059d4043d74aea4fad 100644 (file)
@@ -248,7 +248,7 @@ class CRM_Case_Form_Search extends CRM_Core_Form_Search {
     if (empty($this->_formValues['case_deleted'])) {
       $this->_formValues['case_deleted'] = 0;
     }
-    CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
+    CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
 
     $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
 
index 6572be6c58953556ee47c24cb0f6d092484a22db..86b127dc02d78b97866530dd83dd356c619ca32b 100644 (file)
@@ -8,7 +8,7 @@
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | CiviCRM is free software; you can copy, modify, and distribute it  |
-s | under the terms of the GNU Affero General Public License           |
+ | under the terms of the GNU Affero General Public License           |
  | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
  |                                                                    |
  | CiviCRM is distributed in the hope that it will be useful, but     |
@@ -323,7 +323,7 @@ class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search {
       $this->_sortByCharacter = NULL;
     }
 
-    CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
+    CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
 
     $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
     $this->_returnProperties = &$this->returnProperties();
@@ -378,7 +378,6 @@ class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search {
       'status_id',
       'activity_subject',
     );
-    $specialParams += preg_grep('/^custom_/', array_keys($this->_formValues));
     foreach ($specialParams as $element) {
       $value = CRM_Utils_Array::value($element, $this->_formValues);
       if ($value) {
@@ -424,12 +423,21 @@ class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search {
     }
 
     if ($this->_ssID && empty($_POST)) {
-      $fields = array('contact_type', 'group', 'contact_tags');
+      $specialFields = array('contact_type', 'group', 'contact_tags');
 
-      foreach ($fields as $field) {
-        $fieldValues = CRM_Utils_Array::value($field, $defaults);
-        if ($fieldValues && is_array($fieldValues)) {
-          $defaults[$field] = array_keys($fieldValues);
+      foreach ($defaults as $element => $value) {
+        if (!empty($value) && is_array($value)) {
+          if (in_array($element, $specialFields)) {
+            $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]);
+            }
+          }
         }
       }
     }
index 9e224d1e5909df066354beb656a094c4d3dd757e..e6635b93ba9fdf453fd12d0686b5ea1499ccb12f 100644 (file)
@@ -120,7 +120,7 @@ class CRM_Contribute_Form_Search extends CRM_Core_Form_Search {
       );
     }
 
-    $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 1);
+    $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
     $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
       $this->_action,
       NULL,
@@ -306,7 +306,7 @@ class CRM_Contribute_Form_Search extends CRM_Core_Form_Search {
       }
     }
 
-    CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
+    CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
 
     $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
 
index e76a8a2dd5700913195e2ea47aa679fdc8027358..dd5529d47049bf8e5875096d650411d41cad9fae 100644 (file)
@@ -1240,18 +1240,8 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
         }
 
         $v = array();
-        $p = array();
         foreach ($checkedData as $key => $val) {
-          if ($html_type == 'CheckBox') {
-            if ($val) {
-              $p[] = $key;
-              $v[] = CRM_Utils_Array::value($key, $option);
-            }
-          }
-          else {
-            $p[] = $val;
-            $v[] = CRM_Utils_Array::value($val, $option);
-          }
+          $v[] = CRM_Utils_Array::value($val, $option);
         }
         if (!empty($v)) {
           $display = implode(', ', $v);
index 0914247326779eba41bce8f4141b96a38d38d98a..37b00ab57c85fb0d88f882253b63b2af5ce60901 100644 (file)
@@ -388,6 +388,10 @@ SELECT label, value
           $op = key($value);
           $qillValue = CRM_Core_BAO_CustomField::getDisplayValue($value[$op], $id, $this->_options);
         }
+        else {
+          $qillValue = CRM_Core_BAO_CustomField::getDisplayValue($value, $id, $this->_options);
+          $value = array('IN' => $value);
+        }
 
         $qillOp = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
 
@@ -409,22 +413,23 @@ SELECT label, value
               if (!is_array($value)) {
                 $value = CRM_Utils_Type::escape($strtolower($value), 'String');
               }
-              elseif ($isSerialized && strstr($op, 'IN')) {
+              elseif ($isSerialized) {
+                if (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
+                  $op = key($value);
+                  $value = $value[$op];
+                }
                 $value = implode(',', $value);
               }
 
               // CRM-14563,CRM-16575 : Special handling of multi-select custom fields
-              if (!empty($value)) {
-                if ($isSerialized) {
-                  if (strstr($op, 'IN')) {
-                    $value = str_replace(array('(', ')'), '', str_replace(",", "[[:cntrl:]]|[[:cntrl:]]", $value));
-                  }
-                  $op = (strstr($op, '!') || strstr($op, 'NOT')) ? 'NOT RLIKE' : 'RLIKE';
-                  $value = "[[:cntrl:]]" . $value . "[[:cntrl:]]";
+              if ($isSerialized && !empty($value)) {
+                if (strstr($op, 'IN')) {
+                  $value = str_replace(array('(', ')'), '', str_replace(",", "[[:cntrl:]]|[[:cntrl:]]", $value));
                 }
-                elseif ($wildcard) {
-                  $value = "[[:cntrl:]]%$value%[[:cntrl:]]";
-                  $op = 'LIKE';
+                $op = (strstr($op, '!') || strstr($op, 'NOT')) ? 'NOT RLIKE' : 'RLIKE';
+                $value = "[[:cntrl:]]" . $value . "[[:cntrl:]]";
+                if (!$wildcard) {
+                  $value = str_replace("[[:cntrl:]]|", '', $value);
                 }
               }
 
index ab5e404a00cdb35bc21fe48bf4954d826be8cb63..1f29fabea241f4b22223566fafd06f3b6ae5013e 100644 (file)
@@ -165,7 +165,7 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO {
    * @param array $formValues
    * @return null
    */
-  public static function fixFieldValueOfTypeMemo(&$formValues) {
+  public static function fixCustomFieldValue(&$formValues) {
     if (empty($formValues)) {
       return NULL;
     }
@@ -180,14 +180,24 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO {
       $htmlType = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_CustomField',
         substr($key, 7), 'html_type'
       );
-      if (($htmlType == 'TextArea') &&
+      $dataType = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_CustomField',
+        substr($key, 7), 'data_type'
+      );
+
+      if (is_array($formValues[$key])) {
+        if (!in_array(key($formValues[$key]),  CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
+          $formValues[$key] = array('IN' => $formValues[$key]);
+        }
+      }
+      elseif (($htmlType == 'TextArea' ||
+          ($htmlType == 'Text' && $dataType == 'String')
+        ) &&
         !((substr($formValues[$key], 0, 1) == '%') ||
           (substr($formValues[$key], -1, 1) == '%')
         )
       ) {
-        $formValues[$key] = '%' . $formValues[$key] . '%';
+        $formValues[$key] = array('LIKE' => '%' . $formValues[$key] . '%');
       }
-
     }
   }
 
index 0834673f1b06110fdff84c57cc1dce19f9279a05..5d77cbfd574b2f7ccfb701a3855110e9fdcbde7e 100644 (file)
@@ -284,7 +284,7 @@ class CRM_Event_Form_Search extends CRM_Core_Form_Search {
       $this->_formValues["participant_test"] = 0;
     }
 
-    CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
+    CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
 
     $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
 
index 0e69f987cda298d0bb5fc258efcdbe7b425bdd64..ff21324f5fe99057538f981ea2064fb4c679de61 100644 (file)
@@ -220,7 +220,7 @@ class CRM_Member_Form_Search extends CRM_Core_Form_Search {
       }
     }
 
-    CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
+    CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
 
     $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
 
index 513344cdb83b88ced8d1b95ac930186919fe2d1f..74914ced03cf8ad0f348a08ad63115dd9a2c98b2 100644 (file)
@@ -218,7 +218,7 @@ class CRM_Pledge_Form_Search extends CRM_Core_Form_Search {
       $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
     }
 
-    CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
+    CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
 
     $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);