From 1f37c7789c0c959983c003cbad37370f07c5d486 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@enhancesoft.com>
Date: Tue, 20 Mar 2018 04:12:16 +0000
Subject: [PATCH] Advanced Search: Bug fixes

---
 include/ajax.search.php  |  2 +-
 include/class.forms.php  | 10 +++++++---
 include/class.queue.php  |  8 +++++++-
 include/class.search.php | 11 +++++++++++
 4 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/include/ajax.search.php b/include/ajax.search.php
index cc6f1fbd6..70c0a71ac 100644
--- a/include/ajax.search.php
+++ b/include/ajax.search.php
@@ -177,7 +177,7 @@ class SearchAjaxAPI extends AjaxController {
         $form = $search->getForm($_POST);
         $errors = array();
         if (!$search->update($_POST, $errors)
-            || !$search->save()
+            || !$search->save(true)
         ) {
             $this->_tryAgain($search, $form, $errors);
             return false;
diff --git a/include/class.forms.php b/include/class.forms.php
index c7e20d362..1aec98b1f 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -1890,7 +1890,9 @@ class ChoiceField extends FormField {
 
     function getSearchQ($method, $value, $name=false) {
         $name = $name ?: $this->get('name');
-        $val = '"?'.implode('("|,|$)|"?', array_keys($value)).'("|,|$)';
+        $val = $value;
+        if ($value && is_array($value))
+            $val = '"?'.implode('("|,|$)|"?', array_keys($value)).'("|,|$)';
         switch ($method) {
         case '!includes':
             return Q::not(array("{$name}__regex" => $val));
@@ -1905,7 +1907,7 @@ class ChoiceField extends FormField {
         switch ($method) {
         case 'includes':
             return __('%s includes %s' /* includes -> if a list includes a selection */);
-        case 'includes':
+        case '!includes':
             return __('%s does not include %s' /* includes -> if a list includes a selection */);
         default:
             return parent::describeSearchMethod($method);
@@ -2013,7 +2015,9 @@ class DatetimeField extends FormField {
     }
 
     function from_query($row, $name=false) {
-        return strtotime(parent::from_query($row, $name));
+        $value = parent::from_query($row, $name);
+        $timestamp = is_int($value) ? $value : (int) strtotime($value);
+        return ($timestamp > 0) ? $timestamp : '';
     }
 
     function format($timestamp, $timezone=false) {
diff --git a/include/class.queue.php b/include/class.queue.php
index 502e3fbb3..a6521bc10 100644
--- a/include/class.queue.php
+++ b/include/class.queue.php
@@ -1001,7 +1001,9 @@ class CustomQueue extends VerySimpleModel {
         elseif (($q=CustomQueue::lookup(array(
                         'title' => $vars['name'],
                         'parent_id' => $vars['parent_id'] ?: 0,
-                        'staff_id'  => $this->staff_id))))
+                        'staff_id'  => $this->staff_id)))
+                && $q->getId() != $this->id
+                )
             $errors['name'] = __('Saved queue with same name exists');
 
         $this->title = $vars['name'];
@@ -1155,6 +1157,8 @@ class CustomQueue extends VerySimpleModel {
                 'criteria' => $this->isolateCriteria($form->getClean()),
                 'conditions' => $conditions,
             ]);
+            // Clear currently set criteria.and conditions.
+             $this->criteria = $this->_conditions = null;
         }
 
         return 0 === count($errors);
@@ -1943,6 +1947,8 @@ extends VerySimpleModel {
         ) {
             return new LazyDisplayWrapper($field, $T);
         }
+
+         return new LazyDisplayWrapper($field, '');
     }
 
     function applyTruncate($text, $row) {
diff --git a/include/class.search.php b/include/class.search.php
index e454da06a..d305ab16f 100644
--- a/include/class.search.php
+++ b/include/class.search.php
@@ -909,6 +909,17 @@ class AgentSelectionField extends ChoiceField {
         return Staff::getStaffMembers();
     }
 
+    function toString($value) {
+        $choices =  $this->getChoices();
+        $selection = array();
+        foreach ($value as $k => $v)
+            if (isset($choices[$k]))
+                $selection[] = $choices[$k];
+
+        return $selection ?  implode(',', $selection) :
+            parent::toString($value);
+    }
+
     function applyOrderBy($query, $reverse=false, $name=false) {
         global $cfg;
 
-- 
GitLab