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