Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-08-19-00-06-22
[civicrm-core.git] / CRM / Core / OptionGroup.php
index 4008a3ee830091e4ba532654c7c480bbaede5808..b9560ea7e70a50ae71d5c6193ced9619452f50cb 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -46,6 +46,15 @@ class CRM_Core_OptionGroup {
     'grant_type',
   );
 
+  /**
+   * @param $dao
+   * @param bool $flip
+   * @param bool $grouping
+   * @param bool $localize
+   * @param string $valueColumnName
+   *
+   * @return array
+   */
   static function &valuesCommon(
     $dao, $flip = FALSE, $grouping = FALSE,
     $localize = FALSE, $valueColumnName = 'label'
@@ -163,6 +172,7 @@ WHERE  v.option_group_id = g.id
    * @param $condition
    * @param $labelColumnName
    * @param $onlyActive
+   * @param string $keyColumnName
    */
   protected static function flushValues($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName = 'value') {
     $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName);
@@ -171,6 +181,9 @@ WHERE  v.option_group_id = g.id
     unset(self::$_cache[$cacheKey]);
   }
 
+  /**
+   * @return string
+   */
   protected static function createCacheKey() {
     $cacheKey = "CRM_OG_" . serialize(func_get_args());
     return $cacheKey;
@@ -189,17 +202,22 @@ WHERE  v.option_group_id = g.id
    * @param $localize   boolean if true, localize the results before returning
    * @param $labelColumnName string the column to use for 'label'
    *
+   * @param bool $onlyActive
+   * @param bool $fresh
+   *
    * @return array      the values as specified by the above params
    * @static
    * @void
    */
-  static function &valuesByID($id, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $labelColumnName = 'label') {
-    $cacheKey = "CRM_OG_ID_{$id}_{$flip}_{$grouping}_{$localize}_{$labelColumnName}";
+  static function &valuesByID($id, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE) {
+    $cacheKey = self::createCacheKey($id, $flip, $grouping, $localize, $labelColumnName, $onlyActive);
 
     $cache = CRM_Utils_Cache::singleton();
-    $var = $cache->get($cacheKey);
-    if ($var) {
-      return $var;
+    if (!$fresh) {
+      $var = $cache->get($cacheKey);
+      if ($var) {
+        return $var;
+      }
     }
     $query = "
 SELECT  v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as grouping
@@ -207,10 +225,13 @@ FROM   civicrm_option_value v,
        civicrm_option_group g
 WHERE  v.option_group_id = g.id
   AND  g.id              = %1
-  AND  v.is_active       = 1
   AND  g.is_active       = 1
-  ORDER BY v.weight, v.label;
 ";
+    if ($onlyActive) {
+      $query .= " AND  v.is_active = 1 ";
+    }
+    $query .= " ORDER BY v.weight, v.label";
+
     $p = array(1 => array($id, 'Integer'));
     $dao = CRM_Core_DAO::executeQuery($query, $p);
 
@@ -296,6 +317,13 @@ WHERE  v.option_group_id = g.id
     }
   }
 
+  /**
+   * @param $groupName
+   * @param $value
+   * @param bool $onlyActiveValue
+   *
+   * @return null
+   */
   static function getLabel($groupName, $value, $onlyActiveValue = TRUE) {
     if (empty($groupName) ||
       empty($value)
@@ -325,6 +353,15 @@ WHERE  v.option_group_id = g.id
     return NULL;
   }
 
+  /**
+   * @param $groupName
+   * @param $label
+   * @param string $labelField
+   * @param string $labelType
+   * @param string $valueField
+   *
+   * @return null
+   */
   static function getValue($groupName,
     $label,
     $labelField = 'label',
@@ -367,7 +404,7 @@ WHERE  v.option_group_id = g.id
    * @static
    *
    * @return string   the value from the row where is_default = true
-   */   
+   */
   static function getDefaultValue($groupName) {
     if (empty($groupName)) {
       return NULL;
@@ -389,14 +426,14 @@ WHERE  v.option_group_id = g.id
     $p = array(1 => array($groupName, 'String'));
     return CRM_Core_DAO::singleValueQuery($query, $p);
   }
-  
+
   /**
    * 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
    *
    * @param string $groupName the name of the option group - make sure there is no conflict
-   * @param array  $values    the associative array that has information on the option values
+   * @param array $values the associative array that has information on the option values
    *                          the keys of this array are:
    *                          string 'title'       (required)
    *                          string 'value'       (required)
@@ -405,14 +442,15 @@ WHERE  v.option_group_id = g.id
    *                          int    'weight'      (optional) - the order in which the value are displayed
    *                          bool   'is_default'  (optional) - is this the default one to display when rendered in form
    *                          bool   'is_active'   (optional) - should this element be rendered
-   * @param int    $defaultID (reference) - the option value ID of the default element (if set) is returned else 'null'
-   * @param string $groupLabel            - the optional label of the option group else set to group name
+   * @param int $defaultID (reference) - the option value ID of the default element (if set) is returned else 'null'
+   * @param null $groupTitle
+   *
+   * @internal param string $groupLabel - the optional label of the option group else set to group name
    *
    * @access public
    * @static
    *
    * @return int   the option group ID
-   *
    */
   static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
     self::deleteAssoc($groupName);
@@ -448,6 +486,12 @@ WHERE  v.option_group_id = g.id
     return $group->id;
   }
 
+  /**
+   * @param $groupName
+   * @param $values
+   * @param bool $flip
+   * @param string $field
+   */
   static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
     $query = "
 SELECT v.id as amount_id, v.value, v.label, v.name, v.description, v.weight
@@ -488,6 +532,10 @@ ORDER BY v.weight
     }
   }
 
+  /**
+   * @param $groupName
+   * @param string $operator
+   */
   static function deleteAssoc($groupName, $operator = "=") {
     $query = "
 DELETE g, v
@@ -501,6 +549,12 @@ DELETE g, v
     $dao = CRM_Core_DAO::executeQuery($query, $params);
   }
 
+  /**
+   * @param $groupName
+   * @param $value
+   *
+   * @return null|string
+   */
   static function optionLabel($groupName, $value) {
     $query = "
 SELECT v.label
@@ -515,6 +569,15 @@ SELECT v.label
     return CRM_Core_DAO::singleValueQuery($query, $params);
   }
 
+  /**
+   * @param $groupName
+   * @param $fieldValue
+   * @param string $field
+   * @param string $fieldType
+   * @param bool $active
+   *
+   * @return array
+   */
   static function getRowValues($groupName, $fieldValue, $field = 'name',
     $fieldType = 'String', $active = TRUE
   ) {
@@ -556,6 +619,10 @@ WHERE  v.option_group_id = g.id
    * which is part of the cache key
    * will do a couple of variations & aspire to someone cleaning it up later
    */
+  /**
+   * @param $name
+   * @param array $params
+   */
   static function flush($name, $params = array()){
     $defaults = array(
       'flip' => FALSE,
@@ -591,6 +658,7 @@ WHERE  v.option_group_id = g.id
   static function flushAll() {
     self::$_values = array();
     self::$_cache = array();
+    CRM_Utils_Cache::singleton()->flush();
   }
 }