]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/save.inc
5a65d864b5e5bf298e02b024b343241c82dcb62c
[roundcube.git] / program / steps / addressbook / save.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/addressbook/save.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 a contact entry or to add a new one                            |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: save.inc 543 2007-04-28 18:07:12Z thomasb $
19
20 */
21
22 // cannot edit record
23 if ($CONTACTS->readonly)
24 {
25   $OUTPUT->show_message('contactreadonly', 'error');
26   rcmail_overwrite_action(empty($_POST['_cid']) ? 'add' : 'show');
27   return;
28 }
29
30 // check input
31 if ((!get_input_value('_name', RCUBE_INPUT_POST) || !get_input_value('_email', RCUBE_INPUT_POST)) && $_framed)
32 {
33   $OUTPUT->show_message('formincomplete', 'warning');
34   rcmail_overwrite_action(empty($_POST['_cid']) ? 'add' : 'show');
35   return;
36 }
37
38
39 // setup some vars we need
40 $a_save_cols = array('name', 'firstname', 'surname', 'email');
41 $a_record = array();
42 $cid = get_input_value('_cid', RCUBE_INPUT_POST);
43
44 // read POST values into hash array
45 foreach ($a_save_cols as $col)
46 {
47   $fname = '_'.$col;
48   if (isset($_POST[$fname]))
49     $a_record[$col] = get_input_value($fname, RCUBE_INPUT_POST);
50 }
51
52 // update an existing contact
53 if (!empty($cid))
54 {
55   if ($CONTACTS->update($cid, $a_record))
56   {
57     if ($_framed)
58     {
59       // define list of cols to be displayed
60       $a_js_cols = array();
61       $record = $CONTACTS->get_record($cid, true);
62
63       foreach (array('name', 'email') as $col)
64         $a_js_cols[] = (string)$record[$col];
65
66       // update the changed col in list
67       $OUTPUT->command('parent.update_contact_row', $cid, $a_js_cols);
68     }
69       
70     // show confirmation
71     $OUTPUT->show_message('successfullysaved', 'confirmation');    
72     rcmail_overwrite_action('show');
73   }
74   else
75   {
76     // show error message
77     $OUTPUT->show_message('errorsaving', 'error');
78     rcmail_overwrite_action('show');
79   }
80 }
81
82 // insert a new contact
83 else
84 {
85   // check for existing contacts
86   $existing = $CONTACTS->search('email', $a_record['email'], false);
87   
88   // show warning message
89   if ($existing->count)
90   {
91     $OUTPUT->show_message('contactexists', 'warning');
92     rcmail_overwrite_action('add');
93     return;
94   }
95
96   // insert record and send response
97   if ($insert_id = $CONTACTS->insert($a_record))
98   {
99     if ($_framed)
100     {
101       // add contact row or jump to the page where it should appear
102       $CONTACTS->reset();
103       $result = $CONTACTS->search($CONTACTS->primary_key, $insert_id);
104
105       rcmail_js_contacts_list($result, 'parent.');
106       $OUTPUT->command('parent.contact_list.select', $insert_id);
107
108       // update record count display
109       $CONTACTS->reset();
110       $OUTPUT->command('parent.set_rowcount', rcmail_get_rowcount_text());
111     }
112
113     // show confirmation
114     $OUTPUT->show_message('successfullysaved', 'confirmation');
115     rcmail_overwrite_action('show');
116     $_GET['_cid'] = $insert_id;
117   }
118   else
119   {
120     // show error message
121     $OUTPUT->show_message('errorsaving', 'error');
122     rcmail_overwrite_action('add');
123   }
124 }
125
126 ?>