From 33a17d7e5395a03e9f97a9870c25a7f6ae237524 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 3 Sep 2019 00:51:25 +1200 Subject: [PATCH] Use metadata for pledgeDateRange fields This moves the date handling for date fields to a re-usable function --- CRM/Contact/BAO/Query.php | 61 ++++++++++++++++++++++++++++++++++++++- CRM/Pledge/BAO/Query.php | 53 +++++++++------------------------- 2 files changed, 73 insertions(+), 41 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index ea0bedf7f4..b34d882dcd 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -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. * diff --git a/CRM/Pledge/BAO/Query.php b/CRM/Pledge/BAO/Query.php index 824eef7dc3..1859eb7e06 100644 --- a/CRM/Pledge/BAO/Query.php +++ b/CRM/Pledge/BAO/Query.php @@ -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': -- 2.25.1