]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/delete.inc
eae4de79766e228da78c80e18b9a5bbda00e1f9b
[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 4951 2011-07-21 11:15:18Z 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     $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
146 if (!empty($_SESSION['contact_undo'])) {
147     $_SESSION['contact_undo']['ts'] = time();
148     $msg = html::span(null, rcube_label('contactdeleted'))
149         . ' ' . html::a(array('onclick' => JS_OBJECT_NAME.".command('undo', '', this)"), rcube_label('undo'));
150
151     $OUTPUT->show_message($msg, 'confirmation', null, true, $undo_time);
152 }
153 else {
154     $OUTPUT->show_message('contactdeleted', 'confirmation');
155 }
156
157 // add new rows from next page (if any)
158 if (!empty($records)) {
159     rcmail_js_contacts_list($records);
160 }
161
162 // send response
163 $OUTPUT->send();