Use metadata for pledgeDateRange fields
authoreileen <emcnaughton@wikimedia.org>
Mon, 2 Sep 2019 12:51:25 +0000 (00:51 +1200)
committereileen <emcnaughton@wikimedia.org>
Mon, 2 Sep 2019 12:52:46 +0000 (00:52 +1200)
This moves the date handling for date fields to a re-usable function

CRM/Contact/BAO/Query.php
CRM/Pledge/BAO/Query.php

index ea0bedf7f478c69e49ff9617dff31333e81d23d1..b34d882dcd4a6f8bffee3c2841fc68dcec1ea4e7 100644 (file)
@@ -1822,6 +1822,7 @@ class CRM_Contact_BAO_Query {
       $this->buildRelativeDateQuery($values);
       return;
     }
+    // @todo also handle _low, _high generically here with if ($query->buildDateRangeQuery($values)) {return}
 
     // do not process custom fields or prefixed contact ids or component params
     if (CRM_Core_BAO_CustomField::getKeyID($values[0]) ||
@@ -5264,7 +5265,7 @@ civicrm_relationship.start_date > {$today}
    * @param string $dateFormat
    */
   public function dateQueryBuilder(
-    &$values, $tableName, $fieldName,
+    $values, $tableName, $fieldName,
     $dbFieldName, $fieldTitle,
     $appendTimeStamp = TRUE,
     $dateFormat = 'YmdHis'
@@ -6950,6 +6951,47 @@ AND   displayRelType.is_active = 1
     return isset($this->_fields[$realField]);
   }
 
+  /**
+   * Get the specifications for the field, if available.
+   *
+   * @param string $fieldName
+   *   Fieldname as displayed on the form.
+   *
+   * @return array
+   */
+  public function getFieldSpec($fieldName) {
+    if (isset($this->_fields[$fieldName])) {
+      return $this->_fields[$fieldName];
+    }
+    $lowFieldName = str_replace('_low', '', $fieldName);
+    if (isset($this->_fields[$lowFieldName])) {
+      return array_merge($this->_fields[$lowFieldName], ['field_name' => $lowFieldName]);
+    }
+    $highFieldName = str_replace('_high', '', $fieldName);
+    if (isset($this->_fields[$highFieldName])) {
+      return array_merge($this->_fields[$highFieldName], ['field_name' => $highFieldName]);
+    }
+    return [];
+  }
+
+  public function buildWhereForDate() {
+
+  }
+
+  /**
+   * Is the field a relative date field.
+   *
+   * @param string $fieldName
+   *
+   * @return bool
+   */
+  protected function isADateRangeField($fieldName) {
+    if (substr($fieldName, -4, 4) !== '_low' && substr($fieldName, -5, 5) !== '_high') {
+      return FALSE;
+    }
+    return !empty($this->getFieldSpec($fieldName));
+  }
+
   /**
    * @param $values
    */
@@ -6995,6 +7037,23 @@ AND   displayRelType.is_active = 1
     }
   }
 
+  /**
+   * Build the query for a date field if it is a _high or _low field.
+   *
+   * @param $values
+   *
+   * @return bool
+   */
+  public function buildDateRangeQuery($values) {
+    if ($this->isADateRangeField($values[0])) {
+      $fieldSpec = $this->getFieldSpec($values[0]);
+      $title = empty($fieldSpec['unique_title']) ? $fieldSpec['title'] : $fieldSpec['unique_title'];
+      $this->dateQueryBuilder($values, $fieldSpec['table_name'], $fieldSpec['field_name'], $fieldSpec['name'], $title);
+      return TRUE;
+    }
+    return FALSE;
+  }
+
   /**
    * Add the address table into the query.
    *
index 824eef7dc3ea03fd84b43053a2882f51f1ea93de..1859eb7e06f3e26a1a4782720331fe6199d98975 100644 (file)
@@ -245,51 +245,24 @@ class CRM_Pledge_BAO_Query extends CRM_Core_BAO_Query {
   }
 
   /**
-   * @param $values
-   * @param $query
+   * Get where clause for field.
+   *
+   * @todo most of this could be replaced by using metadata.
+   *
+   * @param array $values
+   * @param \CRM_Contact_BAO_Query $query
+   *
+   * @throws \CRM_Core_Exception
    */
   public static function whereClauseSingle(&$values, &$query) {
+    if ($query->buildDateRangeQuery($values)) {
+      // @todo - move this to Contact_Query in or near the call to
+      // $this->buildRelativeDateQuery($values);
+      return;
+    }
     list($name, $op, $value, $grouping, $wildcard) = $values;
 
     switch ($name) {
-      case 'pledge_create_date_low':
-      case 'pledge_create_date_high':
-        // process to / from date
-        $query->dateQueryBuilder($values,
-          'civicrm_pledge', 'pledge_create_date', 'create_date', 'Pledge Made'
-        );
-      case 'pledge_start_date_low':
-      case 'pledge_start_date_high':
-        // process to / from date
-        $query->dateQueryBuilder($values,
-          'civicrm_pledge', 'pledge_start_date', 'start_date', 'Pledge Start Date'
-        );
-        return;
-
-      case 'pledge_end_date_low':
-      case 'pledge_end_date_high':
-        // process to / from date
-        $query->dateQueryBuilder($values,
-          'civicrm_pledge', 'pledge_end_date', 'end_date', 'Pledge End Date'
-        );
-        return;
-
-      case 'pledge_payment_date_low':
-      case 'pledge_payment_date_high':
-        // process to / from date
-        $query->dateQueryBuilder($values,
-          'civicrm_pledge_payment', 'pledge_payment_date', 'scheduled_date', 'Payment Scheduled'
-        );
-        return;
-
-      case 'pledge_payment_scheduled_date_low':
-      case 'pledge_payment_scheduled_date_high':
-        // process to / from date
-        $query->dateQueryBuilder($values,
-          'civicrm_pledge_payment', 'pledge_payment_scheduled_date', 'scheduled_date', 'Payment Scheduled'
-        );
-        return;
-
       case 'pledge_amount':
       case 'pledge_amount_low':
       case 'pledge_amount_high':