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