Method to automatically retrieve option labels CRM-12464
authorColeman Watts <coleman@civicrm.org>
Mon, 15 Jul 2013 19:02:09 +0000 (12:02 -0700)
committerColeman Watts <coleman@civicrm.org>
Tue, 16 Jul 2013 00:31:55 +0000 (17:31 -0700)
----------------------------------------
* CRM-12464: Add PseudoConstants to Schema Metadata
  http://issues.civicrm.org/jira/browse/CRM-12464

CRM/Core/DAO.php
CRM/Core/PseudoConstant.php

index bc642752bed19b9f89a7de562c7f7a936a1090d6..1c3bc53ef33d862f7d3fa79675b49b14ae17f911 100644 (file)
@@ -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
    *
index f70baac25b667bffff38f3884bf825f82dcca144..82faf63e33f6578c44dc8c0e7929a64b21bbb686 100644 (file)
@@ -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