Merge branch 4.5 into master
[civicrm-core.git] / api / v3 / Participant.php
index a746e8a00e999523fac43a04dcbe942d3600a264..52893a6f03512508f95a43bd7e23876787ecbe94 100644 (file)
@@ -2,7 +2,7 @@
 
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  * Required parameters : event_id AND contact_id for new creation
  *                     : participant as name/value with participantid for edit
  *
- * @param   array  $params     an associative array of name/value property values of civicrm_participant
+ * @param array $params
+ *   An associative array of name/value property values of civicrm_participant.
  *
- * @return array apiresult
+ * @return array
+   *   apiresult
  * {@getfields participant_create}
  * @access public
  */
@@ -59,17 +61,17 @@ function civicrm_api3_participant_create($params) {
   if (!empty($params['event_id'])) {
     $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template');
     if (!empty($isTemplate)) {
-      return civicrm_api3_create_error(ts('Event templates are not meant to be registered'));
+      return civicrm_api3_create_error(ts('Event templates are not meant to be registered.'));
     }
   }
 
-  $value = array();
+  $values = $participant = array();
   _civicrm_api3_custom_format_params($params, $values, 'Participant');
   $params = array_merge($values, $params);
 
   $participantBAO = CRM_Event_BAO_Participant::create($params);
 
-  if(empty($params['price_set_id']) && empty($params['id']) && !empty($params['fee_level'])){
+  if (empty($params['price_set_id']) && empty($params['id']) && !empty($params['fee_level'])) {
     _civicrm_api3_participant_createlineitem($params, $participantBAO);
   }
   _civicrm_api3_object_to_array($participantBAO, $participant[$participantBAO->id]);
@@ -78,34 +80,61 @@ function civicrm_api3_participant_create($params) {
 }
 
 /**
+ * @todo this should be done in the BAO not the api
  * Create a default participant line item
  */
-function _civicrm_api3_participant_createlineitem(&$params, $participant){
-  $sql = "
-SELECT      ps.id AS setID, pf.id AS priceFieldID, pfv.id AS priceFieldValueID
-FROM  civicrm_price_set_entity cpse
-LEFT JOIN civicrm_price_set ps ON cpse.price_set_id = ps.id AND cpse.entity_id = {$params['event_id']} AND cpse.entity_table = 'civicrm_event'
-LEFT JOIN   civicrm_price_field pf ON pf.`price_set_id` = ps.id
-LEFT JOIN   civicrm_price_field_value pfv ON pfv.price_field_id = pf.id and pfv.label = '{$params['fee_level']}'
-where ps.id is not null
-";
-
-  $dao = CRM_Core_DAO::executeQuery($sql);
-  if ($dao->fetch()) {
-    $amount = CRM_Utils_Array::value('fee_amount', $params, 0);
-    $lineItemparams = array(
-      'price_field_id' => $dao->priceFieldID,
-      'price_field_value_id' => $dao->priceFieldValueID,
-      'entity_table' => 'civicrm_participant',
-      'entity_id' => $participant->id,
-      'label' => $params['fee_level'],
-      'qty' => 1,
-      'participant_count' => 0,
-      'unit_price' => $amount,
-      'line_total' => $amount,
-      'version' => 3,
+function _civicrm_api3_participant_createlineitem(&$params, $participant) {
+  // it is possible that a fee level contains information about multiple
+  // price field values.
+
+  $priceFieldValueDetails = CRM_Utils_Array::explodePadded(
+    $params["fee_level"]);
+
+  foreach ($priceFieldValueDetails as $detail) {
+    if (preg_match('/- ([0-9]+)$/', $detail, $matches)) {
+      // it is possible that a price field value is payd for multiple times.
+      // (FIXME: if the price field value ends in minus followed by whitespace
+      // and a number, things will go wrong.)
+
+      $qty = $matches[1];
+      preg_match('/^(.*) - [0-9]+$/', $detail, $matches);
+      $label = $matches[1];
+    }
+    else {
+      $label = $detail;
+      $qty = 1;
+    }
+
+    $sql = "
+      SELECT      ps.id AS setID, pf.id AS priceFieldID, pfv.id AS priceFieldValueID, pfv.amount AS amount
+      FROM  civicrm_price_set_entity cpse
+      LEFT JOIN civicrm_price_set ps ON cpse.price_set_id = ps.id AND cpse.entity_id = %1 AND cpse.entity_table = 'civicrm_event'
+      LEFT JOIN   civicrm_price_field pf ON pf.`price_set_id` = ps.id
+      LEFT JOIN   civicrm_price_field_value pfv ON pfv.price_field_id = pf.id
+      where ps.id is not null and pfv.label = %2
+    ";
+
+    $qParams = array(
+      1 => array($params['event_id'], 'Integer'),
+      2 => array($label, 'String'),
     );
-    civicrm_api('line_item', 'create', $lineItemparams);
+
+    $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
+    if ($dao->fetch()) {
+      $lineItemParams = array(
+        'price_field_id' => $dao->priceFieldID,
+        'price_field_value_id' => $dao->priceFieldValueID,
+        'entity_table' => 'civicrm_participant',
+        'entity_id' => $participant->id,
+        'label' => $label,
+        'qty' => $qty,
+        'participant_count' => 0,
+        'unit_price' => $dao->amount,
+        'line_total' => $qty * $dao->amount,
+      );
+      civicrm_api3('line_item', 'create', $lineItemParams);
+    }
+
   }
 }
 
@@ -114,7 +143,8 @@ where ps.id is not null
  * 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_participant_create_spec(&$params) {
   $params['status_id']['api.default'] = "1";
@@ -131,9 +161,11 @@ function _civicrm_api3_participant_create_spec(&$params) {
  * If more than one matching participant exists, return an error, unless
  * the client has requested to return the first found contact
  *
- * @param  array   $params           (reference ) input parameters
+ * @param array $params
+ *   (reference ) input parameters.
  *
- * @return array (reference )        array of properties, if error an array with an error id and error message
+ * @return array
+   *   (reference )        array of properties, if error an array with an error id and error message
  * {@getfields participant_get}
  * @access public
  */
@@ -157,10 +189,12 @@ function civicrm_api3_participant_get($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_participant_get_spec(&$params) {
   $params['participant_test']['api.default'] = 0;
+  $params['participant_test']['title'] = 'Get Test Participants';
 }
 
 /**
@@ -168,7 +202,8 @@ function _civicrm_api3_participant_get_spec(&$params) {
  *
  * This API is used for deleting a contact participant
  *
- * @param  array $params Array containing  Id of the contact participant to be deleted
+ * @param array $params
+ *   Array containing Id of the contact participant to be deleted.
  *
  * {@getfields participant_delete}
  * @throws Exception
@@ -186,4 +221,3 @@ function civicrm_api3_participant_delete($params) {
     throw new Exception('Error while deleting participant');
   }
 }
-