source: subversion/branches/devel-addressbook/program/steps/addressbook/func.inc @ 4250

Last change on this file since 4250 was 4250, checked in by thomasb, 3 years ago

Fix contacts saving; correctly handle multiple e-mail addresses in autocomplete

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.1 KB
Line 
1<?php
2
3/*
4 +-----------------------------------------------------------------------+
5 | program/steps/addressbook/func.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 |   Provide addressbook functionality and GUI objects                   |
13 |                                                                       |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16 +-----------------------------------------------------------------------+
17
18 $Id$
19
20*/
21
22// add list of address sources to client env
23$js_list = $RCMAIL->get_address_sources();
24
25// select source
26$source = get_input_value('_source', RCUBE_INPUT_GPC);
27
28// if source is not set use first directory
29if (empty($source))
30    $source = $js_list[key($js_list)]['id'];
31
32// instantiate a contacts object according to the given source
33$CONTACTS = $RCMAIL->get_address_book($source);
34
35$CONTACTS->set_pagesize($CONFIG['pagesize']);
36
37// set list properties and session vars
38if (!empty($_GET['_page']))
39    $CONTACTS->set_page(($_SESSION['page'] = intval($_GET['_page'])));
40else
41    $CONTACTS->set_page(isset($_SESSION['page']) ?$_SESSION['page'] : 1);
42 
43if (!empty($_REQUEST['_gid']))
44    $CONTACTS->set_group(get_input_value('_gid', RCUBE_INPUT_GPC));
45
46// set message set for search result
47if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']]))
48    $CONTACTS->set_search_set($_SESSION['search'][$_REQUEST['_search']]);
49
50// set data source env
51$OUTPUT->set_env('source', $source ? $source : '0');
52$OUTPUT->set_env('readonly', $CONTACTS->readonly, false);
53if (!$OUTPUT->ajax_call) {
54    $OUTPUT->set_env('address_sources', $js_list);
55    $OUTPUT->set_pagetitle(rcube_label('addressbook'));
56}
57
58
59// general definition of contact coltypes
60$CONTACT_COLTYPES = array(
61  'name'         => array('type' => 'text', 'size' => 40, 'limit' => 1, 'label' => rcube_label('name')),
62  'firstname'    => array('type' => 'text', 'size' => 19, 'limit' => 1, 'label' => rcube_label('firstname')),
63  'surname'      => array('type' => 'text', 'size' => 19, 'limit' => 1, 'label' => rcube_label('surname')),
64  'middlename'   => array('type' => 'text', 'size' => 19, 'limit' => 1, 'label' => rcube_label('middlename')),
65  'prefix'       => array('type' => 'text', 'size' => 8,  'limit' => 1, 'label' => rcube_label('nameprefix')),
66  'suffix'       => array('type' => 'text', 'size' => 8,  'limit' => 1, 'label' => rcube_label('namesuffix')),
67  'nickname'     => array('type' => 'text', 'size' => 40, 'limit' => 1, 'label' => rcube_label('nickname')),
68  'jobtitle'     => array('type' => 'text', 'size' => 40, 'limit' => 1, 'label' => rcube_label('jobtitle')),
69  'organization' => array('type' => 'text', 'size' => 19, 'limit' => 1, 'label' => rcube_label('organization')),
70  'department'   => array('type' => 'text', 'size' => 19, 'limit' => 1, 'label' => rcube_label('department')),
71  'gender'       => array('type' => 'select', 'limit' => 1, 'label' => rcube_label('gender'), 'options' => array('male' => rcube_label('male'), 'female' => rcube_label('female'))),
72  'maidenname'   => array('type' => 'text', 'size' => 40, 'limit' => 1, 'label' => rcube_label('maidenname')),
73  'email'        => array('type' => 'text', 'size' => 40, 'label' => rcube_label('email'), 'subtypes' => array('home','work','other')),
74  'phone'        => array('type' => 'text', 'size' => 40, 'label' => rcube_label('phone'), 'subtypes' => array('home','home2','work','work2','mobile','main','homefax','workfax','car','pager','video','assistant','other')),
75  'address'      => array('type' => 'composite', 'label' => rcube_label('address'), 'subtypes' => array('home','work','other'), 'childs' => array(
76    'street'     => array('type' => 'text', 'size' => 40, 'label' => rcube_label('street')),
77    'locality'   => array('type' => 'text', 'size' => 28, 'label' => rcube_label('locality')),
78    'zipcode'    => array('type' => 'text', 'size' => 8, 'label' => rcube_label('zipcode')),
79    'region'     => array('type' => 'text', 'size' => 12, 'label' => rcube_label('region')),
80    'country'    => array('type' => 'text', 'size' => 40, 'label' => rcube_label('country')),
81  )),
82  'birthday'     => array('type' => 'date', 'size' => 12, 'label' => rcube_label('birthday'), 'limit' => 1, 'render_func' => 'rcmail_format_date_col'),
83  'website'      => array('type' => 'text', 'size' => 40, 'label' => rcube_label('website'), 'subtypes' => array('home','work','blog','other')),
84  'im'           => array('type' => 'text', 'size' => 40, 'label' => rcube_label('instantmessenger'), 'subtypes' => array('aim','icq','msn','yahoo','jabber','other')),
85  'notes'        => array('type' => 'textarea', 'size' => 40, 'rows' => 15, 'label' => rcube_label('notes'), 'limit' => 1),
86  // TODO: define fields for vcards GEO, PHOTO, KEY, X-ANNIVERSARY, X-ASSISTANT, X-MANAGER
87);
88
89// reduce/extend $CONTACT_COLTYPES with specification from the current $CONTACT object
90if (is_array($CONTACTS->coltypes)) {
91    // remove cols not listed by the backend class
92    $contact_cols = $CONTACTS->coltypes[0] ? array_flip($CONTACTS->coltypes) : $CONTACTS->coltypes;
93    $CONTACT_COLTYPES = array_intersect_key($CONTACT_COLTYPES, $contact_cols);
94    // add associative coltypes definition
95    if (!$CONTACTS->coltypes[0]) {
96        foreach ($CONTACTS->coltypes as $col => $colprop)
97            $CONTACT_COLTYPES[$col] = $CONTACT_COLTYPES[$col] ? array_merge($CONTACT_COLTYPES[$col], $colprop) : $colprop;
98    }
99}
100
101
102function rcmail_directory_list($attrib)
103{
104    global $RCMAIL, $OUTPUT;
105
106    if (!$attrib['id'])
107        $attrib['id'] = 'rcmdirectorylist';
108
109    $out = '';
110    $local_id = '0';
111    $jsdata = array();
112    $current = get_input_value('_source', RCUBE_INPUT_GPC);
113    $line_templ = html::tag('li', array(
114        'id' => 'rcmli%s', 'class' => 'addressbook %s'),
115        html::a(array('href' => '%s',
116            'onclick' => "return ".JS_OBJECT_NAME.".command('list','%s',this)"), '%s'));
117
118    if (!$current && strtolower($RCMAIL->config->get('address_book_type', 'sql')) != 'ldap') {
119        $current = '0';
120    }
121    else if (!$current) {
122        // DB address book not used, see if a source is set, if not use the
123        // first LDAP directory.
124        $current = key((array)$RCMAIL->config->get('ldap_public', array()));
125    }
126
127    foreach ((array)$OUTPUT->env['address_sources'] as $j => $source) {
128        $id = $source['id'] ? $source['id'] : $j;
129        $js_id = JQ($id);
130        $dom_id = preg_replace('/[^a-z0-9\-_]/i', '', $id);
131        $out .= sprintf($line_templ, $dom_id, ($current == $id ? 'selected' : ''),
132            Q(rcmail_url(null, array('_source' => $id))),
133            $js_id, (!empty($source['name']) ? Q($source['name']) : Q($id)));
134        $groupdata = rcmail_contact_groups(array('out' => $out, 'jsdata' => $jsdata, 'source' => $id));
135        $jsdata = $groupdata['jsdata'];
136        $out = $groupdata['out'];
137    }
138
139    $OUTPUT->set_env('contactgroups', $jsdata);
140    $OUTPUT->add_gui_object('folderlist', $attrib['id']);
141
142    return html::tag('ul', $attrib, $out, html::$common_attrib);
143}
144
145
146function rcmail_contact_groups($args)
147{
148    global $RCMAIL;
149
150    $groups = $RCMAIL->get_address_book($args['source'])->list_groups();
151
152    if (!empty($groups)) {
153        $line_templ = html::tag('li', array(
154            'id' => 'rcmliG%s%s', 'class' => 'contactgroup'),
155            html::a(array('href' => '#',
156                'onclick' => "return ".JS_OBJECT_NAME.".command('listgroup',{'source':'%s','id':'%s'},this)"), '%s'));
157
158        $jsdata = array();
159        foreach ($groups as $group) {
160            $args['out'] .= sprintf($line_templ, $args['source'], $group['ID'], $args['source'], $group['ID'], Q($group['name']));
161            $args['jsdata']['G'.$args['source'].$group['ID']] = array(
162                'source' => $args['source'], 'id' => $group['ID'],
163                'name' => $group['name'], 'type' => 'group');
164        }
165    }
166
167    return $args;
168}
169
170
171// return the message list as HTML table
172function rcmail_contacts_list($attrib)
173{
174    global $CONTACTS, $OUTPUT;
175
176    // count contacts for this user
177    $result = $CONTACTS->list_records();
178
179    // add id to message list table if not specified
180    if (!strlen($attrib['id']))
181        $attrib['id'] = 'rcmAddressList';
182
183    // define list of cols to be displayed
184    $a_show_cols = array('name');
185
186    // create XHTML table
187    $out = rcube_table_output($attrib, $result->records, $a_show_cols, $CONTACTS->primary_key);
188
189    // set client env
190    $OUTPUT->add_gui_object('contactslist', $attrib['id']);
191    $OUTPUT->set_env('current_page', (int)$CONTACTS->list_page);
192    $OUTPUT->set_env('pagecount', ceil($result->count/$CONTACTS->page_size));
193    $OUTPUT->include_script('list.js');
194
195    // add some labels to client
196    $OUTPUT->add_label('deletecontactconfirm');
197
198    return $out;
199}
200
201
202function rcmail_js_contacts_list($result, $prefix='')
203{
204    global $OUTPUT;
205
206    if (empty($result) || $result->count == 0)
207        return;
208
209    // define list of cols to be displayed
210    $a_show_cols = array('name');
211 
212    while ($row = $result->next()) {
213        $a_row_cols = array();
214   
215        // format each col
216        foreach ($a_show_cols as $col)
217            $a_row_cols[$col] = Q($row[$col]);
218
219        $OUTPUT->command($prefix.'add_contact_row', $row['ID'], $a_row_cols);
220    }
221}
222
223
224// similar function as /steps/settings/identities.inc::rcmail_identity_frame()
225function rcmail_contact_frame($attrib)
226{
227    global $OUTPUT;
228
229    if (!$attrib['id'])
230        $attrib['id'] = 'rcmcontactframe';
231   
232    $attrib['name'] = $attrib['id'];
233
234    $OUTPUT->set_env('contentframe', $attrib['name']);
235    $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
236
237    return html::iframe($attrib);
238}
239
240
241function rcmail_rowcount_display($attrib)
242{
243    global $OUTPUT;
244
245    if (!$attrib['id'])
246        $attrib['id'] = 'rcmcountdisplay';
247
248    $OUTPUT->add_gui_object('countdisplay', $attrib['id']);
249
250    return html::span($attrib, rcmail_get_rowcount_text());
251}
252
253
254function rcmail_get_rowcount_text()
255{
256    global $CONTACTS;
257 
258    // read nr of contacts
259    $result = $CONTACTS->get_result();
260    if (!$result) {
261        $result = $CONTACTS->count();
262    }
263
264    if ($result->count == 0)
265        $out = rcube_label('nocontactsfound');
266    else
267        $out = rcube_label(array(
268            'name'  => 'contactsfromto',
269            'vars'  => array(
270            'from'  => $result->first + 1,
271            'to'    => min($result->count, $result->first + $CONTACTS->page_size),
272            'count' => $result->count)
273        ));
274
275    return $out;
276}
277
278
279function rcmail_contact_form($form, $record, $attrib = null)
280{
281    global $RCMAIL, $CONFIG;
282
283    // Allow plugins to modify contact form content
284    $plugin = $RCMAIL->plugins->exec_hook('contact_form', array(
285        'form' => $form, 'record' => $record));
286
287    $form = $plugin['form'];
288    $record = $plugin['record'];
289    $formdata = array();
290    $del_button = $attrib['deleteicon'] ? html::img(array('src' => $CONFIG['skin_path'] . $attrib['deleteicon'], 'alt' => rcube_label('delete'))) : rcube_label('delete');
291    $edit_mode = $RCMAIL->action != 'show';
292    $out = '';
293   
294    // get default coltypes
295    $coltypes = $GLOBALS['CONTACT_COLTYPES'];
296    $coltype_lables = array();
297   
298    foreach ($coltypes as $col => $prop) {
299        if ($prop['subtypes']) {
300            $select_subtype = new html_select(array('name' => '_subtype_'.$col.'[]', 'class' => 'contactselectsubtype'));
301            $select_subtype->add($prop['subtypes']);
302            $coltypes[$col]['subtypes_select'] = $select_subtype->show();
303        }
304        if ($prop['childs']) {
305            foreach ($prop['childs'] as $childcol => $cp)
306                $coltype_lables[$childcol] = array('label' => $cp['label']);
307        }
308    }
309
310    foreach ($form as $section => $fieldset) {
311        // skip empty sections
312        if (empty($fieldset['content']))
313            continue;
314
315        $select_add = new html_select(array('class' => 'addfieldmenu', 'rel' => $section));
316        $select_add->add(rcube_label('addfield'), '');
317
318        // render head section with name fields (not a regular list of rows)
319        if ($section == 'head') {
320            $content = '';
321           
322            $names_arr = array($record['prefix'], $record['firstname'], $record['middlename'], $record['surname'], $record['suffix']);
323            if ($record['name'] == join(' ', array_filter($names_arr)))
324              unset($record['name']);
325
326            // group fields
327            $field_blocks = array(
328                'names'    => array('prefix','firstname','middlename','surname','suffix'),
329                'addnames' => array('name','nickname'),
330                'jobnames' => array('organization','department','jobtitle'),
331            );
332            foreach ($field_blocks as $blockname => $colnames) {
333                $fields = '';
334                foreach ($colnames as $col) {
335                    // skip cols unknown to the backend
336                    if (!$coltypes[$col])
337                        continue;
338
339                    if ($RCMAIL->action == 'show') {
340                        if (!empty($record[$col]))
341                            $fields .= html::span('namefield ' . $col, Q($record[$col])) . " ";
342                    }
343                    else {
344                        $colprop = (array)$fieldset['content'][$col] + (array)$coltypes[$col];
345                        $colprop['id'] = 'ff_'.$col;
346                        if (empty($record[$col]) && !$colprop['visible']) {
347                            $colprop['style'] = 'display:none';
348                            $select_add->add($colprop['label'], $col);
349                        }
350                        $fields .= rcmail_get_edit_field($col, $record[$col], $colprop, $colprop['type']);
351                    }
352                }
353                $content .= html::div($blockname, $fields);
354            }
355           
356            if ($edit_mode)
357                $content .= html::p('addfield', $select_add->show(null));
358
359            $out .= html::tag('fieldset', $attrib, (!empty($fieldset['name']) ? html::tag('legend', null, Q($fieldset['name'])) : '') . $content) ."\n";
360            continue;
361        }
362
363        $content = '';
364        if (is_array($fieldset['content'])) {
365            foreach ($fieldset['content'] as $col => $colprop) {
366                // skip cols unknown to the backend
367                if (!$coltypes[$col])
368                    continue;
369               
370                // remove subtype part of col name
371                list($field, $subtype) = explode(':', $col);
372                if (!$subtype)
373                  $subtype = 'home';
374
375                $fullkey = $col.':'.$subtype;
376                $label = isset($colprop['label']) ? $colprop['label'] : rcube_label($col);
377               
378                // merge colprop with global coltype configuration
379                if ($coltypes[$field])
380                    $colprop += $coltypes[$field];
381
382                // prepare subtype selector in edit mode
383                if ($edit_mode && is_array($colprop['subtypes'])) {
384                    $select_subtype = new html_select(array('name' => '_subtype_'.$col.'[]', 'class' => 'contactselectsubtype'));
385                    $select_subtype->add($colprop['subtypes']);
386                }
387                else
388                    $select_subtype = null;
389
390                if (!empty($colprop['value'])) {
391                    $values = (array)$colprop['value'];
392                }
393                else {
394                    // iterate over possible subtypes and collect values with their subtype
395                    if (is_array($colprop['subtypes'])) {
396                        $values = $subtypes = array();
397                        foreach ($colprop['subtypes'] as $i => $st) {
398                            $newval = false;
399                            if ($record[$field.':'.$st]) {
400                                $subtypes[count($values)] = $st;
401                                $newval = $record[$field.':'.$st];
402                            }
403                            else if ($i == 0 && $record[$field]) {
404                                $subtypes[count($values)] = $st;
405                                $newval = $record[$field];
406                            }
407                            if ($newval !== false) {
408                                if (is_array($newval) && isset($newval[0]))
409                                    $values = array_merge($values, $newval);
410                                else
411                                    $values[] = $newval;
412                            }
413                        }
414                    }
415                    else {
416                        $values = $record[$fullkey] ?: $record[$field];
417                        $subtypes = null;
418                    }
419                }
420
421                // hack: create empty values array to force this field to be displayed
422                if (empty($values) && $colprop['visible'])
423                    $values[] = '';
424
425                $rows = '';
426                foreach ((array)$values as $i => $val) {
427                    if ($subtypes[$i])
428                        $subtype = $subtypes[$i];
429
430                    // render composite field
431                    if ($colprop['type'] == 'composite') {
432                        $composite = ''; $j = 0;
433                        foreach ($colprop['childs'] as $childcol => $cp) {
434                            $childvalue = $val[$childcol] ? $val[$childcol] : $val[$j];
435
436                            if ($edit_mode) {
437                                if ($colprop['subtypes'] || $colprop['limit'] != 1) $cp['array'] = true;
438                                $composite .= rcmail_get_edit_field($childcol, $childvalue, $cp, $cp['type']) . " ";
439                            }
440                            else {
441                                $childval = $cp['render_func'] ? call_user_func($cp['render_func'], $childvalue, $childcol) : Q($childvalue);
442                                $composite .= html::span('data ' . $childcol, $childval) . " ";
443                            }
444                            $j++;
445                        }
446
447                        $coltypes[$field] += (array)$colprop;
448                        $coltypes[$field]['count']++;
449                        $val = $composite;
450                    }
451                    else if ($edit_mode) {
452                        // call callback to render/format value
453                        if ($colprop['render_func'])
454                            $val = call_user_func($colprop['render_func'], $val, $col);
455
456                        $coltypes[$field] = (array)$colprop + $coltypes[$field];
457                        $formdata[$field][] = array('subtype' => $subtype, 'value' => $val);
458
459                        if ($colprop['subtypes'] || $colprop['limit'] != 1)
460                            $colprop['array'] = true;
461
462                        $val = rcmail_get_edit_field($col, $val, $colprop, $colprop['type']);
463                        $coltypes[$field]['count']++;
464                    }
465                    else if ($colprop['render_func'])
466                        $val = call_user_func($colprop['render_func'], $val, $col);
467                    else if (is_array($colprop['options']) && isset($colprop['options'][$val]))
468                        $val = $colprop['options'][$val];
469                    else
470                        $val = Q($val);
471
472                    // use subtype as label
473                    if ($colprop['subtypes'])
474                        $label = $subtype;
475
476                    // add delete button/link
477                    if ($edit_mode && !($colprop['visible'] && $colprop['limit'] == 1))
478                        $val .= html::a(array('href' => '#del', 'class' => 'contactfieldbutton deletebutton', 'title' => rcube_label('delete'), 'rel' => $col), $del_button);
479
480                    // display row with label
481                    if ($label) {
482                        $rows .= html::div('row',
483                            html::div('contactfieldlabel label', $select_subtype ? $select_subtype->show($subtype) : Q($label)) .
484                            html::div('contactfieldcontent '.$colprop['type'], $val));
485                    }
486                    else   // row without label
487                        $rows .= html::div('row', html::div('contactfield', $val));
488                }
489               
490                // add option to the add-field menu
491                if (!$colprop['limit'] || $coltypes[$field]['count'] < $colprop['limit']) {
492                    $select_add->add($colprop['label'], $col);
493                    $select_add->_count++;
494                }
495               
496                // wrap rows in fieldgroup container
497                $content .= html::div('contactfieldgroup contactcontroller' . $col, $rows);
498            }
499
500            // also render add-field selector
501            if ($edit_mode && $select_add->_count)
502                $content .= html::p('addfield', $select_add->show(null));
503
504            $content = html::div(array('id' => 'contactsection' . $section), $content);
505        }
506        else {
507            $content = $fieldset['content'];
508        }
509
510        $out .= html::tag('fieldset', null, html::tag('legend', null, Q($fieldset['name'])) . $content) ."\n";
511    }
512
513    if ($edit_mode) {
514      $RCMAIL->output->set_env('contactdata', $formdata);
515      $RCMAIL->output->set_env('coltypes', $coltypes + $coltype_lables);
516      $RCMAIL->output->set_env('delbutton', $del_button);
517      $RCMAIL->output->add_label('delete');
518    }
519
520    return $out;
521}
522
523
524function rcmail_format_date_col($val)
525{
526    global $RCMAIL;
527    return format_date($val, $RCMAIL->config->get('date_format', 'Y-m-d'));
528}
529
530
531// register UI objects
532$OUTPUT->add_handlers(array(
533    'directorylist' => 'rcmail_directory_list',
534//  'groupslist' => 'rcmail_contact_groups',
535    'addresslist' => 'rcmail_contacts_list',
536    'addressframe' => 'rcmail_contact_frame',
537    'recordscountdisplay' => 'rcmail_rowcount_display',
538    'searchform' => array($OUTPUT, 'search_form')
539));
Note: See TracBrowser for help on using the repository browser.