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