From a8c235266c596832fe377421410262cd5508ab3c Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 30 Jul 2013 09:52:33 -0700 Subject: [PATCH] More robust ways to get pseudoconstant labels and names CRM-12464 ---------------------------------------- * CRM-12464: Add PseudoConstants to Schema Metadata http://issues.civicrm.org/jira/browse/CRM-12464 --- CRM/Contact/BAO/Contact.php | 2 +- CRM/Core/BAO/UFGroup.php | 2 +- CRM/Core/DAO.php | 2 +- CRM/Core/PseudoConstant.php | 38 ++++++++++++++----- CRM/Profile/Selector/Listings.php | 6 +-- CRM/Report/Form/Grant/Detail.php | 4 +- CRM/Utils/Token.php | 2 +- .../org.civicrm.report.grant/Grant.php | 4 +- 8 files changed, 40 insertions(+), 20 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index d31a946947..2edbefdf59 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -2356,7 +2356,7 @@ AND civicrm_openid.is_primary = 1"; // get preferred languages if (!empty($contact->preferred_language)) { - $values['preferred_language'] = CRM_Core_PseudoConstant::getValue('CRM_Contact_DAO_Contact', 'preferred_language', $contact->preferred_language); + $values['preferred_language'] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'preferred_language', $contact->preferred_language); } // Calculating Year difference diff --git a/CRM/Core/BAO/UFGroup.php b/CRM/Core/BAO/UFGroup.php index 3755d2be4a..20d4465d4f 100644 --- a/CRM/Core/BAO/UFGroup.php +++ b/CRM/Core/BAO/UFGroup.php @@ -982,7 +982,7 @@ class CRM_Core_BAO_UFGroup extends CRM_Core_DAO_UFGroup { } elseif ($name === 'preferred_language') { $params[$index] = $details->$name; - $values[$index] = CRM_Core_PseudoConstant::getValue('CRM_Contact_DAO_Contact', 'preferred_language', $details->$name); + $values[$index] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'preferred_language', $details->$name); } elseif ($name == 'group') { $groups = CRM_Contact_BAO_GroupContact::getContactGroup($cid, 'Added', NULL, FALSE, TRUE); diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index bbf4eebc87..6e7159e714 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -1782,7 +1782,7 @@ EOS; 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); + $label = CRM_Core_PseudoConstant::getLabel(get_class($this), $name, $this->$name); if ($label !== FALSE) { // Append 'label' onto the field name $labelName = $name . '_label'; diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index 097cfa3d3b..ef8b6daca7 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -440,20 +440,41 @@ class CRM_Core_PseudoConstant { } /** - * Fetch the label (or other value) for a field given its key + * Fetch the translated label for a field given its key * - * @param String $daoName + * @param String $baoName * @param String $fieldName * @param String|Int $key - * @param Array $params will be passed into self::get + * + * TODO: Accept multivalued input? * * @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, 'get'); + static function getLabel($baoName, $fieldName, $key) { + $values = $baoName::buildOptions($fieldName, 'get'); + if ($values === FALSE) { + return FALSE; + } + return CRM_Utils_Array::value($key, $values); + } + + /** + * Fetch the machine name for a field given its key + * + * @param String $baoName + * @param String $fieldName + * @param String|Int $key + * + * @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 getName($baoName, $fieldName, $key) { + $values = $baoName::buildOptions($fieldName, 'validate'); if ($values === FALSE) { return FALSE; } @@ -463,18 +484,17 @@ class CRM_Core_PseudoConstant { /** * Fetch the key for a field option given its name * - * @param String $daoName + * @param String $baoName * @param String $fieldName * @param String|Int $value - * @param Array $params will be passed into self::get * * @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, 'validate'); + static function getKey($baoName, $fieldName, $value) { + $values = $baoName::buildOptions($fieldName, 'validate'); if ($values === FALSE) { return FALSE; } diff --git a/CRM/Profile/Selector/Listings.php b/CRM/Profile/Selector/Listings.php index 65e08c0471..25506a4a74 100644 --- a/CRM/Profile/Selector/Listings.php +++ b/CRM/Profile/Selector/Listings.php @@ -605,7 +605,7 @@ class CRM_Profile_Selector_Listings extends CRM_Core_Selector_Base implements CR ) { $url = CRM_Utils_System::fixURL($result->$name); $typeId = substr($name, 0, -4) . "-website_type_id"; - $typeName = CRM_Core_PseudoConstant::getValue('CRM_Core_DAO_Website', 'website_type_id', $result->$typeId); + $typeName = CRM_Core_PseudoConstant::getLabel('CRM_Core_DAO_Website', 'website_type_id', $result->$typeId); if ($typeName) { $row[] = "{$result->$name} (${typeName})"; } @@ -614,7 +614,7 @@ class CRM_Profile_Selector_Listings extends CRM_Core_Selector_Base implements CR } } elseif ($name == 'preferred_language') { - $row[] = CRM_Core_PseudoConstant::getValue('CRM_Contact_DAO_Contact', 'preferred_language', $result->$name); + $row[] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'preferred_language', $result->$name); } elseif ($multipleSelectFields && array_key_exists($name, $multipleSelectFields) @@ -641,7 +641,7 @@ class CRM_Profile_Selector_Listings extends CRM_Core_Selector_Base implements CR elseif (strpos($name, '-im')) { if (!empty($result->$name)) { $providerId = $name . "-provider_id"; - $providerName = CRM_Core_PseudoConstant::getValue('CRM_Core_DAO_IM', 'provider_id', $result->$providerId); + $providerName = CRM_Core_PseudoConstant::getLabel('CRM_Core_DAO_IM', 'provider_id', $result->$providerId); $row[] = $result->$name . " ({$providerName})"; } else { diff --git a/CRM/Report/Form/Grant/Detail.php b/CRM/Report/Form/Grant/Detail.php index edaa9abab4..80b5354f35 100644 --- a/CRM/Report/Form/Grant/Detail.php +++ b/CRM/Report/Form/Grant/Detail.php @@ -360,13 +360,13 @@ class CRM_Report_Form_Grant_Detail extends CRM_Report_Form { if (array_key_exists('civicrm_grant_grant_type_id', $row)) { if ($value = $row['civicrm_grant_grant_type_id']) { - $rows[$rowNum]['civicrm_grant_grant_type_id'] = CRM_Core_PseudoConstant::getValue('CRM_Grant_DAO_Grant', 'grant_type_id', $value); + $rows[$rowNum]['civicrm_grant_grant_type_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Grant_DAO_Grant', 'grant_type_id', $value); } $entryFound = TRUE; } if (array_key_exists('civicrm_grant_status_id', $row)) { if ($value = $row['civicrm_grant_status_id']) { - $rows[$rowNum]['civicrm_grant_status_id'] = CRM_Core_PseudoConstant::getValue('CRM_Grant_DAO_Grant', 'status_id', $value); + $rows[$rowNum]['civicrm_grant_status_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Grant_DAO_Grant', 'status_id', $value); } $entryFound = TRUE; } diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index cbe44e723d..6ca9aa722e 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -460,7 +460,7 @@ class CRM_Utils_Token { break; case 'approvalStatus': - $value = CRM_Core_PseudoConstant::getValue('CRM_Mailing_DAO_Mailing', 'approval_status_id', $mailing->approval_status_id); + $value = CRM_Core_PseudoConstant::getLabel('CRM_Mailing_DAO_Mailing', 'approval_status_id', $mailing->approval_status_id); break; case 'approvalNote': diff --git a/tools/extensions/org.civicrm.report.grant/Grant.php b/tools/extensions/org.civicrm.report.grant/Grant.php index ca69ce7533..dd4b734dc2 100644 --- a/tools/extensions/org.civicrm.report.grant/Grant.php +++ b/tools/extensions/org.civicrm.report.grant/Grant.php @@ -309,13 +309,13 @@ class org_civicrm_report_grant extends CRM_Report_Form { foreach ($rows as $rowNum => $row) { if (array_key_exists('civicrm_grant_grant_type_id', $row)) { if ($value = $row['civicrm_grant_grant_type_id']) { - $rows[$rowNum]['civicrm_grant_grant_type_id'] = CRM_Core_PseudoConstant::getValue('CRM_Grant_DAO_Grant', 'grant_type_id', $value); + $rows[$rowNum]['civicrm_grant_grant_type_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Grant_DAO_Grant', 'grant_type_id', $value); } $entryFound = TRUE; } if (array_key_exists('civicrm_grant_status_id', $row)) { if ($value = $row['civicrm_grant_status_id']) { - $rows[$rowNum]['civicrm_grant_status_id'] = CRM_Core_PseudoConstant::getValue('CRM_Grant_DAO_Grant', 'status_id', $value); + $rows[$rowNum]['civicrm_grant_status_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Grant_DAO_Grant', 'status_id', $value); } $entryFound = TRUE; } -- 2.25.1