Merge pull request #365 from eileenmcnaughton/4.3
[civicrm-core.git] / api / v3 / Event.php
index d83549d0ac28103f6641ac3939c0f7335aca8bac..04a9ab25ab30a5137484999424be2b23fb4869ff 100644 (file)
@@ -42,7 +42,6 @@
 /**
  * Files required for this package
  */
-require_once 'CRM/Event/BAO/Event.php';
 
 /**
  * Create a Event
@@ -57,33 +56,45 @@ require_once 'CRM/Event/BAO/Event.php';
  * @access public
  */
 function civicrm_api3_event_create($params) {
+  civicrm_api3_verify_one_mandatory($params, NULL, array('event_type_id', 'template_id'));
+
+  // Clone event from template
+  if (!empty($params['template_id']) && empty($params['id'])) {
+    $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
-  $value = array();
+  $values = array();
   _civicrm_api3_custom_format_params($params, $values, 'Event');
   $params = array_merge($values, $params);
-  require_once 'CRM/Event/BAO/Event.php';
 
   $eventBAO = CRM_Event_BAO_Event::create($params);
-    $event = array();
-    _civicrm_api3_object_to_array($eventBAO, $event[$eventBAO->id]);
+  $event = array();
+  _civicrm_api3_object_to_array($eventBAO, $event[$eventBAO->id]);
   return civicrm_api3_create_success($event, $params);
 }
-/*
+
+/**
  * Adjust Metadata for Create action
  *
  * The metadata is used for setting defaults, documentation & validation
  * @param array $params array or parameters determined by getfields
  */
 function _civicrm_api3_event_create_spec(&$params) {
-  $params['event_type_id']['api.required'] = 1;;
   $params['start_date']['api.required'] = 1;
   $params['title']['api.required'] = 1;
   $params['is_active']['api.default'] = 1;
   $params['financial_type_id']['api.aliases'] = array('contribution_type_id');
 }
-/*
+
+/**
  * Support for schema changes made in 4.2
  * The main purpose of the API is to provide integrators a level of stability not provided by
  * the core code or schema - this means we have to provide support for api calls (where possible)
@@ -114,7 +125,7 @@ function civicrm_api3_event_get($params) {
     unset($params['return.sort']);
   }
 
-  //legacy support for $params['return.sort']
+  //legacy support for $params['return.offset']
   if (CRM_Utils_Array::value('return.offset', $params)) {
     $params['options']['offset'] = $params['return.offset'];
     unset($params['return.offset']);
@@ -133,7 +144,7 @@ function civicrm_api3_event_get($params) {
     $eventDAO->whereAdd( '( is_template = 1 )' );
   }
   else {
-  $eventDAO->whereAdd('( is_template IS NULL ) OR ( is_template = 0 )');
+    $eventDAO->whereAdd('( is_template IS NULL ) OR ( is_template = 0 )');
   }
 
   if (CRM_Utils_Array::value('isCurrent', $params)) {
@@ -158,16 +169,18 @@ function civicrm_api3_event_get($params) {
 
   return civicrm_api3_create_success($event, $params, 'event', 'get', $eventDAO);
 }
-/*
+
+/**
  * Adjust Metadata for Get action
-*
-* The metadata is used for setting defaults, documentation & validation
-* @param array $params array or parameters determined by getfields
-*/
+ *
+ * The metadata is used for setting defaults, documentation & validation
+ * @param array $params array or parameters determined by getfields
+ */
 function _civicrm_api3_event_get_spec(&$params) {
   $params['financial_type_id']['api.aliases'] = array('contribution_type_id');
 }
-/*
+
+/**
  * Support for schema changes made in 4.2
  * The main purpose of the API is to provide integrators a level of stability not provided by
  * the core code or schema - this means we have to provide support for api calls (where possible)
@@ -200,7 +213,7 @@ function civicrm_api3_event_delete($params) {
 }
 /*
 
-/*
+/**
  * Function to add 'is_full' & 'available_seats' to the return array. (this might be better in the BAO)
  * Default BAO function returns a string if full rather than a Bool - which is more appropriate to a form
  *
@@ -209,7 +222,6 @@ function civicrm_api3_event_delete($params) {
  *
  */
 function _civicrm_api3_event_getisfull(&$event, $event_id) {
-  require_once 'CRM/Event/BAO/Participant.php';
   $eventFullResult = CRM_Event_BAO_Participant::eventFull($event_id, 1);
   if (!empty($eventFullResult) && is_int($eventFullResult)) {
     $event[$event_id]['available_places'] = $eventFullResult;