| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Retrieve one or more OptionValues |
| 5 | * |
| 6 | * @param array $ params input parameters |
| 7 | * |
| 8 | * {@example OptionValueGet.php 0} |
| 9 | * @example OptionValueGet.php |
| 10 | * |
| 11 | * @return array details of found Option Values |
| 12 | * {@getfields OptionValue_get} |
| 13 | * @access public |
| 14 | */ |
| 15 | function civicrm_api3_option_value_get($params) { |
| 16 | |
| 17 | if (empty($params['option_group_id']) && !empty($params['option_group_name'])) { |
| 18 | $opt = array('version' => 3, 'name' => $params['option_group_name']); |
| 19 | $optionGroup = civicrm_api('OptionGroup', 'Get', $opt); |
| 20 | if (empty($optionGroup['id'])) { |
| 21 | return civicrm_api3_create_error("option group name does not correlate to a single option group"); |
| 22 | } |
| 23 | $params['option_group_id'] = $optionGroup['id']; |
| 24 | } |
| 25 | |
| 26 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Add a OptionValue. OptionValues are used to classify CRM entities (including Contacts, Groups and Actions). |
| 31 | * |
| 32 | * Allowed @params array keys are: |
| 33 | * |
| 34 | * {@example OptionValueCreate.php} |
| 35 | * |
| 36 | * @return array of newly created option_value property values. |
| 37 | * {@getfields OptionValue_create} |
| 38 | * @access public |
| 39 | */ |
| 40 | function civicrm_api3_option_value_create($params) { |
| 41 | |
| 42 | $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 43 | civicrm_api('option_value', 'getfields', array('version' => 3, 'cache_clear' => 1, 'option_group_id' => $params['option_group_id'])); |
| 44 | return $result; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Adjust Metadata for Create action |
| 49 | * |
| 50 | * The metadata is used for setting defaults, documentation & validation |
| 51 | * @param array $params array or parameters determined by getfields |
| 52 | */ |
| 53 | function _civicrm_api3_option_value_create_spec(&$params) { |
| 54 | $params['is_active']['api.default'] = 1; |
| 55 | //continue to support component |
| 56 | $params['component_id']['api.aliases'] = array('component'); |
| 57 | $params['name']['api.aliases'] = array('label'); |
| 58 | $params['option_group_id']['api.required'] = TRUE; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Deletes an existing OptionValue |
| 63 | * |
| 64 | * @param array $params |
| 65 | * |
| 66 | * {@example OptionValueDelete.php 0} |
| 67 | * |
| 68 | * @return array Api result |
| 69 | * {@getfields OptionValue_create} |
| 70 | * @access public |
| 71 | */ |
| 72 | function civicrm_api3_option_value_delete($params) { |
| 73 | // we will get the option group id before deleting so we can flush pseudoconstants |
| 74 | $optionGroupID = civicrm_api('option_value', 'getvalue', array('version' => 3, 'id' => $params['id'], 'return' => 'option_group_id')); |
| 75 | if(CRM_Core_BAO_OptionValue::del((int) $params['id'])){ |
| 76 | civicrm_api('option_value', 'getfields', array('version' => 3, 'cache_clear' => 1, 'option_group_id' => $optionGroupID)); |
| 77 | return civicrm_api3_create_success(); |
| 78 | } |
| 79 | else{ |
| 80 | civicrm_api3_create_error('Could not delete OptionValue ' . $params['id']); |
| 81 | } |
| 82 | } |
| 83 | |