Merge pull request #19140 from civicrm/5.33
[civicrm-core.git] / CRM / Event / BAO / Query.php
index 03d83044cb10a789dc7d595ae84291588fb7d344..ba9a353df8df9151cf96f383d40f583a51edafc9 100644 (file)
@@ -1,36 +1,18 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 5                                                  |
- +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2019                                |
- +--------------------------------------------------------------------+
- | This file is a part of CiviCRM.                                    |
- |                                                                    |
- | CiviCRM is free software; you can copy, modify, and distribute it  |
- | under the terms of the GNU Affero General Public License           |
- | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
- |                                                                    |
- | CiviCRM is distributed in the hope that it will be useful, but     |
- | WITHOUT ANY WARRANTY; without even the implied warranty of         |
- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
- | See the GNU Affero General Public License for more details.        |
+ | Copyright CiviCRM LLC. All rights reserved.                        |
  |                                                                    |
- | You should have received a copy of the GNU Affero General Public   |
- | License and the CiviCRM Licensing Exception along                  |
- | with this program; if not, contact CiviCRM LLC                     |
- | at info[AT]civicrm[DOT]org. If you have questions about the        |
- | GNU Affero General Public License or the licensing of CiviCRM,     |
- | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
  +--------------------------------------------------------------------+
  */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2019
- * $Id$
- *
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 class CRM_Event_BAO_Query extends CRM_Core_BAO_Query {
 
@@ -47,16 +29,15 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query {
     $fields = array_merge($fields, CRM_Event_DAO_Event::import());
     $fields = array_merge($fields, self::getParticipantFields());
     $fields = array_merge($fields, CRM_Core_DAO_Discount::export());
-
+    $fields['event'] = self::getPseudoEventDateFieldMetadata();
     return $fields;
   }
 
   /**
    * @return array
    */
-  public static function &getParticipantFields() {
-    $fields = CRM_Event_BAO_Participant::importableFields('Individual', TRUE, TRUE);
-    return $fields;
+  public static function getParticipantFields() {
+    return CRM_Event_BAO_Participant::importableFields('Individual', TRUE, TRUE);
   }
 
   /**
@@ -68,25 +49,25 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query {
     if (($query->_mode & CRM_Contact_BAO_Query::MODE_EVENT) ||
       CRM_Contact_BAO_Query::componentPresent($query->_returnProperties, 'participant_')
     ) {
-      $query->_select['participant_id'] = "civicrm_participant.id as participant_id";
+      $query->_select['participant_id'] = 'civicrm_participant.id as participant_id';
       $query->_element['participant_id'] = 1;
       $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
 
       //add fee level
       if (!empty($query->_returnProperties['participant_fee_level'])) {
-        $query->_select['participant_fee_level'] = "civicrm_participant.fee_level as participant_fee_level";
+        $query->_select['participant_fee_level'] = 'civicrm_participant.fee_level as participant_fee_level';
         $query->_element['participant_fee_level'] = 1;
       }
 
       //add participant contact ID
       if (!empty($query->_returnProperties['participant_contact_id'])) {
-        $query->_select['participant_contact_id'] = "civicrm_participant.contact_id as participant_contact_id";
+        $query->_select['participant_contact_id'] = 'civicrm_participant.contact_id as participant_contact_id';
         $query->_element['participant_contact_id'] = 1;
       }
 
       //add fee amount
       if (!empty($query->_returnProperties['participant_fee_amount'])) {
-        $query->_select['participant_fee_amount'] = "civicrm_participant.fee_amount as participant_fee_amount";
+        $query->_select['participant_fee_amount'] = 'civicrm_participant.fee_amount as participant_fee_amount';
         $query->_element['participant_fee_amount'] = 1;
       }
 
@@ -227,10 +208,11 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query {
   }
 
   /**
-   * @param $query
+   * Get event related where clauses.
+   *
+   * @param \CRM_Contact_BAO_Query $query
    */
   public static function where(&$query) {
-    $grouping = NULL;
     foreach (array_keys($query->_params) as $id) {
       if (empty($query->_params[$id][0])) {
         continue;
@@ -241,7 +223,6 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query {
         if ($query->_mode == CRM_Contact_BAO_Query::MODE_CONTACTS) {
           $query->_useDistinct = TRUE;
         }
-        $grouping = $query->_params[$id][3];
         self::whereClauseSingle($query->_params[$id], $query);
       }
     }
@@ -249,14 +230,24 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query {
 
   /**
    * @param $values
-   * @param $query
+   * @param \CRM_Contact_BAO_Query $query
+   *
+   * @throws \CRM_Core_Exception
    */
   public static function whereClauseSingle(&$values, &$query) {
     $checkPermission = empty($query->_skipPermission);
     list($name, $op, $value, $grouping, $wildcard) = $values;
     $fields = array_merge(CRM_Event_BAO_Event::fields(), CRM_Event_BAO_Participant::exportableFields());
+    $fieldSpec = $fields[$values[0]] ?? [];
 
     switch ($name) {
+      case 'event_low':
+      case 'event_high':
+        $query->dateQueryBuilder($values,
+          'civicrm_event', 'event', 'start_date', ts('Event Active On'), TRUE, 'YmdHis', 'end_date'
+        );
+        return;
+
       case 'event_start_date_low':
       case 'event_start_date_high':
         $query->dateQueryBuilder($values,
@@ -356,9 +347,9 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query {
         return;
 
       case 'participant_status_id':
-        if ($value && is_array($value) && strpos($op, 'IN') === FALSE) {
-          $op = 'IN';
-        }
+        $query->handleWhereFromMetadata($fieldSpec, $name, $value, $op);
+        return;
+
       case 'participant_status':
       case 'participant_source':
       case 'participant_id':
@@ -371,7 +362,6 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query {
 
         $qillName = $name;
         if (in_array($name, [
-          'participant_status_id',
           'participant_source',
           'participant_id',
           'participant_contact_id',
@@ -481,12 +471,12 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query {
     $from = NULL;
     switch ($name) {
       case 'civicrm_participant':
-        $from = " LEFT JOIN civicrm_participant ON civicrm_participant.contact_id = contact_a.id ";
+        $from = ' LEFT JOIN civicrm_participant ON civicrm_participant.contact_id = contact_a.id ';
         break;
 
       case 'civicrm_event':
         //CRM-17121
-        $from = " LEFT JOIN civicrm_event ON civicrm_participant.event_id = civicrm_event.id ";
+        $from = ' LEFT JOIN civicrm_event ON civicrm_participant.event_id = civicrm_event.id ';
         break;
 
       case 'event_type':
@@ -584,8 +574,11 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query {
     $fields = [
       'participant_status_id',
       'participant_register_date',
+      // Super-weird but we have to make it work.....
+      'event',
     ];
     $metadata = civicrm_api3('Participant', 'getfields', [])['values'];
+    $metadata['event'] = self::getPseudoEventDateFieldMetadata();
     return array_intersect_key($metadata, array_flip($fields));
   }
 
@@ -628,11 +621,6 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query {
        FALSE, ['class' => 'crm-select2', 'multiple' => 'multiple', 'placeholder' => ts('- any -')]
     );
 
-    CRM_Core_Form_Date::buildDateRange($form, 'event', 1, '_start_date_low', '_end_date_high', ts('From'), FALSE);
-
-    $form->addElement('hidden', 'event_date_range_error');
-    $form->addFormRule(['CRM_Event_BAO_Query', 'formRule'], $form);
-
     $form->addElement('checkbox', "event_include_repeating_events", NULL, ts('Include participants from all events in the %1 series', [1 => '<em>%1</em>']));
 
     $form->addSelect('participant_role_id',
@@ -672,30 +660,20 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query {
   }
 
   /**
-   * Check if the values in the date range are in correct chronological order.
-   *
-   * @todo Get this to work with CRM_Utils_Rule::validDateRange
-   *
-   * @param array $fields
-   * @param array $files
-   * @param CRM_Core_Form $form
+   * Get metadata from pseudo search field 'event'.
    *
-   * @return bool|array
+   * @return array
    */
-  public static function formRule($fields, $files, $form) {
-    $errors = [];
-
-    if ((empty($fields['event_start_date_low']) || empty($fields['event_end_date_high']))) {
-      return TRUE;
-    }
-    $lowDate = strtotime($fields['event_start_date_low']);
-    $highDate = strtotime($fields['event_end_date_high']);
-
-    if ($lowDate > $highDate) {
-      $errors['event_date_range_error'] = ts('Please check that your Event Date Range is in correct chronological order.');
-    }
-
-    return empty($errors) ? TRUE : $errors;
+  protected static function getPseudoEventDateFieldMetadata(): array {
+    return [
+      'name' => 'event',
+      'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
+      'title' => ts('Event Active On'),
+      'table_name' => 'civicrm_event',
+      'where' => 'civicrm_event.start_date',
+      'where_end' => 'civicrm_event.end_date',
+      'html' => ['type' => 'SelectDate', 'formatType' => 'activityDateTime'],
+    ];
   }
 
 }