| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | CiviCRM version 4.4 | |
| 6 | +--------------------------------------------------------------------+ |
| 7 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 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-2013 |
| 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(CRM_Utils_Array::value('contact_id', $params)) { |
| 116 | $ids['userId'] = $params['contact_id']; |
| 117 | } |
| 118 | //for edit membership id should be present |
| 119 | if (CRM_Utils_Array::value('id', $params)) { |
| 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'])) { |
| 191 | $activeOnly = CRM_Utils_Array::value('is_current', $params['filters'], FALSE); |
| 192 | } |
| 193 | $activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly); |
| 194 | |
| 195 | if (CRM_Utils_Array::value('contact_id', $params) && !is_array($params['contact_id'])) { |
| 196 | $membershipValues = _civicrm_api3_membership_get_customv2behaviour($params, $membershipTypeId, $activeOnly ); |
| 197 | } |
| 198 | else { |
| 199 | //legacy behaviour only ever worked when contact_id passed in - use standard api function otherwise |
| 200 | $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE); |
| 201 | } |
| 202 | |
| 203 | $options = _civicrm_api3_get_options_from_params($params, TRUE,'membership','get'); |
| 204 | $return = $options['return']; |
| 205 | if(empty($membershipValues) || |
| 206 | (!empty($return) |
| 207 | && !array_key_exists('related_contact_id', $return) |
| 208 | && !array_key_exists('relationship_name', $return) |
| 209 | ) |
| 210 | ) { |
| 211 | return civicrm_api3_create_success($membershipValues, $params, 'membership', 'get'); |
| 212 | } |
| 213 | |
| 214 | $members = _civicrm_api3_membership_relationsship_get_customv2behaviour($params, $membershipValues, $contactID ); |
| 215 | return civicrm_api3_create_success($members, $params, 'membership', 'get'); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * When we copied apiv3 from api v2 we brought across some custom behaviours - in the case of |
| 220 | * membership a complicated return array is constructed. The original |
| 221 | * behaviour made contact_id a required field. We still need to keep this for v3 when contact_id |
| 222 | * is passed in as part of the reasonable expectation developers have that we will keep the api |
| 223 | * as stable as possible |
| 224 | * |
| 225 | * @param array $params parameters passed into get function |
| 226 | * @return array result for calling function |
| 227 | */ |
| 228 | function _civicrm_api3_membership_get_customv2behaviour(&$params, $membershipTypeId, $activeOnly) { |
| 229 | // get the membership for the given contact ID |
| 230 | $membershipParams = array('contact_id' => $params['contact_id']); |
| 231 | if ($membershipTypeId) { |
| 232 | $membershipParams['membership_type_id'] = $membershipTypeId; |
| 233 | } |
| 234 | $membershipValues = array(); |
| 235 | CRM_Member_BAO_Membership::getValues($membershipParams, $membershipValues, $activeOnly); |
| 236 | return $membershipValues; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | /** |
| 241 | * non-standard behaviour inherited from v2 |
| 242 | * @param array $params parameters passed into get function |
| 243 | * @return array result for calling function |
| 244 | */ |
| 245 | function _civicrm_api3_membership_relationsship_get_customv2behaviour(&$params, $membershipValues, $contactID) { |
| 246 | $relationships = array(); |
| 247 | foreach ($membershipValues as $membershipId => $values) { |
| 248 | // populate the membership type name for the membership type id |
| 249 | $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']); |
| 250 | |
| 251 | $membershipValues[$membershipId]['membership_name'] = $membershipType['name']; |
| 252 | |
| 253 | if (CRM_Utils_Array::value('relationship_type_id', $membershipType)) { |
| 254 | $relationships[$membershipType['relationship_type_id']] = $membershipId; |
| 255 | } |
| 256 | |
| 257 | // populating relationship type name. |
| 258 | $relationshipType = new CRM_Contact_BAO_RelationshipType(); |
| 259 | $relationshipType->id = CRM_Utils_Array::value('relationship_type_id', $membershipType); |
| 260 | if ($relationshipType->find(TRUE)) { |
| 261 | $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b; |
| 262 | } |
| 263 | |
| 264 | _civicrm_api3_custom_data_get($membershipValues[$membershipId], 'Membership', $membershipId, NULL, $values['membership_type_id']); |
| 265 | } |
| 266 | |
| 267 | $members = $membershipValues; |
| 268 | |
| 269 | // populating contacts in members array based on their relationship with direct members. |
| 270 | if (!empty($relationships)) { |
| 271 | foreach ($relationships as $relTypeId => $membershipId) { |
| 272 | // As members are not direct members, there should not be |
| 273 | // membership id in the result array. |
| 274 | unset($membershipValues[$membershipId]['id']); |
| 275 | $relationship = new CRM_Contact_BAO_Relationship(); |
| 276 | $relationship->contact_id_b = $contactID; |
| 277 | $relationship->relationship_type_id = $relTypeId; |
| 278 | if ($relationship->find()) { |
| 279 | while ($relationship->fetch()) { |
| 280 | clone($relationship); |
| 281 | $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a; |
| 282 | $members[$membershipId]['related_contact_id'] = $relationship->contact_id_a; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | } |
| 287 | } |
| 288 | return $members; |
| 289 | } |