8add1ae2ac8de0a32aceaaa33b0f8a2849ae79e0
[civicrm-core.git] / api / v3 / MembershipStatus.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 * This api exposes CiviCRM membership status.
30 *
31 * @package CiviCRM_APIv3
32 */
33
34 /**
35 * Create a Membership Status.
36 *
37 * @param array $params
38 * Array of name/value property values of civicrm_membership_status.
39 *
40 * @return array
41 */
42 function civicrm_api3_membership_status_create($params) {
43 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
44 }
45
46 /**
47 * Get a membership status.
48 *
49 * This api is used for finding an existing membership status.
50 *
51 * @param array $params
52 * An associative array of name/value property values of civicrm_membership_status.
53 *
54 * @return array
55 * Array of all found membership status property values.
56 */
57 function civicrm_api3_membership_status_get($params) {
58 return _civicrm_api3_basic_get('CRM_Member_BAO_MembershipStatus', $params);
59 }
60
61 /**
62 * Update an existing membership status.
63 *
64 * This api is used for updating an existing membership status.
65 * Required parameters: id of a membership status
66 *
67 * @param array $params
68 * Array of name/value property values of civicrm_membership_status.
69 *
70 * @deprecated - should just use create
71 *
72 * @return array
73 * Array of updated membership status property values
74 */
75 function civicrm_api3_membership_status_update($params) {
76
77 civicrm_api3_verify_mandatory($params, NULL, array('id'));
78 //don't allow duplicate names.
79 $name = CRM_Utils_Array::value('name', $params);
80 if ($name) {
81 $status = new CRM_Member_DAO_MembershipStatus();
82 $status->name = $params['name'];
83 if ($status->find(TRUE) && $status->id != $params['id']) {
84 return civicrm_api3_create_error(ts('A membership status with this name already exists.'));
85 }
86 }
87
88 $membershipStatusBAO = new CRM_Member_BAO_MembershipStatus();
89 $membershipStatusBAO->id = $params['id'];
90 if ($membershipStatusBAO->find(TRUE)) {
91 $fields = $membershipStatusBAO->fields();
92 foreach ($fields as $name => $field) {
93 if (array_key_exists($name, $params)) {
94 $membershipStatusBAO->$name = $params[$name];
95 }
96 }
97 $membershipStatusBAO->save();
98 }
99 $membershipStatus = array();
100 _civicrm_api3_object_to_array(clone($membershipStatusBAO), $membershipStatus);
101 $membershipStatus['is_error'] = 0;
102 return $membershipStatus;
103 }
104
105 /**
106 * Deletes an existing membership status.
107 *
108 * This API is used for deleting a membership status
109 *
110 * @param array $params
111 *
112 * @return array
113 */
114 function civicrm_api3_membership_status_delete($params) {
115
116 $memberStatusDelete = CRM_Member_BAO_MembershipStatus::del($params['id'], TRUE);
117 return $memberStatusDelete ? civicrm_api3_create_error($memberStatusDelete['error_message']) : civicrm_api3_create_success();
118 }
119
120 /**
121 * Derives the Membership Status of a given Membership Record.
122 *
123 * This API is used for deriving Membership Status of a given Membership
124 * record using the rules encoded in the membership_status table.
125 *
126 * @param array $membershipParams
127 *
128 * @throws API_Exception
129 *
130 * @return array
131 * Array of status id and status name
132 */
133 function civicrm_api3_membership_status_calc($membershipParams) {
134 if (!($membershipID = CRM_Utils_Array::value('membership_id', $membershipParams))) {
135 throw new API_Exception('membershipParams do not contain membership_id');
136 }
137
138 if (empty($membershipParams['id'])) {
139 //for consistency lets make sure id is set as this will get passed to hooks downstream
140 $membershipParams['id'] = $membershipID;
141 }
142 $query = "
143 SELECT start_date, end_date, join_date, membership_type_id
144 FROM civicrm_membership
145 WHERE id = %1
146 ";
147
148 $params = array(1 => array($membershipID, 'Integer'));
149 $dao = CRM_Core_DAO::executeQuery($query, $params);
150 if ($dao->fetch()) {
151 $membershipTypeID = empty($membershipParams['membership_type_id']) ? $dao->membership_type_id : $membershipParams['membership_type_id'];
152 $result = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dao->start_date, $dao->end_date, $dao->join_date, 'today', CRM_Utils_Array::value('ignore_admin_only', $membershipParams), $membershipTypeID, $membershipParams);
153 //make is error zero only when valid status found.
154 if (!empty($result['id'])) {
155 $result['is_error'] = 0;
156 }
157 }
158 else {
159 $dao->free();
160 throw new API_Exception('did not find a membership record');
161 }
162 $dao->free();
163 return $result;
164 }