]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/save.inc
ef4387afd282684984ceb7d64b335f349a7a037a
[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-2011, The Roundcube Dev Team                       |
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 5130 2011-08-25 08:30:01Z alec $
19
20 */
21
22 $CONTACTS = rcmail_contact_source(null, true, true);
23 $cid      = get_input_value('_cid', RCUBE_INPUT_POST);
24 $return_action = empty($cid) ? 'add' : 'edit';
25
26 // Source changed, display the form again
27 if (!empty($_GET['_reload'])) {
28    rcmail_overwrite_action($return_action);
29    return;
30 }
31
32 // cannot edit record
33 if ($CONTACTS->readonly) {
34   $OUTPUT->show_message('contactreadonly', 'error');
35   rcmail_overwrite_action($return_action);
36   return;
37 }
38
39 // read POST values into hash array
40 $a_record = array();
41 foreach ($GLOBALS['CONTACT_COLTYPES'] as $col => $colprop) {
42   $fname = '_'.$col;
43   if ($colprop['composite'])
44     continue;
45   // gather form data of composite fields
46   if ($colprop['childs']) {
47     $values = array();
48     foreach ($colprop['childs'] as $childcol => $cp) {
49       $vals = get_input_value('_'.$childcol, RCUBE_INPUT_POST, true);
50       foreach ((array)$vals as $i => $val)
51         $values[$i][$childcol] = $val;
52     }
53     $subtypes = get_input_value('_subtype_' . $col, RCUBE_INPUT_POST);
54     foreach ($subtypes as $i => $subtype)
55       if ($values[$i])
56         $a_record[$col.':'.$subtype][] = $values[$i];
57   }
58   // assign values and subtypes
59   else if (is_array($_POST[$fname])) {
60     $values = get_input_value($fname, RCUBE_INPUT_POST, true);
61     $subtypes = get_input_value('_subtype_' . $col, RCUBE_INPUT_POST);
62     foreach ($values as $i => $val) {
63       $subtype = $subtypes[$i] ? ':'.$subtypes[$i] : '';
64       $a_record[$col.$subtype][] = $val;
65     }
66   }
67   else if (isset($_POST[$fname])) {
68     $a_record[$col] = get_input_value($fname, RCUBE_INPUT_POST, true);
69   }
70 }
71
72 // Generate contact's display name (must be before validation)
73 if (empty($a_record['name'])) {
74     $a_record['name'] = rcube_addressbook::compose_display_name($a_record, true);
75     // Reset it if equals to email address (from compose_display_name())
76     if ($a_record['name'] == $a_record['email'][0])
77         $a_record['name'] = '';
78 }
79
80 // do input checks (delegated to $CONTACTS instance)
81 if (!$CONTACTS->validate($a_record)) {
82     $err = (array)$CONTACTS->get_error() + array('message' => 'formincomplete', 'type' => 'warning');
83     $OUTPUT->show_message($err['message'], $err['type']);
84     $GLOBALS['EDIT_RECORD'] = $a_record;  // store submitted data to be used in edit form
85     rcmail_overwrite_action($return_action);
86     return;
87 }
88
89 // get raw photo data if changed
90 if (isset($a_record['photo'])) {
91     if ($a_record['photo'] == '-del-') {
92         $a_record['photo'] = '';
93     }
94     else if ($tempfile = $_SESSION['contacts']['files'][$a_record['photo']]) {
95         $tempfile = $RCMAIL->plugins->exec_hook('attachment_get', $tempfile);
96         if ($tempfile['status'])
97             $a_record['photo'] = $tempfile['data'] ? $tempfile['data'] : @file_get_contents($tempfile['path']);
98     }
99     else
100         unset($a_record['photo']);
101
102     // cleanup session data
103     $RCMAIL->plugins->exec_hook('attachments_cleanup', array('group' => 'contact'));
104     $RCMAIL->session->remove('contacts');
105 }
106
107 $source = get_input_value('_source', RCUBE_INPUT_GPC);
108
109 // update an existing contact
110 if (!empty($cid))
111 {
112   $plugin = $RCMAIL->plugins->exec_hook('contact_update',
113     array('id' => $cid, 'record' => $a_record, 'source' => $source));
114   $a_record = $plugin['record'];
115
116   if (!$plugin['abort'])
117     $result = $CONTACTS->update($cid, $a_record);
118   else
119     $result = $plugin['result'];
120
121   if ($result) {
122     // LDAP DN change
123     if (is_string($result) && strlen($result)>1) {
124       $newcid = $result;
125       // change cid in POST for 'show' action
126       $_POST['_cid'] = $newcid;
127     }
128
129     // define list of cols to be displayed
130     $a_js_cols = array();
131     $record = $CONTACTS->get_record($newcid ? $newcid : $cid, true);
132     $record['email'] = reset($CONTACTS->get_col_values('email', $record, true));
133     if (empty($record['name']))
134       $record['name']  = rcube_addressbook::compose_display_name($record, true);
135
136     foreach (array('name', 'email') as $col)
137       $a_js_cols[] = Q((string)$record[$col]);
138
139     // update the changed col in list
140     $OUTPUT->command('parent.update_contact_row', $cid, $a_js_cols, $newcid, $source);
141
142     // show confirmation
143     $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
144     rcmail_overwrite_action('show');
145   }
146   else {
147     // show error message
148     $err = $CONTACTS->get_error();
149     $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : ($err['message'] ? $err['message'] : 'errorsaving'), 'error', null, false);
150     rcmail_overwrite_action('show');
151   }
152 }
153
154 // insert a new contact
155 else {
156   // Name of the addressbook already selected on the list
157   $orig_source = get_input_value('_orig_source', RCUBE_INPUT_GPC);
158
159   if (!strlen($source))
160     $source = $orig_source;
161
162   // show notice if existing contacts with same e-mail are found
163   $existing = false;
164   foreach ($CONTACTS->get_col_values('email', $a_record, true) as $email) {
165       if ($email && ($res = $CONTACTS->search('email', $email, false, false, true)) && $res->count) {
166           $OUTPUT->show_message('contactexists', 'notice', null, false);
167           break;
168       }
169   }
170
171   $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
172     'record' => $a_record, 'source' => $source));
173   $a_record = $plugin['record'];
174
175   // insert record and send response
176   if (!$plugin['abort'])
177     $insert_id = $CONTACTS->insert($a_record);
178   else
179     $insert_id = $plugin['result'];
180
181   if ($insert_id) {
182     // add new contact to the specified group
183     if ($CONTACTS->groups && $CONTACTS->group_id) {
184       $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array(
185         'group_id' => $CONTACTS->group_id, 'ids' => $insert_id, 'source' => $source));
186
187       if (!$plugin['abort']) {
188         if (($maxnum = $RCMAIL->config->get('max_group_members', 0)) && ($CONTACTS->count()->count + 1 > $maxnum))
189           $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum));
190
191         $CONTACTS->add_to_group($gid, $plugin['ids']);
192       }
193     }
194
195     if ((string)$source === (string)$orig_source) {
196       // add contact row or jump to the page where it should appear
197       $CONTACTS->reset();
198       $result = $CONTACTS->search($CONTACTS->primary_key, $insert_id);
199
200       rcmail_js_contacts_list($result, 'parent.');
201       $OUTPUT->command('parent.contact_list.select', html_identifier($insert_id));
202
203       // update record count display
204       $CONTACTS->reset();
205       $OUTPUT->command('parent.set_rowcount', rcmail_get_rowcount_text());
206     }
207     else {
208       // re-set iframe
209       $OUTPUT->command('parent.show_contentframe');
210     }
211
212     // show confirmation
213     $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
214     $OUTPUT->send('iframe');
215   }
216   else {
217     // show error message
218     $err = $CONTACTS->get_error();
219     $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : ($err['message'] ? $err['message'] : 'errorsaving'), 'error', null, false);
220     rcmail_overwrite_action('add');
221   }
222 }