Skip to content

Commit

Permalink
Merge pull request #15191 from eileenmcnaughton/pledge_high_low
Browse files Browse the repository at this point in the history
Use metadata for pledgeDateRange fields
  • Loading branch information
seamuslee001 authored Sep 2, 2019
2 parents 149ab58 + 33a17d7 commit 57682f5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 41 deletions.
61 changes: 60 additions & 1 deletion CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,7 @@ public function whereClauseSingle(&$values, $apiEntity = NULL) {
$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]) ||
Expand Down Expand Up @@ -5264,7 +5265,7 @@ public static function &defaultHierReturnProperties() {
* @param string $dateFormat
*/
public function dateQueryBuilder(
&$values, $tableName, $fieldName,
$values, $tableName, $fieldName,
$dbFieldName, $fieldTitle,
$appendTimeStamp = TRUE,
$dateFormat = 'YmdHis'
Expand Down Expand Up @@ -6950,6 +6951,47 @@ protected function isARelativeDateField($fieldName) {
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
*/
Expand Down Expand Up @@ -6995,6 +7037,23 @@ protected function buildRelativeDateQuery(&$values) {
}
}

/**
* 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.
*
Expand Down
53 changes: 13 additions & 40 deletions CRM/Pledge/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,51 +245,24 @@ public static function where(&$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':
Expand Down

0 comments on commit 57682f5

Please sign in to comment.