source: github/program/steps/addressbook/delete.inc @ 7fe3811

HEADcourier-fixdev-browser-capabilitiespdorelease-0.8
Last change on this file since 7fe3811 was 7fe3811, checked in by thomascube <thomas@…>, 16 months ago

Changed license to GNU GPLv3+ with exceptions for skins and plugins

  • 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    $search  = (array)$_SESSION['search'][$search_request];
76    $records = array();
77
78    // Get records from all sources (refresh search)
79    foreach ($search as $s => $set) {
80        $source = $RCMAIL->get_address_book($s);
81
82        // reset page
83        $source->set_page(1);
84        $source->set_pagesize(9999);
85        $source->set_search_set($set);
86
87        // get records
88        $result = $source->list_records(array('name', 'email'));
89
90        if (!$result->count) {
91            unset($search[$s]);
92            continue;
93        }
94
95        while ($row = $result->next()) {
96            $row['sourceid'] = $s;
97            $key = $row['name'] . ':' . $row['sourceid'];
98            $records[$key] = $row;
99        }
100        unset($result);
101
102        $search[$s] = $source->get_search_set();
103    }
104
105    $_SESSION['search'][$search_request] = $search;
106
107    // create resultset object
108    $count  = count($records);
109    $first  = ($page-1) * $PAGE_SIZE;
110    $result = new rcube_result_set($count, $first);
111
112    // get records from the next page to add to the list
113    $pages = ceil((count($records) + $delcnt) / $PAGE_SIZE);
114    if ($_GET['_from'] != 'show' && $pages > 1 && $page < $pages) {
115        // sort the records
116        ksort($records, SORT_LOCALE_STRING);
117
118        $first += $PAGE_SIZE;
119        // create resultset object
120        $res = new rcube_result_set($count, $first - $delcnt);
121
122        if ($PAGE_SIZE < $count) {
123            $records = array_slice($records, $first - $delcnt, $delcnt);
124        }
125
126        $res->records = array_values($records);
127        $records = $res;
128    }
129    else {
130        unset($records);
131    }
132}
133else {
134    // count contacts for this user
135    $result = $CONTACTS->count();
136
137    // get records from the next page to add to the list
138    $pages = ceil(($result->count + $delcnt) / $PAGE_SIZE);
139    if ($_GET['_from'] != 'show' && $pages > 1 && $page < $pages) {
140        $CONTACTS->set_page($page);
141        $records = $CONTACTS->list_records(null, -$delcnt);
142    }
143}
144
145// update message count display
146$OUTPUT->set_env('pagecount', ceil($result->count / $PAGE_SIZE));
147$OUTPUT->command('set_rowcount', rcmail_get_rowcount_text($result));
148
149if (!empty($_SESSION['contact_undo'])) {
150    $_SESSION['contact_undo']['ts'] = time();
151    $msg = html::span(null, rcube_label('contactdeleted'))
152        . ' ' . html::a(array('onclick' => JS_OBJECT_NAME.".command('undo', '', this)"), rcube_label('undo'));
153
154    $OUTPUT->show_message($msg, 'confirmation', null, true, $undo_time);
155}
156else {
157    $OUTPUT->show_message('contactdeleted', 'confirmation');
158}
159
160// add new rows from next page (if any)
161if (!empty($records)) {
162    rcmail_js_contacts_list($records);
163}
164
165// send response
166$OUTPUT->send();
Note: See TracBrowser for help on using the repository browser.