4 +-----------------------------------------------------------------------+
5 | program/steps/addressbook/save.inc |
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 |
12 | Save a contact entry or to add a new one |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: save.inc 5541 2011-12-04 17:05:42Z thomasb $
22 $CONTACTS = rcmail_contact_source(null, true, true);
23 $cid = get_input_value('_cid', RCUBE_INPUT_POST);
24 $return_action = empty($cid) ? 'add' : 'edit';
26 // Source changed, display the form again
27 if (!empty($_GET['_reload'])) {
28 rcmail_overwrite_action($return_action);
33 if ($CONTACTS->readonly) {
34 $OUTPUT->show_message('contactreadonly', 'error');
35 rcmail_overwrite_action($return_action);
39 // read POST values into hash array
41 foreach ($GLOBALS['CONTACT_COLTYPES'] as $col => $colprop) {
43 if ($colprop['composite'])
45 // gather form data of composite fields
46 if ($colprop['childs']) {
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;
53 $subtypes = isset($_REQUEST['_subtype_' . $col]) ? (array)get_input_value('_subtype_' . $col, RCUBE_INPUT_POST) : array('');
54 foreach ($subtypes as $i => $subtype) {
55 $suffix = $subtype ? ':'.$subtype : '';
57 $a_record[$col.$suffix][] = $values[$i];
60 // assign values and subtypes
61 else if (is_array($_POST[$fname])) {
62 $values = get_input_value($fname, RCUBE_INPUT_POST, true);
63 $subtypes = get_input_value('_subtype_' . $col, RCUBE_INPUT_POST);
64 foreach ($values as $i => $val) {
65 $subtype = $subtypes[$i] ? ':'.$subtypes[$i] : '';
66 $a_record[$col.$subtype][] = $val;
69 else if (isset($_POST[$fname])) {
70 $a_record[$col] = get_input_value($fname, RCUBE_INPUT_POST, true);
74 // Generate contact's display name (must be before validation)
75 if (empty($a_record['name'])) {
76 $a_record['name'] = rcube_addressbook::compose_display_name($a_record, true);
77 // Reset it if equals to email address (from compose_display_name())
78 if ($a_record['name'] == $a_record['email'][0])
79 $a_record['name'] = '';
82 // do input checks (delegated to $CONTACTS instance)
83 if (!$CONTACTS->validate($a_record)) {
84 $err = (array)$CONTACTS->get_error() + array('message' => 'formincomplete', 'type' => 'warning');
85 $OUTPUT->show_message($err['message'], $err['type']);
86 $GLOBALS['EDIT_RECORD'] = $a_record; // store submitted data to be used in edit form
87 rcmail_overwrite_action($return_action);
91 // get raw photo data if changed
92 if (isset($a_record['photo'])) {
93 if ($a_record['photo'] == '-del-') {
94 $a_record['photo'] = '';
96 else if ($tempfile = $_SESSION['contacts']['files'][$a_record['photo']]) {
97 $tempfile = $RCMAIL->plugins->exec_hook('attachment_get', $tempfile);
98 if ($tempfile['status'])
99 $a_record['photo'] = $tempfile['data'] ? $tempfile['data'] : @file_get_contents($tempfile['path']);
102 unset($a_record['photo']);
104 // cleanup session data
105 $RCMAIL->plugins->exec_hook('attachments_cleanup', array('group' => 'contact'));
106 $RCMAIL->session->remove('contacts');
109 $source = get_input_value('_source', RCUBE_INPUT_GPC);
111 // update an existing contact
114 $plugin = $RCMAIL->plugins->exec_hook('contact_update',
115 array('id' => $cid, 'record' => $a_record, 'source' => $source));
116 $a_record = $plugin['record'];
118 if (!$plugin['abort'])
119 $result = $CONTACTS->update($cid, $a_record);
121 $result = $plugin['result'];
125 if (is_string($result) && strlen($result)>1) {
127 // change cid in POST for 'show' action
128 $_POST['_cid'] = $newcid;
131 // define list of cols to be displayed
132 $a_js_cols = array();
133 $record = $CONTACTS->get_record($newcid ? $newcid : $cid, true);
134 $record['email'] = reset($CONTACTS->get_col_values('email', $record, true));
135 if (empty($record['name']))
136 $record['name'] = rcube_addressbook::compose_display_name($record, true);
138 foreach (array('name', 'email') as $col)
139 $a_js_cols[] = Q((string)$record[$col]);
141 // update the changed col in list
142 $OUTPUT->command('parent.update_contact_row', $cid, $a_js_cols, $newcid, $source);
145 $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
146 rcmail_overwrite_action('show');
149 // show error message
150 $err = $CONTACTS->get_error();
151 $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : ($err['message'] ? $err['message'] : 'errorsaving'), 'error', null, false);
152 rcmail_overwrite_action('show');
156 // insert a new contact
158 // Name of the addressbook already selected on the list
159 $orig_source = get_input_value('_orig_source', RCUBE_INPUT_GPC);
161 if (!strlen($source))
162 $source = $orig_source;
164 // show notice if existing contacts with same e-mail are found
166 foreach ($CONTACTS->get_col_values('email', $a_record, true) as $email) {
167 if ($email && ($res = $CONTACTS->search('email', $email, 1, false, true)) && $res->count) {
168 $OUTPUT->show_message('contactexists', 'notice', null, false);
173 $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
174 'record' => $a_record, 'source' => $source));
175 $a_record = $plugin['record'];
177 // insert record and send response
178 if (!$plugin['abort'])
179 $insert_id = $CONTACTS->insert($a_record);
181 $insert_id = $plugin['result'];
184 // add new contact to the specified group
185 if ($CONTACTS->groups && $CONTACTS->group_id) {
186 $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array(
187 'group_id' => $CONTACTS->group_id, 'ids' => $insert_id, 'source' => $source));
189 if (!$plugin['abort']) {
190 if (($maxnum = $RCMAIL->config->get('max_group_members', 0)) && ($CONTACTS->count()->count + 1 > $maxnum))
191 $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum));
193 $CONTACTS->add_to_group($gid, $plugin['ids']);
197 if ((string)$source === (string)$orig_source) {
198 // add contact row or jump to the page where it should appear
200 $result = $CONTACTS->search($CONTACTS->primary_key, $insert_id);
202 rcmail_js_contacts_list($result, 'parent.');
203 $OUTPUT->command('parent.contact_list.select', html_identifier($insert_id));
205 // update record count display
207 $OUTPUT->command('parent.set_rowcount', rcmail_get_rowcount_text());
211 $OUTPUT->command('parent.show_contentframe');
215 $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
216 $OUTPUT->send('iframe');
219 // show error message
220 $err = $CONTACTS->get_error();
221 $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : ($err['message'] ? $err['message'] : 'errorsaving'), 'error', null, false);
222 rcmail_overwrite_action('add');