source: subversion/trunk/roundcubemail/program/steps/addressbook/edit.inc @ 5680

Last change on this file since 5680 was 5680, checked in by alec, 18 months ago
  • Fix label too long (#1488283), fix polish translation
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
Line 
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$
19
20*/
21
22if ($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}
43else {
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;
62rcmail_set_sourcename($CONTACTS);
63
64function 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
82function 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
114function 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('properties'),
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
175function rcmail_upload_photo_form($attrib)
176{
177  global $OUTPUT;
178
179  // set defaults
180  $attrib += array('id' => 'rcmUploadform', 'buttons' => 'yes');
181
182  // find max filesize value
183  $max_filesize = parse_bytes(ini_get('upload_max_filesize'));
184  $max_postsize = parse_bytes(ini_get('post_max_size'));
185  if ($max_postsize && $max_postsize < $max_filesize)
186    $max_filesize = $max_postsize;
187  $max_filesize = show_bytes($max_filesize);
188
189  $hidden = new html_hiddenfield(array('name' => '_cid', 'value' => $GLOBALS['cid']));
190  $input = new html_inputfield(array('type' => 'file', 'name' => '_photo', 'size' => $attrib['size']));
191  $button = new html_inputfield(array('type' => 'button'));
192
193  $out = html::div($attrib,
194    $OUTPUT->form_tag(array('id' => $attrib['id'].'Frm', 'name' => 'uploadform', 'method' => 'post', 'enctype' => 'multipart/form-data'),
195      $hidden->show() .
196      html::div(null, $input->show()) .
197      html::div('hint', rcube_label(array('name' => 'maxuploadsize', 'vars' => array('size' => $max_filesize)))) .
198      (get_boolean($attrib['buttons']) ? html::div('buttons',
199        $button->show(rcube_label('close'), array('class' => 'button', 'onclick' => "$('#$attrib[id]').hide()")) . ' ' .
200        $button->show(rcube_label('upload'), array('class' => 'button mainaction', 'onclick' => JS_OBJECT_NAME . ".command('upload-photo', this.form)"))
201      ) : '')
202    )
203  );
204
205  $OUTPUT->add_label('addphoto','replacephoto');
206  $OUTPUT->add_gui_object('uploadform', $attrib['id'].'Frm');
207  return $out;
208}
209
210// similar function as in /steps/settings/edit_identity.inc
211function get_form_tags($attrib)
212{
213    global $CONTACTS, $EDIT_FORM, $RCMAIL, $SOURCE_ID;
214
215    $form_start = $form_end = '';
216
217    if (empty($EDIT_FORM)) {
218        $hiddenfields = new html_hiddenfield();
219
220        if ($RCMAIL->action == 'edit')
221            $hiddenfields->add(array('name' => '_source', 'value' => $SOURCE_ID));
222        $hiddenfields->add(array('name' => '_gid', 'value' => $CONTACTS->group_id));
223
224        if (($result = $CONTACTS->get_result()) && ($record = $result->first()))
225            $hiddenfields->add(array('name' => '_cid', 'value' => $record['ID']));
226
227        $form_start = $RCMAIL->output->request_form(array(
228            'name' => "form", 'method' => "post",
229            'task' => $RCMAIL->task, 'action' => 'save',
230            'request' => 'save.'.intval($record['ID']),
231            'noclose' => true) + $attrib, $hiddenfields->show());
232        $form_end = !strlen($attrib['form']) ? '</form>' : '';
233
234        $EDIT_FORM = !empty($attrib['form']) ? $attrib['form'] : 'form';
235        $RCMAIL->output->add_gui_object('editform', $EDIT_FORM);
236    }
237
238    return array($form_start, $form_end);
239}
240
241function rcmail_source_selector($attrib)
242{
243    global $RCMAIL, $SOURCE_ID;
244
245    $sources_list = $RCMAIL->get_address_sources(true);
246
247    if (count($sources_list) < 2) {
248        $source = $sources_list[$SOURCE_ID];
249        $hiddenfield = new html_hiddenfield(array('name' => '_source', 'value' => $SOURCE_ID));
250        return html::span($attrib, Q($source['name']) . $hiddenfield->show());
251    }
252
253    $attrib['name'] = '_source';
254    $attrib['onchange'] = JS_OBJECT_NAME . ".command('save', 'reload', this.form)";
255
256    $select = new html_select($attrib);
257
258    foreach ($sources_list as $source)
259        $select->add($source['name'], $source['id']);
260
261    return $select->show($SOURCE_ID);
262}
263
264
265$OUTPUT->add_handlers(array(
266    'contactedithead' => 'rcmail_contact_edithead',
267    'contacteditform' => 'rcmail_contact_editform',
268    'contactphoto'    => 'rcmail_contact_photo',
269    'photouploadform' => 'rcmail_upload_photo_form',
270    'sourceselector'  => 'rcmail_source_selector',
271));
272
273if ($RCMAIL->action == 'add' && $OUTPUT->template_exists('contactadd'))
274    $OUTPUT->send('contactadd');
275
276// this will be executed if no template for addcontact exists
277$OUTPUT->send('contactedit');
Note: See TracBrowser for help on using the repository browser.