Merge pull request #3602 from yashodha/CRM-14629
[civicrm-core.git] / CRM / Core / OptionGroup.php
index 08bf0844dbdb2f5025a9911d0a80a924b5e6b203..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'
@@ -92,6 +101,7 @@ class CRM_Core_OptionGroup {
    * @param $labelColumnName string the column to use for 'label'
    * @param $onlyActive boolean return only the action option values
    * @param $fresh      boolean ignore cache entries and go back to DB
+   * @param $keyColumnName string the column to use for 'key'
    *
    * @return array      the values as specified by the above params
    * @static
@@ -100,22 +110,25 @@ class CRM_Core_OptionGroup {
   static function &values(
     $name, $flip = FALSE, $grouping = FALSE,
     $localize = FALSE, $condition = NULL,
-    $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE
+    $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE, $keyColumnName = 'value'
   ) {
-    $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive);
-
-    if (array_key_exists($cacheKey, self::$_cache) && !$fresh) {
-      return self::$_cache[$cacheKey];
-    }
-
     $cache = CRM_Utils_Cache::singleton();
-    $var = $cache->get($cacheKey);
-    if ($var && !$fresh) {
-      return $var;
+    $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName);
+
+    if (!$fresh) {
+      // Fetch from static var
+      if (array_key_exists($cacheKey, self::$_cache)) {
+        return self::$_cache[$cacheKey];
+      }
+      // Fetch from main cache
+      $var = $cache->get($cacheKey);
+      if ($var) {
+        return $var;
+      }
     }
 
     $query = "
-SELECT  v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as grouping
+SELECT  v.{$labelColumnName} as {$labelColumnName} ,v.{$keyColumnName} as value, v.grouping as grouping
 FROM   civicrm_option_value v,
        civicrm_option_group g
 WHERE  v.option_group_id = g.id
@@ -133,7 +146,7 @@ WHERE  v.option_group_id = g.id
       $query .= $condition;
     }
 
-    $query .= "  ORDER BY v.weight";
+    $query .= " ORDER BY v.weight";
 
     $p = array(1 => array($name, 'String'));
     $dao = CRM_Core_DAO::executeQuery($query, $p);
@@ -159,16 +172,20 @@ 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) {
-    $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive);
+  protected static function flushValues($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName = 'value') {
+    $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName);
     $cache = CRM_Utils_Cache::singleton();
     $cache->delete($cacheKey);
     unset(self::$_cache[$cacheKey]);
   }
 
-  protected static function createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive) {
-    $cacheKey = "CRM_OG_{$name}_{$flip}_{$grouping}_{$localize}_{$condition}_{$labelColumnName}_{$onlyActive}";
+  /**
+   * @return string
+   */
+  protected static function createCacheKey() {
+    $cacheKey = "CRM_OG_" . serialize(func_get_args());
     return $cacheKey;
   }
 
@@ -185,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
@@ -203,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);
 
@@ -292,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)
@@ -321,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',
@@ -363,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;
@@ -385,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)
@@ -401,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);
@@ -444,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
@@ -484,6 +532,10 @@ ORDER BY v.weight
     }
   }
 
+  /**
+   * @param $groupName
+   * @param string $operator
+   */
   static function deleteAssoc($groupName, $operator = "=") {
     $query = "
 DELETE g, v
@@ -497,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
@@ -511,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
   ) {
@@ -552,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,
@@ -587,6 +658,7 @@ WHERE  v.option_group_id = g.id
   static function flushAll() {
     self::$_values = array();
     self::$_cache = array();
+    CRM_Utils_Cache::singleton()->flush();
   }
 }