Merge pull request #3679 from yashodha/CRM-14951
[civicrm-core.git] / api / v3 / MembershipStatus.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
731a0992 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
731a0992 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 *
731a0992 35 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
36 * @version $Id: MembershipStatus.php 30171 2010-10-14 09:11:27Z mover $
37 *
38 */
39
6a488035
TO
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 */
51function civicrm_api3_membership_status_create($params) {
52 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
53}
54
11e09c59 55/**
6a488035 56 * Adjust Metadata for Create action
11e09c59
TO
57 *
58 * The metadata is used for setting defaults, documentation & validation
59 * @param array $params array or parameters determined by getfields
60 */
6a488035
TO
61function _civicrm_api3_membership_status_create_spec(&$params) {
62 $params['name']['api.aliases'] = array('label');
63}
64
65/**
66 * Get a membership status.
67 *
68 * This api is used for finding an existing membership status.
69 *
70 * @param array $params an associative array of name/value property values of civicrm_membership_status
71 *
72 * @return Array of all found membership status property values.
73 * {@getfields MembershipStatus_get}
74 * @access public
75 */
76function civicrm_api3_membership_status_get($params) {
77 return _civicrm_api3_basic_get('CRM_Member_BAO_MembershipStatus', $params);
78}
79
80/**
81 * Update an existing membership status
82 *
83 * This api is used for updating an existing membership status.
e9d619eb 84 * Required parameters : id of a membership status
6a488035
TO
85 *
86 * @param Array $params an associative array of name/value property values of civicrm_membership_status
87 * @deprecated - should just use create
88 *
89 * @return array of updated membership status property values
90 * @access public
91 */
92function &civicrm_api3_membership_status_update($params) {
93
94 civicrm_api3_verify_mandatory($params, NULL, array('id'));
95 //don't allow duplicate names.
96 $name = CRM_Utils_Array::value('name', $params);
97 if ($name) {
6a488035
TO
98 $status = new CRM_Member_DAO_MembershipStatus();
99 $status->name = $params['name'];
100 if ($status->find(TRUE) && $status->id != $params['id']) {
101 return civicrm_api3_create_error(ts('A membership status with this name already exists.'));
102 }
103 }
104
6a488035
TO
105 $membershipStatusBAO = new CRM_Member_BAO_MembershipStatus();
106 $membershipStatusBAO->id = $params['id'];
107 if ($membershipStatusBAO->find(TRUE)) {
108 $fields = $membershipStatusBAO->fields();
109 foreach ($fields as $name => $field) {
110 if (array_key_exists($name, $params)) {
111 $membershipStatusBAO->$name = $params[$name];
112 }
113 }
114 $membershipStatusBAO->save();
115 }
116 $membershipStatus = array();
117 _civicrm_api3_object_to_array(clone($membershipStatusBAO), $membershipStatus);
118 $membershipStatus['is_error'] = 0;
119 return $membershipStatus;
120}
121
122/**
123 * Deletes an existing membership status
124 *
125 * This API is used for deleting a membership status
126 *
127 * @param array Params array containing 'id' - Id of the membership status to be deleted
128 * {@getfields MembershipStatus_delete}
129 *
130 * @return array i
131 * @access public
132 */
133function civicrm_api3_membership_status_delete($params) {
134
135 $memberStatusDelete = CRM_Member_BAO_MembershipStatus::del($params['id'], TRUE);
136 return $memberStatusDelete ? civicrm_api3_create_error($memberStatusDelete['error_message']) : civicrm_api3_create_success();
137}
138
139/**
140 * Derives the Membership Status of a given Membership Reocrd
141 *
142 * This API is used for deriving Membership Status of a given Membership
143 * record using the rules encoded in the membership_status table.
144 *
77b97be7
EM
145 * @param $membershipParams
146 *
147 * @throws API_Exception
148 * @internal param Int $membershipID Id of a membership
149 * @internal param String $statusDate
6a488035
TO
150 *
151 * @return Array Array of status id and status name
152 * @public
153 */
154function civicrm_api3_membership_status_calc($membershipParams) {
6a488035 155 if (!($membershipID = CRM_Utils_Array::value('membership_id', $membershipParams))) {
5f11bbcc 156 throw new API_Exception('membershipParams do not contain membership_id');
6a488035
TO
157 }
158
5f11bbcc
EM
159 if(empty($membershipParams['id'])) {
160 //for consistency lets make sure id is set as this will get passed to hooks downstream
161 $membershipParams['id'] = $membershipID;
162 }
6a488035 163 $query = "
5f11bbcc 164SELECT start_date, end_date, join_date, membership_type_id
6a488035
TO
165 FROM civicrm_membership
166 WHERE id = %1
167";
168
169 $params = array(1 => array($membershipID, 'Integer'));
5f11bbcc 170 $dao = CRM_Core_DAO::executeQuery($query, $params);
6a488035 171 if ($dao->fetch()) {
5f11bbcc
EM
172 $membershipTypeID = empty($membershipParams['membership_type_id']) ? $dao->membership_type_id : $membershipParams['membership_type_id'];
173 $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);
6a488035 174 //make is error zero only when valid status found.
a7488080 175 if (!empty($result['id'])) {
6a488035
TO
176 $result['is_error'] = 0;
177 }
178 }
179 else {
5f11bbcc
EM
180 $dao->free();
181 throw new API_Exception('did not find a membership record');
6a488035
TO
182 }
183 $dao->free();
184 return $result;
185}
186