]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/delete.inc
3f8885efbe1e37a0017ed1f398291fb039476f40
[roundcube.git] / program / steps / addressbook / delete.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/addressbook/delete.inc                                  |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2005-2009, The Roundcube Dev Team                       |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
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: delete.inc 5967 2012-03-05 19:59:07Z alec $
19
20 */
21
22 // process ajax requests only
23 if (!$OUTPUT->ajax_call)
24     return;
25
26 $cids   = rcmail_get_cids();
27 $delcnt = 0;
28
29 // remove previous deletes
30 $undo_time = $RCMAIL->config->get('undo_timeout', 0);
31 $RCMAIL->session->remove('contact_undo');
32
33 foreach ($cids as $source => $cid)
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');
43             $OUTPUT->send();
44         }
45         continue;
46     }
47
48     $plugin = $RCMAIL->plugins->exec_hook('contact_delete', array(
49         'id' => $cid, 'source' => $source));
50
51     $deleted = !$plugin['abort'] ? $CONTACTS->delete($cid, $undo_time < 1) : $plugin['result'];
52
53     if (!$deleted) {
54         $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'contactdelerror', 'error');
55         $OUTPUT->command('list_contacts');
56         $OUTPUT->send();
57     }
58     else {
59         $delcnt += $deleted;
60
61         // store deleted contacts IDs in session for undo action
62         if ($undo_time > 0 && $CONTACTS->undelete) {
63             $_SESSION['contact_undo']['data'][$source] = $cid;
64         }
65     }
66 }
67
68 $page = isset($_SESSION['page']) ? $_SESSION['page'] : 1;
69
70 // update saved search after data changed
71 if (($search_request = $_REQUEST['_search']) && isset($_SESSION['search'][$search_request])) {
72     $sort_col = $RCMAIL->config->get('addressbook_sort_col', 'name');
73     $search  = (array)$_SESSION['search'][$search_request];
74     $records = array();
75
76     // Get records from all sources (refresh search)
77     foreach ($search as $s => $set) {
78         $source = $RCMAIL->get_address_book($s);
79
80         // reset page
81         $source->set_page(1);
82         $source->set_pagesize(9999);
83         $source->set_search_set($set);
84
85         // get records
86         $result = $source->list_records(array('name', 'email'));
87
88         if (!$result->count) {
89             unset($search[$s]);
90             continue;
91         }
92
93         while ($row = $result->next()) {
94             $row['sourceid'] = $s;
95             $key = rcmail_contact_key($row, $sort_col);
96             $records[$key] = $row;
97         }
98         unset($result);
99
100         $search[$s] = $source->get_search_set();
101     }
102
103     $_SESSION['search'][$search_request] = $search;
104
105     // create resultset object
106     $count  = count($records);
107     $first  = ($page-1) * $CONFIG['pagesize'];
108     $result = new rcube_result_set($count, $first);
109
110     // get records from the next page to add to the list
111     $pages = ceil((count($records) + $delcnt) / $CONFIG['pagesize']);
112     if ($_GET['_from'] != 'show' && $pages > 1 && $page < $pages) {
113         // sort the records
114         ksort($records, SORT_LOCALE_STRING);
115
116         $first += $CONFIG['pagesize'];
117         // create resultset object
118         $res = new rcube_result_set($count, $first - $delcnt);
119
120         if ($CONFIG['pagesize'] < $count) {
121             $records = array_slice($records, $first - $delcnt, $delcnt);
122         }
123
124         $res->records = array_values($records);
125         $records = $res;
126     }
127     else {
128         unset($records);
129     }
130 }
131 else {
132     // count contacts for this user
133     $result = $CONTACTS->count();
134
135     // get records from the next page to add to the list
136     $pages = ceil(($result->count + $delcnt) / $CONFIG['pagesize']);
137     if ($_GET['_from'] != 'show' && $pages > 1 && $page < $pages) {
138         $CONTACTS->set_page($page);
139         $records = $CONTACTS->list_records(null, -$delcnt);
140     }
141 }
142
143 // update message count display
144 $OUTPUT->set_env('pagecount', ceil($result->count / $CONFIG['pagesize']));
145 $OUTPUT->command('set_rowcount', rcmail_get_rowcount_text($result));
146
147 if (!empty($_SESSION['contact_undo'])) {
148     $_SESSION['contact_undo']['ts'] = time();
149     $msg = html::span(null, rcube_label('contactdeleted'))
150         . ' ' . html::a(array('onclick' => JS_OBJECT_NAME.".command('undo', '', this)"), rcube_label('undo'));
151
152     $OUTPUT->show_message($msg, 'confirmation', null, true, $undo_time);
153 }
154 else {
155     $OUTPUT->show_message('contactdeleted', 'confirmation');
156 }
157
158 // add new rows from next page (if any)
159 if (!empty($records)) {
160     rcmail_js_contacts_list($records);
161 }
162
163 // send response
164 $OUTPUT->send();