]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/settings/save_identity.inc
Imported Upstream version 0.1~rc1~dfsg
[roundcube.git] / program / steps / settings / save_identity.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/settings/save_identity.inc                              |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland                 |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Save an identity record or to add a new one                         |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: save_identity.inc 543 2007-04-28 18:07:12Z thomasb $
19
20 */
21
22 $a_save_cols = array('name', 'email', 'organization', 'reply-to', 'bcc', 'standard', 'signature', 'html_signature');
23 $a_html_cols = array('signature');
24 $a_boolean_cols = array('standard', 'html_signature');
25 $updated = $default_id = false;
26
27 // check input
28 if (empty($_POST['_name']) || empty($_POST['_email']))
29   {
30   $OUTPUT->show_message('formincomplete', 'warning');
31   rcmail_overwrite_action('edit-identitiy');
32   return;
33   }
34
35
36 // update an existing contact
37 if ($_POST['_iid'])
38   {
39   $a_write_sql = array();
40
41   foreach ($a_save_cols as $col)
42     {
43     $fname = '_'.$col;
44     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, in_array($col, $a_html_cols))));
48     }
49
50   // set "off" values for checkboxes that were not checked, and therefore
51   // not included in the POST body.
52   foreach ($a_boolean_cols as $col)
53     {
54     $fname = '_' . $col;
55     if (!isset($_POST[$fname]))
56       $a_write_sql[] = sprintf("%s=0", $DB->quoteIdentifier($col));
57     }
58
59   if (sizeof($a_write_sql))
60     {
61     $DB->query(
62       "UPDATE ".get_table_name('identities')."
63        SET ".join(', ', $a_write_sql)."
64        WHERE  identity_id=?
65        AND    user_id=?
66        AND    del<>1",
67       get_input_value('_iid', RCUBE_INPUT_POST),
68       $_SESSION['user_id']);
69                        
70     $updated = $DB->affected_rows();
71     }
72        
73   if ($updated)
74     {
75     $OUTPUT->show_message('successfullysaved', 'confirmation');
76     
77     if (!empty($_POST['_standard']))
78       $default_id = get_input_value('_iid', RCUBE_INPUT_POST);
79     
80     if ($_POST['_framed'])
81       {
82       // update the changed col in list
83       // ...      
84       }
85     }
86   else if ($DB->is_error())
87     {
88     // show error message
89     $OUTPUT->show_message('errorsaving', 'error');
90     rcmail_overwrite_action('edit-identitiy');
91     return;
92     }
93   }
94
95 // insert a new contact
96 else
97   {
98   $a_insert_cols = $a_insert_values = array();
99
100   foreach ($a_save_cols as $col)
101     {
102     $fname = '_'.$col;
103     if (!isset($_POST[$fname]))
104       continue;
105     
106     $a_insert_cols[] = $DB->quoteIdentifier($col);
107     $a_insert_values[] = $DB->quote(get_input_value($fname, RCUBE_INPUT_POST, in_array($col, $a_html_cols)));
108     }
109     
110   if (sizeof($a_insert_cols))
111     {
112     $DB->query("INSERT INTO ".get_table_name('identities')."
113                 (user_id, ".join(', ', $a_insert_cols).")
114                 VALUES (?, ".join(', ', $a_insert_values).")",
115                 $_SESSION['user_id']);
116
117     $insert_id = $DB->insert_id(get_sequence_name('identities'));
118     }
119     
120   if ($insert_id)
121     {
122     $_GET['_iid'] = $insert_id;
123
124     if (!empty($_POST['_standard']))
125       $default_id = $insert_id;
126
127     if ($_POST['_framed'])
128       {
129       // add contact row or jump to the page where it should appear
130       // ....
131       }
132     }
133   else
134     {
135     // show error message
136     $OUTPUT->show_message('errorsaving', 'error');
137     rcmail_overwrite_action('edit-identity');
138     return;
139     }
140   }
141
142
143 // mark all other identities as 'not-default'
144 if ($default_id)
145   $DB->query(
146     "UPDATE ".get_table_name('identities')."
147      SET ".$DB->quoteIdentifier('standard')."='0'
148      WHERE  user_id=?
149      AND    identity_id<>?
150      AND    del<>1",
151     $_SESSION['user_id'],
152     $default_id);
153
154 // go to next step
155 rcmail_overwrite_action($_framed ? 'edit-identity' : 'identities');
156
157 ?>