From: AS Date: Tue, 30 Apr 2013 03:59:05 +0000 (-0700) Subject: Added generic method CRM_Core_PseudoConstant::get(). CRM-12464 X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=887a4028914f63667f4dd155eacd8839ce1110cb;p=civicrm-core.git Added generic method CRM_Core_PseudoConstant::get(). CRM-12464 ---------------------------------------- * CRM-12464: Search improvements in 4.4 http://issues.civicrm.org/jira/browse/CRM-12464 --- diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index ba954aba61..ea8b5db65f 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -55,7 +55,10 @@ class CRM_Core_DAO extends DB_DataObject { // special value for mail bulk inserts to avoid // potential duplication, assuming a smaller number reduces number of queries // by some factor, so some tradeoff. CRM-8678 - BULK_MAIL_INSERT_COUNT = 10; + BULK_MAIL_INSERT_COUNT = 10, + QUERY_FORMAT_WILDCARD = 1, + QUERY_FORMAT_NO_QUOTES = 2; + /* * Define entities that shouldn't be created or deleted when creating/ deleting * test objects - this prevents world regions, countries etc from being added / deleted @@ -965,10 +968,17 @@ FROM civicrm_domain $item[1] == 'Memo' || $item[1] == 'Link' ) { - if (isset($item[2]) && - $item[2] - ) { - $item[0] = "'%{$item[0]}%'"; + // Support class constants stipulating wildcard characters and/or + // non-quoting of strings. Also support legacy code which may be + // passing in TRUE or 1 for $item[2], which used to indicate the + // use of wildcard characters. + if (!empty($item[2])) { + if ($item[2] & CRM_Core_DAO::QUERY_FORMAT_WILDCARD || $item[2] === TRUE) { + $item[0] = "'%{$item[0]}%'"; + } + elseif (!($item[2] & CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES)) { + $item[0] = "'{$item[0]}'"; + } } else { $item[0] = "'{$item[0]}'"; diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index d23081b288..4c551a6c70 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -49,6 +49,13 @@ */ class CRM_Core_PseudoConstant { + /** + * static cache for pseudoconstant arrays + * @var array + * @static + */ + private static $cache; + /** * location type * @var array @@ -392,6 +399,81 @@ class CRM_Core_PseudoConstant { */ private static $accountOptionValues; + /** + * Get options for a given field. + * @param String $daoName + * @param String $fieldName + * @param Array $params + * + * @return Array on success, FALSE on error. + * + * @static + */ + public static function get($daoName, $fieldName, $params = array()) { + $dao = new $daoName; + $fields = $dao->fields(); + $fieldSpec = $fields[$fieldName]; + + // If the field is an enum, use 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); + } + 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( + $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) + ); + return $ret; + } + if (!empty($pseudoconstant['table'])) { + // Sort params so the serialized string will be consistent. + ksort($params); + $cacheKey = "{$daoName}{$fieldName}" . serialize($params); + + if (isset(self::$cache[$cacheKey])) { + return self::$cache[$cacheKey]; + } + + $query = " + SELECT + %1 AS id, %2 AS label + FROM + %3 + "; + if (!empty($pseudoconstant['condition'])) { + $query .= " WHERE {$pseudoconstant['condition']}"; + } + $query .= " ORDER BY %2"; + $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), + 3 => array($pseudoconstant['table'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES), + ); + + self::$cache[$cacheKey] = array(); + $dao = CRM_Core_DAO::executeQuery($query, $queryParams); + while ($dao->fetch()) { + self::$cache[$cacheKey][$dao->id] = $dao->label; + } + return self::$cache[$cacheKey]; + } + } + // If we're still here, it's an error. Return FALSE. + return FALSE; + } + /** * populate the object from the database. generic populate * method diff --git a/tests/phpunit/CRM/Core/PseudoConstantTest.php b/tests/phpunit/CRM/Core/PseudoConstantTest.php new file mode 100644 index 0000000000..3e6d0cebb3 --- /dev/null +++ b/tests/phpunit/CRM/Core/PseudoConstantTest.php @@ -0,0 +1,56 @@ + 'PseudoConstant', + 'description' => 'Tests for pseudoconstant option values', + 'group' => 'Core', + ); + } + + function setUp() { + parent::setUp(); + } + + function testGender() { + $expected = array( + 1 => 'Female', + 2 => 'Male', + 3 => 'Transgender', + ); + + $actual = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); + $this->assertEquals($expected, $actual); + } +}