preliminary tidy up
[civicrm-core.git] / CRM / Event / BAO / Query.php
index 0daddeb87c0099f5f111f4192f57c57507115c8e..6137a052487f38113af5aa8275ac844ca4fc8a3f 100644 (file)
@@ -1,5 +1,4 @@
 <?php
-
 /*
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
@@ -24,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -38,7 +37,7 @@ class CRM_Event_BAO_Query {
   /**
    * @return array
    */
-  static function &getFields() {
+  public static function &getFields() {
     $fields = array();
     $fields = array_merge($fields, CRM_Event_DAO_Event::import());
     $fields = array_merge($fields, self::getParticipantFields());
@@ -50,20 +49,19 @@ class CRM_Event_BAO_Query {
   /**
    * @return array
    */
-  static function &getParticipantFields() {
+  public static function &getParticipantFields() {
     $fields = CRM_Event_BAO_Participant::importableFields('Individual', TRUE, TRUE);
     return $fields;
   }
 
   /**
-   * Build select for CiviEvent
+   * Build select for CiviEvent.
    *
    * @param $query
    *
    * @return void
-   * @access public
    */
-  static function select(&$query) {
+  public static function select(&$query) {
     if (($query->_mode & CRM_Contact_BAO_Query::MODE_EVENT) ||
       CRM_Contact_BAO_Query::componentPresent($query->_returnProperties, 'participant_')
     ) {
@@ -77,6 +75,12 @@ class CRM_Event_BAO_Query {
         $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->_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";
@@ -135,6 +139,10 @@ class CRM_Event_BAO_Query {
         $query->_tables['participant_status'] = 1;
         $query->_whereTables['civicrm_participant'] = 1;
         $query->_whereTables['participant_status'] = 1;
+        $query->_pseudoConstantsSelect['participant_status'] = array(
+          'pseudoField' => 'participant_status',
+          'idCol' => 'participant_status_id',
+        );
       }
 
       //add participant_role and participant_role_id
@@ -211,7 +219,7 @@ class CRM_Event_BAO_Query {
   /**
    * @param $query
    */
-  static function where(&$query) {
+  public static function where(&$query) {
     $grouping = NULL;
     foreach (array_keys($query->_params) as $id) {
       if (empty($query->_params[$id][0])) {
@@ -233,8 +241,10 @@ class CRM_Event_BAO_Query {
    * @param $values
    * @param $query
    */
-  static function whereClauseSingle(&$values, &$query) {
+  public static function whereClauseSingle(&$values, &$query) {
     list($name, $op, $value, $grouping, $wildcard) = $values;
+    $fields = array_merge(CRM_Event_BAO_Event::fields(), CRM_Event_BAO_Participant::exportableFields());
+
     switch ($name) {
       case 'event_start_date_low':
       case 'event_start_date_high':
@@ -250,13 +260,6 @@ class CRM_Event_BAO_Query {
         );
         return;
 
-      case 'event_id':
-        $query->_where[$grouping][] = "civicrm_event.id $op {$value}";
-        $eventTitle = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $value, 'title');
-        $query->_qill[$grouping][] = ts('Event') . " $op {$eventTitle}";
-        $query->_tables['civicrm_event'] = $query->_whereTables['civicrm_event'] = 1;
-        return;
-
       case 'event_include_repeating_events':
         /**
          * Include Repeating Events
@@ -264,7 +267,7 @@ class CRM_Event_BAO_Query {
         //Get parent of this event
         $exEventId = '';
         if ($query->_where[$grouping]) {
-          foreach($query->_where[$grouping] as $key => $val) {
+          foreach ($query->_where[$grouping] as $key => $val) {
             if (strstr($val, 'civicrm_event.id =')) {
               $exEventId = $val;
               $extractEventId = explode(" ", $val);
@@ -280,12 +283,12 @@ class CRM_Event_BAO_Query {
         if ($thisEventHasParent) {
           $getAllConnections = CRM_Core_BAO_RecurringEntity::getEntitiesForParent($thisEventHasParent, 'civicrm_event');
           $allEventIds = array();
-          foreach($getAllConnections as $key => $val) {
+          foreach ($getAllConnections as $key => $val) {
             $allEventIds[] = $val['id'];
           }
           if (!empty($allEventIds)) {
             $op = "IN";
-            $value = "(".implode(",", $allEventIds).")";
+            $value = "(" . implode(",", $allEventIds) . ")";
           }
         }
         $query->_where[$grouping][] = "civicrm_event.id $op {$value}";
@@ -293,14 +296,11 @@ class CRM_Event_BAO_Query {
         $query->_tables['civicrm_event'] = $query->_whereTables['civicrm_event'] = 1;
         return;
 
-      case 'event_type_id':
-
-        $eventTypes = CRM_Core_OptionGroup::values("event_type");
-        $query->_where[$grouping][] = "civicrm_participant.event_id = civicrm_event.id and civicrm_event.event_type_id = '{$value}'";
-        $query->_qill[$grouping][] = ts('Event Type - %1', array(1 => $eventTypes[$value]));
-        $query->_tables['civicrm_event'] = $query->_whereTables['civicrm_event'] = 1;
-        return;
-
+      case 'participant_is_test':
+        $key = array_search('civicrm_participant.is_test = 0', $query->_where[$grouping]);
+        if (!empty($key)) {
+          unset($query->_where[$grouping][$key]);
+        }
       case 'participant_test':
         // We dont want to include all tests for sql OR CRM-7827
         if (!$value || $query->getOperator() != 'OR') {
@@ -326,18 +326,6 @@ class CRM_Event_BAO_Query {
         $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
         return;
 
-      case 'participant_fee_amount':
-        $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.fee_amount",
-          $op,
-          $value,
-          "Money"
-        );
-        if ($value) {
-          $query->_qill[$grouping][] = ts("Fee Amount") . " $op $value";
-        }
-        $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
-        return;
-
       case 'participant_fee_amount_high':
       case 'participant_fee_amount_low':
         $query->numberRangeBuilder($values,
@@ -345,105 +333,37 @@ class CRM_Event_BAO_Query {
         );
         return;
 
-      case 'participant_pay_later':
-        $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.is_pay_later",
-          $op,
-          $value,
-          "Boolean"
-        );
-        $query->_qill[$grouping][] = $value ? ts("Participant is Pay Later") : ts("Participant is not Pay Later");
-        $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
-        return;
-
       case 'participant_status':
       case 'participant_status_id':
-        $val = array();
-        if (is_array($value)) {
-          foreach ($value as $k => $v) {
-            if ($v) {
-              $val[$k] = $k;
-            }
-          }
-          $status = implode(',', $val);
-        }
-        else {
-          $status = $value;
-        }
-
-        if (count($val) > 0) {
-          $op = 'IN';
-          $status = "({$status})";
-        }
-
-        $statusTypes = CRM_Event_PseudoConstant::participantStatus();
-        $names = array();
-
-        if (!empty($val)) {
-          foreach ($val as $id => $dontCare) {
-            $names[] = $statusTypes[$id];
-          }
-        }
-        else {
-          if (!empty($value)) {
-            $names[] = $statusTypes[$value];
-          }
-        }
-
-        $query->_qill[$grouping][] = ts('Participant Status %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names);
-
-        $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.status_id",
-          $op,
-          $status,
-          "Integer"
-        );
-        $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
-        return;
-
       case 'participant_role':
       case 'participant_role_id':
-        $val = array();
-        if (is_array($value)) {
-          foreach ($value as $k => $v) {
-            if ($v) {
-              $val[$k] = $k;
-            }
+      case 'participant_source':
+      case 'participant_id':
+      case 'participant_contact_id':
+      case 'participant_is_pay_later':
+      case 'participant_fee_amount':
+      case 'participant_fee_level':
+        $qillName = $name;
+        if (in_array($name, array('participant_status_id', 'participant_role_id', 'participant_source', 'participant_id', 'participant_contact_id', 'participant_fee_amount', 'participant_fee_level', 'participant_is_pay_later'))) {
+          $name = str_replace('participant_', '', $name);
+          if ($name == 'is_pay_later') {
+            $qillName = $name;
+          }
+          if ($name == 'participant_role_id') {
+            $qillName = str_replace('_id', '', $name);
           }
-        }
-        else {
-          $val = array($value => 1);
         }
 
-        $roleTypes = CRM_Event_PseudoConstant::participantRole();
+        $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String';
 
-        $names = array();
-        foreach ($val as $id => $dontCare) {
-          if (!empty($roleTypes[$id]) ) {
-            $names[] = $roleTypes[$id];
-          }
+        if (in_array($name, array('participant_status', 'participant_role'))) {
+          $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("$name.label", $op, $value, $dataType);
         }
-
-        if (!empty($names)) {
-          $query->_qill[$grouping][] =
-            ts('Participant Role %1', array(1 => $op)) .
-            ' ' .
-            implode(' ' . ts('or') . ' ', $names);
-          $query->_where[$grouping][] =
-            " civicrm_participant.role_id REGEXP '[[:<:]]" .
-            implode('[[:>:]]|[[:<:]]', array_keys($val)) .
-            "[[:>:]]' ";
-
-          $query->_tables['civicrm_participant'] =
-            $query->_whereTables['civicrm_participant'] = 1;
+        else {
+          $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.$name", $op, $value, $dataType);
         }
-        return;
-
-      case 'participant_source':
-        $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.source",
-          $op,
-          $value,
-          "String"
-        );
-        $query->_qill[$grouping][] = ts("Participant Source") . " $op $value";
+        list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Event_DAO_Participant', $name, $value, $op);
+        $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value));
         $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
         return;
 
@@ -453,38 +373,29 @@ class CRM_Event_BAO_Query {
         );
         return;
 
-      case 'participant_id':
-        $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.id", $op, $value, "Integer");
-        $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
-        return;
-
       case 'event_id':
-        $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_event.id",
-          $op,
-          $value,
-          "Integer"
-        );
-        $query->_tables['civicrm_event'] = $query->_whereTables['civicrm_event'] = 1;
-        $title = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $value, "title");
-        $query->_qill[$grouping][] = ts('Event') . " $op $value";
-        return;
+      case 'participant_event_id':
+        $name = str_replace('participant_', '', $name);
+      case 'event_is_public':
+      case 'event_type_id':
 
-      case 'participant_contact_id':
-        $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.contact_id",
-          $op,
-          $value,
-          "Integer"
-        );
-        $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
-        return;
+        $qillName = $name;
+        if (in_array($name, array(
+              'event_id',
+              'event_is_public',
+            )
+          )) {
+          $name = str_replace('event_', '', $name);
+        }
+        $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String';
 
-      case 'event_is_public':
-        $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_event.is_public",
-          $op,
-          $value,
-          "Integer"
-        );
+        $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_event.$name", $op, $value, $dataType);
         $query->_tables['civicrm_event'] = $query->_whereTables['civicrm_event'] = 1;
+        if (!array_key_exists($qillName, $fields)) {
+          break;
+        }
+        list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Event_DAO_Event', $name, $value, $op);
+        $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value));
         return;
 
       case 'participant_campaign_id':
@@ -506,7 +417,7 @@ class CRM_Event_BAO_Query {
    *
    * @return null|string
    */
-  static function from($name, $mode, $side) {
+  public static function from($name, $mode, $side) {
     $from = NULL;
     switch ($name) {
       case 'civicrm_participant':
@@ -546,12 +457,11 @@ class CRM_Event_BAO_Query {
   }
 
   /**
-   * Getter for the qill object
+   * Getter for the qill object.
    *
    * @return string
-   * @access public
    */
-  function qill() {
+  public function qill() {
     return (isset($this->_qill)) ? $this->_qill : "";
   }
 
@@ -561,7 +471,8 @@ class CRM_Event_BAO_Query {
    *
    * @return array|null
    */
-  static function defaultReturnProperties($mode,
+  public  static function defaultReturnProperties(
+    $mode,
     $includeCustomFields = TRUE
   ) {
     $properties = NULL;
@@ -609,7 +520,7 @@ class CRM_Event_BAO_Query {
   /**
    * @param CRM_Core_Form $form
    */
-  static function buildSearchForm(&$form) {
+  public static function buildSearchForm(&$form) {
     $dataURLEventFee = CRM_Utils_System::url('civicrm/ajax/eventFee',
       "reset=1",
       FALSE, NULL, FALSE
@@ -637,17 +548,16 @@ class CRM_Event_BAO_Query {
     CRM_Core_Form_Date::buildDateRange($form, 'event', 1, '_start_date_low', '_end_date_high', ts('From'), FALSE);
     $eventIncludeRepeatingEvents = &$form->addElement('checkbox', "event_include_repeating_events", NULL, ts(' Include Repeating Events (If Any) ? '));
 
-    $status = CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label');
-    foreach ($status as $id => $Name) {
-      $form->_participantStatus = &$form->addElement('checkbox', "participant_status_id[$id]", NULL, $Name);
-    }
+    $form->addSelect('participant_status_id',
+      array('entity' => 'participant', 'label' => ts('Participant Status'), 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
+    );
 
-    foreach (CRM_Event_PseudoConstant::participantRole() as $rId => $rName) {
-      $form->_participantRole = &$form->addElement('checkbox', "participant_role_id[$rId]", NULL, $rName);
-    }
+    $form->addSelect('participant_role_id',
+      array('entity' => 'participant', 'label' => ts('Participant Role'), 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
+    );
 
     $form->addYesNo('participant_test', ts('Participant is a Test?'), TRUE);
-    $form->addYesNo('participant_pay_later', ts('Participant is Pay Later?'), TRUE);
+    $form->addYesNo('participant_is_pay_later', ts('Participant is Pay Later?'), TRUE);
     $form->addElement('text', 'participant_fee_amount_low', ts('From'), array('size' => 8, 'maxlength' => 8));
     $form->addElement('text', 'participant_fee_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8));
 
@@ -681,16 +591,17 @@ class CRM_Event_BAO_Query {
    * @param $row
    * @param int $id
    */
-  static function searchAction(&$row, $id) {}
+  public static function searchAction(&$row, $id) {
+  }
 
   /**
    * @param $tables
    */
-  static function tableNames(&$tables) {
+  public static function tableNames(&$tables) {
     //add participant table
     if (!empty($tables['civicrm_event'])) {
       $tables = array_merge(array('civicrm_participant' => 1), $tables);
     }
   }
-}
 
+}