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