thomascube
2005-11-18 fbf77b4493f1b77c99751d8a86365c712ae3fb1b
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/addressbook/save.inc                                    |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2005, RoundCube Dev. - Switzerland                      |
30233b 9  | Licensed under the GNU GPL                                            |
4e17e6 10  |                                                                       |
T 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$
19
20 */
21
22
23 $a_save_cols = array('name', 'firstname', 'surname', 'email');
24
25
10a699 26 // check input
T 27 if (empty($_POST['_name']) || empty($_POST['_email']))
28   {
29   show_message('formincomplete', 'warning');
30   rcmail_overwrite_action($_POST['_cid'] ? 'show' : 'add');
31   return;
32   }
33
34
4e17e6 35 // update an existing contact
T 36 if ($_POST['_cid'])
37   {
38   $a_write_sql = array();
39
40   foreach ($a_save_cols as $col)
41     {
42     $fname = '_'.$col;
43     if (!isset($_POST[$fname]))
44       continue;
45     
10a699 46     $a_write_sql[] = sprintf("%s=%s", $DB->quoteIdentifier($col), $DB->quote(strip_tags($_POST[$fname])));
4e17e6 47     }
T 48
49   if (sizeof($a_write_sql))
50     {
d7cb77 51     $DB->query("UPDATE ".get_table_name('contacts')."
e0ddd4 52                 SET    changed=now(), ".join(', ', $a_write_sql)."
d7cb77 53                 WHERE  contact_id=?
S 54                 AND    user_id=?
55                 AND    del<>'1'",
56                 $_POST['_cid'],
57                 $_SESSION['user_id']);
4e17e6 58                        
T 59     $updated = $DB->affected_rows();
60     }
61        
62   if ($updated)
63     {
64     $_action = 'show';
65     show_message('successfullysaved', 'confirmation');    
66     
67     if ($_POST['_framed'])
68       {
69       // define list of cols to be displayed
70       $a_show_cols = array('name', 'email');
71       $a_js_cols = array();
72   
d7cb77 73       $sql_result = $DB->query("SELECT * FROM ".get_table_name('contacts')."
S 74                                 WHERE  contact_id=?
75                                 AND    user_id=?
76                                 AND    del<>'1'",
4e17e6 77                                $_POST['_cid'],
d7cb77 78                                $_SESSION['user_id']);
4e17e6 79                          
T 80       $sql_arr = $DB->fetch_assoc($sql_result);
81       foreach ($a_show_cols as $col)
82         $a_js_cols[] = (string)$sql_arr[$col];
83
84       // update the changed col in list
85       $OUTPUT->add_script(sprintf("if(parent.%s)parent.%s.update_contact_row('%d', %s);",
86                           $JS_OBJECT_NAME,
87                           $JS_OBJECT_NAME,
88                           $_POST['_cid'],
89                           array2js($a_js_cols)));
90
91       // show confirmation
92       show_message('successfullysaved', 'confirmation');
93       }
94     }
95   else
96     {
97     // show error message
98     show_message('errorsaving', 'error');
10a699 99     rcmail_overwrite_action('show');
4e17e6 100     }
T 101   }
102
103 // insert a new contact
104 else
105   {
106   $a_insert_cols = $a_insert_values = array();
10a699 107   
T 108   // check for existing contacts
109   $sql_result = $DB->query("SELECT 1 FROM ".get_table_name('contacts')."
110                             WHERE  user_id=?
111                             AND    email=?
112                             AND    del<>'1'",
113                            $_SESSION['user_id'],
114                            $_POST['_email']);
115
116   // show warning message
117   if ($DB->num_rows($sql_result))
118     {
119     show_message('contactexists', 'warning');
120     $_action = 'add';
121     return;
122     }
4e17e6 123
T 124   foreach ($a_save_cols as $col)
125     {
126     $fname = '_'.$col;
127     if (!isset($_POST[$fname]))
128       continue;
129     
130     $a_insert_cols[] = $col;
10a699 131     $a_insert_values[] = $DB->quote(strip_tags($_POST[$fname]));
4e17e6 132     }
T 133     
134   if (sizeof($a_insert_cols))
135     {
d7cb77 136     $DB->query("INSERT INTO ".get_table_name('contacts')."
10a699 137                 (user_id, changed, ".join(', ', $a_insert_cols).")
e0ddd4 138                 VALUES (?, now(), ".join(', ', $a_insert_values).")",
d7cb77 139                 $_SESSION['user_id']);
4e17e6 140                        
T 141     $insert_id = $DB->insert_id();
142     }
143     
144   if ($insert_id)
145     {
146     $_action = 'show';
147     $_GET['_cid'] = $insert_id;
148
149     if ($_POST['_framed'])
150       {
151       // add contact row or jump to the page where it should appear
152       $commands = sprintf("if(parent.%s)parent.", $JS_OBJECT_NAME);
d7cb77 153       $sql_result = $DB->query("SELECT * FROM ".get_table_name('contacts')."
S 154                                 WHERE  contact_id=?
155                                 AND    user_id=?",
156                                 $insert_id,
157                                 $_SESSION['user_id']);
4e17e6 158       $commands .= rcmail_js_contacts_list($sql_result, $JS_OBJECT_NAME);
T 159
160       $commands .= sprintf("if(parent.%s)parent.%s.select('%d');\n",
161                            $JS_OBJECT_NAME, 
162                            $JS_OBJECT_NAME,
163                            $insert_id);
164       
165       // update record count display
166       $commands .= sprintf("if(parent.%s)parent.%s.set_rowcount('%s');\n",
167                            $JS_OBJECT_NAME, 
168                            $JS_OBJECT_NAME,
169                            rcmail_get_rowcount_text());
170
171       $OUTPUT->add_script($commands);
172       
173       // show confirmation
174       show_message('successfullysaved', 'confirmation');      
175       }
176     }
177   else
178     {
179     // show error message
180     show_message('errorsaving', 'error');
10a699 181     rcmail_overwrite_action('add');
4e17e6 182     }
T 183   }
184
185
186 ?>