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

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

Improve vcard saving; use callback functions to render certain contact fields; add button to delete a contact field

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