Merge pull request #5983 from relldoesphp/CRM-16544
[civicrm-core.git] / CRM / Core / OptionGroup.php
index da62d5ea6c4b885e7e80ab3101f1e562b0384445..7770090bd2bd7a72af7db7dd5be9af361664e53b 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id$
  *
  */
@@ -36,10 +36,9 @@ class CRM_Core_OptionGroup {
   static $_values = array();
   static $_cache = array();
 
-  /*
+  /**
    * $_domainIDGroups array maintains the list of option groups for whom
    * domainID is to be considered.
-   *
    */
   static $_domainIDGroups = array(
     'from_email_address',
@@ -55,7 +54,7 @@ class CRM_Core_OptionGroup {
    *
    * @return array
    */
-  static function &valuesCommon(
+  public static function &valuesCommon(
     $dao, $flip = FALSE, $grouping = FALSE,
     $localize = FALSE, $valueColumnName = 'label'
   ) {
@@ -112,11 +111,11 @@ class CRM_Core_OptionGroup {
    * @param string $keyColumnName
    *   the column to use for 'key'.
    *
-   * @return array      the values as specified by the above params
-   * @static
+   * @return array
+   *   the values as specified by the above params
    * @void
    */
-  static function &values(
+  public static function &values(
     $name, $flip = FALSE, $grouping = FALSE,
     $localize = FALSE, $condition = NULL,
     $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE, $keyColumnName = 'value'
@@ -199,7 +198,7 @@ WHERE  v.option_group_id = g.id
   }
 
   /**
-   * This function retrieves all the values for the specific option group by id
+   * This function retrieves all the values for the specific option group by id.
    * this is primarily used to create various html based form elements
    * (radio, select, checkbox etc). OptionGroups for most cases have the
    * 'label' in the label colum and the 'id' or 'name' in the value column
@@ -218,8 +217,8 @@ WHERE  v.option_group_id = g.id
    * @param bool $onlyActive
    * @param bool $fresh
    *
-   * @return array of values as specified by the above params
-   * @static
+   * @return array
+   *   Array of values as specified by the above params
    * @void
    */
   public static function &valuesByID($id, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE) {
@@ -276,7 +275,6 @@ WHERE  v.option_group_id = g.id
    *
    * @return void
    *
-   * @static
    */
   public static function lookupValues(&$params, &$names, $flip = FALSE) {
     foreach ($names as $postName => $value) {
@@ -303,14 +301,14 @@ WHERE  v.option_group_id = g.id
           }
 
           if ($flip) {
-            $p        = array(1 => array($postValue, 'String'));
+            $p = array(1 => array($postValue, 'String'));
             $lookupBy = 'v.label= %1';
-            $select   = "v.value";
+            $select = "v.value";
           }
           else {
-            $p        = array(1 => array($postValue, 'Integer'));
+            $p = array(1 => array($postValue, 'Integer'));
             $lookupBy = 'v.value = %1';
-            $select   = "v.label";
+            $select = "v.label";
           }
 
           $p[2] = array($value['groupName'], 'String');
@@ -357,7 +355,7 @@ WHERE  v.option_group_id = g.id
       $query .= " AND  v.is_active = 1 ";
     }
     $p = array(
-    1 => array($groupName, 'String'),
+      1 => array($groupName, 'String'),
       2 => array($value, 'Integer'),
     );
     $dao = CRM_Core_DAO::executeQuery($query, $p);
@@ -376,7 +374,7 @@ WHERE  v.option_group_id = g.id
    *
    * @return null
    */
-  static function getValue(
+  public static function getValue(
     $groupName,
     $label,
     $labelField = 'label',
@@ -399,7 +397,7 @@ WHERE  v.option_group_id = g.id
 ";
 
     $p = array(
-    1 => array($groupName, 'String'),
+      1 => array($groupName, 'String'),
       2 => array($label, $labelType),
     );
     $dao = CRM_Core_DAO::executeQuery($query, $p);
@@ -417,9 +415,9 @@ WHERE  v.option_group_id = g.id
    * @param string $groupName
    *   The name of the option group.
    *
-   * @static
    *
-   * @return string   the value from the row where is_default = true
+   * @return string
+   *   the value from the row where is_default = true
    */
   public static function getDefaultValue($groupName) {
     if (empty($groupName)) {
@@ -444,7 +442,7 @@ WHERE  v.option_group_id = g.id
   }
 
   /**
-   * Creates a new option group with the passed in values
+   * Creates a new option group with the passed in values.
    * @TODO: Should update the group if it already exists intelligently, so multi-lingual is
    * not messed up. Currently deletes the old group
    *
@@ -465,18 +463,18 @@ WHERE  v.option_group_id = g.id
    * @param null $groupTitle
    *   The optional label of the option group else set to group name.
    *
-   * @static
    *
-   * @return int   the option group ID
+   * @return int
+   *   the option group ID
    */
   public static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
     self::deleteAssoc($groupName);
     if (!empty($values)) {
-      $group              = new CRM_Core_DAO_OptionGroup();
-      $group->name        = $groupName;
-      $group->title       = empty($groupTitle) ? $groupName : $groupTitle;
+      $group = new CRM_Core_DAO_OptionGroup();
+      $group->name = $groupName;
+      $group->title = empty($groupTitle) ? $groupName : $groupTitle;
       $group->is_reserved = 1;
-      $group->is_active   = 1;
+      $group->is_active = 1;
       $group->save();
 
       foreach ($values as $v) {
@@ -581,7 +579,7 @@ SELECT v.label
    AND g.name  = %1
    AND v.value = %2";
     $params = array(
-    1 => array($groupName, 'String'),
+      1 => array($groupName, 'String'),
       2 => array($value, 'String'),
     );
     return CRM_Core_DAO::singleValueQuery($query, $params);
@@ -593,12 +591,14 @@ SELECT v.label
    * @param string $field
    * @param string $fieldType
    * @param bool $active
+   * @param bool $localize
+   *   if true, localize the results before returning.
    *
    * @return array
    */
-  static function getRowValues(
+  public static function getRowValues(
     $groupName, $fieldValue, $field = 'name',
-    $fieldType = 'String', $active = TRUE
+    $fieldType = 'String', $active = TRUE, $localize = FALSE
   ) {
     $query = "
 SELECT v.id, v.label, v.value, v.name, v.weight, v.description
@@ -615,7 +615,7 @@ WHERE  v.option_group_id = g.id
     }
 
     $p = array(
-    1 => array($groupName, 'String'),
+      1 => array($groupName, 'String'),
       2 => array($fieldValue, $fieldType),
     );
     $dao = CRM_Core_DAO::executeQuery($query, $p);
@@ -623,24 +623,36 @@ WHERE  v.option_group_id = g.id
 
     if ($dao->fetch()) {
       foreach (array(
-        'id', 'name', 'value', 'label', 'weight', 'description') as $fld) {
+                 'id',
+                 'name',
+                 'value',
+                 'label',
+                 'weight',
+                 'description',
+               ) as $fld) {
         $row[$fld] = $dao->$fld;
       }
     }
+
+    if ($localize) {
+      foreach (array('label', 'description') as $f) {
+        $row[$f] = ts($row[$f]);
+      }
+    }
+
     return $row;
   }
 
-  /*
-   * Wrapper for calling values with fresh set to true to empty the given value
+  /**
+   * Wrapper for calling values with fresh set to true to empty the given value.
    *
    * Since there appears to be some inconsistency
    * (@todo remove inconsistency) around the pseudoconstant operations
    * (for example CRM_Contribution_Pseudoconstant::paymentInstrument doesn't specify isActive
    * which is part of the cache key
    * will do a couple of variations & aspire to someone cleaning it up later
-   */
-  /**
-   * @param $name
+   *
+   * @param string $name
    * @param array $params
    */
   public static function flush($name, $params = array()) {
@@ -680,4 +692,5 @@ WHERE  v.option_group_id = g.id
     self::$_cache = array();
     CRM_Utils_Cache::singleton()->flush();
   }
+
 }