search fixes part3
authormonishdeb <monish.deb@webaccessglobal.com>
Mon, 27 Jul 2015 13:44:15 +0000 (19:14 +0530)
committermonishdeb <monish.deb@webaccessglobal.com>
Sat, 22 Aug 2015 18:00:12 +0000 (23:30 +0530)
Conflicts:
CRM/Contact/Form/Search/Advanced.php

CRM/Contact/BAO/Query.php
CRM/Contact/Form/Search/Advanced.php
CRM/Contact/Form/Search/Builder.php
CRM/Contact/Form/Search/Criteria.php
CRM/Contact/Selector.php
CRM/Core/BAO/Mapping.php

index c6464638946eb0f090a9eba344ed5a746af50253..6e2150bcd19a340a0e2af19fa125daaa42651fa9 100644 (file)
@@ -370,7 +370,6 @@ class CRM_Contact_BAO_Query {
     'email',
     'im',
     'address_name',
-    'master_id',
     'location_type',
   );
 
@@ -1337,10 +1336,10 @@ class CRM_Contact_BAO_Query {
         // make sure there is only one element
         // this is used when we are running under smog and need to know
         // how the contact was added (CRM-1203)
+        $groups = CRM_Utils_Array::value($this->_paramLookup['group'][0][1], $this->_paramLookup['group'][0][2], $this->_paramLookup['group'][0][2]);
         if ((count($this->_paramLookup['group']) == 1) &&
-          (count($this->_paramLookup['group'][0][2]) == 1)
+          (count($groups) == 1)
         ) {
-          $groups = array_keys($this->_paramLookup['group'][0][2]);
           $groupId = $groups[0];
 
           //check if group is saved search
@@ -1571,38 +1570,6 @@ class CRM_Contact_BAO_Query {
     elseif (is_string($values) && strpos($values, '%') !== FALSE) {
       $result = array($id, 'LIKE', $values, 0, 0);
     }
-    elseif ($id == 'group') {
-      if (is_array($values)) {
-        foreach ($values as $groupIds => $val) {
-          $matches = array();
-          if (preg_match('/-(\d+)$/', $groupIds, $matches)) {
-            if (strlen($matches[1]) > 0) {
-              $values[$matches[1]] = 1;
-              unset($values[$groupIds]);
-            }
-          }
-        }
-      }
-      else {
-        $groupIds = explode(',', $values);
-        unset($values);
-        foreach ($groupIds as $groupId) {
-          $values[$groupId] = 1;
-        }
-      }
-
-      $result = array($id, 'IN', $values, 0, 0);
-    }
-    elseif ($id == 'contact_tags' || $id == 'tag') {
-      if (!is_array($values)) {
-        $tagIds = explode(',', $values);
-        unset($values);
-        foreach ($tagIds as $tagId) {
-          $values[$tagId] = 1;
-        }
-      }
-      $result = array($id, 'IN', $values, 0, 0);
-    }
     elseif ($id == 'contact_type' ||
       (!empty($values) && is_array($values) && !in_array(key($values), CRM_Core_DAO::acceptedSQLOperators(), TRUE))
     ) {
@@ -1997,21 +1964,9 @@ class CRM_Contact_BAO_Query {
         $where = "civicrm_address.state_province_id";
       }
 
-      $states = CRM_Core_PseudoConstant::stateProvince();
-      if (is_numeric($value)) {
-        $this->_where[$grouping][] = self::buildClause($where, $op, $value, 'Positive');
-        $value = $states[(int ) $value];
-      }
-      else {
-        $intVal = CRM_Utils_Array::key($value, $states);
-        $this->_where[$grouping][] = self::buildClause($where, $op, $intVal, 'Positive');
-      }
-      if (!$lType) {
-        $this->_qill[$grouping][] = ts('State') . " $op '$value'";
-      }
-      else {
-        $this->_qill[$grouping][] = ts('State') . " ($lType) $op '$value'";
-      }
+      $this->_where[$grouping][] = self::buildClause($where, $op, $value, 'Positive');
+      list($qillop, $qillVal) = self::buildQillForFieldValue('CRM_Core_DAO_Address', "state_province_id", $value, $op);
+      $this->_qill[$grouping][] = ts("State %1 %2", array(1 => $qillop, 2 => $qillVal));
     }
     elseif (!empty($field['pseudoconstant'])) {
       $this->optionValueQuery(
@@ -2208,7 +2163,7 @@ class CRM_Contact_BAO_Query {
         $this->_where[$grouping][] = CRM_Core_DAO::createSQLFilter($fieldName, $value, $type);
       }
       else {
-        if (strpos($op, 'IN') === FALSE) {
+        if (strpos($op, 'IN') === FALSE)  {
           $value = $strtolower($value);
         }
         if ($wildcard) {
@@ -2260,6 +2215,9 @@ class CRM_Contact_BAO_Query {
         array(
           'address_name',
           'street_address',
+          'street_name',
+          'street_number_suffix',
+          'street_unit',
           'supplemental_address_1',
           'supplemental_address_2',
           'city',
@@ -2267,14 +2225,28 @@ class CRM_Contact_BAO_Query {
           'postal_code_suffix',
           'geo_code_1',
           'geo_code_2',
+          'master_id',
         )
       )) {
         //fix for search by profile with address fields.
         $tName = "{$locationType[$locType[1]]}-address";
       }
-      elseif ($locType[0] == 'on_hold') {
+      elseif (in_array($locType[0],
+          array(
+            'on_hold',
+            'signature_html',
+            'signature_text',
+            'is_bulkmail',
+          )
+        )) {
         $tName = "{$locationType[$locType[1]]}-email";
       }
+      elseif ($locType[0] == 'provider_id') {
+        $tName = "{$locationType[$locType[1]]}-im";
+      }
+      elseif ($locType[0] == 'openid') {
+        $tName = "{$locationType[$locType[1]]}-openid";
+      }
       else {
         $tName = "{$locationType[$locType[1]]}-{$locType[0]}";
       }
@@ -2612,7 +2584,7 @@ class CRM_Contact_BAO_Query {
 
         default:
           $locationTypeName = $extraLocationTypeClause = '';
-          if (strpos($name, '-address') != 0 || strpos($name, '-master_id') != 0) {
+          if (strpos($name, '-address') != 0) {
             $locationTypeName = 'address';
           }
           elseif (strpos($name, '-phone') != 0) {
@@ -2623,24 +2595,24 @@ class CRM_Contact_BAO_Query {
           }
           elseif (strpos($name, '-im') != 0) {
             $locationTypeName = 'im';
-            $extraLocationType = "provider_id";
           }
           elseif (strpos($name, '-openid') != 0) {
-            $extraLocationType = $locationTypeName = 'openid';
+            $locationTypeName = 'openid';
           }
           elseif (strpos($name, '-location_type') != 0) {
-            $locationTypeName = 'address';
-            $extraLocationTypeClause = "\n $side JOIN civicrm_location_type `{$name}` ON ( `{$name}`.id = `{$name}_address`.location_type_id)";
-            $name .= "_address";
+            $extraLocationType = "\n$side JOIN civicrm_location_type `{$name}` ON ( `{$name}`.id = `{$name}-address`.location_type_id )";
+            $locationTypeName = "address";
+            $name .= "-address";
           }
+
           if ($locationTypeName) {
             //we have a join on an location table - possibly in conjunction with search builder - CRM-14263
             $parts = explode('-', $name);
             $locationID = array_search($parts[0], CRM_Core_BAO_Address::buildOptions('location_type_id', 'get', array('name' => $parts[0])));
-            if (!empty($extraLocationType) && !empty($parts[2]) && is_numeric($parts[2])) {
-              $extraLocationTypeClause = " and `{$name}`.{$extraLocationType} = $parts[2]";
+            $from .= " $side JOIN civicrm_{$locationTypeName} `{$name}` ON ( contact_a.id = `{$name}`.contact_id ) and `{$name}`.location_type_id = $locationID ";
+            if (!empty($extraLocationType)) {
+              $from .= $extraLocationType;
             }
-            $from .= " $side JOIN civicrm_{$locationTypeName} `{$name}` ON ( contact_a.id = `{$name}`.contact_id ) and `{$name}`.location_type_id = $locationID $extraLocationTypeClause ";
           }
           else {
             $from .= CRM_Core_Component::from($name, $mode, $side);
@@ -2802,20 +2774,15 @@ class CRM_Contact_BAO_Query {
       $this->_useDistinct = TRUE;
     }
 
+    if (isset($value)) {
+      $value = CRM_Utils_Array::value($op, $value, $value);
+    }
+
     $groupIds = NULL;
     $names = array();
     $isSmart = FALSE;
     $isNotOp = ($op == 'NOT IN' || $op == '!=');
 
-    if ($value) {
-      if (strpos($op, 'IN') === FALSE) {
-        $value = key($value);
-      }
-      else {
-        $value = array_keys($value);
-      }
-    }
-
     $statii = array();
     $gcsValues = &$this->getWhereValues('group_contact_status', $grouping);
     if ($gcsValues &&
@@ -3042,19 +3009,17 @@ WHERE  $smartGroupClause
   public function tag(&$values) {
     list($name, $op, $value, $grouping, $wildcard) = $values;
 
-    $tagNames = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
+    list($qillop, $qillVal) = self::buildQillForFieldValue('CRM_Core_DAO_EntityTag', "tag_id", $value, $op, array('onlyActive' => FALSE));
+    // API/Search Builder format array(operator => array(values))
     if (is_array($value)) {
+      if (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
+        $op = key($value);
+        $value = $value[$op];
+      }
       if (count($value) > 1) {
         $this->_useDistinct = TRUE;
       }
-      foreach ($value as $id => $dontCare) {
-        $names[] = CRM_Utils_Array::value($id, $tagNames);
-      }
-      $names = implode(' ' . ts('or') . ' ', $names);
-      $value = implode(',', array_keys($value));
-    }
-    else {
-      $names = CRM_Utils_Array::value($value, $tagNames);
+      $value = implode(',', (array) $value);
     }
 
     $useAllTagTypes = $this->getWhereValues('all_tag_types', $grouping);
@@ -3094,7 +3059,6 @@ WHERE  $smartGroupClause
       else {
         $this->_where[$grouping][] = "({$etTable}.tag_id $op (" . $value . ") OR {$etCaseTable}.tag_id $op (" . $value . ") OR {$etActTable}.tag_id $op (" . $value . "))";
       }
-      $this->_qill[$grouping][] = ts('Tag %1 %2', array(1 => $op, 2 => $tagTypesText[2])) . ' ' . $names;
     }
     else {
       $this->_tables[$etTable] = $this->_whereTables[$etTable]
@@ -3109,9 +3073,8 @@ WHERE  $smartGroupClause
       else {
         $this->_where[$grouping][] = "{$etTable}.tag_id $op (" . $value . ')';
       }
-      $this->_qill[$grouping][] = ts('Tagged %1', array(1 => $op)) . ' ' . $names;
     }
-
+    $this->_qill[$grouping][] = ts('Tagged %1 %2', array(1 => $qillop, 2 => $qillVal));
   }
 
   /**
@@ -3939,7 +3902,13 @@ WHERE  $smartGroupClause
   public function preferredCommunication(&$values) {
     list($name, $op, $value, $grouping, $wildcard) = $values;
 
-    $value = str_replace(array('(', ')'), '', explode(",", $value));
+    if (!is_array($values)) {
+      $value = str_replace(array('(', ')'), '', explode(",", $value));
+    }
+    elseif (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
+      $op = key($value);
+      $value = $value[$op];
+    }
     list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contact_DAO_Contact', $name, $value, $op);
 
     if (self::caseImportant($op)) {
index db0b74df2ad7fbed897e90bc0179eed682efb4ea..68bf7d8ac29f1fc2c2452dc63824bf543fe36b7f 100644 (file)
@@ -348,22 +348,6 @@ class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search {
     }
 
     $config = CRM_Core_Config::singleton();
-    $group = CRM_Utils_Array::value('group', $this->_formValues);
-    if ($group && is_array($group)) {
-      unset($this->_formValues['group']);
-      foreach ($group as $key => $value) {
-        $this->_formValues['group'][$value] = 1;
-      }
-    }
-
-    $tag = CRM_Utils_Array::value('contact_tags', $this->_formValues);
-    if ($tag && is_array($tag)) {
-      unset($this->_formValues['contact_tags']);
-      foreach ($tag as $key => $value) {
-        $this->_formValues['contact_tags'][$value] = 1;
-      }
-    }
-
     $specialParams = array(
       'financial_type_id',
       'contribution_soft_credit_type_id',
@@ -380,6 +364,9 @@ class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search {
       'contribution_page_id',
       'contribution_product_id',
       'payment_instrument_id',
+      'group',
+      'contact_tags',
+      'preferred_communication_method',
     );
     foreach ($specialParams as $element) {
       $value = CRM_Utils_Array::value($element, $this->_formValues);
@@ -404,11 +391,7 @@ class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search {
       foreach ($taglist as $value) {
         if ($value) {
           $value = explode(',', $value);
-          foreach ($value as $tId) {
-            if (is_numeric($tId)) {
-              $this->_formValues['contact_tags'][$tId] = 1;
-            }
-          }
+          $this->_formValues['contact_tags'] = (array) $value;
         }
       }
     }
index d1b6c9b6ae334bffcd81efcffbb64f10510c763f..59c52cbde7a18ae7ed131689b662dfb1e37d754d 100644 (file)
@@ -163,31 +163,6 @@ class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search {
         ) {
           $errorMsg["value[$v[3]][$v[4]]"] = ts('Please clear your value if you want to use %1 operator.', array(1 => $v[1]));
         }
-        elseif (($v[0] == 'group' || $v[0] == 'tag') && !empty($v[2])) {
-          $grpId = array_keys($v[2]);
-          if (!key($v[2])) {
-            $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value.");
-          }
-
-          if (count($grpId) > 1) {
-            if ($v[1] != 'IN' && $v[1] != 'NOT IN') {
-              $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a valid value.");
-            }
-            foreach ($grpId as $val) {
-              $error = CRM_Utils_Type::validate($val, 'Integer', FALSE);
-              if ($error != $val) {
-                $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a valid value.");
-                break;
-              }
-            }
-          }
-          else {
-            $error = CRM_Utils_Type::validate($grpId[0], 'Integer', FALSE);
-            if ($error != $grpId[0]) {
-              $errorMsg["value[$v[3]][$v[4]]"] = ts('Please enter valid %1 id.', array(1 => $v[0]));
-            }
-          }
-        }
         elseif (substr($v[0], 0, 7) === 'do_not_' or substr($v[0], 0, 3) === 'is_') {
           if (isset($v[2])) {
             $v2 = array($v[2]);
index 91e8e90065619b6d499b51a9beaec86bda5f2bf7..543e545bbe04156c8552381469474e0290ac7131 100644 (file)
@@ -254,17 +254,12 @@ class CRM_Contact_Form_Search_Criteria {
     $form->addRadio('privacy_toggle', ts('Privacy Options'), $options, array('allowClear' => FALSE));
 
     // preferred communication method
-    $comm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
-
-    $commPreff = array();
-    foreach ($comm as $k => $v) {
-      $commPreff[] = $form->createElement('advcheckbox', $k, NULL, $v);
-    }
 
     $onHold[] = $form->createElement('advcheckbox', 'on_hold', NULL, '');
     $form->addGroup($onHold, 'email_on_hold', ts('Email On Hold'));
 
-    $form->addGroup($commPreff, 'preferred_communication_method', ts('Preferred Communication Method'));
+    $form->addSelect('preferred_communication_method',
+      array('entity' => 'contact', 'multiple' => 'multiple', 'label' => ts('Preferred Communication Method'), 'option_url' => NULL, 'placeholder' => ts('- any -')));
 
     //CRM-6138 Preferred Language
     $form->addSelect('preferred_language', array('class' => 'twenty', 'context' => 'search'));
index 7bd1562f29f893dba8007a1fe748bc1e1b75cf94..3e9c65a0d92cdda1d309e679af93ee8cbcc90374 100644 (file)
@@ -636,7 +636,7 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se
     // for CRM-3157 purposes
     if (in_array('world_region', $names)) {
       $pseudoconstants['world_region'] = array(
-        'dbName' => 'world_region_id',
+        'dbName' => 'worldregion_id',
         'values' => CRM_Core_PseudoConstant::worldRegion(),
       );
     }
index 36d0912313c882cb1da274e59397ac021977f9f0..68673562b9ad953fc040ce6b3aaf68e6e1a0b5a4 100644 (file)
@@ -1022,15 +1022,6 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping {
           }
 
           $value = $params['value'][$key][$k];
-          if ($fldName == 'group' || $fldName == 'tag') {
-            $value = trim($value);
-
-            $v = explode(',', $value);
-            $value = array();
-            foreach ($v as $i) {
-              $value[$i] = 1;
-            }
-          }
 
           if ($v[0] == 'Contribution' && substr($fldName, 0, 7) != 'custom_'
             && substr($fldName, 0, 10) != 'financial_'