]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/edit.inc
528002db099410fdd05d34fc90e64d68ce954588
[roundcube.git] / program / steps / addressbook / edit.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/addressbook/edit.inc                                    |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2005-2007, The Roundcube Dev Team                       |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Show edit form for a contact entry or to add a new one              |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: edit.inc 5041 2011-08-10 11:53:41Z thomasb $
19
20 */
21
22 if ($RCMAIL->action == 'edit') {
23     // Get contact ID and source ID from request
24     $cids   = rcmail_get_cids();
25     $source = key($cids);
26     $cid    = array_shift($cids[$source]);
27
28     // Initialize addressbook
29     $CONTACTS = rcmail_contact_source($source, true);
30
31     // Contact edit
32     if ($cid && ($record = $CONTACTS->get_record($cid, true))) {
33         $OUTPUT->set_env('cid', $record['ID']);
34     }
35
36     // adding not allowed here
37     if ($CONTACTS->readonly) {
38         $OUTPUT->show_message('sourceisreadonly');
39         rcmail_overwrite_action('show');
40         return;
41     }
42 }
43 else {
44     $source = get_input_value('_source', RCUBE_INPUT_GPC);
45
46     if (!strlen($source)) {
47         // Give priority to configured default
48         $source = $RCMAIL->config->get('default_addressbook');
49     }
50
51     $CONTACTS = $RCMAIL->get_address_book($source, true);
52
53     // find writable addressbook
54     if (!$CONTACTS || $CONTACTS->readonly)
55         $source = rcmail_default_source(true);
56
57     // Initialize addressbook
58     $CONTACTS = rcmail_contact_source($source, true);
59 }
60
61 $SOURCE_ID = $source;
62 rcmail_set_sourcename($CONTACTS);
63
64 function rcmail_get_edit_record()
65 {
66     global $RCMAIL, $CONTACTS;
67
68      // check if we have a valid result
69      if ($GLOBALS['EDIT_RECORD']) {
70          $record = $GLOBALS['EDIT_RECORD'];
71      }
72      else if ($RCMAIL->action != 'add'
73          && !(($result = $CONTACTS->get_result()) && ($record = $result->first()))
74      ) {
75          $RCMAIL->output->show_message('contactnotfound');
76          return false;
77      }
78
79      return $record;
80 }
81
82 function rcmail_contact_edithead($attrib)
83 {
84     // check if we have a valid result
85     $record = rcmail_get_edit_record();
86     $i_size = !empty($attrib['size']) ? $attrib['size'] : 20;
87
88     $form = array(
89         'head' => array(
90             'content' => array(
91                 'prefix' => array('size' => $i_size),
92                 'firstname' => array('size' => $i_size, 'visible' => true),
93                 'middlename' => array('size' => $i_size),
94                 'surname' => array('size' => $i_size, 'visible' => true),
95                 'suffix' => array('size' => $i_size),
96                 'name' => array('size' => 2*$i_size),
97                 'nickname' => array('size' => 2*$i_size),
98                 'organization' => array('size' => 2*$i_size),
99                 'department' => array('size' => 2*$i_size),
100                 'jobtitle' => array('size' => 2*$i_size),
101             )
102         )
103     );
104
105     list($form_start, $form_end) = get_form_tags($attrib);
106     unset($attrib['form'], $attrib['name'], $attrib['size']);
107
108     // return the address edit form
109     $out = rcmail_contact_form($form, $record, $attrib);
110
111     return $form_start . $out . $form_end;
112 }
113
114 function rcmail_contact_editform($attrib)
115 {
116     global $RCMAIL, $CONTACT_COLTYPES;
117
118     $record = rcmail_get_edit_record();
119
120     // add some labels to client
121     $RCMAIL->output->add_label('noemailwarning', 'nonamewarning');
122
123     // copy (parsed) address template to client
124     if (preg_match_all('/\{([a-z0-9]+)\}([^{]*)/i', $RCMAIL->config->get('address_template', ''), $templ, PREG_SET_ORDER))
125       $RCMAIL->output->set_env('address_template', $templ);
126
127     $i_size = !empty($attrib['size']) ? $attrib['size'] : 40;
128     $t_rows = !empty($attrib['textarearows']) ? $attrib['textarearows'] : 10;
129     $t_cols = !empty($attrib['textareacols']) ? $attrib['textareacols'] : 40;
130
131     $form = array(
132         'contact' => array(
133             'name'    => rcube_label('contactproperties'),
134             'content' => array(
135                 'email' => array('size' => $i_size, 'visible' => true),
136                 'phone' => array('size' => $i_size, 'visible' => true),
137                 'address' => array('visible' => true),
138                 'website' => array('size' => $i_size),
139                 'im' => array('size' => $i_size),
140             ),
141         ),
142         'personal' => array(
143             'name'    => rcube_label('personalinfo'),
144             'content' => array(
145                 'gender' => array('visible' => true),
146                 'maidenname' => array('size' => $i_size),
147                 'birthday' => array('visible' => true),
148                 'anniversary' => array(),
149                 'manager' => array('size' => $i_size),
150                 'assistant' => array('size' => $i_size),
151                 'spouse' => array('size' => $i_size),
152             ),
153         ),
154     );
155
156     if (isset($CONTACT_COLTYPES['notes'])) {
157         $form['notes'] = array(
158             'name'    => rcube_label('notes'),
159             'content' => array(
160                 'notes' => array('size' => $t_cols, 'rows' => $t_rows, 'label' => false, 'visible' => true, 'limit' => 1),
161             ),
162             'single' => true,
163         );
164     }
165
166     list($form_start, $form_end) = get_form_tags($attrib);
167     unset($attrib['form']);
168
169     // return the complete address edit form as table
170     $out = rcmail_contact_form($form, $record, $attrib);
171
172     return $form_start . $out . $form_end;
173 }
174
175 function rcmail_upload_photo_form($attrib)
176 {
177   global $OUTPUT;
178
179   // add ID if not given
180   if (!$attrib['id'])
181     $attrib['id'] = 'rcmUploadbox';
182
183   // find max filesize value
184   $max_filesize = parse_bytes(ini_get('upload_max_filesize'));
185   $max_postsize = parse_bytes(ini_get('post_max_size'));
186   if ($max_postsize && $max_postsize < $max_filesize)
187     $max_filesize = $max_postsize;
188   $max_filesize = show_bytes($max_filesize);
189
190   $hidden = new html_hiddenfield(array('name' => '_cid', 'value' => $GLOBALS['cid']));
191   $input = new html_inputfield(array('type' => 'file', 'name' => '_photo', 'size' => $attrib['size']));
192   $button = new html_inputfield(array('type' => 'button'));
193
194   $out = html::div($attrib,
195     $OUTPUT->form_tag(array('name' => 'uploadform', 'method' => 'post', 'enctype' => 'multipart/form-data'),
196       $hidden->show() .
197       html::div(null, $input->show()) .
198       html::div('hint', rcube_label(array('name' => 'maxuploadsize', 'vars' => array('size' => $max_filesize)))) .
199       html::div('buttons',
200         $button->show(rcube_label('close'), array('class' => 'button', 'onclick' => "$('#$attrib[id]').hide()")) . ' ' .
201         $button->show(rcube_label('upload'), array('class' => 'button mainaction', 'onclick' => JS_OBJECT_NAME . ".command('upload-photo', this.form)"))
202       )
203     )
204   );
205
206   $OUTPUT->add_label('addphoto','replacephoto');
207   $OUTPUT->add_gui_object('uploadbox', $attrib['id']);
208   return $out;
209 }
210
211 // similar function as in /steps/settings/edit_identity.inc
212 function get_form_tags($attrib)
213 {
214     global $CONTACTS, $EDIT_FORM, $RCMAIL, $SOURCE_ID;
215
216     $form_start = $form_end = '';
217
218     if (empty($EDIT_FORM)) {
219         $hiddenfields = new html_hiddenfield();
220
221         if ($RCMAIL->action == 'edit')
222             $hiddenfields->add(array('name' => '_source', 'value' => $SOURCE_ID));
223         $hiddenfields->add(array('name' => '_gid', 'value' => $CONTACTS->group_id));
224
225         if (($result = $CONTACTS->get_result()) && ($record = $result->first()))
226             $hiddenfields->add(array('name' => '_cid', 'value' => $record['ID']));
227
228         $form_start = $RCMAIL->output->request_form(array(
229             'name' => "form", 'method' => "post",
230             'task' => $RCMAIL->task, 'action' => 'save',
231             'request' => 'save.'.intval($record['ID']),
232             'noclose' => true) + $attrib, $hiddenfields->show());
233         $form_end = !strlen($attrib['form']) ? '</form>' : '';
234
235         $EDIT_FORM = !empty($attrib['form']) ? $attrib['form'] : 'form';
236         $RCMAIL->output->add_gui_object('editform', $EDIT_FORM);
237     }
238
239     return array($form_start, $form_end); 
240 }
241
242 function rcmail_source_selector($attrib)
243 {
244     global $RCMAIL, $SOURCE_ID;
245
246     $sources_list = $RCMAIL->get_address_sources(true);
247
248     if (count($sources_list) < 2) {
249         $source = $sources_list[$SOURCE_ID];
250         $hiddenfield = new html_hiddenfield(array('name' => '_source', 'value' => $SOURCE_ID));
251         return html::span($attrib, Q($source['name']) . $hiddenfield->show());
252     }
253
254     $attrib['name'] = '_source';
255     $attrib['onchange'] = JS_OBJECT_NAME . ".command('save', 'reload', this.form)";
256
257     $select = new html_select($attrib);
258
259     foreach ($sources_list as $source)
260         $select->add($source['name'], $source['id']);
261
262     return $select->show($SOURCE_ID);
263 }
264
265
266 $OUTPUT->add_handlers(array(
267     'contactedithead' => 'rcmail_contact_edithead',
268     'contacteditform' => 'rcmail_contact_editform',
269     'contactphoto'    => 'rcmail_contact_photo',
270     'photouploadform' => 'rcmail_upload_photo_form',
271     'sourceselector'  => 'rcmail_source_selector',
272 ));
273
274 if ($RCMAIL->action == 'add' && $OUTPUT->template_exists('contactadd'))
275     $OUTPUT->send('contactadd');
276
277 // this will be executed if no template for addcontact exists
278 $OUTPUT->send('contactedit');