thomascube
2006-08-30 107bde9cfd9a0392d18544b5a433552ce6f2f0a6
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
10a699 22 // check input
d1d2c4 23 if ((empty($_POST['_name']) || empty($_POST['_email'])) && empty($_GET['_framed']))
10a699 24   {
T 25   show_message('formincomplete', 'warning');
ea7c46 26   rcmail_overwrite_action(empty($_POST['_cid']) ? 'add' : 'show');
10a699 27   return;
T 28   }
29
d1d2c4 30 // setup some vars we need
64009e 31 $a_save_cols = array('name', 'firstname', 'surname', 'email');
d1d2c4 32 $contacts_table = get_table_name('contacts');
10a699 33
4e17e6 34 // update an existing contact
ea7c46 35 if (!empty($_POST['_cid']))
4e17e6 36   {
T 37   $a_write_sql = array();
38
39   foreach ($a_save_cols as $col)
40     {
41     $fname = '_'.$col;
42     if (!isset($_POST[$fname]))
43       continue;
44     
13c1af 45     $a_write_sql[] = sprintf("%s=%s",
T 46                              $DB->quoteIdentifier($col),
ea7c46 47                              $DB->quote(get_input_value($fname, RCUBE_INPUT_POST)));
4e17e6 48     }
T 49
50   if (sizeof($a_write_sql))
51     {
d1d2c4 52     $DB->query("UPDATE $contacts_table
107bde 53                 SET    changed=".$DB->now().", ".join(', ', $a_write_sql)."
d7cb77 54                 WHERE  contact_id=?
S 55                 AND    user_id=?
1cded8 56                 AND    del<>1",
d7cb77 57                 $_POST['_cid'],
S 58                 $_SESSION['user_id']);
4e17e6 59                        
T 60     $updated = $DB->affected_rows();
61     }
62        
63   if ($updated)
64     {
65     $_action = 'show';
66     show_message('successfullysaved', 'confirmation');    
67     
ea7c46 68     if ($_framed)
4e17e6 69       {
T 70       // define list of cols to be displayed
71       $a_show_cols = array('name', 'email');
72       $a_js_cols = array();
73   
d1d2c4 74       $sql_result = $DB->query("SELECT * FROM $contacts_table
d7cb77 75                                 WHERE  contact_id=?
S 76                                 AND    user_id=?
1cded8 77                                 AND    del<>1",
4e17e6 78                                $_POST['_cid'],
d7cb77 79                                $_SESSION['user_id']);
4e17e6 80                          
T 81       $sql_arr = $DB->fetch_assoc($sql_result);
82       foreach ($a_show_cols as $col)
83         $a_js_cols[] = (string)$sql_arr[$col];
84
85       // update the changed col in list
86       $OUTPUT->add_script(sprintf("if(parent.%s)parent.%s.update_contact_row('%d', %s);",
87                           $JS_OBJECT_NAME,
88                           $JS_OBJECT_NAME,
89                           $_POST['_cid'],
90                           array2js($a_js_cols)));
91
92       // show confirmation
93       show_message('successfullysaved', 'confirmation');
94       }
95     }
96   else
97     {
98     // show error message
99     show_message('errorsaving', 'error');
10a699 100     rcmail_overwrite_action('show');
4e17e6 101     }
T 102   }
103
104 // insert a new contact
105 else
106   {
107   $a_insert_cols = $a_insert_values = array();
d1d2c4 108
10a699 109   // check for existing contacts
d1d2c4 110   $sql = "SELECT 1 FROM $contacts_table
S 111           WHERE  user_id = {$_SESSION['user_id']}
112           AND del <> '1' ";
113
114   // get email and name, build sql for existing user check
115   if (isset($_GET['_emails']) && isset($_GET['_names']))
116     {
117     $sql   .= "AND email IN (";
ea7c46 118     $emails = explode(',', get_input_value('_emails', RCUBE_INPUT_GET));
T 119     $names  = explode(',', get_input_value('_names', RCUBE_INPUT_GET));
d1d2c4 120     $count  = count($emails);
S 121     $n = 0;
122     foreach ($emails as $email)
123       {
124       $end  = (++$n == $count) ? '' : ',';
ea7c46 125       $sql .= $DB->quote($email) . $end;
d1d2c4 126       }
S 127     $sql .= ")";
128     $ldap_form = true; 
129     }
130   else if (isset($_POST['_email'])) 
ea7c46 131     $sql  .= "AND email = " . $DB->quote(get_input_value('_email', RCUBE_INPUT_POST));
d1d2c4 132
S 133   $sql_result = $DB->query($sql);
10a699 134
T 135   // show warning message
136   if ($DB->num_rows($sql_result))
137     {
138     show_message('contactexists', 'warning');
d1d2c4 139
S 140     if ($ldap_form)
141       rcmail_overwrite_action('ldappublicsearch');
142     else
143       rcmail_overwrite_action('add');
144
10a699 145     return;
T 146     }
4e17e6 147
d1d2c4 148   if ($ldap_form)
4e17e6 149     {
d1d2c4 150     $n = 0; 
S 151     foreach ($emails as $email) 
152       {
153       $DB->query("INSERT INTO $contacts_table 
949dea 154                  (user_id, name, email)
ea7c46 155                  VALUES ({$_SESSION['user_id']}," . $DB->quote($names[$n++]) . "," . 
T 156                                       $DB->quote($email) . ")");
d1d2c4 157       $insert_id[] = $DB->insert_id();
S 158       }
4e17e6 159     }
d1d2c4 160   else
4e17e6 161     {
d1d2c4 162     foreach ($a_save_cols as $col)
S 163       {
164       $fname = '_'.$col;
165       if (!isset($_POST[$fname]))
166         continue;
167     
168       $a_insert_cols[] = $col;
ea7c46 169       $a_insert_values[] = $DB->quote(get_input_value($fname, RCUBE_INPUT_POST));
d1d2c4 170       }
S 171     
172     if (sizeof($a_insert_cols))
173       {
174       $DB->query("INSERT INTO $contacts_table
107bde 175                   (user_id, changed, del, ".join(', ', $a_insert_cols).")
T 176                   VALUES (?, ".$DB->now().", 0, ".join(', ', $a_insert_values).")",
d7cb77 177                 $_SESSION['user_id']);
4e17e6 178                        
d1d2c4 179       $insert_id = $DB->insert_id(get_sequence_name('contacts'));
S 180       }
4e17e6 181     }
T 182     
183   if ($insert_id)
184     {
d1d2c4 185     if (!$ldap_form)
S 186       {
187       $_action = 'show';
188       $_GET['_cid'] = $insert_id;
4e17e6 189
ea7c46 190       if ($_framed)
d1d2c4 191         {
S 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
195                                   WHERE  contact_id=?
196                                   AND    user_id=?",
197                                   $insert_id,
198                                   $_SESSION['user_id']);
199         $commands .= rcmail_js_contacts_list($sql_result, $JS_OBJECT_NAME);
200
201         $commands .= sprintf("if(parent.%s)parent.%s.select('%d');\n",
202                              $JS_OBJECT_NAME, 
203                              $JS_OBJECT_NAME,
204                              $insert_id);
205       
206         // update record count display
207         $commands .= sprintf("if(parent.%s)parent.%s.set_rowcount('%s');\n",
208                              $JS_OBJECT_NAME, 
209                              $JS_OBJECT_NAME,
210                              rcmail_get_rowcount_text());
211
212         $OUTPUT->add_script($commands);
213         }
214
215       // show confirmation
216       show_message('successfullysaved', 'confirmation');      
217       }
218     else 
4e17e6 219       {
T 220       // add contact row or jump to the page where it should appear
d1d2c4 221       $commands = '';
S 222       foreach ($insert_id as $id) 
223         {
224         $sql_result = $DB->query("SELECT * FROM $contacts_table
225                                   WHERE  contact_id = $id
226                                   AND    user_id    = {$_SESSION['user_id']}");
227         
228         $commands .= sprintf("if(parent.%s)parent.", $JS_OBJECT_NAME);
229         $commands .= rcmail_js_contacts_list($sql_result, $JS_OBJECT_NAME);
230         $last_id = $id;
231         }
4e17e6 232
d1d2c4 233       // display the last insert id
4e17e6 234       $commands .= sprintf("if(parent.%s)parent.%s.select('%d');\n",
d1d2c4 235                             $JS_OBJECT_NAME, 
S 236                             $JS_OBJECT_NAME,
237                             $last_id);
238
4e17e6 239       // update record count display
T 240       $commands .= sprintf("if(parent.%s)parent.%s.set_rowcount('%s');\n",
241                            $JS_OBJECT_NAME, 
242                            $JS_OBJECT_NAME,
243                            rcmail_get_rowcount_text());
244
245       $OUTPUT->add_script($commands);
d1d2c4 246       rcmail_overwrite_action('ldappublicsearch');
4e17e6 247       }
d1d2c4 248
S 249     // show confirmation
250     show_message('successfullysaved', 'confirmation');      
4e17e6 251     }
T 252   else
253     {
254     // show error message
255     show_message('errorsaving', 'error');
10a699 256     rcmail_overwrite_action('add');
4e17e6 257     }
T 258   }
259
d1d2c4 260 ?>