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