From a621a9d7ecf334c4894ef8f5168eb6208e5ae0e4 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Wed, 14 Mar 2012 17:33:05 -0400
Subject: [PATCH] Accept DateTime object as input to format_date()

---
 program/steps/addressbook/search.inc |   99 ++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 88 insertions(+), 11 deletions(-)

diff --git a/program/steps/addressbook/search.inc b/program/steps/addressbook/search.inc
index 352556d..6422bc7 100644
--- a/program/steps/addressbook/search.inc
+++ b/program/steps/addressbook/search.inc
@@ -7,7 +7,10 @@
  | This file is part of the Roundcube Webmail client                     |
  | Copyright (C) 2005-2011, The Roundcube Dev Team                       |
  | Copyright (C) 2011, Kolab Systems AG                                  |
- | Licensed under the GNU GPL                                            |
+ |                                                                       |
+ | Licensed under the GNU General Public License version 3 or            |
+ | any later version with exceptions for skins & plugins.                |
+ | See the README file for a full license statement.                     |
  |                                                                       |
  | PURPOSE:                                                              |
  |   Search action (and form) for address book contacts                  |
@@ -21,6 +24,63 @@
 
 */
 
+if ($RCMAIL->action == 'search-create') {
+    $id   = get_input_value('_search', RCUBE_INPUT_POST);
+    $name = get_input_value('_name', RCUBE_INPUT_POST, true);
+
+    if (($params = $_SESSION['search_params']) && $params['id'] == $id) {
+
+        $data = array(
+            'type' => rcube_user::SEARCH_ADDRESSBOOK,
+            'name' => $name,
+            'data' => array(
+                'fields' => $params['data'][0],
+                'search' => $params['data'][1],
+            ),
+        );
+
+        $plugin = $RCMAIL->plugins->exec_hook('saved_search_create', array('data' => $data));
+
+        if (!$plugin['abort'])
+            $result = $RCMAIL->user->insert_search($plugin['data']);
+        else
+            $result = $plugin['result'];
+    }
+
+    if ($result) {
+        $OUTPUT->show_message('savedsearchcreated', 'confirmation');
+        $OUTPUT->command('insert_saved_search', Q($name), Q($result));
+    }
+    else
+        $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'savedsearchcreateerror', 'error');
+
+    $OUTPUT->send();
+}
+
+if ($RCMAIL->action == 'search-delete') {
+    $id = get_input_value('_sid', RCUBE_INPUT_POST);
+
+    $plugin = $RCMAIL->plugins->exec_hook('saved_search_delete', array('id' => $id));
+
+    if (!$plugin['abort'])
+        $result = $RCMAIL->user->delete_search($id);
+    else
+        $result = $plugin['result'];
+
+    if ($result) {
+        $OUTPUT->show_message('savedsearchdeleted', 'confirmation');
+        $OUTPUT->command('remove_search_item', Q($id));
+        // contact list will be cleared, clear also page counter
+        $OUTPUT->command('set_rowcount', rcube_label('nocontactsfound'));
+        $OUTPUT->set_env('pagecount', 0);
+    }
+    else
+        $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'savedsearchdeleteerror', 'error');
+
+    $OUTPUT->send();
+}
+
+
 if (!isset($_GET['_form'])) {
     rcmail_contact_search();
 }
@@ -31,12 +91,18 @@
 
 function rcmail_contact_search()
 {
-    global $RCMAIL, $OUTPUT, $CONFIG, $SEARCH_MODS_DEFAULT;
+    global $RCMAIL, $OUTPUT, $SEARCH_MODS_DEFAULT, $PAGE_SIZE;
 
     $adv = isset($_POST['_adv']);
+    $sid = get_input_value('_sid', RCUBE_INPUT_GET);
 
+    // get search criteria from saved search
+    if ($sid && ($search = $RCMAIL->user->get_search($sid))) {
+        $fields = $search['data']['fields'];
+        $search = $search['data']['search'];
+    }
     // get fields/values from advanced search form
-    if ($adv) {
+    else if ($adv) {
         foreach (array_keys($_POST) as $key) {
             $s = trim(get_input_value($key, RCUBE_INPUT_POST, true));
             if (strlen($s) && preg_match('/^_search_([a-zA-Z0-9_-]+)$/', $key, $m)) {
@@ -74,10 +140,14 @@
         }
     }
 
+    // Values matching mode
+    $mode = (int) $RCMAIL->config->get('addressbook_search_mode');
+
     // get sources list
     $sources    = $RCMAIL->get_address_sources();
     $search_set = array();
     $records    = array();
+    $sort_col   = $RCMAIL->config->get('addressbook_sort_col', 'name');
 
     foreach ($sources as $s) {
         $source = $RCMAIL->get_address_book($s['id']);
@@ -105,18 +175,18 @@
         $source->set_pagesize(9999);
 
         // get contacts count
-        $result = $source->search($fields, $search, false, false);
+        $result = $source->search($fields, $search, $mode, false);
 
         if (!$result->count) {
             continue;
         }
 
         // get records
-        $result = $source->list_records(array('name', 'email'));
+        $result = $source->list_records(array('name', 'firstname', 'surname', 'email'));
 
         while ($row = $result->next()) {
             $row['sourceid'] = $s['id'];
-            $key = $row['name'] . ':' . $row['sourceid'];
+            $key = rcmail_contact_key($row, $sort_col);
             $records[$key] = $row;
         }
 
@@ -132,8 +202,8 @@
     $result = new rcube_result_set($count);
 
     // cut first-page records
-    if ($CONFIG['pagesize'] < $count) {
-        $records = array_slice($records, 0, $CONFIG['pagesize']);
+    if ($PAGE_SIZE < $count) {
+        $records = array_slice($records, 0, $PAGE_SIZE);
     }
 
     $result->records = array_values($records);
@@ -145,6 +215,7 @@
 
     // save search settings in session
     $_SESSION['search'][$search_request] = $search_set;
+    $_SESSION['search_params'] = array('id' => $search_request, 'data' => array($fields, $search));
     $_SESSION['page'] = 1;
 
     if ($adv)
@@ -153,6 +224,7 @@
     if ($result->count > 0) {
         // create javascript list
         rcmail_js_contacts_list($result);
+        $OUTPUT->show_message('contactsearchsuccessful', 'confirmation', array('nr' => $result->count));
     }
     else {
         $OUTPUT->show_message('nocontactsfound', 'notice');
@@ -160,11 +232,16 @@
 
     // update message count display
     $OUTPUT->command('set_env', 'search_request', $search_request);
-    $OUTPUT->command('set_env', 'pagecount', ceil($result->count / $CONFIG['pagesize']));
+    $OUTPUT->command('set_env', 'pagecount', ceil($result->count / $PAGE_SIZE));
     $OUTPUT->command('set_rowcount', rcmail_get_rowcount_text($result));
+    // Re-set current source
+    $OUTPUT->command('set_env', 'search_id', $sid);
+    $OUTPUT->command('set_env', 'source', '');
+    $OUTPUT->command('set_env', 'group', '');
 
     // unselect currently selected directory/group
-    $OUTPUT->command('unselect_directory');
+    if (!$sid)
+        $OUTPUT->command('unselect_directory');
     $OUTPUT->command('update_group_commands');
 
     // send response
@@ -179,7 +256,7 @@
 
     $form = array(
         'main' => array(
-            'name'    => rcube_label('contactproperties'),
+            'name'    => rcube_label('properties'),
             'content' => array(
             ),
         ),

--
Gitblit v1.9.1