Merge pull request #1229 from totten/master-exttesthook
[civicrm-core.git] / CRM / Core / DAO.php
index 64ecaecff1480557a7280cc0311ea91c5ab200bb..9a83e35c592e4a851c26913ff1f342ac3868f4ba 100644 (file)
@@ -1782,7 +1782,7 @@ EOS;
     foreach ($fields as $field) {
       $name = CRM_Utils_Array::value('name', $field);
       if ($name && isset($this->$name)) {
-        $label = CRM_Core_PseudoConstant::getValue(get_class($this), $name, $this->$name);
+        $label = CRM_Core_PseudoConstant::getLabel(get_class($this), $name, $this->$name);
         if ($label !== FALSE) {
           // Append 'label' onto the field name
           $labelName = $name . '_label';
@@ -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 $fieldname string name of fields
+   * @param $filter array filter to be applied indexed by operator
+   * @param $type String type of field (not actually used - nor in api @todo )
+   * @param $alias String alternative field name ('as') @todo- not actually used
+   */
+  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
+    $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