| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
| 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 | * |
| 30 | * This api exposes CiviCRM membership contact records. |
| 31 | * |
| 32 | * @package CiviCRM_APIv3 |
| 33 | */ |
| 34 | |
| 35 | /** |
| 36 | * Adjust Metadata for Delete action. |
| 37 | * |
| 38 | * The metadata is used for setting defaults, documentation & validation. |
| 39 | * |
| 40 | * @param array $params |
| 41 | * Array of parameters determined by getfields. |
| 42 | */ |
| 43 | function _civicrm_api3_membership_delete_spec(&$params) { |
| 44 | $params['preserve_contribution'] = [ |
| 45 | 'api.required' => 0, |
| 46 | 'title' => 'Preserve Contribution', |
| 47 | 'description' => 'By default this is 0, or 0 if not set. Set to 1 to preserve the associated contribution record when membership is deleted.', |
| 48 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
| 49 | ]; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Deletes an existing contact Membership. |
| 54 | * |
| 55 | * @param array $params |
| 56 | * Array array holding id - Id of the contact membership to be deleted. |
| 57 | * @return array API result array. |
| 58 | * @throws API_Exception |
| 59 | */ |
| 60 | function civicrm_api3_membership_delete($params) { |
| 61 | if (isset($params['preserve_contribution'])) { |
| 62 | if (CRM_Member_BAO_Membership::del($params['id'], $params['preserve_contribution'])) { |
| 63 | return civicrm_api3_create_success(TRUE, $params); |
| 64 | } |
| 65 | else { |
| 66 | throw new API_Exception(ts('Could not delete membership')); |
| 67 | } |
| 68 | } |
| 69 | else { |
| 70 | return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Create a Contact Membership. |
| 76 | * |
| 77 | * This API is used for creating a Membership for a contact. |
| 78 | * Required parameters : membership_type_id and status_id. |
| 79 | * |
| 80 | * @param array $params |
| 81 | * Array of name/value property values of civicrm_membership. |
| 82 | * |
| 83 | * @return array |
| 84 | * API result array. |
| 85 | */ |
| 86 | function civicrm_api3_membership_create($params) { |
| 87 | // check params for membership id during update |
| 88 | if (!empty($params['id']) && !isset($params['skipStatusCal'])) { |
| 89 | // Don't calculate status on existing membership - expect API use to pass them in |
| 90 | // or leave unchanged. |
| 91 | $params['skipStatusCal'] = 1; |
| 92 | } |
| 93 | else { |
| 94 | // also check for status id if override is set (during add/update) |
| 95 | if (!empty($params['is_override']) && empty($params['status_id'])) { |
| 96 | return civicrm_api3_create_error('Status ID required'); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | $values = []; |
| 101 | _civicrm_api3_custom_format_params($params, $values, 'Membership'); |
| 102 | $params = array_merge($params, $values); |
| 103 | |
| 104 | // Calculate membership dates |
| 105 | // Fixme: This code belongs in the BAO |
| 106 | if (empty($params['id']) || !empty($params['num_terms'])) { |
| 107 | // If this is a new membership or we have a specified number of terms calculate membership dates. |
| 108 | if (empty($params['id'])) { |
| 109 | // This is a new membership, calculate the membership dates. |
| 110 | $calcDates = CRM_Member_BAO_MembershipType::getDatesForMembershipType( |
| 111 | $params['membership_type_id'], |
| 112 | CRM_Utils_Array::value('join_date', $params), |
| 113 | CRM_Utils_Array::value('start_date', $params), |
| 114 | CRM_Utils_Array::value('end_date', $params), |
| 115 | CRM_Utils_Array::value('num_terms', $params, 1) |
| 116 | ); |
| 117 | } |
| 118 | else { |
| 119 | // This is an existing membership, calculate the membership dates after renewal |
| 120 | // num_terms is treated as a 'special sauce' for is_renewal but this |
| 121 | // isn't really helpful for completing pendings. |
| 122 | $calcDates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType( |
| 123 | $params['id'], |
| 124 | NULL, |
| 125 | CRM_Utils_Array::value('membership_type_id', $params), |
| 126 | $params['num_terms'] |
| 127 | ); |
| 128 | } |
| 129 | foreach (['join_date', 'start_date', 'end_date'] as $date) { |
| 130 | if (empty($params[$date]) && isset($calcDates[$date])) { |
| 131 | $params[$date] = $calcDates[$date]; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // Fixme: This code belongs in the BAO |
| 137 | $ids = []; |
| 138 | if (empty($params['id'])) { |
| 139 | $params['action'] = CRM_Core_Action::ADD; |
| 140 | // we need user id during add mode |
| 141 | if (!empty($params['contact_id'])) { |
| 142 | $ids['userId'] = $params['contact_id']; |
| 143 | } |
| 144 | } |
| 145 | else { |
| 146 | // edit mode |
| 147 | $params['action'] = CRM_Core_Action::UPDATE; |
| 148 | // @todo remove $ids['membership'] is required in CRM_Price_BAO_LineItem::processPriceSet |
| 149 | $ids['membership'] = $params['id']; |
| 150 | } |
| 151 | |
| 152 | // @todo stop passing $ids (membership and userId may be set above) |
| 153 | $membershipBAO = CRM_Member_BAO_Membership::create($params, $ids, TRUE); |
| 154 | |
| 155 | if (array_key_exists('is_error', $membershipBAO)) { |
| 156 | // In case of no valid status for given dates, $membershipBAO |
| 157 | // is going to contain 'is_error' => "Error Message" |
| 158 | return civicrm_api3_create_error(ts('The membership can not be saved, no valid membership status for given dates')); |
| 159 | } |
| 160 | |
| 161 | $membership = []; |
| 162 | _civicrm_api3_object_to_array($membershipBAO, $membership[$membershipBAO->id]); |
| 163 | |
| 164 | return civicrm_api3_create_success($membership, $params, 'Membership', 'create', $membershipBAO); |
| 165 | |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Adjust Metadata for Create action. |
| 170 | * |
| 171 | * The metadata is used for setting defaults, documentation & validation. |
| 172 | * |
| 173 | * @param array $params |
| 174 | * Array of parameters determined by getfields. |
| 175 | */ |
| 176 | function _civicrm_api3_membership_create_spec(&$params) { |
| 177 | $params['contact_id']['api.required'] = 1; |
| 178 | $params['membership_type_id']['api.required'] = 1; |
| 179 | $params['is_test']['api.default'] = 0; |
| 180 | $params['membership_type_id']['api.aliases'] = ['membership_type']; |
| 181 | $params['status_id']['api.aliases'] = ['membership_status']; |
| 182 | $params['skipStatusCal'] = [ |
| 183 | 'title' => 'Skip status calculation', |
| 184 | 'description' => 'By default this is 0 if id is not set and 1 if it is set.', |
| 185 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
| 186 | ]; |
| 187 | $params['num_terms'] = [ |
| 188 | 'title' => 'Number of terms', |
| 189 | 'description' => 'Terms to add/renew. If this parameter is passed, dates will be calculated automatically. If no id is passed (new membership) and no dates are given, num_terms will be assumed to be 1.', |
| 190 | 'type' => CRM_Utils_Type::T_INT, |
| 191 | ]; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Adjust Metadata for Get action. |
| 196 | * |
| 197 | * The metadata is used for setting defaults, documentation & validation. |
| 198 | * |
| 199 | * @param array $params |
| 200 | * Array of parameters determined by getfields. |
| 201 | */ |
| 202 | function _civicrm_api3_membership_get_spec(&$params) { |
| 203 | $params['membership_type_id']['api.aliases'] = ['membership_type']; |
| 204 | $params['active_only'] = [ |
| 205 | 'title' => 'Active Only', |
| 206 | 'description' => 'Only retrieve active memberships', |
| 207 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
| 208 | ]; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Get contact Membership record. |
| 213 | * |
| 214 | * This api will return the membership records for the contacts |
| 215 | * having membership based on the relationship with the direct members. |
| 216 | * |
| 217 | * @param array $params |
| 218 | * Key/value pairs for contact_id and some. |
| 219 | * options affecting the desired results; has legacy support |
| 220 | * for just passing the contact_id itself as the argument |
| 221 | * |
| 222 | * @return array |
| 223 | * Array of all found membership property values. |
| 224 | */ |
| 225 | function civicrm_api3_membership_get($params) { |
| 226 | $activeOnly = $membershipTypeId = $membershipType = NULL; |
| 227 | |
| 228 | $contactID = CRM_Utils_Array::value('contact_id', $params); |
| 229 | if (!empty($params['filters']) && is_array($params['filters']) && isset($params['filters']['is_current'])) { |
| 230 | $activeOnly = $params['filters']['is_current']; |
| 231 | unset($params['filters']['is_current']); |
| 232 | } |
| 233 | $activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly); |
| 234 | if ($activeOnly && empty($params['status_id'])) { |
| 235 | $params['status_id'] = ['IN' => CRM_Member_BAO_MembershipStatus::getMembershipStatusCurrent()]; |
| 236 | } |
| 237 | |
| 238 | $options = _civicrm_api3_get_options_from_params($params, TRUE, 'Membership', 'get'); |
| 239 | if ($options['is_count']) { |
| 240 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 241 | } |
| 242 | $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Membership'); |
| 243 | |
| 244 | $return = $options['return']; |
| 245 | if (empty($membershipValues) || |
| 246 | (!empty($return) |
| 247 | && !array_key_exists('related_contact_id', $return) |
| 248 | && !array_key_exists('relationship_name', $return) |
| 249 | ) |
| 250 | ) { |
| 251 | return civicrm_api3_create_success($membershipValues, $params, 'Membership', 'get'); |
| 252 | } |
| 253 | |
| 254 | $members = _civicrm_api3_membership_relationsship_get_customv2behaviour($params, $membershipValues, $contactID); |
| 255 | return civicrm_api3_create_success($members, $params, 'Membership', 'get'); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Perform api v2 custom behaviour. |
| 260 | * |
| 261 | * When we copied apiv3 from api v2 we brought across some custom behaviours - in the case of |
| 262 | * membership a complicated return array is constructed. The original |
| 263 | * behaviour made contact_id a required field. We still need to keep this for v3 when contact_id |
| 264 | * is passed in as part of the reasonable expectation developers have that we will keep the api |
| 265 | * as stable as possible |
| 266 | * |
| 267 | * @param array $params |
| 268 | * Parameters passed into get function. |
| 269 | * @param int $membershipTypeId |
| 270 | * @param $activeOnly |
| 271 | * |
| 272 | * @return array |
| 273 | * result for calling function |
| 274 | */ |
| 275 | function _civicrm_api3_membership_get_customv2behaviour(&$params, $membershipTypeId, $activeOnly) { |
| 276 | // get the membership for the given contact ID |
| 277 | $membershipParams = ['contact_id' => $params['contact_id']]; |
| 278 | if ($membershipTypeId) { |
| 279 | $membershipParams['membership_type_id'] = $membershipTypeId; |
| 280 | } |
| 281 | $membershipValues = []; |
| 282 | CRM_Member_BAO_Membership::getValues($membershipParams, $membershipValues, $activeOnly); |
| 283 | return $membershipValues; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Non-standard behaviour inherited from v2. |
| 288 | * |
| 289 | * @param array $params |
| 290 | * Parameters passed into get function. |
| 291 | * @param $membershipValues |
| 292 | * @param int $contactID |
| 293 | * |
| 294 | * @return array |
| 295 | * result for calling function |
| 296 | */ |
| 297 | function _civicrm_api3_membership_relationsship_get_customv2behaviour(&$params, $membershipValues, $contactID) { |
| 298 | $relationships = []; |
| 299 | foreach ($membershipValues as $membershipId => $values) { |
| 300 | // populate the membership type name for the membership type id |
| 301 | $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']); |
| 302 | |
| 303 | $membershipValues[$membershipId]['membership_name'] = $membershipType['name']; |
| 304 | |
| 305 | if (!empty($membershipType['relationship_type_id'])) { |
| 306 | $relationships[$membershipType['relationship_type_id']] = $membershipId; |
| 307 | } |
| 308 | |
| 309 | // populating relationship type name. |
| 310 | $relationshipType = new CRM_Contact_BAO_RelationshipType(); |
| 311 | $relationshipType->id = CRM_Utils_Array::value('relationship_type_id', $membershipType); |
| 312 | if ($relationshipType->find(TRUE)) { |
| 313 | $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b; |
| 314 | } |
| 315 | |
| 316 | _civicrm_api3_custom_data_get($membershipValues[$membershipId], CRM_Utils_Array::value('check_permissions', $params), 'Membership', $membershipId, NULL, $values['membership_type_id']); |
| 317 | } |
| 318 | |
| 319 | $members = $membershipValues; |
| 320 | |
| 321 | // Populating contacts in members array based on their relationship with direct members. |
| 322 | if (!empty($relationships)) { |
| 323 | foreach ($relationships as $relTypeId => $membershipId) { |
| 324 | // As members are not direct members, there should not be |
| 325 | // membership id in the result array. |
| 326 | unset($membershipValues[$membershipId]['id']); |
| 327 | $relationship = new CRM_Contact_BAO_Relationship(); |
| 328 | $relationship->contact_id_b = $contactID; |
| 329 | $relationship->relationship_type_id = $relTypeId; |
| 330 | if ($relationship->find()) { |
| 331 | while ($relationship->fetch()) { |
| 332 | clone($relationship); |
| 333 | $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a; |
| 334 | $members[$membershipId]['related_contact_id'] = $relationship->contact_id_a; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | } |
| 339 | } |
| 340 | return $members; |
| 341 | } |