From: eileen Date: Fri, 2 Aug 2013 10:53:11 +0000 (+1200) Subject: CRM-13149 array filter now working on relationship type X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=faf8c53b54f11f2537d8f985e99833953c548156;p=civicrm-core.git CRM-13149 array filter now working on relationship type --- diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index b2ce7079e4..4a171303b6 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -890,7 +890,7 @@ LEFT JOIN civicrm_country ON (civicrm_address.country_id = civicrm_country.id) if(!empty($params['relationship_type_id'])) { if(is_array($params['relationship_type_id'])) { // get our special function from DAO to deal with this - // $where .= $this->createSQLFilter('relationship_type_id', $params['relationship_type_id'], 'Integer'); + $where .= " AND " . CRM_Core_DAO::createSQLFilter('relationship_type_id', $params['relationship_type_id'], 'Integer'); } else { $where .= ' AND relationship_type_id = ' . CRM_Utils_Type::escape($params['relationship_type_id'], 'Positive'); diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index a441923db2..8ade8e0e13 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -1811,5 +1811,59 @@ EOS; return $contexts; } -} + /** + * SQL version of api function to assign filters to the DAO based on the syntax + * $field => array('IN' => array(4,6,9)) + * OR + * $field => array('LIKE' => array('%me%)) + * etc + * + * @param $field sql filter to be applied + * @param $fi + */ + public function createSQLFilter($fieldName, $filter, $type, $alias = NULL) { + // http://issues.civicrm.org/jira/browse/CRM-9150 - stick with 'simple' operators for now + // support for other syntaxes is discussed in ticket but being put off for now + //@todo consolidate this and the version from api/v3/utils.php into one location + $acceptedSQLOperators = array('=', '<=', '>=', '>', '<', 'LIKE', "<>", "!=", "NOT LIKE", 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'); + foreach ($filter as $operator => $criteria) { + if (in_array($operator, $acceptedSQLOperators)) { + switch ($operator) { + // unary operators + + case 'IS NULL': + case 'IS NOT NULL': + return (sprintf('%s %s', $fieldName, $operator)); + break; + + // ternary operators + case 'BETWEEN': + case 'NOT BETWEEN': + if (empty($criteria[0]) || empty($criteria[1])) { + throw new exception("invalid criteria for $operator"); + } + return (sprintf('%s ' . $operator . ' "%s" AND "%s"', $fieldName, CRM_Core_DAO::escapeString($criteria[0]), CRM_Core_DAO::escapeString($criteria[1]))); + break; + + // n-ary operators + case 'IN': + case 'NOT IN': + if (empty($criteria)) { + throw new exception("invalid criteria for $operator"); + } + $escapedCriteria = array_map(array( + 'CRM_Core_DAO', + 'escapeString' + ), $criteria); + return (sprintf('%s %s ("%s")', $fieldName, $operator, implode('", "', $escapedCriteria))); + break; + + // binary operators + default: + return(sprintf('%s %s "%s"', $fieldName, $operator, CRM_Core_DAO::escapeString($criteria))); + } + } + } + } +} \ No newline at end of file