From a42ef93c177148b30f69393869b200be8b93abfc Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 30 Apr 2013 16:38:37 -0700 Subject: [PATCH] Support additional options for pseudoConstant::get CRM-12464 ---------------------------------------- * CRM-12464: Search improvements in 4.4 http://issues.civicrm.org/jira/browse/CRM-12464 --- CRM/Core/PseudoConstant.php | 95 +++++++++++++------ tests/phpunit/CRM/Core/PseudoConstantTest.php | 2 +- 2 files changed, 68 insertions(+), 29 deletions(-) diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index af97571dd1..224bcf8870 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -376,12 +376,33 @@ class CRM_Core_PseudoConstant { * @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])) { @@ -389,64 +410,82 @@ class CRM_Core_PseudoConstant { } $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. diff --git a/tests/phpunit/CRM/Core/PseudoConstantTest.php b/tests/phpunit/CRM/Core/PseudoConstantTest.php index a54c027665..0389fee30c 100644 --- a/tests/phpunit/CRM/Core/PseudoConstantTest.php +++ b/tests/phpunit/CRM/Core/PseudoConstantTest.php @@ -90,7 +90,7 @@ class CRM_Core_PseudoConstantTest extends CiviUnitTestCase { foreach ($fields as $daoName => $daoFields) { foreach ($daoFields as $field) { - $message = "DAO name: '{$daoName}', field: '{$fieldName}'"; + $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'"; // Ensure sample value is contained in the returned optionValues. $optionValues = CRM_Core_PseudoConstant::get($daoName, $field['fieldName']); -- 2.25.1