4 +-----------------------------------------------------------------------+
5 | program/steps/addressbook/save.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2005, RoundCube Dev. - Switzerland |
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 159 2006-03-03 16:34:35Z roundcube $
23 if ((empty($_POST['_name']) || empty($_POST['_email'])) && empty($_GET['_framed']))
25 show_message('formincomplete', 'warning');
26 rcmail_overwrite_action(empty($_POST['_cid']) ? 'add' : 'show');
30 // setup some vars we need
31 $a_save_cols = array('name', 'firstname', 'surname', 'email');
32 $contacts_table = get_table_name('contacts');
34 // update an existing contact
35 if (!empty($_POST['_cid']))
37 $a_write_sql = array();
39 foreach ($a_save_cols as $col)
42 if (!isset($_POST[$fname]))
45 $a_write_sql[] = sprintf("%s=%s",
46 $DB->quoteIdentifier($col),
47 $DB->quote(get_input_value($fname, RCUBE_INPUT_POST)));
50 if (sizeof($a_write_sql))
52 $DB->query("UPDATE $contacts_table
53 SET changed=now(), ".join(', ', $a_write_sql)."
58 $_SESSION['user_id']);
60 $updated = $DB->affected_rows();
66 show_message('successfullysaved', 'confirmation');
70 // define list of cols to be displayed
71 $a_show_cols = array('name', 'email');
74 $sql_result = $DB->query("SELECT * FROM $contacts_table
79 $_SESSION['user_id']);
81 $sql_arr = $DB->fetch_assoc($sql_result);
82 foreach ($a_show_cols as $col)
83 $a_js_cols[] = (string)$sql_arr[$col];
85 // update the changed col in list
86 $OUTPUT->add_script(sprintf("if(parent.%s)parent.%s.update_contact_row('%d', %s);",
90 array2js($a_js_cols)));
93 show_message('successfullysaved', 'confirmation');
99 show_message('errorsaving', 'error');
100 rcmail_overwrite_action('show');
104 // insert a new contact
107 $a_insert_cols = $a_insert_values = array();
109 // check for existing contacts
110 $sql = "SELECT 1 FROM $contacts_table
111 WHERE user_id = {$_SESSION['user_id']}
114 // get email and name, build sql for existing user check
115 if (isset($_GET['_emails']) && isset($_GET['_names']))
117 $sql .= "AND email IN (";
118 $emails = explode(',', get_input_value('_emails', RCUBE_INPUT_GET));
119 $names = explode(',', get_input_value('_names', RCUBE_INPUT_GET));
120 $count = count($emails);
122 foreach ($emails as $email)
124 $end = (++$n == $count) ? '' : ',';
125 $sql .= $DB->quote($email) . $end;
130 else if (isset($_POST['_email']))
131 $sql .= "AND email = " . $DB->quote(get_input_value('_email', RCUBE_INPUT_POST));
133 $sql_result = $DB->query($sql);
135 // show warning message
136 if ($DB->num_rows($sql_result))
138 show_message('contactexists', 'warning');
141 rcmail_overwrite_action('ldappublicsearch');
143 rcmail_overwrite_action('add');
151 foreach ($emails as $email)
153 $DB->query("INSERT INTO $contacts_table
154 (user_id, name, email
155 VALUES ({$_SESSION['user_id']}," . $DB->quote($names[$n++]) . "," .
156 $DB->quote($email) . ")");
157 $insert_id[] = $DB->insert_id();
162 foreach ($a_save_cols as $col)
165 if (!isset($_POST[$fname]))
168 $a_insert_cols[] = $col;
169 $a_insert_values[] = $DB->quote(get_input_value($fname, RCUBE_INPUT_POST));
172 if (sizeof($a_insert_cols))
174 $DB->query("INSERT INTO $contacts_table
175 (user_id, changed, del, ".join(', ', $a_insert_cols).")
176 VALUES (?, now(), 0, ".join(', ', $a_insert_values).")",
177 $_SESSION['user_id']);
179 $insert_id = $DB->insert_id(get_sequence_name('contacts'));
188 $_GET['_cid'] = $insert_id;
192 // add contact row or jump to the page where it should appear
193 $commands = sprintf("if(parent.%s)parent.", $JS_OBJECT_NAME);
194 $sql_result = $DB->query("SELECT * FROM $contacts_table
198 $_SESSION['user_id']);
199 $commands .= rcmail_js_contacts_list($sql_result, $JS_OBJECT_NAME);
201 $commands .= sprintf("if(parent.%s)parent.%s.select('%d');\n",
206 // update record count display
207 $commands .= sprintf("if(parent.%s)parent.%s.set_rowcount('%s');\n",
210 rcmail_get_rowcount_text());
212 $OUTPUT->add_script($commands);
216 show_message('successfullysaved', 'confirmation');
220 // add contact row or jump to the page where it should appear
222 foreach ($insert_id as $id)
224 $sql_result = $DB->query("SELECT * FROM $contacts_table
225 WHERE contact_id = $id
226 AND user_id = {$_SESSION['user_id']}");
228 $commands .= sprintf("if(parent.%s)parent.", $JS_OBJECT_NAME);
229 $commands .= rcmail_js_contacts_list($sql_result, $JS_OBJECT_NAME);
233 // display the last insert id
234 $commands .= sprintf("if(parent.%s)parent.%s.select('%d');\n",
239 // update record count display
240 $commands .= sprintf("if(parent.%s)parent.%s.set_rowcount('%s');\n",
243 rcmail_get_rowcount_text());
245 $OUTPUT->add_script($commands);
246 rcmail_overwrite_action('ldappublicsearch');
250 show_message('successfullysaved', 'confirmation');
254 // show error message
255 show_message('errorsaving', 'error');
256 rcmail_overwrite_action('add');