<?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. |
| |
/**
*
* @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
* $Id$
*
*/
'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'
* @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
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
$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);
* @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;
}
* @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
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);
}
}
+ /**
+ * @param $groupName
+ * @param $value
+ * @param bool $onlyActiveValue
+ *
+ * @return null
+ */
static function getLabel($groupName, $value, $onlyActiveValue = TRUE) {
if (empty($groupName) ||
empty($value)
return NULL;
}
+ /**
+ * @param $groupName
+ * @param $label
+ * @param string $labelField
+ * @param string $labelType
+ * @param string $valueField
+ *
+ * @return null
+ */
static function getValue($groupName,
$label,
$labelField = 'label',
return NULL;
}
+ /**
+ * Get option_value.value from default option_value row for an option group
+ *
+ * @param string $groupName the name of the option group
+ *
+ * @access public
+ * @static
+ *
+ * @return string the value from the row where is_default = true
+ */
+ static function getDefaultValue($groupName) {
+ if (empty($groupName)) {
+ return NULL;
+ }
+ $query = "
+SELECT v.value
+FROM civicrm_option_value v,
+ civicrm_option_group g
+WHERE v.option_group_id = g.id
+ AND g.name = %1
+ AND v.is_active = 1
+ AND g.is_active = 1
+ AND v.is_default = 1
+";
+ if (in_array($groupName, self::$_domainIDGroups)) {
+ $query .= " AND v.domain_id = " . CRM_Core_Config::domainID();
+ }
+
+ $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)
* 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);
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
}
}
+ /**
+ * @param $groupName
+ * @param string $operator
+ */
static function deleteAssoc($groupName, $operator = "=") {
$query = "
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
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
) {
* 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,
static function flushAll() {
self::$_values = array();
self::$_cache = array();
+ CRM_Utils_Cache::singleton()->flush();
}
}