2 // $Id: MembershipStatus.php 45502 2013-02-08 13:32:55Z kurund $
6 +--------------------------------------------------------------------+
7 | CiviCRM version 4.3 |
8 +--------------------------------------------------------------------+
9 | Copyright CiviCRM LLC (c) 2004-2013 |
10 +--------------------------------------------------------------------+
11 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * File for the CiviCRM APIv2 membership status functions
34 * @package CiviCRM_APIv2
35 * @subpackage API_Membership
37 * @copyright CiviCRM LLC (c) 2004-2013
38 * @version $Id: MembershipStatus.php 45502 2013-02-08 13:32:55Z kurund $
43 * Files required for this package
45 require_once 'api/v2/utils.php';
48 * Create a Membership Status
50 * This API is used for creating a Membership Status
52 * @param array $params an associative array of name/value property values of civicrm_membership_status
54 * @return array of newly created membership status property values.
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.');
64 return civicrm_create_error('Params can not be empty.');
67 $name = CRM_Utils_Array
::value('name', $params);
69 $name = CRM_Utils_Array
::value('label', $params);
72 return civicrm_create_error('Missing required fields');
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.'));
83 require_once 'CRM/Member/BAO/MembershipStatus.php';
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");
91 $values['id'] = $membershipStatusBAO->id
;
92 $values['is_error'] = 0;
98 * Get a membership status.
100 * This api is used for finding an existing membership status.
102 * @param array $params an associative array of name/value property values of civicrm_membership_status
104 * @return Array of all found membership status property values.
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.');
113 require_once 'CRM/Member/BAO/MembershipStatus.php';
114 $membershipStatusBAO = new CRM_Member_BAO_MembershipStatus();
116 $properties = array_keys($membershipStatusBAO->fields());
118 foreach ($properties as $name) {
119 if (array_key_exists($name, $params)) {
120 $membershipStatusBAO->$name = $params[$name];
124 if ($membershipStatusBAO->find()) {
125 $membershipStatus = array();
126 while ($membershipStatusBAO->fetch()) {
127 _civicrm_object_to_array(clone($membershipStatusBAO), $membershipStatus);
128 $membershipStatuses[$membershipStatusBAO->id
] = $membershipStatus;
132 return civicrm_create_error('Exact match not found');
134 return $membershipStatuses;
138 * Update an existing membership status
140 * This api is used for updating an existing membership status.
141 * Required parrmeters : id of a membership status
143 * @param Array $params an associative array of name/value property values of civicrm_membership_status
145 * @return array of updated membership status property values
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');
154 if (!isset($params['id'])) {
155 return civicrm_create_error('Required parameter missing');
158 //don't allow duplicate names.
159 $name = CRM_Utils_Array
::value('name', $params);
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.'));
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];
179 $membershipStatusBAO->save();
181 $membershipStatus = array();
182 _civicrm_object_to_array(clone($membershipStatusBAO), $membershipStatus);
183 $membershipStatus['is_error'] = 0;
184 return $membershipStatus;
188 * Deletes an existing membership status
190 * This API is used for deleting a membership status
192 * @param Int $membershipStatusID Id of the membership status to be deleted
194 * @return null if successfull, object of CRM_Core_Error otherwise
197 function civicrm_membership_status_delete(&$params) {
198 if (!is_array($params)) {
199 return civicrm_create_error('Params is not an array');
202 if (!CRM_Utils_Array
::value('id', $params)) {
203 return civicrm_create_error('Invalid or no value for membershipStatusID');
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();
212 * Derives the Membership Status of a given Membership Reocrd
214 * This API is used for deriving Membership Status of a given Membership
215 * record using the rules encoded in the membership_status table.
217 * @param Int $membershipID Id of a membership
218 * @param String $statusDate
220 * @return Array Array of status id and status name
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'));
228 if (!($membershipID = CRM_Utils_Array
::value('membership_id', $membershipParams))) {
229 return civicrm_create_error('membershipParams do not contain membership_id');
233 SELECT start_date, end_date, join_date
234 FROM civicrm_membership
237 $params = array(1 => array($membershipID, 'Integer'));
238 $dao = CRM_Core_DAO
::executeQuery($query, $params);
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
,
249 //make is error zero only when valid status found.
250 if (CRM_Utils_Array
::value('id', $result)) {
251 $result['is_error'] = 0;
255 $result = civicrm_create_error('did not find a membership record');