CRM-12133 fix default handling to be in BAO layer, also add tests on component_id & add to xml
function __construct() {
parent::__construct();
}
-
+ /*
+ * Create email address - 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
+ */
+ static function create($params) {
+ if (empty($params['id'])){
+ self::setDefaults($params);
+ }
+ $ids = array();
+ if (CRM_Utils_Array::value('id', $params)) {
+ $ids = array('optionValue' => $params['id']);
+ }
+ return CRM_Core_BAO_OptionValue::add($params, $ids);
+ ;
+ }
+ /**
+ * Set default 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
+ *
+ * @param unknown_type $params
+ */
+ static function setDefaults(&$params){
+ if(empty($params['label'])){
+ $params['label'] = $params['name'];
+ }
+ if(empty($params['name'])){
+ $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']))
+ );
+ }
+ 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']))
+ );
+ }
+ }
/**
* Takes a bunch of params that are needed to match certain criteria and
* retrieves the relevant objects. Typically the valid params are only
*/
function civicrm_api3_option_value_create($params) {
- // CRM-10921: do not fill-in defaults if this is an update
- if (!CRM_Utils_Array::value('id', $params)) {
- if (!CRM_Utils_Array::value('label', $params) && CRM_Utils_Array::value('name', $params)) {
- // 'label' defaults to 'name'
- $params['label'] = $params['name'];
- }
- if (!CRM_Utils_Array::value('value', $params) && CRM_Utils_Array::value('option_group_id', $params)) {
- // 'value' defaults to next weight in option_group
- $params['value'] = (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
- array('option_group_id' => $params['option_group_id'])
- );
- }
- if (!CRM_Utils_Array::value('weight', $params) && CRM_Utils_Array::value('value', $params)) {
- // 'weight' defaults to 'value'
- $params['weight'] = $params['value'];
- } elseif (CRM_Utils_Array::value('weight', $params) && $params['weight'] == 'next' && CRM_Utils_Array::value('option_group_id', $params)) {
- // weight is numeric, so it's safe-ish to treat symbol 'next' as magical value
- $params['weight'] = (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
- array('option_group_id' => $params['option_group_id'])
- );
- }
- }
-
- if (isset($params['component'])) {// allow a component to be reset to ''
- // convert 'component' to 'component_id'
- if (empty($params['component'])) {
- $params['component_id'] = '';
- } else {
- $params['component_id'] = array_search($params['component'], CRM_Core_PseudoConstant::component());
- }
- unset($params['component']);
- }
-
- if (CRM_Utils_Array::value('id', $params)) {
- $ids = array('optionValue' => $params['id']);
- }
- $optionValueBAO = CRM_Core_BAO_OptionValue::add($params, $ids);
+ $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
civicrm_api('option_value', 'getfields', array('version' => 3, 'cache_clear' => 1, 'option_group_id' => $params['option_group_id']));
- $values = array();
- _civicrm_api3_object_to_array($optionValueBAO, $values[$optionValueBAO->id]);
- return civicrm_api3_create_success($values, $params);
+ return $result;
}
/**
*/
function _civicrm_api3_option_value_create_spec(&$params) {
$params['is_active']['api.default'] = 1;
- $params['component']['type'] = CRM_Utils_Type::T_STRING;
- $params['component']['options'] = array_values(CRM_Core_PseudoConstant::component());
+ //continue to support component
+ $params['component_id']['api.aliases'] = array('component');
$params['name']['api.aliases'] = array('label');
- // $params['component_id']['pseudoconstant'] = 'component';
+ $params['option_group_id']['api.required'] = TRUE;
}
/**
'name' => 'from_email_address',
'sequential' => 1,
'version' => $this->_apiversion,
- 'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com')
+ 'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com'),
));
$this->assertAPISuccess($result);
$optionValueId = $result['values'][0]['api.option_value.create']['id'];
'return' => 'domain_id',
));
$this->assertEquals(2, $domain_id);
+ }
+ /*
+ * Check that component_id is honoured
+ */
+ public function testCreateOptionSpecifyComponentID() {
+ $result = civicrm_api('option_group', 'get', array(
+ 'name' => 'from_email_address',
+ 'sequential' => 1,
+ 'version' => $this->_apiversion,
+ 'api.option_value.create' => array('component_id' => 2, 'name' => 'my@y.com'),
+ ));
+ $this->assertAPISuccess($result);
+ $optionValueId = $result['values'][0]['api.option_value.create']['id'];
+ $component_id = civicrm_api('option_value', 'getvalue', array(
+ 'id' => $optionValueId,
+ 'version' => $this->_apiversion,
+ 'return' => 'component_id',
+ ));
+ $this->assertEquals(2, $component_id);
+ }
+ /*
+ * Check that component continues to be honoured
+ */
+ public function testCreateOptionSpecifyComponent() {
+ $result = civicrm_api('option_group', 'get', array(
+ 'name' => 'from_email_address',
+ 'sequential' => 1,
+ 'version' => $this->_apiversion,
+ 'api.option_value.create' => array(
+ 'component_id' => 'CiviContribute',
+ 'name' => 'my@y.com'
+ ),
+ ));
+ $this->assertAPISuccess($result);
+ $optionValueId = $result['values'][0]['api.option_value.create']['id'];
+ $component_id = civicrm_api('option_value', 'getvalue', array(
+ 'id' => $optionValueId,
+ 'version' => $this->_apiversion,
+ 'return' => 'component_id',
+ ));
+ $this->assertEquals(2, $component_id);
+ }
+ /*
+ * Check that component string is honoured
+ */
+ public function testCreateOptionSpecifyComponentString() {
+ $result = civicrm_api('option_group', 'get', array(
+ 'name' => 'from_email_address',
+ 'sequential' => 1,
+ 'version' => $this->_apiversion,
+ 'api.option_value.create' => array(
+ 'component_id' => 'CiviContribute',
+ 'name' => 'my@y.com'),
+
+ ));
+ $this->assertAPISuccess($result);
+ $optionValueId = $result['values'][0]['api.option_value.create']['id'];
+ $component_id = civicrm_api('option_value', 'getvalue', array(
+ 'id' => $optionValueId,
+ 'version' => $this->_apiversion,
+ 'return' => 'component_id',
+ ));
+ $this->assertEquals(2, $component_id);
+ }
+ /*
+ * Check that domain_id is honoured
+ */
+ public function testCRM12133CreateOptionWeightNoValue() {
+ $optionGroup = civicrm_api(
+ 'option_group', 'get', array(
+ 'name' => 'gender',
+ 'sequential' => 1,
+ 'version' => $this->_apiversion,
+ ));
+ $this->assertAPISuccess($optionGroup);
+ $params = array(
+ 'option_group_id' => $optionGroup['id'],
+ 'label' => 'my@y.com',
+ 'version' => $this->_apiversion,
+ 'weight' => 3,
+ );
+ $optionValue = civicrm_api('option_value', 'create', $params);
+ $this->assertAPISuccess($optionValue);
+ $params['weight'] = 4;
+ $optionValue2 = civicrm_api('option_value', 'create', $params );
+ $this->assertAPISuccess($optionValue2);
+ $options = civicrm_api('option_value', 'get', array('version' => 3, 'option_group_id' => $optionGroup['id']));
+ $this->assertNotEquals($options['values'][$optionValue['id']]['value'], $options['values'][$optionValue2['id']]['value']);
+
+ //cleanup
+ civicrm_api('option_value', 'delete', array('version' => 3, 'id' => $optionValue['id']));
+ civicrm_api('option_value', 'delete', array('version' => 3, 'id' => $optionValue2['id']));
}
/*
);
$this->assertFalse(in_array('newest', $fields['values']));
}
-
}
<name>component_id</name>
<type>int unsigned</type>
<comment>Component that this option value belongs/caters to.</comment>
- <add>2.0</add>
+ <add>2.0</add>
+ <pseudoconstant>
+ <name>component</name>
+ <table>civicrm_component</table>
+ <keyColumn>id</keyColumn>
+ <labelColumn>name</labelColumn>
+ </pseudoconstant>
</field>
<foreignKey>
<name>component_id</name>