}
}
- // CRM-11582
- foreach($tr as $key => $value) {
- $key = preg_quote($key);
- $query = preg_replace("/$key\b/", $value, $query);
- }
- return $query;
+ return strtr($query, $tr);
}
static function freeResult($ids = NULL) {
continue;
}
// Pick an option value if needed
- $options = $daoName::buildOptions($dbName);
- if ($options) {
- $object->$dbName = key($options);
- continue;
+ if ($value['type'] !== CRM_Utils_Type::T_BOOLEAN) {
+ $options = $daoName::buildOptions($dbName, 'create');
+ if ($options) {
+ $object->$dbName = key($options);
+ continue;
+ }
}
switch ($value['type']) {
$object->$dbName = $counter;
break;
- case CRM_Utils_Type::T_BOOL:
case CRM_Utils_Type::T_BOOLEAN:
if (isset($value['default'])) {
$object->$dbName = $value['default'];
* This function can be overridden by each BAO to add more logic related to context.
* The overriding function will generally call the lower-level CRM_Core_PseudoConstant::get
*
- * @param String $fieldName
- * @param String $context: e.g. "search" "edit" "create" "view"
- * @param Array $props: whatever is known about this bao object
+ * @param string $fieldName
+ * @param string $context: @see CRM_Core_DAO::buildOptionsContext
+ * @param array $props: whatever is known about this bao object
*/
public static function buildOptions($fieldName, $context = NULL, $props = array()) {
// If a given bao does not override this function
$baoName = get_called_class();
- return CRM_Core_PseudoConstant::get($baoName, $fieldName);
+ return CRM_Core_PseudoConstant::get($baoName, $fieldName, array(), $context);
}
-}
+ /**
+ * Populate option labels for this object's fields.
+ *
+ * @throws exception if called directly on the base class
+ */
+ public function getOptionLabels() {
+ $fields = $this->fields();
+ if ($fields === NULL) {
+ throw new exception ('Cannot call getOptionLabels on CRM_Core_DAO');
+ }
+ foreach ($fields as $field) {
+ $name = CRM_Utils_Array::value('name', $field);
+ if ($name && isset($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';
+ $this->$labelName = $label;
+ }
+ }
+ }
+ }
+
+ /**
+ * Provides documentation and validation for the buildOptions $context param
+ *
+ * @param String $context
+ */
+ public static function buildOptionsContext($context = NULL) {
+ $contexts = array(
+ 'get' => "All options are returned, even if they are disabled. Labels are translated.",
+ 'create' => "Options are filtered appropriately for the object being created/updated. Labels are translated.",
+ 'search' => "Searchable options are returned. Labels are translated.",
+ 'validate' => "All options are returned, even if they are disabled. Machine names are used in place of labels.",
+ );
+ // Validation: enforce uniformity of this param
+ if ($context !== NULL && !isset($contexts[$context])) {
+ throw new exception("'$context' is not a valid context for buildOptions.");
+ }
+ 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