Merge pull request #365 from eileenmcnaughton/4.3
[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 * Files required for this package
43 */
44
45 require_once 'CRM/Member/BAO/MembershipStatus.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 * {@getfields MembershipStatus_create}
56 * @access public
57 */
58 function civicrm_api3_membership_status_create($params) {
59 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
60 }
61
62 /**
63 * Adjust Metadata for Create action
64 *
65 * The metadata is used for setting defaults, documentation & validation
66 * @param array $params array or parameters determined by getfields
67 */
68 function _civicrm_api3_membership_status_create_spec(&$params) {
69 $params['name']['api.aliases'] = array('label');
70 }
71
72 /**
73 * Get a membership status.
74 *
75 * This api is used for finding an existing membership status.
76 *
77 * @param array $params an associative array of name/value property values of civicrm_membership_status
78 *
79 * @return Array of all found membership status property values.
80 * {@getfields MembershipStatus_get}
81 * @access public
82 */
83 function civicrm_api3_membership_status_get($params) {
84 return _civicrm_api3_basic_get('CRM_Member_BAO_MembershipStatus', $params);
85 }
86
87 /**
88 * Update an existing membership status
89 *
90 * This api is used for updating an existing membership status.
91 * Required parrmeters : id of a membership status
92 *
93 * @param Array $params an associative array of name/value property values of civicrm_membership_status
94 * @deprecated - should just use create
95 *
96 * @return array of updated membership status property values
97 * @access public
98 */
99 function &civicrm_api3_membership_status_update($params) {
100
101 civicrm_api3_verify_mandatory($params, NULL, array('id'));
102 //don't allow duplicate names.
103 $name = CRM_Utils_Array::value('name', $params);
104 if ($name) {
105 require_once 'CRM/Member/DAO/MembershipStatus.php';
106 $status = new CRM_Member_DAO_MembershipStatus();
107 $status->name = $params['name'];
108 if ($status->find(TRUE) && $status->id != $params['id']) {
109 return civicrm_api3_create_error(ts('A membership status with this name already exists.'));
110 }
111 }
112
113 require_once 'CRM/Member/BAO/MembershipStatus.php';
114 $membershipStatusBAO = new CRM_Member_BAO_MembershipStatus();
115 $membershipStatusBAO->id = $params['id'];
116 if ($membershipStatusBAO->find(TRUE)) {
117 $fields = $membershipStatusBAO->fields();
118 foreach ($fields as $name => $field) {
119 if (array_key_exists($name, $params)) {
120 $membershipStatusBAO->$name = $params[$name];
121 }
122 }
123 $membershipStatusBAO->save();
124 }
125 $membershipStatus = array();
126 _civicrm_api3_object_to_array(clone($membershipStatusBAO), $membershipStatus);
127 $membershipStatus['is_error'] = 0;
128 return $membershipStatus;
129 }
130
131 /**
132 * Deletes an existing membership status
133 *
134 * This API is used for deleting a membership status
135 *
136 * @param array Params array containing 'id' - Id of the membership status to be deleted
137 * {@getfields MembershipStatus_delete}
138 *
139 * @return array i
140 * @access public
141 */
142 function civicrm_api3_membership_status_delete($params) {
143
144 $memberStatusDelete = CRM_Member_BAO_MembershipStatus::del($params['id'], TRUE);
145 return $memberStatusDelete ? civicrm_api3_create_error($memberStatusDelete['error_message']) : civicrm_api3_create_success();
146 }
147
148 /**
149 * Derives the Membership Status of a given Membership Reocrd
150 *
151 * This API is used for deriving Membership Status of a given Membership
152 * record using the rules encoded in the membership_status table.
153 *
154 * @param Int $membershipID Id of a membership
155 * @param String $statusDate
156 *
157 * @return Array Array of status id and status name
158 * @public
159 */
160 function civicrm_api3_membership_status_calc($membershipParams) {
161
162 if (!($membershipID = CRM_Utils_Array::value('membership_id', $membershipParams))) {
163 return civicrm_api3_create_error('membershipParams do not contain membership_id');
164 }
165
166 $query = "
167 SELECT start_date, end_date, join_date
168 FROM civicrm_membership
169 WHERE id = %1
170 ";
171
172 $params = array(1 => array($membershipID, 'Integer'));
173 $dao = &CRM_Core_DAO::executeQuery($query, $params);
174 if ($dao->fetch()) {
175 require_once 'CRM/Member/BAO/MembershipStatus.php';
176
177 // Take the is_admin column in MembershipStatus into consideration when requested
178 if (! CRM_Utils_Array::value('ignore_admin_only', $membershipParams) ) {
179 $result = &CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dao->start_date, $dao->end_date, $dao->join_date, 'today', TRUE);
180 }
181 else {
182 $result = &CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dao->start_date, $dao->end_date, $dao->join_date);
183 }
184
185 //make is error zero only when valid status found.
186 if (CRM_Utils_Array::value('id', $result)) {
187 $result['is_error'] = 0;
188 }
189 }
190 else {
191 $result = civicrm_api3_create_error('did not find a membership record');
192 }
193 $dao->free();
194 return $result;
195 }
196