CRM-13966 - Improve event selection in reports
authorColeman Watts <coleman@civicrm.org>
Thu, 28 Aug 2014 20:50:38 +0000 (21:50 +0100)
committerColeman Watts <coleman@civicrm.org>
Thu, 28 Aug 2014 21:18:08 +0000 (22:18 +0100)
CRM/Report/Form.php
CRM/Report/Form/Event.php
CRM/Report/Form/Event/Income.php
CRM/Report/Form/Event/IncomeCountSummary.php
CRM/Report/Form/Event/ParticipantListCount.php
CRM/Report/Form/Event/ParticipantListing.php
CRM/Report/Form/Event/Summary.php

index d5d62e655374a3f1840d1efc0627be525e78c7cb..38976d9353ddc1ac7200d861aaf4139b25826644 100644 (file)
@@ -47,7 +47,8 @@ class CRM_Report_Form extends CRM_Core_Form {
     OP_SELECT = 64,
     OP_MULTISELECT = 65,
     OP_MULTISELECT_SEPARATOR = 66,
-    OP_MONTH = 128;
+    OP_MONTH = 128,
+    OP_ENTITYREF = 256;
 
   /**
    * The id of the report instance
@@ -698,6 +699,9 @@ class CRM_Report_Form extends CRM_Core_Form {
           if (CRM_Utils_Array::value('operatorType', $field) == CRM_Report_Form::OP_MULTISELECT) {
             $this->_defaults["{$fieldName}_op"] = 'in';
           }
+          if (CRM_Utils_Array::value('operatorType', $field) == CRM_Report_Form::OP_ENTITYREF) {
+            $this->_defaults["{$fieldName}_op"] = 'in';
+          }
           elseif (CRM_Utils_Array::value('operatorType', $field) == CRM_Report_Form::OP_MULTISELECT_SEPARATOR) {
             $this->_defaults["{$fieldName}_op"] = 'mhas';
           }
@@ -927,7 +931,7 @@ class CRM_Report_Form extends CRM_Core_Form {
               else {
                 $this->addElement('select', "{$fieldName}_value", NULL, $field['options'], array(
                   'style' => 'min-width:250px',
-                  'class' => 'crm-select2',
+                  'class' => 'crm-select2 huge',
                   'multiple' => TRUE,
                   'placeholder' => ts('- select -'),
                 ));
@@ -942,6 +946,12 @@ class CRM_Report_Form extends CRM_Core_Form {
               $this->addElement('select', "{$fieldName}_value", NULL, $field['options']);
             break;
 
+          case CRM_Report_Form::OP_ENTITYREF:
+            $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations);
+            $this->setEntityRefDefaults($field, $table);
+            $this->addEntityRef("{$fieldName}_value", NULL, $field['attributes']);
+            break;
+
           case CRM_Report_Form::OP_DATE:
             // build datetime fields
             CRM_Core_Form_Date::buildDateRange($this, $fieldName, $count, '_from','_to', 'From:', FALSE, $operations);
@@ -966,7 +976,7 @@ class CRM_Report_Form extends CRM_Core_Form {
               array('onchange' => "return showHideMaxMinVal( '$fieldName', this.value );")
             );
             // we need text box for value input
-            $this->add('text', "{$fieldName}_value", NULL);
+            $this->add('text', "{$fieldName}_value", NULL, array('class' => 'huge'));
             break;
         }
       }
@@ -1212,6 +1222,7 @@ class CRM_Report_Form extends CRM_Core_Form {
 
       case CRM_Report_Form::OP_MONTH:
       case CRM_Report_Form::OP_MULTISELECT:
+      case CRM_Report_Form::OP_ENTITYREF:
         return array(
           'in' => ts('Is one of'),
           'notin' => ts('Is not one of'),
@@ -1407,6 +1418,9 @@ class CRM_Report_Form extends CRM_Core_Form {
 
       case 'in':
       case 'notin':
+        if (is_string($value) && strlen($value)) {
+          $value = explode(',', $value);
+        }
         if ($value !== NULL && is_array($value) && count($value) > 0) {
           $sqlOP = $this->getSQLOperator($op);
           if (CRM_Utils_Array::value('type', $field) == CRM_Utils_Type::T_STRING) {
@@ -2637,9 +2651,17 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND
               $min = CRM_Utils_Array::value("{$fieldName}_min", $this->_params);
               $max = CRM_Utils_Array::value("{$fieldName}_max", $this->_params);
               $val = CRM_Utils_Array::value("{$fieldName}_value", $this->_params);
-              if (in_array($op, array(
-                    'bw', 'nbw')) && ($min || $max)) {
-                $value = "{$pair[$op]} " . $min . ' and ' . $max;
+              if (in_array($op, array('bw', 'nbw')) && ($min || $max)) {
+                $value = "{$pair[$op]} $min " . ts('and') . " $max";
+              }
+              elseif ($val && CRM_Utils_Array::value('operatorType', $field) & self::OP_ENTITYREF) {
+                $this->setEntityRefDefaults($field, $tableName);
+                $result = civicrm_api3($field['attributes']['entity'], 'getlist', array('id' => $val) + CRM_Utils_Array::value('api', $field['attributes'], array()));
+                $values = array();
+                foreach ($result['values'] as $v) {
+                  $values[] = $v['label'];
+                }
+                $value = "{$pair[$op]} " . implode(', ', $values);
               }
               elseif ($op == 'nll' || $op == 'nnll') {
                 $value = $pair[$op];
@@ -3795,4 +3817,18 @@ LEFT JOIN civicrm_contact {$field['alias']} ON {$field['alias']}.id = {$this->_a
       CRM_Utils_System::civiExit();
     }
   }
+
+  /**
+   * Apply common settings to entityRef fields
+   * @param array $field
+   * @param string $table
+   */
+  private function setEntityRefDefaults(&$field, $table) {
+    $field['attributes'] = $field['attributes'] ? $field['attributes'] : array();
+    $field['attributes'] += array(
+      'entity' => CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($table)),
+      'multiple' => TRUE,
+      'placeholder' => ts('- select -'),
+    );
+  }
 }
index 76dc448bf6d13d64ccdabc74c54969f612ed6e0e..87ea62873ef9138b6190ad5276381ec62cf68bfd 100644 (file)
  *
  */
 class CRM_Report_Form_Event extends CRM_Report_Form {
-  /**
-   * Get a standardized array of <select> options for "Event Title"
-   * filter values.
-   * @return Array
-   */
-  function getEventFilterOptions() {
-    $events = array();
-    $query = "
-        select id, start_date, title from civicrm_event
-        where (is_template IS NULL OR is_template = 0) AND is_active
-        order by title ASC, start_date
-    ";
-    $dao = CRM_Core_DAO::executeQuery($query);
-    while($dao->fetch()) {
-       $events[$dao->id] = "{$dao->title} - " . CRM_Utils_Date::customFormat(substr($dao->start_date, 0, 10)) . " (ID {$dao->id})";
-    }
-    return $events;
-  }
+  // Nothing here.
+  // FIXME: Do these reports really have nothing in common? Really?
 }
 
index b22e4d8726feaf9c5cd681e231108588f067b73f..664a87766ba311bf88d4b0eb52bb363bab910aca 100644 (file)
@@ -50,16 +50,14 @@ class CRM_Report_Form_Event_Income extends CRM_Report_Form_Event {
   function __construct() {
 
     $this->_columns = array(
-      'civicrm_event' =>
-      array(
+      'civicrm_event' => array(
         'dao' => 'CRM_Event_DAO_Event',
-        'filters' =>
-        array(
-          'id' =>
-          array('title' => ts('Event Title'),
-            'operatorType' => CRM_Report_Form::OP_MULTISELECT,
+        'filters' => array(
+          'id' => array(
+            'title' => ts('Event'),
+            'operatorType' => CRM_Report_Form::OP_ENTITYREF,
             'type' => CRM_Utils_Type::T_INT,
-            'options' => $this->getEventFilterOptions(),
+            'attributes' => array('select' => array('minimumInputLength' => 0)),
           ),
         ),
       ),
@@ -321,7 +319,7 @@ class CRM_Report_Form_Event_Income extends CRM_Report_Form_Event {
     $this->_setVariable = TRUE;
 
     $noSelection = FALSE;
-    if (empty($this->_params['id_value'][0])) {
+    if (empty($this->_params['id_value'])) {
       $this->_params['id_value'] = array();
       $this->_setVariable = FALSE;
 
@@ -336,6 +334,9 @@ class CRM_Report_Form_Event_Income extends CRM_Report_Form_Event {
       }
       $noSelection = TRUE;
     }
+    else {
+      $this->_params['id_value'] = explode(',', $this->_params['id_value']);
+    }
 
     $this->_rowsFound = count($this->_params['id_value']);
 
index 5a04c1c7b0ce5a2a405da39aeb1b765d6be35f15..cd3ba9a4055b131ed0565daa84ea27f5aab98e13 100644 (file)
@@ -83,9 +83,11 @@ class CRM_Report_Form_Event_IncomeCountSummary extends CRM_Report_Form_Event {
         ),
         'filters' =>
         array(
-          'id' => array('title' => ts('Event Title'),
-            'operatorType' => CRM_Report_Form::OP_MULTISELECT,
-            'options' => $this->getEventFilterOptions(),
+          'id' => array(
+            'title' => ts('Event'),
+            'operatorType' => CRM_Report_Form::OP_ENTITYREF,
+            'type' => CRM_Utils_Type::T_INT,
+            'attributes' => array('select' => array('minimumInputLength' => 0)),
           ),
           'event_type_id' => array(
             'name' => 'event_type_id',
@@ -237,8 +239,7 @@ class CRM_Report_Form_Event_IncomeCountSummary extends CRM_Report_Form_Event {
             }
           }
           if (!empty($this->_params['id_value'])) {
-            $participant = implode(', ', $this->_params['id_value']);
-            $this->_participantWhere = " AND civicrm_participant.event_id IN ( {$participant} ) ";
+            $this->_participantWhere = " AND civicrm_participant.event_id IN ( {$this->_params['id_value']} ) ";
           }
 
           if (!empty($clause)) {
index c837659f7698cdf7369ed51da873ac7ed3b60cd1..37c0bc737e02d7bed6101cc8d0c90705c066818a 100644 (file)
@@ -168,12 +168,12 @@ class CRM_Report_Form_Event_ParticipantListCount extends CRM_Report_Form_Event {
         'grouping' => 'event-fields',
         'filters' =>
         array(
-          'event_id' =>
-          array(
+          'event_id' => array(
             'name' => 'event_id',
             'title' => ts('Event'),
-            'operatorType' => CRM_Report_Form::OP_MULTISELECT,
-            'options' => $this->getEventFilterOptions(),
+            'operatorType' => CRM_Report_Form::OP_ENTITYREF,
+            'type' => CRM_Utils_Type::T_INT,
+            'attributes' => array('entity' => 'event', 'select' => array('minimumInputLength' => 0)),
           ),
           'sid' =>
           array(
index e8f4caa6d17f4cf72edd93a97acc3841c577480b..db21cd93351a30335e3bc5154c2976958ecc9a13 100644 (file)
@@ -214,10 +214,12 @@ class CRM_Report_Form_Event_ParticipantListing extends CRM_Report_Form_Event {
         'grouping' => 'event-fields',
         'filters' =>
         array(
-          'event_id' => array('name' => 'event_id',
-                      'title' => ts('Event'),
-                      'operatorType' => CRM_Report_Form::OP_MULTISELECT,
-                      'options' => $this->getEventFilterOptions(),
+          'event_id' => array(
+            'name' => 'event_id',
+            'title' => ts('Event'),
+            'operatorType' => CRM_Report_Form::OP_ENTITYREF,
+            'type' => CRM_Utils_Type::T_INT,
+            'attributes' => array('entity' => 'event', 'select' => array('minimumInputLength' => 0)),
           ),
           'sid' => array(
             'name' => 'status_id',
index e93de00af50ac11f808eb4e186f49e05df68c09d..2c94619bfbaaca6247931ce0ddb27712366363a4 100644 (file)
@@ -83,9 +83,11 @@ class CRM_Report_Form_Event_Summary extends CRM_Report_Form_Event {
         ),
         'filters' =>
         array(
-          'id' => array('title' => ts('Event Title'),
-            'operatorType' => CRM_Report_Form::OP_MULTISELECT,
-            'options' => $this->getEventFilterOptions(),
+          'id' => array(
+            'title' => ts('Event'),
+            'operatorType' => CRM_Report_Form::OP_ENTITYREF,
+            'type' => CRM_Utils_Type::T_INT,
+            'attributes' => array('select' => array('minimumInputLength' => 0)),
           ),
           'event_type_id' => array(
             'name' => 'event_type_id',
@@ -159,8 +161,7 @@ class CRM_Report_Form_Event_Summary extends CRM_Report_Form_Event {
             }
           }
           if (!empty($this->_params['id_value'])) {
-            $participant = implode(', ', $this->_params['id_value']);
-            $this->_participantWhere = " AND civicrm_participant.event_id IN ( {$participant} ) ";
+            $this->_participantWhere = " AND civicrm_participant.event_id IN ( {$this->_params['id_value']} ) ";
           }
 
           if (!empty($clause)) {