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
*/
* 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'])){
$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
*/
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);
$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'
);