Merge pull request #17656 from civicrm/5.27
[civicrm-core.git] / api / v3 / PriceSet.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
a30c801b 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
a30c801b
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12
13/**
244bbdd8
CW
14 * This api exposes CiviCRM Price Sets.
15 *
16 * PriceSets contain PriceFields (which have their own api).
17 * Use chaining to create a PriceSet and associated PriceFields in one api call.
6a488035
TO
18 *
19 * @package CiviCRM_APIv3
6a488035
TO
20 */
21
22/**
244bbdd8 23 * Create or update a PriceSet.
6a488035 24 *
cf470720 25 * @param array $params
244bbdd8 26 * name/value pairs to insert in new 'PriceSet'
6a488035 27 *
a6c01b45 28 * @return array
72b3a70c 29 * api result array
6a488035
TO
30 */
31function civicrm_api3_price_set_create($params) {
98fd592b 32 $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'PriceSet');
6a488035
TO
33 // Handle price_set_entity
34 if (!empty($result['id']) && !empty($params['entity_table']) && !empty($params['entity_id'])) {
35 $entityId = $params['entity_id'];
36 if (!is_array($params['entity_id'])) {
37 $entityId = explode(',', $entityId);
38 }
39 foreach ($entityId as $eid) {
40 $eid = (int) trim($eid);
41 if ($eid) {
9da8dc8c 42 CRM_Price_BAO_PriceSet::addTo($params['entity_table'], $eid, $result['id']);
6a488035
TO
43 }
44 }
45 }
46 return $result;
47}
48
11e09c59 49/**
0aa0303c
EM
50 * Adjust Metadata for Create action.
51 *
52 * The metadata is used for setting defaults, documentation & validation.
6a488035 53 *
cf470720 54 * @param array $params
b081365f 55 * Array of parameters determined by getfields.
6a488035
TO
56 */
57function _civicrm_api3_price_set_create_spec(&$params) {
e0c1764e 58 $params['title']['api.required'] = TRUE;
6a488035
TO
59}
60
61/**
22242c87 62 * Returns array of price_sets matching a set of one or more group properties.
6a488035 63 *
cf470720
TO
64 * @param array $params
65 * Array of one or more valid property_name=>value pairs. If $params is set.
16b10e64 66 * as null, all price_sets will be returned (default limit is 25)
6a488035 67 *
a6c01b45 68 * @return array
72b3a70c 69 * Array of matching price_sets
6a488035
TO
70 */
71function civicrm_api3_price_set_get($params) {
418e884f 72 // hack to make getcount work. - not sure the best approach here
73 // as creating an alternate getcount function also feels a bit hacky
22e263ad 74 if (isset($params['options']) && isset($params['options']['is_count'])) {
418e884f 75 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
76 }
6a488035 77 $result = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE);
8268052f 78 // Fetch associated entities if the return has not been previously limited.
79 if (!isset($params['return'])) {
80 foreach ($result as &$item) {
81 $item['entity'] = CRM_Price_BAO_PriceSet::getUsedBy($item['id'], 'entity');
82 }
6a488035
TO
83 }
84 return civicrm_api3_create_success($result, $params);
85}
86
87/**
244bbdd8 88 * Delete an existing PriceSet.
6a488035 89 *
244bbdd8 90 * This method is used to delete any existing PriceSet given its id.
6a488035 91 *
cf470720 92 * @param array $params
b081365f 93 * Array containing id of the group to be deleted.
6a488035 94 *
a6c01b45 95 * @return array
72b3a70c 96 * API result array
6a488035
TO
97 */
98function civicrm_api3_price_set_delete($params) {
99 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
100}