/** * Add an Option Value. * * @param array $params * Reference array contains the values submitted by the form. * @param array $ids * Reference array contains the id. * * * @return CRM_Core_DAO_OptionValue */ public 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 (empty($ids['optionValue'])) { $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); $params['is_optgroup'] = CRM_Utils_Array::value('is_optgroup', $params, FALSE); $params['filter'] = CRM_Utils_Array::value('filter', $params, FALSE); } elseif (isset($params['value'])) { CRM_Core_BAO_CustomOption::updateValue($ids['optionValue'], $params['value']); } // action is taken depending upon the mode $optionValue = new CRM_Core_DAO_OptionValue(); $optionValue->copyValues($params); if (!empty($params['is_default'])) { $query = 'UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = %1'; // tweak default reset, and allow multiple default within group. if ($resetDefaultFor = CRM_Utils_Array::value('reset_default_for', $params)) { if (is_array($resetDefaultFor)) { $colName = key($resetDefaultFor); $colVal = $resetDefaultFor[$colName]; $query .= " AND ( {$colName} IN ( {$colVal} ) )"; } } $p = array(1 => array($params['option_group_id'], 'Integer')); CRM_Core_DAO::executeQuery($query, $p); } // CRM-13814 : evalute option group id if (!array_key_exists('option_group_id', $params) && !empty($ids['optionValue'])) { $groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $ids['optionValue'], 'option_group_id', 'id'); } else { $groupId = $params['option_group_id']; } $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $groupId, 'name', 'id'); if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) { $optionValue->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID()); } $optionValue->id = CRM_Utils_Array::value('optionValue', $ids); $optionValue->save(); CRM_Core_PseudoConstant::flush(); return $optionValue; }