* @param String $daoName
* @param String $fieldName
* @param Array $params
+ * - name string name of the option group
+ * - flip boolean results are return in id => label format if false
+ * if true, the results are reversed
+ * - grouping boolean if true, return the value in 'grouping' column (currently unsupported for tables other than option_value)
+ * - localize boolean if true, localize the results before returning
+ * - condition string add another condition to the sql query
+ * - labelColumnName string the column to use for 'label'
+ * - onlyActive boolean return only the action option values
+ * - fresh boolean ignore cache entries and go back to DB
*
* @return Array on success, FALSE on error.
- *
+ *
* @static
*/
public static function get($daoName, $fieldName, $params = array()) {
+ // Merge defaults
+ $params += array(
+ 'flip' => FALSE,
+ 'grouping' => FALSE,
+ 'localize' => FALSE,
+ 'condition' => NULL,
+ 'labelColumnName' => NULL,
+ 'onlyActive' => TRUE,
+ 'fresh' => FALSE,
+ );
+ $flip = $params['flip'];
+
$dao = new $daoName;
$fields = $dao->fields();
if (empty($fields[$fieldName])) {
}
$fieldSpec = $fields[$fieldName];
- // If the field is an enum, use explode the enum definition and return the array.
+ // If the field is an enum, explode the enum definition and return the array.
if (array_key_exists('enumValues', $fieldSpec)) {
// use of a space after the comma is inconsistent in xml
$enumStr = str_replace(', ', ',', $fieldSpec['enumValues']);
- return explode(',', $enumStr);
+ $values = explode(',', $enumStr);
+ return $flip ? array_flip($values) : $values;
}
+
elseif (!empty($fieldSpec['pseudoconstant'])) {
$pseudoconstant = $fieldSpec['pseudoconstant'];
if(!empty($pseudoconstant['optionGroupName'])) {
- // Translate $params array into function arguments;
- // populate default function params if not supplied in the array.
- $ret = CRM_Core_OptionGroup::values(
+ // Call our generic fn for retrieving from the option_value table
+ return CRM_Core_OptionGroup::values(
$pseudoconstant['optionGroupName'],
- CRM_Utils_Array::value('flip', $params, FALSE),
- CRM_Utils_Array::value('grouping', $params, FALSE),
- CRM_Utils_Array::value('localize', $params, FALSE),
- CRM_Utils_Array::value('condition', $params, NULL),
- CRM_Utils_Array::value('labelColumnName', $params, 'label'),
- CRM_Utils_Array::value('onlyActive', $params, TRUE),
- CRM_Utils_Array::value('fresh', $params, FALSE)
+ $flip,
+ $params['grouping'],
+ $params['localize'],
+ $params['condition'],
+ $params['labelColumnName'] ? $params['labelColumnName'] : 'label',
+ $params['onlyActive'],
+ $params['fresh']
);
- return $ret;
}
if (!empty($pseudoconstant['table'])) {
- // Sort params so the serialized string will be consistent.
+ // Normalize params so the serialized cache string will be consistent.
+ CRM_Utils_Array::remove($params, 'flip', 'fresh');
ksort($params);
$cacheKey = "{$daoName}{$fieldName}" . serialize($params);
- if (isset(self::$cache[$cacheKey])) {
+ // Return cached options
+ if (isset(self::$cache[$cacheKey]) && empty($params['fresh'])) {
return self::$cache[$cacheKey];
}
- $query = "
- SELECT
- %1 AS id, %2 AS label
- FROM
- %3
- ";
- if (!empty($pseudoconstant['condition'])) {
- $query .= " WHERE {$pseudoconstant['condition']}";
+ $select = "SELECT %1 AS id, %2 AS label";
+ $from = "FROM %3";
+ $wheres = array();
+ $order = "ORDER BY %2";
+ // Condition param can be passed as an sql string
+ if (!empty($params['condition'])) {
+ $wheres[] = $params['condition'];
}
- $query .= " ORDER BY %2";
+ // Support for onlyActive param if option table contains is_active field
+ if (!empty($params['onlyActive'])) {
+ $daoName = CRM_Core_AllCoreTables::getClassForTable($pseudoconstant['table']);
+ $dao = new $daoName;
+ if (in_array('is_active', $dao->fields())) {
+ $wheres[] = 'is_active = 1';
+ }
+ $dao->free();
+ }
+ // Support labelColumnName param
+ $labelColumnName = $params['labelColumnName'] ? $params['labelColumnName'] : $pseudoconstant['labelColumn'];
$queryParams = array(
1 => array($pseudoconstant['keyColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
- 2 => array($pseudoconstant['labelColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
+ 2 => array($labelColumnName, 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
3 => array($pseudoconstant['table'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
);
self::$cache[$cacheKey] = array();
+ $query = "$select $from";
+ if ($wheres) {
+ $query .= " WHERE " . implode($wheres, ' AND ');
+ }
+ $query .= ' ' . $order;
$dao = CRM_Core_DAO::executeQuery($query, $queryParams);
while ($dao->fetch()) {
self::$cache[$cacheKey][$dao->id] = $dao->label;
}
- if (CRM_Utils_Array::value('localize', $params)) {
+ $dao->free();
+ if (!empty($params['localize'])) {
$i18n = CRM_Core_I18n::singleton();
$i18n->localizeArray(self::$cache[$cacheKey]);
}
- return self::$cache[$cacheKey];
+
+ return $flip ? array_flip(self::$cache[$cacheKey]) : self::$cache[$cacheKey];
}
}
// If we're still here, it's an error. Return FALSE.