From a1ef51e2c50a5944484752ec3880aa4044ce2888 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 2 May 2013 15:27:53 -0700 Subject: [PATCH] Add support for orderColumn CRM-12464 ---------------------------------------- * CRM-12464: Search improvements in 4.4 http://issues.civicrm.org/jira/browse/CRM-12464 --- CRM/Core/PseudoConstant.php | 106 +++++++++++------- tests/phpunit/CRM/Core/PseudoConstantTest.php | 2 +- 2 files changed, 65 insertions(+), 43 deletions(-) diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index dea911b368..15ecee40b0 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -230,6 +230,7 @@ class CRM_Core_PseudoConstant { * - condition string add another condition to the sql query * - keyColumn string the column to use for 'id' * - labelColumn string the column to use for 'label' + * - orderColumn string the column to use for sorting, defaults to 'weight' * - onlyActive boolean return only the action option values * - fresh boolean ignore cache entries and go back to DB * @@ -251,8 +252,8 @@ class CRM_Core_PseudoConstant { if (isset($fieldSpec['enumValues'])) { // use of a space after the comma is inconsistent in xml $enumStr = str_replace(', ', ',', $fieldSpec['enumValues']); - $values = explode(',', $enumStr); - return $flip ? array_flip($values) : $values; + $output = explode(',', $enumStr); + return $flip ? array_flip($output) : $output; } elseif (!empty($fieldSpec['pseudoconstant'])) { @@ -276,7 +277,7 @@ class CRM_Core_PseudoConstant { $flip, $params['grouping'], $params['localize'], - $params['condition'] ? $params['condition'] : $pseudoconstant['condition'], + $params['condition'], $params['labelColumn'] ? $params['labelColumn'] : 'label', $params['onlyActive'], $params['fresh'] @@ -288,53 +289,74 @@ class CRM_Core_PseudoConstant { // Normalize params so the serialized cache string will be consistent. CRM_Utils_Array::remove($params, 'flip', 'fresh'); ksort($params); - $cacheKey = "{$daoName}{$fieldName}" . serialize($params); + $cacheKey = $daoName . $fieldName . serialize($params); - // Return cached options + // Retrieve cached options if (isset(self::$cache[$cacheKey]) && empty($params['fresh'])) { - return $flip ? array_flip(self::$cache[$cacheKey]) : self::$cache[$cacheKey]; + $output = self::$cache[$cacheKey]; } - - $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']; - } - // Support for onlyActive param if option table contains is_active field - if (!empty($params['onlyActive'])) { + else { + // Get list of fields for the option table $daoName = CRM_Core_AllCoreTables::getClassForTable($pseudoconstant['table']); $dao = new $daoName; - if (in_array('is_active', $dao->fields())) { - $wheres[] = 'is_active = 1'; - } + $availableFields = $dao->fields(); $dao->free(); - } - $queryParams = array( - 1 => array($params['keyColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES), - 2 => array($params['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(); - $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; - } - $dao->free(); - if (!empty($params['localize'])) { - $i18n = CRM_Core_I18n::singleton(); - $i18n->localizeArray(self::$cache[$cacheKey]); - } + $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']; + } + // Support for onlyActive param if option table contains is_active field + if (!empty($params['onlyActive'])) { + if (in_array('is_active', $availableFields)) { + $wheres[] = 'is_active = 1'; + } + } + $queryParams = array( + 1 => array($params['keyColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES), + 2 => array($params['labelColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES), + 3 => array($pseudoconstant['table'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES), + ); + // Add orderColumn param + if (!empty($params['orderColumn'])) { + $queryParams[4] = array($params['orderColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES); + $order = "ORDER BY %4"; + } + // Support no sorting if $params[orderColumn] is FALSE + elseif (isset($params['orderColumn']) && $params['orderColumn'] === FALSE) { + $order = ''; + } + // Default to 'weight' if that column exists + elseif (in_array('weight', $availableFields)) { + $order = "ORDER BY weight"; + } - return $flip ? array_flip(self::$cache[$cacheKey]) : self::$cache[$cacheKey]; + $output = array(); + $query = "$select $from"; + if ($wheres) { + $query .= " WHERE " . implode($wheres, ' AND '); + } + $query .= ' ' . $order; + $dao = CRM_Core_DAO::executeQuery($query, $queryParams); + while ($dao->fetch()) { + $output[$dao->id] = $dao->label; + } + $dao->free(); + if (!empty($params['localize'])) { + $i18n = CRM_Core_I18n::singleton(); + $i18n->localizeArray($output); + // Maintain sort by label + if ($order == "ORDER BY %2") { + CRM_Utils_Array::asort($output); + } + } + self::$cache[$cacheKey] = $output; + } + return $flip ? array_flip($output) : $output; } } // 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 0b8610b3b3..54d4f18b21 100644 --- a/tests/phpunit/CRM/Core/PseudoConstantTest.php +++ b/tests/phpunit/CRM/Core/PseudoConstantTest.php @@ -242,7 +242,7 @@ class CRM_Core_PseudoConstantTest extends CiviUnitTestCase { 2 => 'Household', 3 => 'Organization', ); - // By default this should return by name + // By default this should return an array keyed by name $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type'); $this->assertEquals($byName, $result); // But we can also fetch by ID -- 2.25.1