Merge pull request #2674 from deepak-srivastava/CRM-12467-soft-credit-search
[civicrm-core.git] / api / v3 / Event.php
index d05a3f4eb6b072d6a3db693adef542926758c708..47194379da241ba8c9dcbe2b435b4612cb918010 100644 (file)
@@ -1,9 +1,8 @@
 <?php
-// $Id$
 
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -42,7 +41,6 @@
 /**
  * Files required for this package
  */
-require_once 'CRM/Event/BAO/Event.php';
 
 /**
  * Create a Event
@@ -57,17 +55,28 @@ 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);
 }
 
@@ -78,7 +87,6 @@ function civicrm_api3_event_create($params) {
  * @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;
@@ -111,19 +119,19 @@ function _civicrm_api3_event_create_legacy_support_42(&$params){
 function civicrm_api3_event_get($params) {
 
   //legacy support for $params['return.sort']
-  if (CRM_Utils_Array::value('return.sort', $params)) {
+  if (!empty($params['return.sort'])) {
     $params['options']['sort'] = $params['return.sort'];
     unset($params['return.sort']);
   }
 
-  //legacy support for $params['return.sort']
-  if (CRM_Utils_Array::value('return.offset', $params)) {
+  //legacy support for $params['return.offset']
+  if (!empty($params['return.offset'])) {
     $params['options']['offset'] = $params['return.offset'];
     unset($params['return.offset']);
   }
 
   //legacy support for $params['return.max_results']
-  if (CRM_Utils_Array::value('return.max_results', $params)) {
+  if (!empty($params['return.max_results'])) {
     $params['options']['limit'] = $params['return.max_results'];
     unset($params['return.max_results']);
   }
@@ -131,14 +139,14 @@ function civicrm_api3_event_get($params) {
   $eventDAO = new CRM_Event_BAO_Event();
   _civicrm_api3_dao_set_filter($eventDAO, $params, TRUE, 'Event');
 
-  if (CRM_Utils_Array::value('is_template', $params)) {
+  if (!empty($params['is_template'])) {
     $eventDAO->whereAdd( '( is_template = 1 )' );
   }
-  else {
-  $eventDAO->whereAdd('( is_template IS NULL ) OR ( is_template = 0 )');
+  elseif(empty($eventDAO->id)){
+    $eventDAO->whereAdd('( is_template IS NULL ) OR ( is_template = 0 )');
   }
 
-  if (CRM_Utils_Array::value('isCurrent', $params)) {
+  if (!empty($params['isCurrent'])) {
     $eventDAO->whereAdd('(start_date >= CURDATE() || end_date >= CURDATE())');
   }
 
@@ -150,7 +158,7 @@ function civicrm_api3_event_get($params) {
   while ($eventDAO->fetch()) {
     $event[$eventDAO->id] = array();
     CRM_Core_DAO::storeValues($eventDAO, $event[$eventDAO->id]);
-    if (CRM_Utils_Array::value('return.is_full', $params)) {
+    if (!empty($params['return.is_full'])) {
       _civicrm_api3_event_getisfull($event, $eventDAO->id);
     }
     _civicrm_api3_event_get_legacy_support_42($event, $eventDAO->id);
@@ -213,7 +221,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;
@@ -224,3 +231,50 @@ function _civicrm_api3_event_getisfull(&$event, $event_id) {
   $event[$event_id]['is_full'] = $event[$event_id]['available_places'] == 0 ? 1 : 0;
 }
 
+
+/**
+ * Overrides _civicrm_api3_generic_getlist_params.
+ *
+ * @param $request array
+ */
+function _civicrm_api3_event_getlist_params(&$request) {
+  $fieldsToReturn = array('start_date', 'event_type_id', 'title', 'summary');
+  $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
+  $request['params']['options']['sort'] = 'start_date DESC';
+  $request['params'] += array(
+    'is_template' => 0,
+    'is_active' => 1,
+  );
+}
+
+/**
+ * Overrides _civicrm_api3_generic_getlist_output
+ *
+ * @param $result array
+ * @param $request array
+ *
+ * @return array
+ */
+function _civicrm_api3_event_getlist_output($result, $request) {
+  $output = array();
+  if (!empty($result['values'])) {
+    foreach ($result['values'] as $row) {
+      $data = array(
+        'id' => $row[$request['id_field']],
+        'label' => $row[$request['label_field']],
+        'description' => array(CRM_Core_Pseudoconstant::getLabel('CRM_Event_BAO_Event', 'event_type_id', $row['event_type_id'])),
+      );
+      if (!empty($row['start_date'])) {
+        $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['start_date']);
+      }
+      if (!empty($row['summary'])) {
+        $data['description'][] = $row['summary'];
+      }
+      foreach ($request['extra'] as $field) {
+        $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL;
+      }
+      $output[] = $data;
+    }
+  }
+  return $output;
+}