function __construct() {
parent::__construct();
}
+ /**
+ * Create option value - note that the create function calls 'add' but
+ * has more business logic
+ *
+ * @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)
+ * - 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 array $params
+ */
+ static function setDefaults(&$params){
+ if(CRM_Utils_Array::value('label', $params, NULL) === NULL){
+ $params['label'] = $params['name'];
+ }
+ if(CRM_Utils_Array::value('name', $params, NULL) === NULL){
+ $params['name'] = $params['label'];
+ }
+ if(CRM_Utils_Array::value('weight', $params, NULL) === NULL){
+ $params['weight'] = self::getDefaultWeight($params);
+ }
+ if (CRM_Utils_Array::value('value', $params, NULL) === NULL){
+ $params['value'] = self::getDefaultValue($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 getDefaultWeight($params){
+ return (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
+ array('option_group_id' => $params['option_group_id']));
+ }
+ /**
+ * 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 getDefaultValue($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'
);
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['option_group_id']['api.default'] = CRM_Core_DAO::getFieldValue(
+ 'CRM_Core_DAO_OptionGroup', 'custom_search', 'id', 'name'
+ );
$params['name']['api.aliases'] = array('class_name');
}
*/
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;
}
/**
$params['option_group_id'] = CRM_Core_DAO::getFieldValue(
'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
);
+ if (!empty($params['component_id']) && !is_numeric($params['component_id'])) {
+ $params['component_id'] = array_search($params['component_id'], CRM_Core_PseudoConstant::component());
+ }
return civicrm_api3_option_value_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['option_group_id']['api.default'] = CRM_Core_DAO::getFieldValue(
+ 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
+ );
// $params['component']['api.required'] = TRUE;
}
'description' => 'Longish description of the example search form',
'class_name' => 'CRM_Contact_Form_Search_Custom_Examplez',
));
- $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+ $this->assertAPISuccess($result);
$this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
$entityId = $result['id'];
$this->assertTrue(is_numeric($entityId), 'In line ' . __LINE__);
'id' => $entityId,
'is_active' => 0,
));
- $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+ $this->assertAPISuccess($result);
$this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
$this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
WHERE name = "CRM_Contact_Form_Search_Custom_Examplez"
'id' => $entityId,
'is_active' => 1,
));
- $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+ $this->assertAPISuccess($result);
$this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
$this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
WHERE name = "CRM_Contact_Form_Search_Custom_Examplez"
'version' => $this->_apiversion,
'id' => $entityId,
));
- $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+ $this->assertAPISuccess($result);
$this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
$this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value
WHERE name = "CRM_Contact_Form_Search_Custom_Examplez"
'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']));
}
-
}
'report_url' => 'example/path',
'component' => 'CiviCase',
));
- $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+ $this->assertAPISuccess($result);
$this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
$entityId = $result['id'];
$this->assertTrue(is_numeric($entityId), 'In line ' . __LINE__);
'id' => $entityId,
'component' => '',
));
- $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+ $this->assertAPISuccess($result);
$this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
$this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
WHERE name = "CRM_Report_Form_Examplez"
'id' => $entityId,
'is_active' => 0,
));
- $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+ $this->assertAPISuccess($result);
$this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
$this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
WHERE name = "CRM_Report_Form_Examplez"
'id' => $entityId,
'is_active' => 1,
));
- $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+ $this->assertAPISuccess($result);
$this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
$this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
WHERE name = "CRM_Report_Form_Examplez"
'version' => $this->_apiversion,
'id' => $entityId,
));
- $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+ $this->assertAPISuccess($result);
$this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
$this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value
WHERE name = "CRM_Report_Form_Examplez"
<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>