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