Merge pull request #4981 from totten/master-cbf2
[civicrm-core.git] / api / v3 / MembershipType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * File for the CiviCRM APIv3 membership type functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Membership
33 *
34 * @copyright CiviCRM LLC (c) 2004-2014
35 * @version $Id: MembershipType.php 30171 2010-10-14 09:11:27Z mover $
36 *
37 */
38
39 /**
40 * API to Create or update a Membership Type
41 *
42 * @param array $params
43 * An associative array of name/value property values of civicrm_membership_type.
44 *
45 * @return array
46 * newly created or updated membership type property values.
47 */
48 function civicrm_api3_membership_type_create($params) {
49 $ids['membershipType'] = CRM_Utils_Array::value('id', $params);
50 $ids['memberOfContact'] = CRM_Utils_Array::value('member_of_contact_id', $params);
51 $ids['contributionType'] = CRM_Utils_Array::value('financial_type_id', $params);
52
53 $membershipTypeBAO = CRM_Member_BAO_MembershipType::add($params, $ids);
54 $membershipType = array();
55 _civicrm_api3_object_to_array($membershipTypeBAO, $membershipType[$membershipTypeBAO->id]);
56 CRM_Member_PseudoConstant::membershipType(NULL, TRUE);
57 civicrm_api3('membership', 'getfields', array('cache_clear' => 1, 'fieldname' => 'membership_type_id'));
58 civicrm_api3('profile', 'getfields', array('action' => 'submit', 'cache_clear' => 1));
59 return civicrm_api3_create_success($membershipType, $params, 'membership_type', 'create', $membershipTypeBAO);
60 }
61
62 /**
63 * Adjust Metadata for Create action
64 *
65 * The metadata is used for setting defaults, documentation & validation
66 * @param array $params
67 * Array or parameters determined by getfields.
68 */
69 function _civicrm_api3_membership_type_create_spec(&$params) {
70 // todo could set default here probably
71 $params['domain_id']['api.required'] = 1;
72 $params['member_of_contact_id']['api.required'] = 1;
73 $params['financial_type_id']['api.required'] = 1;
74 $params['name']['api.required'] = 1;
75 $params['duration_unit']['api.required'] = 1;
76 $params['duration_interval']['api.required'] = 1;
77 }
78
79 /**
80 * Get a Membership Type.
81 *
82 * This api is used for finding an existing membership type.
83 *
84 * @param array $params
85 * An associative array of name/value property values of civicrm_membership_type.
86 * {getfields MembershipType_get}
87 *
88 * @return array
89 * Array of all found membership type property values.
90 */
91 function civicrm_api3_membership_type_get($params) {
92 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
93 }
94
95 /**
96 * Deletes an existing membership type
97 *
98 * This API is used for deleting a membership type
99 * Required parameters : id of a membership type
100 *
101 * @param array $params
102 *
103 * @return bool
104 * true if success, else false
105 */
106 function civicrm_api3_membership_type_delete($params) {
107 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
108 }