]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/save.inc
Imported Upstream version 0.2~alpha
[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 1407 2008-05-19 17:47:45Z 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)))
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     // define list of cols to be displayed
58     $a_js_cols = array();
59     $record = $CONTACTS->get_record($cid, true);
60
61     foreach (array('name', 'email') as $col)
62       $a_js_cols[] = (string)$record[$col];
63
64     // update the changed col in list
65     $OUTPUT->command('parent.update_contact_row', $cid, $a_js_cols);
66       
67     // show confirmation
68     $OUTPUT->show_message('successfullysaved', 'confirmation');    
69     rcmail_overwrite_action('show');
70   }
71   else
72   {
73     // show error message
74     $OUTPUT->show_message('errorsaving', 'error');
75     rcmail_overwrite_action('show');
76   }
77 }
78
79 // insert a new contact
80 else
81 {
82   // check for existing contacts
83   $existing = $CONTACTS->search('email', $a_record['email'], true, false);
84   
85   // show warning message
86   if ($existing->count)
87   {
88     $OUTPUT->show_message('contactexists', 'warning');
89     rcmail_overwrite_action('add');
90     return;
91   }
92
93   // insert record and send response
94   if ($insert_id = $CONTACTS->insert($a_record))
95   {
96     // add contact row or jump to the page where it should appear
97     $CONTACTS->reset();
98     $result = $CONTACTS->search($CONTACTS->primary_key, $insert_id);
99
100     rcmail_js_contacts_list($result, 'parent.');
101     $OUTPUT->command('parent.contact_list.select', $insert_id);
102
103     // update record count display
104     $CONTACTS->reset();
105     $OUTPUT->command('parent.set_rowcount', rcmail_get_rowcount_text());
106
107     // show confirmation
108     $OUTPUT->show_message('successfullysaved', 'confirmation');
109     rcmail_overwrite_action('show');
110     $_GET['_cid'] = $insert_id;
111   }
112   else
113   {
114     // show error message
115     $OUTPUT->show_message('errorsaving', 'error');
116     rcmail_overwrite_action('add');
117   }
118 }
119
120 ?>