]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/save.inc
Imported Upstream version 0.5.1
[roundcube.git] / program / steps / addressbook / save.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/addressbook/save.inc                                    |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2005-2009, Roundcube Dev. - Switzerland                 |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Save a contact entry or to add a new one                            |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: save.inc 4509 2011-02-09 10:51:50Z thomasb $
19
20 */
21
22 $cid = get_input_value('_cid', RCUBE_INPUT_POST);
23 $return_action = empty($cid) ? 'add' : 'edit';
24
25 // cannot edit record
26 if ($CONTACTS->readonly) {
27   $OUTPUT->show_message('contactreadonly', 'error');
28   rcmail_overwrite_action($return_action);
29   return;
30 }
31
32 // Basic input checks
33 if ((!get_input_value('_name', RCUBE_INPUT_POST) || !get_input_value('_email', RCUBE_INPUT_POST))) {
34   $OUTPUT->show_message('formincomplete', 'warning');
35   rcmail_overwrite_action($return_action);
36   return;
37 }
38
39
40 // setup some vars we need
41 $a_save_cols = array('name', 'firstname', 'surname', 'email');
42 $a_record = array();
43
44 // read POST values into hash array
45 foreach ($a_save_cols as $col) {
46   $fname = '_'.$col;
47   if (isset($_POST[$fname]))
48     $a_record[$col] = get_input_value($fname, RCUBE_INPUT_POST);
49 }
50
51 // Validity checks
52 $_email = rcube_idn_to_ascii($a_record['email']);
53 if (!check_email($_email)) {
54   $OUTPUT->show_message('emailformaterror', 'warning', array('email' => $_email));
55   rcmail_overwrite_action($return_action);
56   return;
57 }
58
59 // update an existing contact
60 if (!empty($cid))
61 {
62   $plugin = $RCMAIL->plugins->exec_hook('contact_update',
63     array('id' => $cid, 'record' => $a_record, 'source' => get_input_value('_source', RCUBE_INPUT_GPC)));
64   $a_record = $plugin['record'];
65
66   if (!$plugin['abort'])
67     $result = $CONTACTS->update($cid, $a_record);
68   else
69     $result = $plugin['result'];
70
71   if ($result) {
72     // LDAP DN change
73     if (is_string($result) && strlen($result)>1) {
74       $newcid = $result;
75       // change cid in POST for 'show' action
76       $_POST['_cid'] = $newcid;
77     }
78
79     // define list of cols to be displayed
80     $a_js_cols = array();
81     $record = $CONTACTS->get_record($newcid ? $newcid : $cid, true);
82
83     foreach (array('name', 'email') as $col)
84       $a_js_cols[] = (string)$record[$col];
85
86     // update the changed col in list
87     $OUTPUT->command('parent.update_contact_row', $cid, $a_js_cols, $newcid);
88
89     // show confirmation
90     $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
91     rcmail_overwrite_action('show');
92   }
93   else {
94     // show error message
95     $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false);
96     rcmail_overwrite_action('show');
97   }
98 }
99
100 // insert a new contact
101 else {
102   // check for existing contacts
103   $existing = $CONTACTS->search('email', $a_record['email'], true, false);
104
105   // show warning message
106   if ($existing->count) {
107     $OUTPUT->show_message('contactexists', 'warning', null, false);
108     rcmail_overwrite_action('add');
109     return;
110   }
111
112   $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
113     'record' => $a_record, 'source' => get_input_value('_source', RCUBE_INPUT_GPC)));
114   $a_record = $plugin['record'];
115
116   // insert record and send response
117   if (!$plugin['abort'])
118     $insert_id = $CONTACTS->insert($a_record);
119   else
120     $insert_id = $plugin['result'];
121
122
123   if ($insert_id) {
124     // add new contact to the specified group
125     if ($CONTACTS->group_id) {
126       $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array('group_id' => $CONTACTS->group_id, 'ids' => $insert_id, 'source' => $source));
127
128       if (!$plugin['abort']) {
129         if (($maxnum = $RCMAIL->config->get('max_group_members', 0)) && ($CONTACTS->count()->count + 1 > $maxnum))
130           $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum));
131
132         $CONTACTS->add_to_group($gid, $plugin['ids']);
133       }
134     }
135     
136     // add contact row or jump to the page where it should appear
137     $CONTACTS->reset();
138     $result = $CONTACTS->search($CONTACTS->primary_key, $insert_id);
139
140     rcmail_js_contacts_list($result, 'parent.');
141     $OUTPUT->command('parent.contact_list.select', $insert_id);
142
143     // update record count display
144     $CONTACTS->reset();
145     $OUTPUT->command('parent.set_rowcount', rcmail_get_rowcount_text());
146
147     // show confirmation
148     $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
149     $OUTPUT->send('iframe');
150   }
151   else {
152     // show error message
153     $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false);
154     rcmail_overwrite_action('add');
155   }
156 }