Merge pull request #766 from eileenmcnaughton/test-fix
[civicrm-core.git] / api / v3 / MembershipStatus.php
1 <?php
2 // $Id$
3
4 /*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28 */
29
30 /**
31 * File for the CiviCRM APIv3 membership status functions
32 *
33 * @package CiviCRM_APIv3
34 * @subpackage API_Membership
35 *
36 * @copyright CiviCRM LLC (c) 2004-2013
37 * @version $Id: MembershipStatus.php 30171 2010-10-14 09:11:27Z mover $
38 *
39 */
40
41 /**
42 * Create a Membership Status
43 *
44 * This API is used for creating a Membership Status
45 *
46 * @param array $params an associative array of name/value property values of civicrm_membership_status
47 *
48 * @return 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 * Adjust Metadata for Create action
58 *
59 * The metadata is used for setting defaults, documentation & validation
60 * @param array $params array or parameters determined by getfields
61 */
62 function _civicrm_api3_membership_status_create_spec(&$params) {
63 $params['name']['api.aliases'] = array('label');
64 }
65
66 /**
67 * Get a membership status.
68 *
69 * This api is used for finding an existing membership status.
70 *
71 * @param array $params an associative array of name/value property values of civicrm_membership_status
72 *
73 * @return Array of all found membership status property values.
74 * {@getfields MembershipStatus_get}
75 * @access public
76 */
77 function civicrm_api3_membership_status_get($params) {
78 return _civicrm_api3_basic_get('CRM_Member_BAO_MembershipStatus', $params);
79 }
80
81 /**
82 * Update an existing membership status
83 *
84 * This api is used for updating an existing membership status.
85 * Required parrmeters : id of a membership status
86 *
87 * @param Array $params an associative array of name/value property values of civicrm_membership_status
88 * @deprecated - should just use create
89 *
90 * @return array of updated membership status property values
91 * @access public
92 */
93 function &civicrm_api3_membership_status_update($params) {
94
95 civicrm_api3_verify_mandatory($params, NULL, array('id'));
96 //don't allow duplicate names.
97 $name = CRM_Utils_Array::value('name', $params);
98 if ($name) {
99 $status = new CRM_Member_DAO_MembershipStatus();
100 $status->name = $params['name'];
101 if ($status->find(TRUE) && $status->id != $params['id']) {
102 return civicrm_api3_create_error(ts('A membership status with this name already exists.'));
103 }
104 }
105
106 $membershipStatusBAO = new CRM_Member_BAO_MembershipStatus();
107 $membershipStatusBAO->id = $params['id'];
108 if ($membershipStatusBAO->find(TRUE)) {
109 $fields = $membershipStatusBAO->fields();
110 foreach ($fields as $name => $field) {
111 if (array_key_exists($name, $params)) {
112 $membershipStatusBAO->$name = $params[$name];
113 }
114 }
115 $membershipStatusBAO->save();
116 }
117 $membershipStatus = array();
118 _civicrm_api3_object_to_array(clone($membershipStatusBAO), $membershipStatus);
119 $membershipStatus['is_error'] = 0;
120 return $membershipStatus;
121 }
122
123 /**
124 * Deletes an existing membership status
125 *
126 * This API is used for deleting a membership status
127 *
128 * @param array Params array containing 'id' - Id of the membership status to be deleted
129 * {@getfields MembershipStatus_delete}
130 *
131 * @return array i
132 * @access public
133 */
134 function civicrm_api3_membership_status_delete($params) {
135
136 $memberStatusDelete = CRM_Member_BAO_MembershipStatus::del($params['id'], TRUE);
137 return $memberStatusDelete ? civicrm_api3_create_error($memberStatusDelete['error_message']) : civicrm_api3_create_success();
138 }
139
140 /**
141 * Derives the Membership Status of a given Membership Reocrd
142 *
143 * This API is used for deriving Membership Status of a given Membership
144 * record using the rules encoded in the membership_status table.
145 *
146 * @param Int $membershipID Id of a membership
147 * @param String $statusDate
148 *
149 * @return Array Array of status id and status name
150 * @public
151 */
152 function civicrm_api3_membership_status_calc($membershipParams) {
153
154 if (!($membershipID = CRM_Utils_Array::value('membership_id', $membershipParams))) {
155 return civicrm_api3_create_error('membershipParams do not contain membership_id');
156 }
157
158 $query = "
159 SELECT start_date, end_date, join_date
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 // Take the is_admin column in MembershipStatus into consideration when requested
168 if (! CRM_Utils_Array::value('ignore_admin_only', $membershipParams) ) {
169 $result = &CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dao->start_date, $dao->end_date, $dao->join_date, 'today', TRUE);
170 }
171 else {
172 $result = &CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dao->start_date, $dao->end_date, $dao->join_date);
173 }
174
175 //make is error zero only when valid status found.
176 if (CRM_Utils_Array::value('id', $result)) {
177 $result['is_error'] = 0;
178 }
179 }
180 else {
181 $result = civicrm_api3_create_error('did not find a membership record');
182 }
183 $dao->free();
184 return $result;
185 }
186