Merge pull request #5060 from eileenmcnaughton/api-comment-fixes
[civicrm-core.git] / api / v3 / MembershipStatus.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
731a0992 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 *
731a0992 34 * @copyright CiviCRM LLC (c) 2004-2014
6a488035 35 * @version $Id: MembershipStatus.php 30171 2010-10-14 09:11:27Z mover $
6a488035
TO
36 */
37
6a488035 38/**
d1b0d05e 39 * Create a Membership Status.
6a488035 40 *
cf470720 41 * @param array $params
d1b0d05e 42 * Array of name/value property values of civicrm_membership_status.
6a488035 43 *
a6c01b45 44 * @return array
16b10e64 45 * Array of newly created membership status property values.
6a488035
TO
46 */
47function civicrm_api3_membership_status_create($params) {
48 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
49}
50
6a488035
TO
51/**
52 * Get a membership status.
53 *
54 * This api is used for finding an existing membership status.
55 *
cf470720
TO
56 * @param array $params
57 * An associative array of name/value property values of civicrm_membership_status.
6a488035 58 *
16b10e64
CW
59 * @return array
60 * Array of all found membership status property values.
317fceb4 61 * {@getfields MembershipStatus_get}
6a488035
TO
62 */
63function civicrm_api3_membership_status_get($params) {
64 return _civicrm_api3_basic_get('CRM_Member_BAO_MembershipStatus', $params);
65}
66
67/**
d1b0d05e 68 * Update an existing membership status.
6a488035
TO
69 *
70 * This api is used for updating an existing membership status.
e9d619eb 71 * Required parameters : id of a membership status
6a488035 72 *
2241036a 73 * @param array $params
dc64d047
EM
74 * Array of name/value property values of civicrm_membership_status.
75 *
6a488035
TO
76 * @deprecated - should just use create
77 *
a6c01b45 78 * @return array
16b10e64 79 * Array of updated membership status property values
6a488035 80 */
d1b0d05e 81function civicrm_api3_membership_status_update($params) {
6a488035
TO
82
83 civicrm_api3_verify_mandatory($params, NULL, array('id'));
84 //don't allow duplicate names.
85 $name = CRM_Utils_Array::value('name', $params);
86 if ($name) {
6a488035
TO
87 $status = new CRM_Member_DAO_MembershipStatus();
88 $status->name = $params['name'];
89 if ($status->find(TRUE) && $status->id != $params['id']) {
90 return civicrm_api3_create_error(ts('A membership status with this name already exists.'));
91 }
92 }
93
6a488035
TO
94 $membershipStatusBAO = new CRM_Member_BAO_MembershipStatus();
95 $membershipStatusBAO->id = $params['id'];
96 if ($membershipStatusBAO->find(TRUE)) {
97 $fields = $membershipStatusBAO->fields();
98 foreach ($fields as $name => $field) {
99 if (array_key_exists($name, $params)) {
100 $membershipStatusBAO->$name = $params[$name];
101 }
102 }
103 $membershipStatusBAO->save();
104 }
105 $membershipStatus = array();
106 _civicrm_api3_object_to_array(clone($membershipStatusBAO), $membershipStatus);
107 $membershipStatus['is_error'] = 0;
108 return $membershipStatus;
109}
110
111/**
22242c87 112 * Deletes an existing membership status.
6a488035
TO
113 *
114 * This API is used for deleting a membership status
115 *
decce855 116 * @param array $params
22242c87 117 *
a6c01b45 118 * @return array
6a488035
TO
119 */
120function civicrm_api3_membership_status_delete($params) {
121
122 $memberStatusDelete = CRM_Member_BAO_MembershipStatus::del($params['id'], TRUE);
123 return $memberStatusDelete ? civicrm_api3_create_error($memberStatusDelete['error_message']) : civicrm_api3_create_success();
124}
125
126/**
22242c87 127 * Derives the Membership Status of a given Membership Record.
6a488035
TO
128 *
129 * This API is used for deriving Membership Status of a given Membership
130 * record using the rules encoded in the membership_status table.
131 *
100fef9d 132 * @param array $membershipParams
77b97be7
EM
133 *
134 * @throws API_Exception
6a488035 135 *
317fceb4 136 * @return array
72b3a70c 137 * Array of status id and status name
6a488035
TO
138 */
139function civicrm_api3_membership_status_calc($membershipParams) {
6a488035 140 if (!($membershipID = CRM_Utils_Array::value('membership_id', $membershipParams))) {
5f11bbcc 141 throw new API_Exception('membershipParams do not contain membership_id');
6a488035
TO
142 }
143
22e263ad 144 if (empty($membershipParams['id'])) {
5f11bbcc
EM
145 //for consistency lets make sure id is set as this will get passed to hooks downstream
146 $membershipParams['id'] = $membershipID;
147 }
6a488035 148 $query = "
5f11bbcc 149SELECT start_date, end_date, join_date, membership_type_id
6a488035
TO
150 FROM civicrm_membership
151 WHERE id = %1
152";
153
154 $params = array(1 => array($membershipID, 'Integer'));
5f11bbcc 155 $dao = CRM_Core_DAO::executeQuery($query, $params);
6a488035 156 if ($dao->fetch()) {
5f11bbcc
EM
157 $membershipTypeID = empty($membershipParams['membership_type_id']) ? $dao->membership_type_id : $membershipParams['membership_type_id'];
158 $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 159 //make is error zero only when valid status found.
a7488080 160 if (!empty($result['id'])) {
6a488035
TO
161 $result['is_error'] = 0;
162 }
163 }
164 else {
5f11bbcc
EM
165 $dao->free();
166 throw new API_Exception('did not find a membership record');
6a488035
TO
167 }
168 $dao->free();
169 return $result;
170}