Merge pull request #17656 from civicrm/5.27
[civicrm-core.git] / api / v3 / MembershipType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api exposes CiviCRM membership type.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 /**
19 * API to Create or update a Membership Type.
20 *
21 * @param array $params
22 * Array of name/value property values of civicrm_membership_type.
23 *
24 * @return array
25 * API result array.
26 */
27 function civicrm_api3_membership_type_create($params) {
28 // Workaround for fields using nonstandard serialization
29 foreach (['relationship_type_id', 'relationship_direction'] as $field) {
30 if (isset($params[$field]) && is_array($params[$field])) {
31 $params[$field] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $params[$field]);
32 }
33 }
34 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'MembershipType');
35 }
36
37 /**
38 * Adjust Metadata for Create action.
39 *
40 * The metadata is used for setting defaults, documentation & validation.
41 *
42 * @param array $params
43 * Array of parameters determined by getfields.
44 */
45 function _civicrm_api3_membership_type_create_spec(&$params) {
46 $params['domain_id']['api.default'] = CRM_Core_Config::domainID();
47 $params['member_of_contact_id']['api.required'] = 1;
48 $params['financial_type_id']['api.required'] = 1;
49 $params['name']['api.required'] = 1;
50 $params['duration_unit']['api.required'] = 1;
51 $params['duration_interval']['api.required'] = 1;
52 $params['period_type']['api.required'] = 1;
53 $params['is_active']['api.default'] = 1;
54 }
55
56 /**
57 * Get a Membership Type.
58 *
59 * This api is used for finding an existing membership type.
60 *
61 * @param array $params
62 * Array of name/value property values of civicrm_membership_type.
63 *
64 * @return array
65 * API result array.
66 */
67 function civicrm_api3_membership_type_get($params) {
68 $results = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
69 if (!empty($results['values']) && is_array($results['values'])) {
70 foreach ($results['values'] as &$item) {
71 // Workaround for fields using nonstandard serialization
72 foreach (['relationship_type_id', 'relationship_direction'] as $field) {
73 if (isset($item[$field]) && !is_array($item[$field])) {
74 // @todo - this should be handled by the serialization now...
75 $item[$field] = (array) $item[$field];
76 }
77 }
78 }
79 }
80 return $results;
81 }
82
83 /**
84 * Adjust Metadata for Get action.
85 *
86 * The metadata is used for setting defaults, documentation & validation.
87 *
88 * @param array $params
89 * Array of parameters determined by getfields.
90 */
91 function _civicrm_api3_membership_type_get_spec(&$params) {
92 $params['domain_id']['api.default'] = CRM_Core_Config::domainID();
93 }
94
95 /**
96 * Adjust input for getlist action.
97 *
98 * We want to only return active membership types for getlist. It's a bit
99 * arguable whether this should be applied at the 'get' level but, since it's hard
100 * to unset we'll just do it here.
101 *
102 * The usage of getlist is entity-reference fields & the like
103 * so using only active ones makes sense.
104 *
105 * @param array $request
106 * Array of parameters determined by getfields.
107 */
108 function _civicrm_api3_membership_type_getlist_params(&$request) {
109 if (!isset($request['params']['is_active']) && empty($request['params']['id'])) {
110 $request['params']['is_active'] = 1;
111 }
112 }
113
114 /**
115 * Deletes an existing membership type.
116 *
117 * @param array $params
118 *
119 * @return array
120 * API result array.
121 */
122 function civicrm_api3_membership_type_delete($params) {
123 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
124 }