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