CRM-17836 fix - custom fields attached to relationships are unsearchable
authormonishdeb <monish.deb@webaccessglobal.com>
Wed, 20 Jan 2016 20:24:56 +0000 (01:54 +0530)
committermonishdeb <monish.deb@webaccessglobal.com>
Mon, 25 Jan 2016 12:25:21 +0000 (17:55 +0530)
https://issues.civicrm.org/jira/browse/CRM-17836

CRM/Contact/BAO/Query.php
CRM/Core/BAO/CustomQuery.php

index 86824678e5e3a40ea32e748c9507bfabfa4f8067..2fb40419a3be2f2a8b48c91715c53765bfd82447 100644 (file)
@@ -1549,13 +1549,13 @@ class CRM_Contact_BAO_Query {
         }
       }
       elseif (substr($id, 0, 7) == 'custom_'
-          &&  (
-            substr($id, -9, 9) == '_relative'
-            || substr($id, -5, 5) == '_from'
-            || substr($id, -3, 3) == '_to'
-          )
+        &&  (
+          substr($id, -9, 9) == '_relative'
+          || substr($id, -5, 5) == '_from'
+          || substr($id, -3, 3) == '_to'
+        )
       ) {
-        self::convertCustomDateRelativeFields($formValues, $params, $values, $id);
+        self::convertCustomRelativeFields($formValues, $params, $values, $id);
       }
       elseif (preg_match('/_date_relative$/', $id) ||
         $id == 'event_relative' ||
@@ -4357,7 +4357,7 @@ civicrm_relationship.is_permission_a_b = 0
   }
 
   /**
-   * Convert submitted values for relative custom date fields to query object format.
+   * Convert submitted values for relative custom fields to query object format.
    *
    * The query will support the sqlOperator format so convert to that format.
    *
@@ -4370,7 +4370,7 @@ civicrm_relationship.is_permission_a_b = 0
    * @param string $fieldName
    *   Submitted field name. (Matches form field not DB field.)
    */
-  protected static function convertCustomDateRelativeFields(&$formValues, &$params, $values, $fieldName) {
+  protected static function convertCustomRelativeFields(&$formValues, &$params, $values, $fieldName) {
     if (empty($values)) {
       // e.g we might have relative set & from & to empty. The form flow is a bit funky &
       // this function gets called again after they fields have been converted which can get ugly.
@@ -4387,11 +4387,14 @@ civicrm_relationship.is_permission_a_b = 0
         return;
       }
 
-      list($from, $to) = CRM_Utils_Date::getFromTo(
-        NULL,
-        (empty($formValues[$customFieldName . '_from']) ? NULL : $formValues[$customFieldName . '_from']),
-        CRM_Utils_Array::value($customFieldName . '_to', $formValues)
-      );
+      $from = CRM_Utils_Array::value($customFieldName . '_from', $formValues, NULL);
+      $to = CRM_Utils_Array::value($customFieldName . '_to', $formValues, NULL);
+
+      $customFieldID = CRM_Core_BAO_CustomField::getKeyID($customFieldName);
+      $customFieldType = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $customFieldName, 'data_type');
+      if ($customFieldType == 'Date') {
+        list($from, $to) = CRM_Utils_Date::getFromTo(NULL, $from, $to);
+      }
     }
 
     if ($from) {
index 24d7f0491b79d9e9268e8dadec50f211da00a0a1..67cf0c624aa7fc0464fcd25eaea9211a8abbf74b 100644 (file)
@@ -328,13 +328,7 @@ SELECT f.id, f.label, f.data_type,
           case 'Country':
 
             if ($field['is_search_range'] && is_array($value)) {
-              $this->searchRange($field['id'],
-                $field['label'],
-                $field['data_type'],
-                $fieldName,
-                $value,
-                $grouping
-              );
+              //didn't found any field under any of these three data-types as searchable by range
             }
             else {
               // fix $value here to escape sql injection attacks
@@ -388,13 +382,8 @@ SELECT f.id, f.label, f.data_type,
             break;
 
           case 'Int':
-            if ($field['is_search_range'] && is_array($value)) {
-              $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
-            }
-            else {
-              $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Integer');
-              $this->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $field['label'], 2 => $qillOp, 3 => $qillValue));;
-            }
+            $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Integer');
+            $this->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $field['label'], 2 => $qillOp, 3 => $qillValue));;
             break;
 
           case 'Boolean':
@@ -430,77 +419,14 @@ SELECT f.id, f.label, f.data_type,
             }
 
           case 'Float':
-            if ($field['is_search_range']) {
-              $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
-            }
-            else {
-              $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Float');
-              $this->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $field['label'], 2 => $qillOp, 3 => $qillValue));
-            }
+            $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Float');
+            $this->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $field['label'], 2 => $qillOp, 3 => $qillValue));
             break;
 
           case 'Date':
-            if (in_array($op, CRM_Core_DAO::acceptedSQLOperators())) {
-              $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
-              list($qillOp, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, $field['label'], $value,
-                $op, array(), CRM_Utils_Type::T_DATE);
-              $this->_qill[$grouping][] = "{$field['label']} $qillOp '$qillVal'";
-              break;
-            }
-
-            $fromValue = CRM_Utils_Array::value('from', $value);
-            $toValue = CRM_Utils_Array::value('to', $value);
-            $value = CRM_Utils_Array::value($op, $value, $value);
-
-            if (!$fromValue && !$toValue) {
-              if (!is_array($value) && !CRM_Utils_Date::processDate($value) && !in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
-                continue;
-              }
-
-              // hack to handle yy format during search
-              if (is_numeric($value) && strlen($value) == 4) {
-                $value = "01-01-{$value}";
-              }
-
-              if (is_array($value)) {
-                $date = $qillValue = array();
-                foreach ($value as $key => $val) {
-                  $date[$key] = CRM_Utils_Date::processDate($val);
-                  $qillValue[$key] = CRM_Utils_Date::customFormat($date[$key]);
-                }
-              }
-              else {
-                $date = CRM_Utils_Date::processDate($value);
-                $qillValue = CRM_Utils_Date::customFormat($date);
-              }
-
-              $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $date, 'String');
-              $this->_qill[$grouping][] = $field['label'] . " {$qillOp} " . implode(', ', (array) $qillValue);
-            }
-            else {
-              if (is_numeric($fromValue) && strlen($fromValue) == 4) {
-                $fromValue = "01-01-{$fromValue}";
-              }
-
-              if (is_numeric($toValue) && strlen($toValue) == 4) {
-                $toValue = "01-01-{$toValue}";
-              }
-
-              // TO DO: add / remove time based on date parts
-              $fromDate = CRM_Utils_Date::processDate($fromValue);
-              $toDate = CRM_Utils_Date::processDate($toValue);
-              if (!$fromDate && !$toDate) {
-                continue;
-              }
-              if ($fromDate) {
-                $this->_where[$grouping][] = "$fieldName >= $fromDate";
-                $this->_qill[$grouping][] = $field['label'] . ' >= ' . CRM_Utils_Date::customFormat($fromDate);
-              }
-              if ($toDate) {
-                $this->_where[$grouping][] = "$fieldName <= $toDate";
-                $this->_qill[$grouping][] = $field['label'] . ' <= ' . CRM_Utils_Date::customFormat($toDate);
-              }
-            }
+            $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
+            list($qillOp, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, $field['label'], $value, $op, array(), CRM_Utils_Type::T_DATE);
+            $this->_qill[$grouping][] = "{$field['label']} $qillOp '$qillVal'";
             break;
 
           case 'File':
@@ -555,43 +481,4 @@ SELECT f.id, f.label, f.data_type,
     );
   }
 
-  /**
-   * @param int $id
-   * @param $label
-   * @param $type
-   * @param string $fieldName
-   * @param $value
-   * @param $grouping
-   */
-  public function searchRange(&$id, &$label, $type, $fieldName, &$value, &$grouping) {
-    $qill = array();
-
-    if (isset($value['from'])) {
-      $val = CRM_Utils_Type::escape($value['from'], $type);
-
-      if ($type == 'String') {
-        $this->_where[$grouping][] = "$fieldName >= '$val'";
-      }
-      else {
-        $this->_where[$grouping][] = "$fieldName >= $val";
-      }
-      $qill[] = ts('greater than or equal to \'%1\'', array(1 => $value['from']));
-    }
-
-    if (isset($value['to'])) {
-      $val = CRM_Utils_Type::escape($value['to'], $type);
-      if ($type == 'String') {
-        $this->_where[$grouping][] = "$fieldName <= '$val'";
-      }
-      else {
-        $this->_where[$grouping][] = "$fieldName <= $val";
-      }
-      $qill[] = ts('less than or equal to \'%1\'', array(1 => $value['to']));
-    }
-
-    if (!empty($qill)) {
-      $this->_qill[$grouping][] = $label . ' - ' . implode(' ' . ts('and') . ' ', $qill);
-    }
-  }
-
 }