| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | CiviCRM version 4.5 | |
| 6 | +--------------------------------------------------------------------+ |
| 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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 | * |
| 31 | * File for the CiviCRM APIv3 membership contact functions |
| 32 | * |
| 33 | * @package CiviCRM_APIv3 |
| 34 | * @subpackage API_Membership |
| 35 | * |
| 36 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 37 | * @version $Id: MembershipContact.php 30171 2010-10-14 09:11:27Z mover $ |
| 38 | */ |
| 39 | |
| 40 | /** |
| 41 | * Deletes an existing contact membership |
| 42 | * |
| 43 | * This API is used for deleting a contact membership |
| 44 | * |
| 45 | * @param $params array array holding id - Id of the contact membership to be deleted |
| 46 | * |
| 47 | * @return array api result |
| 48 | * {@getfields membership_delete} |
| 49 | * @access public |
| 50 | */ |
| 51 | function civicrm_api3_membership_delete($params) { |
| 52 | return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Create a Contact Membership |
| 57 | * |
| 58 | * This API is used for creating a Membership for a contact. |
| 59 | * Required parameters : membership_type_id and status_id. |
| 60 | * |
| 61 | * @param array $params an associative array of name/value property values of civicrm_membership |
| 62 | * |
| 63 | * @return array of newly created membership property values. |
| 64 | * {@getfields membership_create} |
| 65 | * @access public |
| 66 | */ |
| 67 | function civicrm_api3_membership_create($params) { |
| 68 | // check params for membership id during update |
| 69 | if (!empty($params['id']) && !isset($params['skipStatusCal'])) { |
| 70 | //don't calculate status on exisiting membership - expect API use to pass them in |
| 71 | // or leave unchanged |
| 72 | $params['skipStatusCal'] = 1; |
| 73 | } |
| 74 | else { |
| 75 | // also check for status id if override is set (during add/update) |
| 76 | if (!empty($params['is_override']) && empty($params['status_id'])) { |
| 77 | return civicrm_api3_create_error('Status ID required'); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | $values = array(); |
| 82 | _civicrm_api3_custom_format_params($params, $values, 'Membership'); |
| 83 | $params = array_merge($params, $values); |
| 84 | |
| 85 | // Fixme: This code belongs in the BAO |
| 86 | if (empty($params['id']) || !empty($params['num_terms'])) { |
| 87 | if (empty($params['id'])) { |
| 88 | $calcDates = CRM_Member_BAO_MembershipType::getDatesForMembershipType( |
| 89 | $params['membership_type_id'], |
| 90 | CRM_Utils_Array::value('join_date', $params), |
| 91 | CRM_Utils_Array::value('start_date', $params), |
| 92 | CRM_Utils_Array::value('end_date', $params), |
| 93 | CRM_Utils_Array::value('num_terms', $params, 1) |
| 94 | ); |
| 95 | } |
| 96 | else { |
| 97 | $calcDates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType( |
| 98 | $params['id'], |
| 99 | NULL, |
| 100 | CRM_Utils_Array::value('membership_type_id', $params), |
| 101 | $params['num_terms'] |
| 102 | ); |
| 103 | } |
| 104 | foreach (array('join_date', 'start_date', 'end_date') as $date) { |
| 105 | if (empty($params[$date]) && isset($calcDates[$date])) { |
| 106 | $params[$date] = $calcDates[$date]; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Fixme: This code belongs in the BAO |
| 112 | $action = CRM_Core_Action::ADD; |
| 113 | // we need user id during add mode |
| 114 | $ids = array (); |
| 115 | if (!empty($params['contact_id'])) { |
| 116 | $ids['userId'] = $params['contact_id']; |
| 117 | } |
| 118 | //for edit membership id should be present |
| 119 | if (!empty($params['id'])) { |
| 120 | $ids['membership'] = $params['id']; |
| 121 | $action = CRM_Core_Action::UPDATE; |
| 122 | } |
| 123 | //need to pass action to handle related memberships. |
| 124 | $params['action'] = $action; |
| 125 | |
| 126 | $membershipBAO = CRM_Member_BAO_Membership::create($params, $ids, TRUE); |
| 127 | |
| 128 | if (array_key_exists('is_error', $membershipBAO)) { |
| 129 | // In case of no valid status for given dates, $membershipBAO |
| 130 | // is going to contain 'is_error' => "Error Message" |
| 131 | return civicrm_api3_create_error(ts('The membership can not be saved, no valid membership status for given dates')); |
| 132 | } |
| 133 | |
| 134 | $membership = array(); |
| 135 | _civicrm_api3_object_to_array($membershipBAO, $membership[$membershipBAO->id]); |
| 136 | |
| 137 | return civicrm_api3_create_success($membership, $params, 'membership', 'create', $membershipBAO); |
| 138 | |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Adjust Metadata for Create action |
| 143 | * |
| 144 | * The metadata is used for setting defaults, documentation & validation |
| 145 | * @param array $params array or parameters determined by getfields |
| 146 | */ |
| 147 | function _civicrm_api3_membership_create_spec(&$params) { |
| 148 | $params['contact_id']['api.required'] = 1; |
| 149 | $params['membership_type_id']['api.required'] = 1; |
| 150 | $params['is_test']['api.default'] = 0; |
| 151 | $params['membership_type_id']['api.aliases'] = array('membership_type'); |
| 152 | $params['status_id']['api.aliases'] = array('membership_status'); |
| 153 | $params['skipStatusCal'] = array( |
| 154 | 'title' => 'Skip status calculation. By default this is 0 if id is not set and 1 if it is set.' |
| 155 | ); |
| 156 | $params['num_terms'] = array( |
| 157 | 'title' => 'Number of 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.', |
| 158 | 'type' => CRM_Utils_Type::T_INT, |
| 159 | ); |
| 160 | } |
| 161 | /** |
| 162 | * Adjust Metadata for Get action |
| 163 | * |
| 164 | * The metadata is used for setting defaults, documentation & validation |
| 165 | * @param array $params array or parameters determined by getfields |
| 166 | */ |
| 167 | function _civicrm_api3_membership_get_spec(&$params) { |
| 168 | $params['membership_type_id']['api.aliases'] = array('membership_type'); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Get contact membership record. |
| 173 | * |
| 174 | * This api will return the membership records for the contacts |
| 175 | * having membership based on the relationship with the direct members. |
| 176 | * |
| 177 | * @param Array $params key/value pairs for contact_id and some |
| 178 | * options affecting the desired results; has legacy support |
| 179 | * for just passing the contact_id itself as the argument |
| 180 | * |
| 181 | * @return Array of all found membership property values. |
| 182 | * @access public |
| 183 | * @todo needs some love - basically only a get for a given contact right now |
| 184 | * {@getfields membership_get} |
| 185 | */ |
| 186 | function civicrm_api3_membership_get($params) { |
| 187 | $activeOnly = $membershipTypeId = $membershipType = NULL; |
| 188 | |
| 189 | $contactID = CRM_Utils_Array::value('contact_id', $params); |
| 190 | if (!empty($params['filters']) && is_array($params['filters']) && isset($params['filters']['is_current'])) { |
| 191 | $activeOnly = $params['filters']['is_current']; |
| 192 | unset($params['filters']['is_current']); |
| 193 | } |
| 194 | $activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly); |
| 195 | if ($activeOnly && empty($params['status_id'])) { |
| 196 | $params['status_id'] = array('IN' => CRM_Member_BAO_MembershipStatus::getMembershipStatusCurrent()); |
| 197 | } |
| 198 | |
| 199 | $options = _civicrm_api3_get_options_from_params($params, TRUE,'membership', 'get'); |
| 200 | if ($options['is_count']) { |
| 201 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 202 | } |
| 203 | $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE); |
| 204 | |
| 205 | $return = $options['return']; |
| 206 | if(empty($membershipValues) || |
| 207 | (!empty($return) |
| 208 | && !array_key_exists('related_contact_id', $return) |
| 209 | && !array_key_exists('relationship_name', $return) |
| 210 | ) |
| 211 | ) { |
| 212 | return civicrm_api3_create_success($membershipValues, $params, 'membership', 'get'); |
| 213 | } |
| 214 | |
| 215 | $members = _civicrm_api3_membership_relationsship_get_customv2behaviour($params, $membershipValues, $contactID ); |
| 216 | return civicrm_api3_create_success($members, $params, 'membership', 'get'); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * When we copied apiv3 from api v2 we brought across some custom behaviours - in the case of |
| 221 | * membership a complicated return array is constructed. The original |
| 222 | * behaviour made contact_id a required field. We still need to keep this for v3 when contact_id |
| 223 | * is passed in as part of the reasonable expectation developers have that we will keep the api |
| 224 | * as stable as possible |
| 225 | * |
| 226 | * @param array $params parameters passed into get function |
| 227 | * @param $membershipTypeId |
| 228 | * @param $activeOnly |
| 229 | * |
| 230 | * @return array result for calling function |
| 231 | */ |
| 232 | function _civicrm_api3_membership_get_customv2behaviour(&$params, $membershipTypeId, $activeOnly) { |
| 233 | // get the membership for the given contact ID |
| 234 | $membershipParams = array('contact_id' => $params['contact_id']); |
| 235 | if ($membershipTypeId) { |
| 236 | $membershipParams['membership_type_id'] = $membershipTypeId; |
| 237 | } |
| 238 | $membershipValues = array(); |
| 239 | CRM_Member_BAO_Membership::getValues($membershipParams, $membershipValues, $activeOnly); |
| 240 | return $membershipValues; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /** |
| 245 | * non-standard behaviour inherited from v2 |
| 246 | * |
| 247 | * @param array $params parameters passed into get function |
| 248 | * @param $membershipValues |
| 249 | * @param $contactID |
| 250 | * |
| 251 | * @return array result for calling function |
| 252 | */ |
| 253 | function _civicrm_api3_membership_relationsship_get_customv2behaviour(&$params, $membershipValues, $contactID) { |
| 254 | $relationships = array(); |
| 255 | foreach ($membershipValues as $membershipId => $values) { |
| 256 | // populate the membership type name for the membership type id |
| 257 | $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']); |
| 258 | |
| 259 | $membershipValues[$membershipId]['membership_name'] = $membershipType['name']; |
| 260 | |
| 261 | if (!empty($membershipType['relationship_type_id'])) { |
| 262 | $relationships[$membershipType['relationship_type_id']] = $membershipId; |
| 263 | } |
| 264 | |
| 265 | // populating relationship type name. |
| 266 | $relationshipType = new CRM_Contact_BAO_RelationshipType(); |
| 267 | $relationshipType->id = CRM_Utils_Array::value('relationship_type_id', $membershipType); |
| 268 | if ($relationshipType->find(TRUE)) { |
| 269 | $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b; |
| 270 | } |
| 271 | |
| 272 | _civicrm_api3_custom_data_get($membershipValues[$membershipId], 'Membership', $membershipId, NULL, $values['membership_type_id']); |
| 273 | } |
| 274 | |
| 275 | $members = $membershipValues; |
| 276 | |
| 277 | // populating contacts in members array based on their relationship with direct members. |
| 278 | if (!empty($relationships)) { |
| 279 | foreach ($relationships as $relTypeId => $membershipId) { |
| 280 | // As members are not direct members, there should not be |
| 281 | // membership id in the result array. |
| 282 | unset($membershipValues[$membershipId]['id']); |
| 283 | $relationship = new CRM_Contact_BAO_Relationship(); |
| 284 | $relationship->contact_id_b = $contactID; |
| 285 | $relationship->relationship_type_id = $relTypeId; |
| 286 | if ($relationship->find()) { |
| 287 | while ($relationship->fetch()) { |
| 288 | clone($relationship); |
| 289 | $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a; |
| 290 | $members[$membershipId]['related_contact_id'] = $relationship->contact_id_a; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | } |
| 295 | } |
| 296 | return $members; |
| 297 | } |