Commit | Line | Data |
---|---|---|
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 | /** | |
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 |
16b10e64 | 41 | * Array of newly created membership status property values. |
6a488035 TO |
42 | */ |
43 | function civicrm_api3_membership_status_create($params) { | |
44 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params); | |
45 | } | |
46 | ||
6a488035 TO |
47 | /** |
48 | * Get a membership status. | |
49 | * | |
50 | * This api is used for finding an existing membership status. | |
51 | * | |
cf470720 TO |
52 | * @param array $params |
53 | * An associative array of name/value property values of civicrm_membership_status. | |
6a488035 | 54 | * |
16b10e64 CW |
55 | * @return array |
56 | * Array of all found membership status property values. | |
6a488035 TO |
57 | */ |
58 | function civicrm_api3_membership_status_get($params) { | |
59 | return _civicrm_api3_basic_get('CRM_Member_BAO_MembershipStatus', $params); | |
60 | } | |
61 | ||
62 | /** | |
d1b0d05e | 63 | * Update an existing membership status. |
6a488035 TO |
64 | * |
65 | * This api is used for updating an existing membership status. | |
b081365f | 66 | * Required parameters: id of a membership status |
6a488035 | 67 | * |
2241036a | 68 | * @param array $params |
dc64d047 EM |
69 | * Array of name/value property values of civicrm_membership_status. |
70 | * | |
6a488035 TO |
71 | * @deprecated - should just use create |
72 | * | |
a6c01b45 | 73 | * @return array |
16b10e64 | 74 | * Array of updated membership status property values |
6a488035 | 75 | */ |
d1b0d05e | 76 | function civicrm_api3_membership_status_update($params) { |
6a488035 TO |
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) { | |
6a488035 TO |
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 | ||
6a488035 TO |
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 | /** | |
22242c87 | 107 | * Deletes an existing membership status. |
6a488035 TO |
108 | * |
109 | * This API is used for deleting a membership status | |
110 | * | |
decce855 | 111 | * @param array $params |
22242c87 | 112 | * |
a6c01b45 | 113 | * @return array |
6a488035 TO |
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 | /** | |
22242c87 | 122 | * Derives the Membership Status of a given Membership Record. |
6a488035 TO |
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 | * | |
100fef9d | 127 | * @param array $membershipParams |
77b97be7 EM |
128 | * |
129 | * @throws API_Exception | |
6a488035 | 130 | * |
317fceb4 | 131 | * @return array |
72b3a70c | 132 | * Array of status id and status name |
6a488035 TO |
133 | */ |
134 | function civicrm_api3_membership_status_calc($membershipParams) { | |
6a488035 | 135 | if (!($membershipID = CRM_Utils_Array::value('membership_id', $membershipParams))) { |
5f11bbcc | 136 | throw new API_Exception('membershipParams do not contain membership_id'); |
6a488035 TO |
137 | } |
138 | ||
22e263ad | 139 | if (empty($membershipParams['id'])) { |
5f11bbcc EM |
140 | //for consistency lets make sure id is set as this will get passed to hooks downstream |
141 | $membershipParams['id'] = $membershipID; | |
142 | } | |
6a488035 | 143 | $query = " |
5f11bbcc | 144 | SELECT start_date, end_date, join_date, membership_type_id |
6a488035 TO |
145 | FROM civicrm_membership |
146 | WHERE id = %1 | |
147 | "; | |
148 | ||
149 | $params = array(1 => array($membershipID, 'Integer')); | |
5f11bbcc | 150 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
6a488035 | 151 | if ($dao->fetch()) { |
5f11bbcc EM |
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); | |
6a488035 | 154 | //make is error zero only when valid status found. |
a7488080 | 155 | if (!empty($result['id'])) { |
6a488035 TO |
156 | $result['is_error'] = 0; |
157 | } | |
158 | } | |
159 | else { | |
5f11bbcc EM |
160 | $dao->free(); |
161 | throw new API_Exception('did not find a membership record'); | |
6a488035 TO |
162 | } |
163 | $dao->free(); | |
164 | return $result; | |
165 | } |