alecpl
2011-11-24 e4a4ca19480690193e36e14ef6b9d592e73e9e9c
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/addressbook/delete.inc                                  |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
f5e7b3 8  | Copyright (C) 2005-2009, The Roundcube Dev Team                       |
30233b 9  | Licensed under the GNU GPL                                            |
4e17e6 10  |                                                                       |
T 11  | PURPOSE:                                                              |
12  |   Delete the submitted contacts (CIDs) from the users address book    |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id$
19
20 */
21
ecf295 22 // process ajax requests only
A 23 if (!$OUTPUT->ajax_call)
24     return;
25
26 $cids   = rcmail_get_cids();
27 $delcnt = 0;
28
7f5a84 29 // remove previous deletes
63fda8 30 $undo_time = $RCMAIL->config->get('undo_timeout', 0);
7f5a84 31 $RCMAIL->session->remove('contact_undo');
A 32
ecf295 33 foreach ($cids as $source => $cid)
A 34 {
35     $CONTACTS = rcmail_contact_source($source);
36
37     if ($CONTACTS->readonly) {
38         // more sources? do nothing, probably we have search results from
39         // more than one source, some of these sources can be readonly
40         if (count($cids) == 1) {
41             $OUTPUT->show_message('contactdelerror', 'error');
42             $OUTPUT->command('list_contacts');
7f5a84 43             $OUTPUT->send();
ecf295 44         }
A 45         continue;
46     }
47
c50d88 48     $plugin = $RCMAIL->plugins->exec_hook('contact_delete', array(
ecf295 49         'id' => $cid, 'source' => $source));
306f15 50
63fda8 51     $deleted = !$plugin['abort'] ? $CONTACTS->delete($cid, $undo_time < 1) : $plugin['result'];
ce92ba 52
c50d88 53     if (!$deleted) {
A 54         $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'contactdelerror', 'error');
55         $OUTPUT->command('list_contacts');
ecf295 56         $OUTPUT->send();
c50d88 57     }
A 58     else {
ecf295 59         $delcnt += $deleted;
7f5a84 60
63fda8 61         // store deleted contacts IDs in session for undo action
A 62         if ($undo_time > 0 && $CONTACTS->undelete) {
7f5a84 63             $_SESSION['contact_undo']['data'][$source] = $cid;
A 64         }
c50d88 65     }
ce92ba 66 }
b25dfd 67
ecf295 68 $page = isset($_SESSION['page']) ? $_SESSION['page'] : 1;
A 69
70 // update saved search after data changed
71 if (($search_request = $_REQUEST['_search']) && isset($_SESSION['search'][$search_request])) {
72     $search  = (array)$_SESSION['search'][$search_request];
73     $records = array();
74
75     // Get records from all sources (refresh search)
76     foreach ($search as $s => $set) {
77         $source = $RCMAIL->get_address_book($s);
78
79         // reset page
80         $source->set_page(1);
81         $source->set_pagesize(9999);
82         $source->set_search_set($set);
83
84         // get records
85         $result = $source->list_records(array('name', 'email'));
86
87         if (!$result->count) {
88             unset($search[$s]);
89             continue;
90         }
91
92         while ($row = $result->next()) {
93             $row['sourceid'] = $s;
94             $key = $row['name'] . ':' . $row['sourceid'];
95             $records[$key] = $row;
96         }
97         unset($result);
98
99         $search[$s] = $source->get_search_set();
100     }
101
102     $_SESSION['search'][$search_request] = $search;
103
104     // create resultset object
105     $count  = count($records);
106     $first  = ($page-1) * $CONFIG['pagesize'];
107     $result = new rcube_result_set($count, $first);
108
109     // get records from the next page to add to the list
110     $pages = ceil((count($records) + $delcnt) / $CONFIG['pagesize']);
111     if ($_GET['_from'] != 'show' && $pages > 1 && $page < $pages) {
112         // sort the records
113         ksort($records, SORT_LOCALE_STRING);
114
115         $first += $CONFIG['pagesize'];
116         // create resultset object
117         $res = new rcube_result_set($count, $first - $delcnt);
118
119         if ($CONFIG['pagesize'] < $count) {
120             $records = array_slice($records, $first - $delcnt, $delcnt);
121         }
122
123         $res->records = array_values($records);
124         $records = $res;
125     }
126     else {
127         unset($records);
128     }
129 }
130 else {
131     // count contacts for this user
132     $result = $CONTACTS->count();
133
134     // get records from the next page to add to the list
135     $pages = ceil(($result->count + $delcnt) / $CONFIG['pagesize']);
136     if ($_GET['_from'] != 'show' && $pages > 1 && $page < $pages) {
137         $CONTACTS->set_page($page);
138         $records = $CONTACTS->list_records(null, -$delcnt);
139     }
140 }
141
142 // update message count display
143 $OUTPUT->set_env('pagecount', ceil($result->count / $CONFIG['pagesize']));
144 $OUTPUT->command('set_rowcount', rcmail_get_rowcount_text($result));
145
7f5a84 146 if (!empty($_SESSION['contact_undo'])) {
A 147     $_SESSION['contact_undo']['ts'] = time();
f52c4f 148     $msg = html::span(null, rcube_label('contactdeleted'))
7f5a84 149         . ' ' . html::a(array('onclick' => JS_OBJECT_NAME.".command('undo', '', this)"), rcube_label('undo'));
A 150
63fda8 151     $OUTPUT->show_message($msg, 'confirmation', null, true, $undo_time);
7f5a84 152 }
A 153 else {
154     $OUTPUT->show_message('contactdeleted', 'confirmation');
155 }
156
ecf295 157 // add new rows from next page (if any)
A 158 if (!empty($records)) {
159     rcmail_js_contacts_list($records);
160 }
161
162 // send response
163 $OUTPUT->send();