* 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
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
*
* @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
* @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