Skip to content
Snippets Groups Projects
Commit 9fe6051a authored by eileen's avatar eileen
Browse files

fixes to value and weight handling CRM-12133

parent e99c9805
Branches
Tags
No related merge requests found
......@@ -40,11 +40,9 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
function __construct() {
parent::__construct();
}
/*
/**
* Create option value - note that the create function calls 'add' but
* has more business logic
* Note that this is the right place to add pre & post hooks if we want them
* ? any reason not to?
*
* @param array $params input parameters
*/
......@@ -64,11 +62,13 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
* This functions sets default parameters if not set:
* - name & label are set to each other as required (it might make more sense for one
* to be required but this would mean a change to the api level)
* - ditto weight & value - but they both default to next weight
* NB am not sure that weight should be set to value as higher priority to
* setting it to the next weight - although this is existing logic
* - weight & value will be set to their respective option groups next values
* if nothing is passed in.
*
* Note this function does not check for presence of $params['id'] so should only be called
* if 'id' is not present
*
* @param unknown_type $params
* @param array $params
*/
static function setDefaults(&$params){
if(empty($params['label'])){
......@@ -78,19 +78,32 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
$params['name'] = $params['label'];
}
if(empty($params['weight'])){
//@todo consider this logic - see block comments
$params['weight'] = CRM_Utils_Array::value('value', $params,
(int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
array('option_group_id' => $params['option_group_id']))
);
$params['weight'] = (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
array('option_group_id' => $params['option_group_id']));
}
if(empty($params['value'])){
$params['value'] = CRM_Utils_Array::value('weight', $params,
(int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
array('option_group_id' => $params['option_group_id']))
);
$params['value'] = self::getNextValue($params);
}
}
/**
* Get next available value
* We will take the highest numeric value (or 0 if no numeric values exist)
* and add one. The calling function is responsible for any
* more complex decision making
* @param array $params
*/
static function getNextValue($params){
$bao = new CRM_Core_BAO_OptionValue();
$bao->option_group_id = $params['option_group_id'];
if(isset($params['domain_id'])){
$bao->domain_id = $params['domain_id'];
}
$bao->selectAdd();
$bao->whereAdd("value REGEXP '^[0-9]+$'");
$bao->selectAdd('(ROUND(COALESCE(MAX(value),0)) +1) as nextvalue');
$bao->find(TRUE);
return $bao->nextvalue;
}
/**
* Takes a bunch of params that are needed to match certain criteria and
* retrieves the relevant objects. Typically the valid params are only
......@@ -141,6 +154,10 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
*/
static function add(&$params, &$ids) {
// CRM-10921: do not reset attributes to default if this is an update
//@todo consider if defaults are being set in the right place. 'dumb' defaults like
// these would be usefully set @ the api layer so they are visible to api users
// complex defaults like the domain id below would make sense in the setDefauls function
// but unclear what other ways this function is being used
if (!CRM_Utils_Array::value('optionValue', $ids)) {
$params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
$params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
......@@ -167,7 +184,6 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
$p = array(1 => array($params['option_group_id'], 'Integer'));
CRM_Core_DAO::executeQuery($query, $p);
}
$groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
$params['option_group_id'], 'name', 'id'
);
......
......@@ -54,7 +54,6 @@ function civicrm_api3_custom_search_create($params) {
function _civicrm_api3_custom_search_create_spec(&$params) {
require_once 'api/v3/OptionValue.php';
_civicrm_api3_option_value_create_spec($params);
$params['weight']['api.default'] = 'next';
$params['name']['api.aliases'] = array('class_name');
}
......
......@@ -50,7 +50,6 @@ function civicrm_api3_report_template_create($params) {
function _civicrm_api3_report_template_create_spec(&$params) {
require_once 'api/v3/OptionValue.php';
_civicrm_api3_option_value_create_spec($params);
$params['weight']['api.default'] = 'next';
$params['value']['api.aliases'] = array('report_url');
$params['name']['api.aliases'] = array('class_name');
// $params['component']['api.required'] = TRUE;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment