From: Eileen McNaughton Date: Thu, 15 May 2014 23:32:31 +0000 (+1200) Subject: CRM-14069 add price set support to event api X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=b2cdd843d65cfda49f2854107945c728f2d286f8;p=civicrm-core.git CRM-14069 add price set support to event api --- diff --git a/CRM/Event/BAO/Event.php b/CRM/Event/BAO/Event.php index 4aa095d33c..ac650e2cf9 100644 --- a/CRM/Event/BAO/Event.php +++ b/CRM/Event/BAO/Event.php @@ -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; diff --git a/CRM/Event/Form/ManageEvent/Fee.php b/CRM/Event/Form/ManageEvent/Fee.php index ff3b4da180..ad48818cda 100644 --- a/CRM/Event/Form/ManageEvent/Fee.php +++ b/CRM/Event/Form/ManageEvent/Fee.php @@ -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 ( diff --git a/CRM/Price/BAO/PriceSet.php b/CRM/Price/BAO/PriceSet.php index 3457fc57da..02128b914c 100644 --- a/CRM/Price/BAO/PriceSet.php +++ b/CRM/Price/BAO/PriceSet.php @@ -937,6 +937,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 * diff --git a/api/v3/Event.php b/api/v3/Event.php index 478d28460e..67158b4bb4 100644 --- a/api/v3/Event.php +++ b/api/v3/Event.php @@ -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 diff --git a/tests/phpunit/api/v3/EventTest.php b/tests/phpunit/api/v3/EventTest.php index e110df881b..04e1dc3b7e 100644 --- a/tests/phpunit/api/v3/EventTest.php +++ b/tests/phpunit/api/v3/EventTest.php @@ -289,6 +289,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);