source: github/program/steps/addressbook/delete.inc @ 0203f16

HEADcourier-fixdev-browser-capabilitiespdorelease-0.8
Last change on this file since 0203f16 was 0203f16, checked in by alecpl <alec@…>, 15 months ago
  • Fix duplicate names handling in addressbook searches (#1488375)
  • Property mode set to 100644
File size: 5.4 KB
Line 
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 |                                                                       |
10 | Licensed under the GNU General Public License version 3 or            |
11 | any later version with exceptions for skins & plugins.                |
12 | See the README file for a full license statement.                     |
13 |                                                                       |
14 | PURPOSE:                                                              |
15 |   Delete the submitted contacts (CIDs) from the users address book    |
16 |                                                                       |
17 +-----------------------------------------------------------------------+
18 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
19 +-----------------------------------------------------------------------+
20
21 $Id$
22
23*/
24
25// process ajax requests only
26if (!$OUTPUT->ajax_call)
27    return;
28
29$cids   = rcmail_get_cids();
30$delcnt = 0;
31
32// remove previous deletes
33$undo_time = $RCMAIL->config->get('undo_timeout', 0);
34$RCMAIL->session->remove('contact_undo');
35
36foreach ($cids as $source => $cid)
37{
38    $CONTACTS = rcmail_contact_source($source);
39
40    if ($CONTACTS->readonly) {
41        // more sources? do nothing, probably we have search results from
42        // more than one source, some of these sources can be readonly
43        if (count($cids) == 1) {
44            $OUTPUT->show_message('contactdelerror', 'error');
45            $OUTPUT->command('list_contacts');
46            $OUTPUT->send();
47        }
48        continue;
49    }
50
51    $plugin = $RCMAIL->plugins->exec_hook('contact_delete', array(
52        'id' => $cid, 'source' => $source));
53
54    $deleted = !$plugin['abort'] ? $CONTACTS->delete($cid, $undo_time < 1) : $plugin['result'];
55
56    if (!$deleted) {
57        $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'contactdelerror', 'error');
58        $OUTPUT->command('list_contacts');
59        $OUTPUT->send();
60    }
61    else {
62        $delcnt += $deleted;
63
64        // store deleted contacts IDs in session for undo action
65        if ($undo_time > 0 && $CONTACTS->undelete) {
66            $_SESSION['contact_undo']['data'][$source] = $cid;
67        }
68    }
69}
70
71$page = isset($_SESSION['page']) ? $_SESSION['page'] : 1;
72
73// update saved search after data changed
74if (($search_request = $_REQUEST['_search']) && isset($_SESSION['search'][$search_request])) {
75    $sort_col = $RCMAIL->config->get('addressbook_sort_col', 'name');
76    $search  = (array)$_SESSION['search'][$search_request];
77    $records = array();
78
79    // Get records from all sources (refresh search)
80    foreach ($search as $s => $set) {
81        $source = $RCMAIL->get_address_book($s);
82
83        // reset page
84        $source->set_page(1);
85        $source->set_pagesize(9999);
86        $source->set_search_set($set);
87
88        // get records
89        $result = $source->list_records(array('name', 'email'));
90
91        if (!$result->count) {
92            unset($search[$s]);
93            continue;
94        }
95
96        while ($row = $result->next()) {
97            $row['sourceid'] = $s;
98            $key = rcmail_contact_key($row, $sort_col);
99            $records[$key] = $row;
100        }
101        unset($result);
102
103        $search[$s] = $source->get_search_set();
104    }
105
106    $_SESSION['search'][$search_request] = $search;
107
108    // create resultset object
109    $count  = count($records);
110    $first  = ($page-1) * $PAGE_SIZE;
111    $result = new rcube_result_set($count, $first);
112
113    // get records from the next page to add to the list
114    $pages = ceil((count($records) + $delcnt) / $PAGE_SIZE);
115    if ($_GET['_from'] != 'show' && $pages > 1 && $page < $pages) {
116        // sort the records
117        ksort($records, SORT_LOCALE_STRING);
118
119        $first += $PAGE_SIZE;
120        // create resultset object
121        $res = new rcube_result_set($count, $first - $delcnt);
122
123        if ($PAGE_SIZE < $count) {
124            $records = array_slice($records, $first - $delcnt, $delcnt);
125        }
126
127        $res->records = array_values($records);
128        $records = $res;
129    }
130    else {
131        unset($records);
132    }
133}
134else {
135    // count contacts for this user
136    $result = $CONTACTS->count();
137
138    // get records from the next page to add to the list
139    $pages = ceil(($result->count + $delcnt) / $PAGE_SIZE);
140    if ($_GET['_from'] != 'show' && $pages > 1 && $page < $pages) {
141        $CONTACTS->set_page($page);
142        $records = $CONTACTS->list_records(null, -$delcnt);
143    }
144}
145
146// update message count display
147$OUTPUT->set_env('pagecount', ceil($result->count / $PAGE_SIZE));
148$OUTPUT->command('set_rowcount', rcmail_get_rowcount_text($result));
149
150if (!empty($_SESSION['contact_undo'])) {
151    $_SESSION['contact_undo']['ts'] = time();
152    $msg = html::span(null, rcube_label('contactdeleted'))
153        . ' ' . html::a(array('onclick' => JS_OBJECT_NAME.".command('undo', '', this)"), rcube_label('undo'));
154
155    $OUTPUT->show_message($msg, 'confirmation', null, true, $undo_time);
156}
157else {
158    $OUTPUT->show_message('contactdeleted', 'confirmation');
159}
160
161// add new rows from next page (if any)
162if (!empty($records)) {
163    rcmail_js_contacts_list($records);
164}
165
166// send response
167$OUTPUT->send();
Note: See TracBrowser for help on using the repository browser.