From 2a3f958d6bbd60682730f98b2eb06c228172c661 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 15 Jul 2013 12:02:09 -0700 Subject: [PATCH] Method to automatically retrieve option labels CRM-12464 ---------------------------------------- * CRM-12464: Add PseudoConstants to Schema Metadata http://issues.civicrm.org/jira/browse/CRM-12464 --- CRM/Core/DAO.php | 29 ++++++++++++++++++++++++++--- CRM/Core/PseudoConstant.php | 28 ++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index bc642752be..1c3bc53ef3 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -1763,9 +1763,9 @@ EOS; * 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: @see CRM_Core_DAO::buildOptionsContext - * @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 @@ -1773,6 +1773,29 @@ EOS; 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::getValue(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 * diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index f70baac25b..82faf63e33 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -409,12 +409,18 @@ class CRM_Core_PseudoConstant { * @param String|Int $key * @param Array $params will be passed into self::get * - * @return string + * @return bool|null|string + * FALSE if the given field has no associated option list + * NULL if the given key has no corresponding option + * String if label is found */ static function getValue($daoName, $fieldName, $key, $params = array()) { - $values = self::get($daoName, $fieldName, $params); - return CRM_Utils_Array::value($key, $values); - } + $values = self::get($daoName, $fieldName, $params); + if ($values === FALSE) { + return FALSE; + } + return CRM_Utils_Array::value($key, $values); + } /** * Fetch the key for a field option given its label/name @@ -424,12 +430,18 @@ class CRM_Core_PseudoConstant { * @param String|Int $value * @param Array $params will be passed into self::get * - * @return string + * @return bool|null|string|number + * FALSE if the given field has no associated option list + * NULL if the given key has no corresponding option + * String|Number if key is found */ static function getKey($daoName, $fieldName, $value, $params = array()) { - $values = self::get($daoName, $fieldName, $params); - return CRM_Utils_Array::key($value, $values); - } + $values = self::get($daoName, $fieldName, $params); + if ($values === FALSE) { + return FALSE; + } + return CRM_Utils_Array::key($value, $values); + } /** * DEPRECATED generic populate method -- 2.25.1