diff --git a/CRM/Report/Form/Contact/Relationship.php b/CRM/Report/Form/Contact/Relationship.php index b696c5497b88..8eaa4b0eb3e4 100644 --- a/CRM/Report/Form/Contact/Relationship.php +++ b/CRM/Report/Form/Contact/Relationship.php @@ -219,12 +219,30 @@ public function __construct() { ), 'type' => CRM_Utils_Type::T_INT, ), + 'is_valid' => array( + 'title' => ts('Relationship Dates Validity'), + 'operatorType' => CRM_Report_Form::OP_SELECT, + 'options' => array( + NULL => ts('- Any -'), + 1 => ts('Not expired'), + 0 => ts('Expired'), + ), + 'type' => CRM_Utils_Type::T_INT, + ), 'relationship_type_id' => array( 'title' => ts('Relationship'), 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE), 'type' => CRM_Utils_Type::T_INT, ), + 'start_date' => array( + 'title' => ts('Start Date'), + 'type' => CRM_Utils_Type::T_DATE, + ), + 'end_date' => array( + 'title' => ts('End Date'), + 'type' => CRM_Utils_Type::T_DATE, + ), ), 'grouping' => 'relation-fields', ), @@ -417,13 +435,17 @@ public function where() { } } else { - - $clause = $this->whereClause($field, - $op, - CRM_Utils_Array::value("{$fieldName}_value", $this->_params), - CRM_Utils_Array::value("{$fieldName}_min", $this->_params), - CRM_Utils_Array::value("{$fieldName}_max", $this->_params) - ); + if ($fieldName == 'is_valid') { + $clause = $this->buildValidityQuery(CRM_Utils_Array::value("{$fieldName}_value", $this->_params)); + } + else { + $clause = $this->whereClause($field, + $op, + CRM_Utils_Array::value("{$fieldName}_value", $this->_params), + CRM_Utils_Array::value("{$fieldName}_min", $this->_params), + CRM_Utils_Array::value("{$fieldName}_max", $this->_params) + ); + } } } } @@ -638,5 +660,23 @@ public function alterDisplay(&$rows) { } } } + + /** + * @param $valid bool - set to 1 if we are looking for a valid relationship, 0 if not + * + * @return array + */ + public function buildValidityQuery($valid) { + $clause = NULL; + if ($valid == '1') { + // relationships dates are not expired + $clause = "((start_date <= CURDATE() OR start_date is null) AND (end_date >= CURDATE() OR end_date is null))"; + } + elseif ($valid == '0') { + // relationships dates are expired or has not started yet + $clause = "(start_date >= CURDATE() OR end_date < CURDATE())"; + } + return $clause; + } }