implemnted patch and fixed bulk email name change possibility
[civicrm-core.git] / CRM / Activity / BAO / Query.php
index 75ba22c180887e63b6d42cca18ff208a57328aaa..24ff055b14fb40ceacc48cc16c6ae2410505116a 100644 (file)
@@ -2,9 +2,9 @@
 
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -29,7 +29,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -188,32 +188,18 @@ class CRM_Activity_BAO_Query {
 
     switch ($name) {
       case 'activity_type_id':
+      case 'activity_type':
         $types = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
-
         //get the component activity types.
         $compActTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE, TRUE);
-
-        $clause = array();
-        if (is_array($value)) {
-          foreach ($value as $id => $dontCare) {
-            if (array_key_exists($id, $types) && $dontCare) {
-              $clause[] = "'" . CRM_Utils_Type::escape($types[$id], 'String') . "'";
-              if (array_key_exists($id, $compActTypes)) {
-                CRM_Contact_BAO_Query::$_considerCompActivities = TRUE;
-              }
-            }
-          }
-          $activityTypes = implode(',', array_keys($value));
-        }
-        else {
-          $clause[] = "'" . CRM_Utils_Type::escape($value, 'String') . "'";
-          $activityTypes = $value;
-          if (array_key_exists($value, $compActTypes)) {
+        $activityTypeIds = self::buildWhereAndQill($query, $value, $types, $op, $grouping, array('Activity Type', 'civicrm_activity.activity_type_id'));
+        
+        foreach ($activityTypeIds as $activityTypeId) {
+          if (array_key_exists($activityTypeId, $compActTypes)) {
             CRM_Contact_BAO_Query::$_considerCompActivities = TRUE;
+            break;
           }
         }
-        $query->_where[$grouping][] = ' civicrm_activity.activity_type_id IN (' . $activityTypes . ')';
-        $query->_qill[$grouping][] = ts('Activity Type') . ' ' . implode(' ' . ts('or') . ' ', $clause);
         break;
 
       case 'activity_survey_id':
@@ -261,19 +247,8 @@ class CRM_Activity_BAO_Query {
 
       case 'activity_status':
         $status = CRM_Core_PseudoConstant::activityStatus();
-        $clause = array();
-        if (is_array($value)) {
-          foreach ($value as $k => $v) {
-            if ($k) {
-              $clause[] = "'" . CRM_Utils_Type::escape($status[$k], 'String') . "'";
-            }
-          }
-        }
-        else {
-          $clause[] = "'" . CRM_Utils_Type::escape($value, 'String') . "'";
-        }
-        $query->_where[$grouping][] = ' civicrm_activity.status_id IN (' . implode(',', array_keys($value)) . ')';
-        $query->_qill[$grouping][] = ts('Activity Status') . ' - ' . implode(' ' . ts('or') . ' ', $clause);
+        
+        $activityTypeIds = self::buildWhereAndQill($query, $value, $status, $op, $grouping, array('Activity Status', 'civicrm_activity.status_id'));
         break;
 
       case 'activity_subject':
@@ -445,6 +420,7 @@ class CRM_Activity_BAO_Query {
    *
    * @access public
    *
+   * @param $form CRM_Core_Form
    * @return void
    * @static
    */
@@ -469,13 +445,13 @@ class CRM_Activity_BAO_Query {
       2 => ts('Assigned to'),
       1 => ts('Added by'),
     );
-    $form->addRadio('activity_role', NULL, $activityRoles);
+    $form->addRadio('activity_role', NULL, $activityRoles, array('allowClear' => TRUE));
     $form->setDefaults(array('activity_role' => 3));
     $activityStatus = CRM_Core_PseudoConstant::activityStatus();
     foreach ($activityStatus as $activityStatusID => $activityStatusName) {
       $activity_status[] = $form->createElement('checkbox', $activityStatusID, NULL, $activityStatusName);
     }
-    $form->addGroup($activity_status, 'activity_status', ts('Activity Status'));
+    $form->addGroup($activity_status, 'activity_status', ts('Activity Status'), TRUE);
     $form->setDefaults(array('activity_status[1]' => 1, 'activity_status[2]' => 1));
     $form->addElement('text', 'activity_subject', ts('Subject'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
     $form->addYesNo('activity_test', ts('Activity is a Test?'));
@@ -493,8 +469,8 @@ class CRM_Activity_BAO_Query {
 
     $surveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, FALSE, FALSE, TRUE);
     if ($surveys) $form->add('select', 'activity_survey_id', ts('Survey / Petition'),
-      array(
-        '' => ts('- none -')) + $surveys, FALSE
+      array('' => ts('- none -')) + $surveys, FALSE,
+      array('class' => 'crm-select2')
     );
 
     $extends = array('Activity');
@@ -519,10 +495,7 @@ class CRM_Activity_BAO_Query {
       CRM_Campaign_BAO_Campaign::accessCampaign()
     ) {
       $buildEngagementLevel = TRUE;
-      $form->add('select', 'activity_engagement_level',
-        ts('Engagement Index'),
-        array('' => ts('- any -')) + CRM_Campaign_PseudoConstant::engagementLevel()
-      );
+      $form->addSelect('activity_engagement_level', array('entity' => 'activity', 'option_url' => NULL));
 
       // Add survey result field.
       $optionGroups  = CRM_Campaign_BAO_Survey::getResultSets( 'name' );
@@ -545,7 +518,7 @@ class CRM_Activity_BAO_Query {
         asort($resultOptions);
         $form->add('select', 'activity_result', ts("Survey Result"),
           $resultOptions, FALSE,
-          array('id' => 'activity_result', 'multiple' => 'multiple', 'title' => ts('- select -'))
+          array('id' => 'activity_result', 'multiple' => 'multiple', 'class' => 'crm-select2')
         );
       }
     }
@@ -592,5 +565,39 @@ class CRM_Activity_BAO_Query {
 
     return $properties;
   }
+  
+  static function buildWhereAndQill(&$query, $value, $pseudoconstantType, $op, $grouping, $params) {
+    $matches = $val = $clause = array();
+    
+    if (is_array($value)) {
+      foreach ($value as $k => $v) {
+        if ($k) {
+          $val[] = $k;
+        }
+      }
+      
+      if (count($val) > 0) {
+        // Overwrite $op so it works with an IN where statement.
+        $op = 'IN';
+      }
+      else {
+        // If we somehow have an empty array, just return
+        return;
+      }
+      $value = $matches[0] = $val;
+    }
+    else {
+      preg_match_all('/\d+/', $value, $matches);
+    }
+    foreach ($matches[0] as $qill) {
+      $clause[] = CRM_Utils_Array::value($qill, $pseudoconstantType);
+    }
+    
+    $query->_qill[$grouping][] = ts($params[0] . ' %1 ', array(1 => $op)) . implode(' ' . ts('or') . ' ', $clause);
+    $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($params[1],
+      $op, $value, "Integer"
+    );
+    return $matches[0];
+  }
 }