Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-05-21-13-15-18
[civicrm-core.git] / api / v2 / MembershipStatus.php
1 <?php
2 // $Id: MembershipStatus.php 45502 2013-02-08 13:32:55Z kurund $
3
4
5 /*
6 +--------------------------------------------------------------------+
7 | CiviCRM version 4.3 |
8 +--------------------------------------------------------------------+
9 | Copyright CiviCRM LLC (c) 2004-2013 |
10 +--------------------------------------------------------------------+
11 | This file is a part of CiviCRM. |
12 | |
13 | CiviCRM is free software; you can copy, modify, and distribute it |
14 | under the terms of the GNU Affero General Public License |
15 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | |
17 | CiviCRM is distributed in the hope that it will be useful, but |
18 | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
20 | See the GNU Affero General Public License for more details. |
21 | |
22 | You should have received a copy of the GNU Affero General Public |
23 | License and the CiviCRM Licensing Exception along |
24 | with this program; if not, contact CiviCRM LLC |
25 | at info[AT]civicrm[DOT]org. If you have questions about the |
26 | GNU Affero General Public License or the licensing of CiviCRM, |
27 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
28 +--------------------------------------------------------------------+
29 */
30
31 /**
32 * File for the CiviCRM APIv2 membership status functions
33 *
34 * @package CiviCRM_APIv2
35 * @subpackage API_Membership
36 *
37 * @copyright CiviCRM LLC (c) 2004-2013
38 * @version $Id: MembershipStatus.php 45502 2013-02-08 13:32:55Z kurund $
39 *
40 */
41
42 /**
43 * Files required for this package
44 */
45 require_once 'api/v2/utils.php';
46
47 /**
48 * Create a Membership Status
49 *
50 * This API is used for creating a Membership Status
51 *
52 * @param array $params an associative array of name/value property values of civicrm_membership_status
53 *
54 * @return array of newly created membership status property values.
55 * @access public
56 */
57 function civicrm_membership_status_create(&$params) {
58 _civicrm_initialize();
59 if (!is_array($params)) {
60 return civicrm_create_error('Params is not an array.');
61 }
62
63 if (empty($params)) {
64 return civicrm_create_error('Params can not be empty.');
65 }
66
67 $name = CRM_Utils_Array::value('name', $params);
68 if (!$name) {
69 $name = CRM_Utils_Array::value('label', $params);
70 }
71 if (!$name) {
72 return civicrm_create_error('Missing required fields');
73 }
74
75 //don't allow duplicate names.
76 require_once 'CRM/Member/DAO/MembershipStatus.php';
77 $status = new CRM_Member_DAO_MembershipStatus();
78 $status->name = $name;
79 if ($status->find(TRUE)) {
80 return civicrm_create_error(ts('A membership status with this name already exists.'));
81 }
82
83 require_once 'CRM/Member/BAO/MembershipStatus.php';
84 $ids = array();
85 $membershipStatusBAO = CRM_Member_BAO_MembershipStatus::add($params, $ids);
86 if (is_a($membershipStatusBAO, 'CRM_Core_Error')) {
87 return civicrm_create_error("Membership is not created");
88 }
89 else {
90 $values = array();
91 $values['id'] = $membershipStatusBAO->id;
92 $values['is_error'] = 0;
93 return $values;
94 }
95 }
96
97 /**
98 * Get a membership status.
99 *
100 * This api is used for finding an existing membership status.
101 *
102 * @param array $params an associative array of name/value property values of civicrm_membership_status
103 *
104 * @return Array of all found membership status property values.
105 * @access public
106 */
107 function civicrm_membership_status_get(&$params) {
108 _civicrm_initialize();
109 if (!is_array($params)) {
110 return civicrm_create_error('Params is not an array.');
111 }
112
113 require_once 'CRM/Member/BAO/MembershipStatus.php';
114 $membershipStatusBAO = new CRM_Member_BAO_MembershipStatus();
115
116 $properties = array_keys($membershipStatusBAO->fields());
117
118 foreach ($properties as $name) {
119 if (array_key_exists($name, $params)) {
120 $membershipStatusBAO->$name = $params[$name];
121 }
122 }
123
124 if ($membershipStatusBAO->find()) {
125 $membershipStatus = array();
126 while ($membershipStatusBAO->fetch()) {
127 _civicrm_object_to_array(clone($membershipStatusBAO), $membershipStatus);
128 $membershipStatuses[$membershipStatusBAO->id] = $membershipStatus;
129 }
130 }
131 else {
132 return civicrm_create_error('Exact match not found');
133 }
134 return $membershipStatuses;
135 }
136
137 /**
138 * Update an existing membership status
139 *
140 * This api is used for updating an existing membership status.
141 * Required parrmeters : id of a membership status
142 *
143 * @param Array $params an associative array of name/value property values of civicrm_membership_status
144 *
145 * @return array of updated membership status property values
146 * @access public
147 */
148 function &civicrm_membership_status_update(&$params) {
149 _civicrm_initialize();
150 if (!is_array($params)) {
151 return civicrm_create_error('Params is not an array');
152 }
153
154 if (!isset($params['id'])) {
155 return civicrm_create_error('Required parameter missing');
156 }
157
158 //don't allow duplicate names.
159 $name = CRM_Utils_Array::value('name', $params);
160 if ($name) {
161 require_once 'CRM/Member/DAO/MembershipStatus.php';
162 $status = new CRM_Member_DAO_MembershipStatus();
163 $status->name = $params['name'];
164 if ($status->find(TRUE) && $status->id != $params['id']) {
165 return civicrm_create_error(ts('A membership status with this name already exists.'));
166 }
167 }
168
169 require_once 'CRM/Member/BAO/MembershipStatus.php';
170 $membershipStatusBAO = new CRM_Member_BAO_MembershipStatus();
171 $membershipStatusBAO->id = $params['id'];
172 if ($membershipStatusBAO->find(TRUE)) {
173 $fields = $membershipStatusBAO->fields();
174 foreach ($fields as $name => $field) {
175 if (array_key_exists($name, $params)) {
176 $membershipStatusBAO->$name = $params[$name];
177 }
178 }
179 $membershipStatusBAO->save();
180 }
181 $membershipStatus = array();
182 _civicrm_object_to_array(clone($membershipStatusBAO), $membershipStatus);
183 $membershipStatus['is_error'] = 0;
184 return $membershipStatus;
185 }
186
187 /**
188 * Deletes an existing membership status
189 *
190 * This API is used for deleting a membership status
191 *
192 * @param Int $membershipStatusID Id of the membership status to be deleted
193 *
194 * @return null if successfull, object of CRM_Core_Error otherwise
195 * @access public
196 */
197 function civicrm_membership_status_delete(&$params) {
198 if (!is_array($params)) {
199 return civicrm_create_error('Params is not an array');
200 }
201
202 if (!CRM_Utils_Array::value('id', $params)) {
203 return civicrm_create_error('Invalid or no value for membershipStatusID');
204 }
205
206 require_once 'CRM/Member/BAO/MembershipStatus.php';
207 $memberStatusDelete = CRM_Member_BAO_MembershipStatus::del($params['id']);
208 return $memberStatusDelete ? civicrm_create_error('Error while deleting membership type Status') : civicrm_create_success();
209 }
210
211 /**
212 * Derives the Membership Status of a given Membership Reocrd
213 *
214 * This API is used for deriving Membership Status of a given Membership
215 * record using the rules encoded in the membership_status table.
216 *
217 * @param Int $membershipID Id of a membership
218 * @param String $statusDate
219 *
220 * @return Array Array of status id and status name
221 * @public
222 */
223 function civicrm_membership_status_calc($membershipParams, $excludeIsAdmin = FALSE) {
224 if (!is_array($membershipParams)) {
225 return civicrm_create_error(ts('membershipParams is not an array'));
226 }
227
228 if (!($membershipID = CRM_Utils_Array::value('membership_id', $membershipParams))) {
229 return civicrm_create_error('membershipParams do not contain membership_id');
230 }
231
232 $query = "
233 SELECT start_date, end_date, join_date
234 FROM civicrm_membership
235 WHERE id = %1
236 ";
237 $params = array(1 => array($membershipID, 'Integer'));
238 $dao = CRM_Core_DAO::executeQuery($query, $params);
239 if ($dao->fetch()) {
240 require_once 'CRM/Member/BAO/MembershipStatus.php';
241 // CRM-7248 added $excludeIsAdmin to this function, also 'today' param
242 $result = &CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dao->start_date,
243 $dao->end_date,
244 $dao->join_date,
245 'today',
246 $excludeIsAdmin
247 );
248
249 //make is error zero only when valid status found.
250 if (CRM_Utils_Array::value('id', $result)) {
251 $result['is_error'] = 0;
252 }
253 }
254 else {
255 $result = civicrm_create_error('did not find a membership record');
256 }
257 $dao->free();
258 return $result;
259 }
260