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