Merge pull request #18852 from eileenmcnaughton/aip
[civicrm-core.git] / api / v3 / MembershipType.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/**
c28e1768 13 * This api exposes CiviCRM membership type.
6a488035
TO
14 *
15 * @package CiviCRM_APIv3
6a488035
TO
16 */
17
6a488035 18/**
1747ab99 19 * API to Create or update a Membership Type.
6a488035 20 *
cf470720 21 * @param array $params
1747ab99 22 * Array of name/value property values of civicrm_membership_type.
6a488035 23 *
a6c01b45 24 * @return array
1747ab99 25 * API result array.
6a488035
TO
26 */
27function civicrm_api3_membership_type_create($params) {
baccd59e 28 // Workaround for fields using nonstandard serialization
cf8f0fff 29 foreach (['relationship_type_id', 'relationship_direction'] as $field) {
baccd59e
CW
30 if (isset($params[$field]) && is_array($params[$field])) {
31 $params[$field] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $params[$field]);
32 }
33 }
1237d8d7 34 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'MembershipType');
6a488035 35}
11e09c59
TO
36
37/**
0aa0303c
EM
38 * Adjust Metadata for Create action.
39 *
40 * The metadata is used for setting defaults, documentation & validation.
1c88e578 41 *
cf470720 42 * @param array $params
b081365f 43 * Array of parameters determined by getfields.
6a488035
TO
44 */
45function _civicrm_api3_membership_type_create_spec(&$params) {
fb38ab8d 46 $params['domain_id']['api.default'] = CRM_Core_Config::domainID();
6a488035 47 $params['member_of_contact_id']['api.required'] = 1;
35671d00 48 $params['financial_type_id']['api.required'] = 1;
6a488035
TO
49 $params['name']['api.required'] = 1;
50 $params['duration_unit']['api.required'] = 1;
51 $params['duration_interval']['api.required'] = 1;
5b1b8db2 52 $params['period_type']['api.required'] = 1;
08fd4b45 53 $params['is_active']['api.default'] = 1;
6a488035
TO
54}
55
56/**
57 * Get a Membership Type.
58 *
59 * This api is used for finding an existing membership type.
60 *
cf470720 61 * @param array $params
1747ab99 62 * Array of name/value property values of civicrm_membership_type.
6a488035 63 *
16b10e64 64 * @return array
1747ab99 65 * API result array.
6a488035
TO
66 */
67function civicrm_api3_membership_type_get($params) {
baccd59e 68 $results = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
9559d77f 69 if (!empty($results['values']) && is_array($results['values'])) {
baccd59e
CW
70 foreach ($results['values'] as &$item) {
71 // Workaround for fields using nonstandard serialization
cf8f0fff 72 foreach (['relationship_type_id', 'relationship_direction'] as $field) {
baccd59e 73 if (isset($item[$field]) && !is_array($item[$field])) {
eb151aab 74 // @todo - this should be handled by the serialization now...
baccd59e
CW
75 $item[$field] = (array) $item[$field];
76 }
77 }
78 }
79 }
80 return $results;
6a488035
TO
81}
82
5d449aac
MWMC
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 */
91function _civicrm_api3_membership_type_get_spec(&$params) {
92 $params['domain_id']['api.default'] = CRM_Core_Config::domainID();
93}
94
1b193115 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 */
108function _civicrm_api3_membership_type_getlist_params(&$request) {
38a14841 109 if (!isset($request['params']['is_active']) && empty($request['params']['id'])) {
1b193115 110 $request['params']['is_active'] = 1;
111 }
112}
113
6a488035 114/**
1747ab99 115 * Deletes an existing membership type.
6a488035 116 *
cf470720 117 * @param array $params
6a488035 118 *
1747ab99
EM
119 * @return array
120 * API result array.
6a488035
TO
121 */
122function civicrm_api3_membership_type_delete($params) {
8c33a68c 123 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
6a488035 124}