INFRA-132 - Misc
[civicrm-core.git] / api / v3 / Membership.php
index 856ae2b967ff394962accaeaf33a576070a43cf2..dfc3ca78d710c79c8d2fd393daf6fa312b18899f 100644 (file)
@@ -1,10 +1,9 @@
 <?php
-
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -33,7 +32,7 @@
  * @package CiviCRM_APIv3
  * @subpackage API_Membership
  *
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * @version $Id: MembershipContact.php 30171 2010-10-14 09:11:27Z mover $
  */
 
  *
  * This API is used for deleting a contact membership
  *
- * @param  $params array  array holding id - Id of the contact membership to be deleted
+ * @param array $params
+ *   Array array holding id - Id of the contact membership to be deleted.
  *
- * @return array api result
- * {@getfields membership_delete}
- * @access public
+ * @return array
+ *   api result
+ *   {@getfields membership_delete}
  */
 function civicrm_api3_membership_delete($params) {
   return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
@@ -58,11 +58,12 @@ function civicrm_api3_membership_delete($params) {
  * This API is used for creating a Membership for a contact.
  * Required parameters : membership_type_id and status_id.
  *
- * @param   array  $params     an associative array of name/value property values of civicrm_membership
+ * @param array $params
+ *   An associative array of name/value property values of civicrm_membership.
  *
- * @return array of newly created membership property values.
- * {@getfields membership_create}
- * @access public
+ * @return array
+ *   Array of newly created membership property values.
+ *   {@getfields membership_create}
  */
 function civicrm_api3_membership_create($params) {
   // check params for membership id during update
@@ -111,18 +112,22 @@ function civicrm_api3_membership_create($params) {
   // Fixme: This code belongs in the BAO
   $action = CRM_Core_Action::ADD;
   // we need user id during add mode
-    $ids = array ();
-    if(CRM_Utils_Array::value('contact_id', $params)) {
-      $ids['userId'] = $params['contact_id'];
-    }
+  $ids = array();
+  if (!empty($params['contact_id'])) {
+    $ids['userId'] = $params['contact_id'];
+  }
   //for edit membership id should be present
-  if (CRM_Utils_Array::value('id', $params)) {
+  if (!empty($params['id'])) {
     $ids['membership'] = $params['id'];
     $action = CRM_Core_Action::UPDATE;
   }
   //need to pass action to handle related memberships.
   $params['action'] = $action;
 
+  if (empty($params['line_item']) && !empty($params['membership_type_id'])) {
+    CRM_Price_BAO_LineItem::getLineItemArray($params, NULL, 'membership', $params['membership_type_id']);
+  }
+
   $membershipBAO = CRM_Member_BAO_Membership::create($params, $ids, TRUE);
 
   if (array_key_exists('is_error', $membershipBAO)) {
@@ -142,7 +147,8 @@ function civicrm_api3_membership_create($params) {
  * Adjust Metadata for Create action
  *
  * The metadata is used for setting defaults, documentation & validation
- * @param array $params array or parameters determined by getfields
+ * @param array $params
+ *   Array or parameters determined by getfields.
  */
 function _civicrm_api3_membership_create_spec(&$params) {
   $params['contact_id']['api.required'] = 1;
@@ -151,7 +157,7 @@ function _civicrm_api3_membership_create_spec(&$params) {
   $params['membership_type_id']['api.aliases'] = array('membership_type');
   $params['status_id']['api.aliases'] = array('membership_status');
   $params['skipStatusCal'] = array(
-    'title' => 'Skip status calculation. By default this is 0 if id is not set and 1 if it is set.'
+    'title' => 'Skip status calculation. By default this is 0 if id is not set and 1 if it is set.',
   );
   $params['num_terms'] = array(
     '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.',
@@ -162,7 +168,8 @@ function _civicrm_api3_membership_create_spec(&$params) {
  * Adjust Metadata for Get action
  *
  * The metadata is used for setting defaults, documentation & validation
- * @param array $params array or parameters determined by getfields
+ * @param array $params
+ *   Array or parameters determined by getfields.
  */
 function _civicrm_api3_membership_get_spec(&$params) {
   $params['membership_type_id']['api.aliases'] = array('membership_type');
@@ -174,14 +181,13 @@ function _civicrm_api3_membership_get_spec(&$params) {
  * This api will return the membership records for the contacts
  * having membership based on the relationship with the direct members.
  *
- * @param  Array $params key/value pairs for contact_id and some
+ * @param array $params
+ *   Key/value pairs for contact_id and some.
  *          options affecting the desired results; has legacy support
  *          for just passing the contact_id itself as the argument
  *
- * @return  Array of all found membership property values.
- * @access public
- * @todo needs some love - basically only a get for a given contact right now
- * {@getfields membership_get}
+ * @return array
+ *   Array of all found membership property values.
  */
 function civicrm_api3_membership_get($params) {
   $activeOnly = $membershipTypeId = $membershipType = NULL;
@@ -192,18 +198,18 @@ function civicrm_api3_membership_get($params) {
     unset($params['filters']['is_current']);
   }
   $activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly);
-  if($activeOnly && empty($params['status_id'])) {
+  if ($activeOnly && empty($params['status_id'])) {
     $params['status_id'] = array('IN' => CRM_Member_BAO_MembershipStatus::getMembershipStatusCurrent());
   }
-  $options = _civicrm_api3_get_options_from_params($params, TRUE,'membership', 'get');
-  if($options['is_count']) {
+
+  $options = _civicrm_api3_get_options_from_params($params, TRUE, 'membership', 'get');
+  if ($options['is_count']) {
     return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
   }
-  $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE);
-
+  $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Membership');
 
   $return = $options['return'];
-  if(empty($membershipValues) ||
+  if (empty($membershipValues) ||
     (!empty($return)
       && !array_key_exists('related_contact_id', $return)
       && !array_key_exists('relationship_name', $return)
@@ -212,7 +218,7 @@ function civicrm_api3_membership_get($params) {
     return civicrm_api3_create_success($membershipValues, $params, 'membership', 'get');
   }
 
-  $members =  _civicrm_api3_membership_relationsship_get_customv2behaviour($params, $membershipValues, $contactID );
+  $members = _civicrm_api3_membership_relationsship_get_customv2behaviour($params, $membershipValues, $contactID);
   return civicrm_api3_create_success($members, $params, 'membership', 'get');
 }
 
@@ -223,8 +229,13 @@ function civicrm_api3_membership_get($params) {
  * is passed in as part of the reasonable expectation developers have that we will keep the api
  * as stable as possible
  *
- * @param array $params parameters passed into get function
- * @return array result for calling function
+ * @param array $params
+ *   Parameters passed into get function.
+ * @param int $membershipTypeId
+ * @param $activeOnly
+ *
+ * @return array
+ *   result for calling function
  */
 function _civicrm_api3_membership_get_customv2behaviour(&$params, $membershipTypeId, $activeOnly) {
   // get the membership for the given contact ID
@@ -240,9 +251,15 @@ function _civicrm_api3_membership_get_customv2behaviour(&$params, $membershipTyp
 
 /**
  * non-standard behaviour inherited from v2
-* @param array $params parameters passed into get function
-* @return array result for calling function
-*/
+ *
+ * @param array $params
+ *   Parameters passed into get function.
+ * @param $membershipValues
+ * @param int $contactID
+ *
+ * @return array
+ *   result for calling function
+ */
 function _civicrm_api3_membership_relationsship_get_customv2behaviour(&$params, $membershipValues, $contactID) {
   $relationships = array();
   foreach ($membershipValues as $membershipId => $values) {
@@ -251,7 +268,7 @@ function _civicrm_api3_membership_relationsship_get_customv2behaviour(&$params,
 
     $membershipValues[$membershipId]['membership_name'] = $membershipType['name'];
 
-    if (CRM_Utils_Array::value('relationship_type_id', $membershipType)) {
+    if (!empty($membershipType['relationship_type_id'])) {
       $relationships[$membershipType['relationship_type_id']] = $membershipId;
     }