Merge pull request #2326 from eileenmcnaughton/CRM-14069
authorEileen McNaughton <eileen@mcnaughty.com>
Tue, 3 Jun 2014 23:10:49 +0000 (11:10 +1200)
committerEileen McNaughton <eileen@mcnaughty.com>
Tue, 3 Jun 2014 23:10:49 +0000 (11:10 +1200)
CRM-14069 initial price set support for event.create
Obviously after a few months no input is forthcoming - merging this

CRM/Event/BAO/Event.php
CRM/Event/Form/ManageEvent/Fee.php
CRM/Price/BAO/PriceSet.php
api/v3/Event.php
tests/phpunit/api/v3/EventTest.php

index 55a667d90043f051fb33d63d1e3d63e1b275cb75..a2d9169e8771428bddab607fd36c8a00000a0a0e 100644 (file)
@@ -144,7 +144,7 @@ class CRM_Event_BAO_Event extends CRM_Event_DAO_Event {
     }
 
     $event = self::add($params);
-
+    CRM_Price_BAO_PriceSet::setPriceSets($params, $event, 'event');
     if (is_a($event, 'CRM_Core_Error')) {
       CRM_Core_DAO::transaction('ROLLBACK');
       return $event;
index ff3b4da180806f2258a0d2146e6d05c2df0aae75..ad48818cda2beed49ae771fe05171223d5f4e70b 100644 (file)
@@ -173,7 +173,7 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
               $optionIds[$rowCount] = $optionIds[$key];
               unset($optionIds[$key]);
             }
-          } 
+          }
         }
         $rowCount++;
       }
@@ -582,6 +582,11 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
 
       // delete all the prior label values or discounts in the custom options table
       // and delete a price set if one exists
+      //@todo note that this removes the reference from existing participants -
+      // even where there is not change - redress?
+      // note that a more tentative form of this is invoked by passing price_set_id as an array
+      // to event.create see CRM-14069
+      // @todo get all of this logic out of form layer (currently partially in BAO/api layer)
       if (CRM_Price_BAO_PriceSet::removeFrom('civicrm_event', $this->_id)) {
         CRM_Core_BAO_Discount::del($this->_id,'civicrm_event');
       }
@@ -589,6 +594,9 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
 
     if ($params['is_monetary']) {
       if (!empty($params['price_set_id'])) {
+        //@todo this is now being done in the event BAO if passed price_set_id as an array
+        // per notes on that fn - looking at the api converting to an array
+        // so calling via the api may cause this to be done in the api
         CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_id, $params['price_set_id']);
         if (!empty($params['price_field_id'])) {
           $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
@@ -717,7 +725,7 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
                   $setParams['extends'] = CRM_Core_Component::getComponentID('CiviEvent');
                   $priceSet = CRM_Price_BAO_PriceSet::create($setParams);
                   $priceSetID = $priceSet->id;
-                } 
+                }
                 else {
                   $priceSetID = $discountPriceSets[$j-1];
                   $setParams = array (
index 038962a740470648ec4e104e8351272633f6202a..900eb2b181c6a79d917a6d3da3fb11d9050cac32 100644 (file)
@@ -953,6 +953,47 @@ WHERE  id = %1";
     return $defaults;
   }
 
+  /**
+   * Supports event create function by setting up required price sets, not tested but expect
+   * it will work for contribution page
+   * @param array $params as passed to api/bao create fn
+   * @param CRM_Core_DAO $entity object for given entity
+   * @param string $entityName name of entity - e.g event
+   */
+  static function setPriceSets(&$params, $entity, $entityName) {
+    if(empty($params['price_set_id']) || !is_array($params['price_set_id'])) {
+      return;
+    }
+    // CRM-14069 note that we may as well start by assuming more than one.
+    // currently the form does not pass in as an array & will be skipped
+    // test is passing in as an array but I feel the api should have a metadata that allows
+    // transform of single to array - seems good for managing transitions - in which case all api
+    // calls that set price_set_id will hit this
+    // e.g in getfields 'price_set_id' => array('blah', 'bao_type' => 'array') - causing
+    // all separated values, strings, json half-separated values (in participant we hit this)
+    // to be converted to json @ api layer
+    $pse = new CRM_Price_DAO_PriceSetEntity();
+    $pse->entity_table = 'civicrm_' . $entityName;
+    $pse->entity_id = $entity->id;
+    while ($pse->fetch()) {
+      if(!in_array($pse->price_set_id, $params['price_set_id'])) {
+        // note an even more aggressive form of this deletion currently happens in event form
+        // past price sets discounts are made inaccessible by this as the discount_id is set to NULL
+        // on the participant record
+        if (CRM_Price_BAO_PriceSet::removeFrom('civicrm_' . $entityName, $entity->id)) {
+          CRM_Core_BAO_Discount::del($this->_id,'civicrm_' . $entityName);
+        }
+      }
+    }
+    foreach ($params['price_set_id'] as $priceSetID) {
+      CRM_Price_BAO_PriceSet::addTo('civicrm_' . $entityName, $entity->id, $priceSetID);
+      //@todo - how should we do this - copied from form
+      //if (CRM_Utils_Array::value('price_field_id', $params)) {
+      //  $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
+      //  CRM_Price_BAO_PriceSet::setIsQuickConfig($priceSetID, 0);
+      //}
+    }
+  }
   /**
    * Get field ids of a price set
    *
index 93eb30c93e6f6bce74697ae57f73aab6eaf916c9..0566c134f015d23b63889f7b1b6bd2fb26113c55 100644 (file)
@@ -62,22 +62,10 @@ function civicrm_api3_event_create($params) {
     $copy = CRM_Event_BAO_Event::copy($params['template_id']);
     $params['id'] = $copy->id;
     unset($params['template_id']);
-    if (empty($params['is_template'])) {
-      $params['is_template'] = 0;
-    }
   }
 
   _civicrm_api3_event_create_legacy_support_42($params);
-
-  //format custom fields so they can be added
-  $values = array();
-  _civicrm_api3_custom_format_params($params, $values, 'Event');
-  $params = array_merge($values, $params);
-
-  $eventBAO = CRM_Event_BAO_Event::create($params);
-  $event = array();
-  _civicrm_api3_object_to_array($eventBAO, $event[$eventBAO->id]);
-  return civicrm_api3_create_success($event, $params);
+  return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Event');
 }
 
 /**
@@ -154,6 +142,8 @@ function civicrm_api3_event_get($params) {
   // the return.is_full to deal with.
   // NB the std dao_to_array function should only return custom if required.
   $event = array();
+  $options = _civicrm_api3_get_options_from_params($params);
+
   $eventDAO->find();
   while ($eventDAO->fetch()) {
     $event[$eventDAO->id] = array();
@@ -163,6 +153,9 @@ function civicrm_api3_event_get($params) {
     }
     _civicrm_api3_event_get_legacy_support_42($event, $eventDAO->id);
     _civicrm_api3_custom_data_get($event[$eventDAO->id], 'Event', $eventDAO->id, NULL, $eventDAO->event_type_id);
+    if(!empty($options['return'])) {
+      $event[$eventDAO->id]['price_set_id'] = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventDAO->id);
+    }
   }
   //end of the loop
 
index 0bead9fd387635f2961236ff5f9e0b8e75e62f7c..1769fc7f637ff86695690a5a8799b507e93f1779 100644 (file)
@@ -296,6 +296,17 @@ class api_v3_EventTest extends CiviUnitTestCase {
     $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
   }
 
+  /**
+   * Test that an event with a price set can be created
+   */
+  function testCreatePaidEvent() {
+    //@todo alter API so that an integer is converted to an array
+    $priceSetParams = array('price_set_id' => (array) 1, 'is_monetary' => 1);
+    $result = $this->callAPISuccess('Event', 'Create', array_merge($this->_params[0], $priceSetParams));
+    $event = $this->callAPISuccess('Event', 'getsingle', array('id' => $result['id'], 'return' => 'price_set_id'));
+    $this->assertArrayKeyExists('price_set_id', $event);
+  }
+
   function testCreateEventParamsNotArray() {
     $params = NULL;
     $result = $this->callAPIFailure('event', 'create', $params);