source: subversion/trunk/roundcubemail/program/steps/addressbook/func.inc @ 5662

Last change on this file since 5662 was 5662, checked in by alec, 18 months ago
  • Add separate pagesize setting for mail messages and contacts (#1488269)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.7 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, The Roundcube Dev Team                       |
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$SEARCH_MODS_DEFAULT = array('name'=>1, 'firstname'=>1, 'surname'=>1, 'email'=>1, '*'=>1);
23
24// general definition of contact coltypes
25$CONTACT_COLTYPES = array(
26  'name'         => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('name'), 'category' => 'main'),
27  'firstname'    => array('type' => 'text', 'size' => 19, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('firstname'), 'category' => 'main'),
28  'surname'      => array('type' => 'text', 'size' => 19, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('surname'), 'category' => 'main'),
29  'email'        => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => rcube_label('email'), 'subtypes' => array('home','work','other'), 'category' => 'main'),
30  'middlename'   => array('type' => 'text', 'size' => 19, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('middlename'), 'category' => 'main'),
31  'prefix'       => array('type' => 'text', 'size' => 8,  'maxlength' => 20, 'limit' => 1, 'label' => rcube_label('nameprefix'), 'category' => 'main'),
32  'suffix'       => array('type' => 'text', 'size' => 8,  'maxlength' => 20, 'limit' => 1, 'label' => rcube_label('namesuffix'), 'category' => 'main'),
33  'nickname'     => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('nickname'), 'category' => 'main'),
34  'jobtitle'     => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('jobtitle'), 'category' => 'main'),
35  'organization' => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('organization'), 'category' => 'main'),
36  'department'   => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('department'), 'category' => 'main'),
37  'gender'       => array('type' => 'select', 'limit' => 1, 'label' => rcube_label('gender'), 'options' => array('male' => rcube_label('male'), 'female' => rcube_label('female')), 'category' => 'personal'),
38  'maidenname'   => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('maidenname'), 'category' => 'personal'),
39  'phone'        => array('type' => 'text', 'size' => 40, 'maxlength' => 20, 'label' => rcube_label('phone'), 'subtypes' => array('home','home2','work','work2','mobile','main','homefax','workfax','car','pager','video','assistant','other'), 'category' => 'main'),
40  'address'      => array('type' => 'composite', 'label' => rcube_label('address'), 'subtypes' => array('home','work','other'), 'childs' => array(
41    'street'     => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => rcube_label('street'), 'category' => 'main'),
42    'locality'   => array('type' => 'text', 'size' => 28, 'maxlength' => 50, 'label' => rcube_label('locality'), 'category' => 'main'),
43    'zipcode'    => array('type' => 'text', 'size' => 8,  'maxlength' => 15, 'label' => rcube_label('zipcode'), 'category' => 'main'),
44    'region'     => array('type' => 'text', 'size' => 12, 'maxlength' => 50, 'label' => rcube_label('region'), 'category' => 'main'),
45    'country'    => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => rcube_label('country'), 'category' => 'main'),
46  ), 'category' => 'main'),
47  'birthday'     => array('type' => 'date', 'size' => 12, 'maxlength' => 16, 'label' => rcube_label('birthday'), 'limit' => 1, 'render_func' => 'rcmail_format_date_col', 'category' => 'personal'),
48  'anniversary'  => array('type' => 'date', 'size' => 12, 'maxlength' => 16, 'label' => rcube_label('anniversary'), 'limit' => 1, 'render_func' => 'rcmail_format_date_col', 'category' => 'personal'),
49  'website'      => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => rcube_label('website'), 'subtypes' => array('homepage','work','blog','profile','other'), 'category' => 'main'),
50  'im'           => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => rcube_label('instantmessenger'), 'subtypes' => array('aim','icq','msn','yahoo','jabber','skype','other'), 'category' => 'main'),
51  'notes'        => array('type' => 'textarea', 'size' => 40, 'rows' => 15, 'maxlength' => 500, 'label' => rcube_label('notes'), 'limit' => 1),
52  'photo'        => array('type' => 'image', 'limit' => 1, 'category' => 'main'),
53  'assistant'    => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('assistant'), 'category' => 'personal'),
54  'manager'      => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('manager'), 'category' => 'personal'),
55  'spouse'       => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('spouse'), 'category' => 'personal'),
56  // TODO: define fields for vcards like GEO, KEY
57);
58
59$PAGE_SIZE = $RCMAIL->config->get('addressbook_pagesize');
60if (!$PAGE_SIZE) {
61    $PAGE_SIZE = $RCMAIL->config->get('pagesize', 50);
62}
63
64// Addressbook UI
65if (!$RCMAIL->action && !$OUTPUT->ajax_call) {
66    // add list of address sources to client env
67    $js_list = $RCMAIL->get_address_sources();
68
69    $source = get_input_value('_source', RCUBE_INPUT_GPC);
70
71    // use first directory by default
72    if (!strlen($source) || !isset($js_list[$source]))
73        $source = $js_list[key($js_list)]['id'];
74
75    // count all/writeable sources
76    $writeable = 0;
77    $count = 0;
78    foreach ($js_list as $sid => $s) {
79        $count++;
80        if (!$s['readonly']) {
81            $writeable++;
82        }
83        // unset hidden sources
84        if ($s['hidden']) {
85            unset($js_list[$sid]);
86        }
87    }
88
89    $search_mods = $RCMAIL->config->get('addressbook_search_mods', $SEARCH_MODS_DEFAULT);
90    $OUTPUT->set_env('search_mods', $search_mods);
91    $OUTPUT->set_env('address_sources', $js_list);
92    $OUTPUT->set_env('writable_source', $writeable);
93
94    $OUTPUT->set_pagetitle(rcube_label('addressbook'));
95    $_SESSION['addressbooks_count'] = $count;
96    $_SESSION['addressbooks_count_writeable'] = $writeable;
97
98    if (!strlen($source))
99      $source = strval(key($js_list));
100
101    $CONTACTS = rcmail_contact_source($source, true);
102}
103
104// remove undo information...
105if ($undo = $_SESSION['contact_undo']) {
106    // ...after timeout
107    $undo_time = $RCMAIL->config->get('undo_timeout', 0);
108    if ($undo['ts'] < time() - $undo_time)
109        $RCMAIL->session->remove('contact_undo');
110}
111
112// instantiate a contacts object according to the given source
113function rcmail_contact_source($source=null, $init_env=false, $writable=false)
114{
115    global $RCMAIL, $OUTPUT, $CONTACT_COLTYPES, $PAGE_SIZE;
116
117    if (!strlen($source)) {
118        $source = get_input_value('_source', RCUBE_INPUT_GPC);
119    }
120
121    // Get object
122    $CONTACTS = $RCMAIL->get_address_book($source, $writable);
123    $CONTACTS->set_pagesize($PAGE_SIZE);
124
125    // set list properties and session vars
126    if (!empty($_GET['_page']))
127        $CONTACTS->set_page(($_SESSION['page'] = intval($_GET['_page'])));
128    else
129        $CONTACTS->set_page(isset($_SESSION['page']) ? $_SESSION['page'] : 1);
130
131    if (!empty($_REQUEST['_gid']))
132        $CONTACTS->set_group(get_input_value('_gid', RCUBE_INPUT_GPC));
133
134    if (!$init_env)
135        return $CONTACTS;
136
137    $OUTPUT->set_env('readonly', $CONTACTS->readonly);
138    $OUTPUT->set_env('source', $source);
139
140    // reduce/extend $CONTACT_COLTYPES with specification from the current $CONTACT object
141    if (is_array($CONTACTS->coltypes)) {
142        // remove cols not listed by the backend class
143        $contact_cols = $CONTACTS->coltypes[0] ? array_flip($CONTACTS->coltypes) : $CONTACTS->coltypes;
144        $CONTACT_COLTYPES = array_intersect_key($CONTACT_COLTYPES, $contact_cols);
145        // add associative coltypes definition
146        if (!$CONTACTS->coltypes[0]) {
147            foreach ($CONTACTS->coltypes as $col => $colprop) {
148                if (is_array($colprop['childs'])) {
149                    foreach ($colprop['childs'] as $childcol => $childprop)
150                        $colprop['childs'][$childcol] = array_merge((array)$CONTACT_COLTYPES[$col]['childs'][$childcol], $childprop);
151                }
152                $CONTACT_COLTYPES[$col] = $CONTACT_COLTYPES[$col] ? array_merge($CONTACT_COLTYPES[$col], $colprop) : $colprop;
153            }
154        }
155    }
156
157    $OUTPUT->set_env('photocol', is_array($CONTACT_COLTYPES['photo']));
158
159    return $CONTACTS;
160}
161
162
163function rcmail_default_source($writable=false)
164{
165    global $RCMAIL;
166
167    // get list of address sources
168    $first = reset($RCMAIL->get_address_sources($writable));
169
170    // use first directory by default
171    return $first['id'];
172}
173
174function rcmail_set_sourcename($abook)
175{
176    global $OUTPUT;
177
178    // get address book name (for display)
179    if ($abook && $_SESSION['addressbooks_count'] > 1) {
180        $name = $abook->get_name();
181        if (!$name && $source == 0) {
182            $name = rcube_label('personaladrbook');
183        }
184        $OUTPUT->set_env('sourcename', $name);
185    }
186}
187
188
189function rcmail_directory_list($attrib)
190{
191    global $RCMAIL, $OUTPUT;
192
193    if (!$attrib['id'])
194        $attrib['id'] = 'rcmdirectorylist';
195
196    $out = '';
197    $local_id = '0';
198    $jsdata = array();
199
200    $line_templ = html::tag('li', array(
201        'id' => 'rcmli%s', 'class' => '%s'),
202        html::a(array('href' => '%s',
203            'rel' => '%s',
204            'onclick' => "return ".JS_OBJECT_NAME.".command('list','%s',this)"), '%s'));
205
206    $sources = (array) $OUTPUT->env['address_sources'];
207    reset($sources);
208
209    // currently selected source
210    $current = get_input_value('_source', RCUBE_INPUT_GPC);
211
212    foreach ($sources as $j => $source) {
213        $id = strval(strlen($source['id']) ? $source['id'] : $j);
214        $js_id = JQ($id);
215
216        // set class name(s)
217        $class_name = 'addressbook';
218        if ($current === $id)
219            $class_name .= ' selected';
220        if ($source['readonly'])
221            $class_name .= ' readonly';
222        if ($source['class_name'])
223            $class_name .= ' ' . $source['class_name'];
224
225        $out .= sprintf($line_templ,
226            html_identifier($id),
227            $class_name,
228            Q(rcmail_url(null, array('_source' => $id))),
229            $source['id'],
230            $js_id, (!empty($source['name']) ? Q($source['name']) : Q($id)));
231
232        $groupdata = array('out' => $out, 'jsdata' => $jsdata, 'source' => $id);
233        if ($source['groups'])
234            $groupdata = rcmail_contact_groups($groupdata);
235        $jsdata = $groupdata['jsdata'];
236        $out = $groupdata['out'];
237    }
238
239    $line_templ = html::tag('li', array(
240        'id' => 'rcmliS%s', 'class' => '%s'),
241        html::a(array('href' => '#', 'rel' => 'S%s',
242            'onclick' => "return ".JS_OBJECT_NAME.".command('listsearch', '%s', this)"), '%s'));
243
244    // Saved searches
245    $sources = $RCMAIL->user->list_searches(rcube_user::SEARCH_ADDRESSBOOK);
246    foreach ($sources as $j => $source) {
247        $id = $source['id'];
248        $js_id = JQ($id);
249
250        // set class name(s)
251        $class_name = 'contactsearch';
252        if ($current === $id)
253            $class_name .= ' selected';
254        if ($source['class_name'])
255            $class_name .= ' ' . $source['class_name'];
256
257        $out .= sprintf($line_templ,
258            html_identifier($id),
259            $class_name,
260            $id,
261            $js_id, (!empty($source['name']) ? Q($source['name']) : Q($id)));
262    }
263
264    $OUTPUT->set_env('contactgroups', $jsdata);
265    $OUTPUT->add_gui_object('folderlist', $attrib['id']);
266    // add some labels to client
267    $OUTPUT->add_label('deletegroupconfirm', 'groupdeleting', 'addingmember', 'removingmember');
268
269    return html::tag('ul', $attrib, $out, html::$common_attrib);
270}
271
272
273function rcmail_contact_groups($args)
274{
275    global $RCMAIL;
276
277    $groups = $RCMAIL->get_address_book($args['source'])->list_groups();
278
279    if (!empty($groups)) {
280        $line_templ = html::tag('li', array(
281            'id' => 'rcmliG%s', 'class' => 'contactgroup'),
282            html::a(array('href' => '#',
283                'rel' => '%s:%s',
284                'onclick' => "return ".JS_OBJECT_NAME.".command('listgroup',{'source':'%s','id':'%s'},this)"), '%s'));
285
286        $jsdata = array();
287        foreach ($groups as $group) {
288            $args['out'] .= sprintf($line_templ,
289                html_identifier($args['source'] . $group['ID']),
290                $args['source'], $group['ID'],
291                $args['source'], $group['ID'], Q($group['name'])
292            );
293            $args['jsdata']['G'.$args['source'].$group['ID']] = array(
294                'source' => $args['source'], 'id' => $group['ID'],
295                'name' => $group['name'], 'type' => 'group');
296        }
297    }
298
299    return $args;
300}
301
302
303// return the contacts list as HTML table
304function rcmail_contacts_list($attrib)
305{
306    global $CONTACTS, $OUTPUT;
307
308    // define list of cols to be displayed
309    $a_show_cols = array('name');
310
311    // add id to message list table if not specified
312    if (!strlen($attrib['id']))
313        $attrib['id'] = 'rcmAddressList';
314
315    // create XHTML table
316    $out = rcube_table_output($attrib, array(), $a_show_cols, $CONTACTS->primary_key);
317
318    // set client env
319    $OUTPUT->add_gui_object('contactslist', $attrib['id']);
320    $OUTPUT->set_env('current_page', (int)$CONTACTS->list_page);
321    $OUTPUT->include_script('list.js');
322
323    // add some labels to client
324    $OUTPUT->add_label('deletecontactconfirm', 'copyingcontact', 'contactdeleting');
325
326    return $out;
327}
328
329
330function rcmail_js_contacts_list($result, $prefix='')
331{
332    global $OUTPUT;
333
334    if (empty($result) || $result->count == 0)
335        return;
336
337    // define list of cols to be displayed
338    $a_show_cols = array('name');
339
340    while ($row = $result->next()) {
341        $a_row_cols = array();
342
343        // build contact ID with source ID
344        if (isset($row['sourceid'])) {
345            $row['ID'] = $row['ID'].'-'.$row['sourceid'];
346        }
347
348        // format each col
349        foreach ($a_show_cols as $col) {
350            $val = $row[$col];
351            if ($val == '' && $col == 'name') {
352                $val = rcube_addressbook::compose_display_name($row, true);
353            }
354
355            $a_row_cols[$col] = Q($val);
356        }
357
358        $OUTPUT->command($prefix.'add_contact_row', $row['ID'], $a_row_cols);
359    }
360}
361
362
363// similar function as /steps/settings/identities.inc::rcmail_identity_frame()
364function rcmail_contact_frame($attrib)
365{
366    global $OUTPUT;
367
368    if (!$attrib['id'])
369        $attrib['id'] = 'rcmcontactframe';
370
371    $attrib['name'] = $attrib['id'];
372
373    $OUTPUT->set_env('contentframe', $attrib['name']);
374    $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
375
376    return html::iframe($attrib);
377}
378
379
380function rcmail_rowcount_display($attrib)
381{
382    global $OUTPUT;
383
384    if (!$attrib['id'])
385        $attrib['id'] = 'rcmcountdisplay';
386
387    $OUTPUT->add_gui_object('countdisplay', $attrib['id']);
388
389    if ($attrib['label'])
390      $_SESSION['contactcountdisplay'] = $attrib['label'];
391
392    return html::span($attrib, rcube_label('loading'));
393}
394
395
396function rcmail_get_rowcount_text($result=null)
397{
398    global $CONTACTS, $PAGE_SIZE;
399
400    // read nr of contacts
401    if (!$result) {
402        $result = $CONTACTS->get_result();
403    }
404
405    if ($result->count == 0)
406        $out = rcube_label('nocontactsfound');
407    else
408        $out = rcube_label(array(
409            'name'  => $_SESSION['contactcountdisplay'] ? $_SESSION['contactcountdisplay'] : 'contactsfromto',
410            'vars'  => array(
411            'from'  => $result->first + 1,
412            'to'    => min($result->count, $result->first + $PAGE_SIZE),
413            'count' => $result->count)
414        ));
415
416    return $out;
417}
418
419
420function rcmail_get_type_label($type)
421{
422    $label = 'type'.$type;
423    if (rcube_label_exists($label, '*', $domain))
424        return rcube_label($label, $domain);
425    else if (preg_match('/\w+(\d+)$/', $label, $m)
426            && ($label = preg_replace('/(\d+)$/', '', $label))
427            && rcube_label_exists($label, '*', $domain))
428        return rcube_label($label, $domain) . ' ' . $m[1];
429
430    return ucfirst($type);
431}
432
433
434function rcmail_contact_form($form, $record, $attrib = null)
435{
436    global $RCMAIL, $CONFIG;
437    static $jqueryui_loaded = 0;
438
439    // Allow plugins to modify contact form content
440    $plugin = $RCMAIL->plugins->exec_hook('contact_form', array(
441        'form' => $form, 'record' => $record));
442
443    $form = $plugin['form'];
444    $record = $plugin['record'];
445    $edit_mode = $RCMAIL->action != 'show';
446    $del_button = $attrib['deleteicon'] ? html::img(array('src' => $CONFIG['skin_path'] . $attrib['deleteicon'], 'alt' => rcube_label('delete'))) : rcube_label('delete');
447    unset($attrib['deleteicon']);
448    $out = '';
449
450    // get default coltypes
451    $coltypes = $GLOBALS['CONTACT_COLTYPES'];
452    $coltype_labels = array();
453
454    foreach ($coltypes as $col => $prop) {
455        if ($prop['subtypes']) {
456            $subtype_names = array_map('rcmail_get_type_label', $prop['subtypes']);
457            $select_subtype = new html_select(array('name' => '_subtype_'.$col.'[]', 'class' => 'contactselectsubtype'));
458            $select_subtype->add($subtype_names, $prop['subtypes']);
459            $coltypes[$col]['subtypes_select'] = $select_subtype->show();
460        }
461        if ($prop['childs']) {
462            foreach ($prop['childs'] as $childcol => $cp)
463                $coltype_labels[$childcol] = array('label' => $cp['label']);
464        }
465    }
466
467    foreach ($form as $section => $fieldset) {
468        // skip empty sections
469        if (empty($fieldset['content']))
470            continue;
471
472        $select_add = new html_select(array('class' => 'addfieldmenu', 'rel' => $section));
473        $select_add->add(rcube_label('addfield'), '');
474
475        // render head section with name fields (not a regular list of rows)
476        if ($section == 'head') {
477            $content = '';
478
479            // unset display name if it is composed from name parts
480            if ($record['name'] == rcube_addressbook::compose_display_name(array('name' => '') + (array)$record))
481              unset($record['name']);
482
483            // group fields
484            $field_blocks = array(
485                'names'    => array('prefix','firstname','middlename','surname','suffix'),
486                'displayname' => array('name'),
487                'nickname' => array('nickname'),
488                'organization' => array('organization'),
489                'department' => array('department'),
490                'jobtitle' => array('jobtitle'),
491            );
492            foreach ($field_blocks as $blockname => $colnames) {
493                $fields = '';
494                foreach ($colnames as $col) {
495                    // skip cols unknown to the backend
496                    if (!$coltypes[$col])
497                        continue;
498
499                    // only string values are expected here
500                    if (is_array($record[$col]))
501                        $record[$col] = join(' ', $record[$col]);
502
503                    if ($RCMAIL->action == 'show') {
504                        if (!empty($record[$col]))
505                            $fields .= html::span('namefield ' . $col, Q($record[$col])) . " ";
506                    }
507                    else {
508                        $colprop = (array)$fieldset['content'][$col] + (array)$coltypes[$col];
509                        $colprop['id'] = 'ff_'.$col;
510                        if (empty($record[$col]) && !$colprop['visible']) {
511                            $colprop['style'] = 'display:none';
512                            $select_add->add($colprop['label'], $col);
513                        }
514                        $fields .= rcmail_get_edit_field($col, $record[$col], $colprop, $colprop['type']);
515                    }
516                }
517                $content .= html::div($blockname, $fields);
518            }
519
520            if ($edit_mode)
521                $content .= html::p('addfield', $select_add->show(null));
522
523            $out .= html::tag('fieldset', $attrib, (!empty($fieldset['name']) ? html::tag('legend', null, Q($fieldset['name'])) : '') . $content) ."\n";
524            continue;
525        }
526
527        $content = '';
528        if (is_array($fieldset['content'])) {
529            foreach ($fieldset['content'] as $col => $colprop) {
530                // remove subtype part of col name
531                list($field, $subtype) = explode(':', $col);
532                if (!$subtype) $subtype = 'home';
533                $fullkey = $col.':'.$subtype;
534
535                // skip cols unknown to the backend
536                if (!$coltypes[$field])
537                    continue;
538
539                // merge colprop with global coltype configuration
540                $colprop += $coltypes[$field];
541                $label = isset($colprop['label']) ? $colprop['label'] : rcube_label($col);
542
543                // prepare subtype selector in edit mode
544                if ($edit_mode && is_array($colprop['subtypes'])) {
545                    $subtype_names = array_map('rcmail_get_type_label', $colprop['subtypes']);
546                    $select_subtype = new html_select(array('name' => '_subtype_'.$col.'[]', 'class' => 'contactselectsubtype'));
547                    $select_subtype->add($subtype_names, $colprop['subtypes']);
548                }
549                else
550                    $select_subtype = null;
551
552                if (!empty($colprop['value'])) {
553                    $values = (array)$colprop['value'];
554                }
555                else {
556                    // iterate over possible subtypes and collect values with their subtype
557                    if (is_array($colprop['subtypes'])) {
558                        $values = $subtypes = array();
559                        foreach ($colprop['subtypes'] as $i => $st) {
560                            $newval = false;
561                            if ($record[$field.':'.$st]) {
562                                $subtypes[count($values)] = $st;
563                                $newval = $record[$field.':'.$st];
564                            }
565                            else if ($i == 0 && $record[$field]) {
566                                $subtypes[count($values)] = $st;
567                                $newval = $record[$field];
568                            }
569                            if ($newval !== false) {
570                                if (is_array($newval) && isset($newval[0]))
571                                    $values = array_merge($values, $newval);
572                                else
573                                    $values[] = $newval;
574                            }
575                        }
576                    }
577                    else {
578                        $values = $record[$fullkey] ? $record[$fullkey] : $record[$field];
579                        $subtypes = null;
580                    }
581                }
582
583                // hack: create empty values array to force this field to be displayed
584                if (empty($values) && $colprop['visible'])
585                    $values[] = '';
586
587                $rows = '';
588                foreach ((array)$values as $i => $val) {
589                    if ($subtypes[$i])
590                        $subtype = $subtypes[$i];
591
592                    // render composite field
593                    if ($colprop['type'] == 'composite') {
594                        $composite = array(); $j = 0;
595                        $template = $RCMAIL->config->get($col . '_template', '{'.join('} {', array_keys($colprop['childs'])).'}');
596                        foreach ($colprop['childs'] as $childcol => $cp) {
597                            $childvalue = $val[$childcol] ? $val[$childcol] : $val[$j];
598
599                            if ($edit_mode) {
600                                if ($colprop['subtypes'] || $colprop['limit'] != 1) $cp['array'] = true;
601                                $composite['{'.$childcol.'}'] = rcmail_get_edit_field($childcol, $childvalue, $cp, $cp['type']) . " ";
602                            }
603                            else {
604                                $childval = $cp['render_func'] ? call_user_func($cp['render_func'], $childvalue, $childcol) : Q($childvalue);
605                                $composite['{'.$childcol.'}'] = html::span('data ' . $childcol, $childval) . " ";
606                            }
607                            $j++;
608                        }
609
610                        $coltypes[$field] += (array)$colprop;
611                        $coltypes[$field]['count']++;
612                        $val = preg_replace('/\{\w+\}/', '', strtr($template, $composite));
613                    }
614                    else if ($edit_mode) {
615                        // call callback to render/format value
616                        if ($colprop['render_func'])
617                            $val = call_user_func($colprop['render_func'], $val, $col);
618
619                        $coltypes[$field] = (array)$colprop + $coltypes[$field];
620
621                        if ($colprop['subtypes'] || $colprop['limit'] != 1)
622                            $colprop['array'] = true;
623
624                        // load jquery UI datepickert for date fields
625                        if ($colprop['type'] == 'date') {
626                            if (!$jqueryui_loaded++) {
627                                $RCMAIL->plugins->load_plugin('jqueryui');
628                                $RCMAIL->output->set_env('date_format', strtr($RCMAIL->config->get('date_format', 'Y-m-d'), array('y'=>'y', 'Y'=>'yy', 'm'=>'mm', 'n'=>'m', 'd'=>'dd', 'j'=>'d')));
629                                foreach (array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec') as $month)
630                                    $month_names[] = rcube_label($month);
631                                $RCMAIL->output->set_env('month_names', $month_names);
632                            }
633                            $colprop['class'] .= ($colprop['class'] ? ' ' : '') . 'datepicker';
634                            $val = format_date($val, $RCMAIL->config->get('date_format', 'Y-m-d'), false);
635                        }
636
637                        $val = rcmail_get_edit_field($col, $val, $colprop, $colprop['type']);
638                        $coltypes[$field]['count']++;
639                    }
640                    else if ($colprop['render_func'])
641                        $val = call_user_func($colprop['render_func'], $val, $col);
642                    else if (is_array($colprop['options']) && isset($colprop['options'][$val]))
643                        $val = $colprop['options'][$val];
644                    else
645                        $val = Q($val);
646
647                    // use subtype as label
648                    if ($colprop['subtypes'])
649                        $label = rcmail_get_type_label($subtype);
650
651                    // add delete button/link
652                    if ($edit_mode && !($colprop['visible'] && $colprop['limit'] == 1))
653                        $val .= html::a(array('href' => '#del', 'class' => 'contactfieldbutton deletebutton', 'title' => rcube_label('delete'), 'rel' => $col), $del_button);
654
655                    // display row with label
656                    if ($label) {
657                        $rows .= html::div('row',
658                            html::div('contactfieldlabel label', $select_subtype ? $select_subtype->show($subtype) : Q($label)) .
659                            html::div('contactfieldcontent '.$colprop['type'], $val));
660                    }
661                    else   // row without label
662                        $rows .= html::div('row', html::div('contactfield', $val));
663                }
664
665                // add option to the add-field menu
666                if (!$colprop['limit'] || $coltypes[$field]['count'] < $colprop['limit']) {
667                    $select_add->add($colprop['label'], $col);
668                    $select_add->_count++;
669                }
670
671                // wrap rows in fieldgroup container
672                if ($rows) {
673                    $content .= html::tag('fieldset', array('class' => 'contactfieldgroup ' . ($colprop['subtypes'] ? 'contactfieldgroupmulti ' : '') . 'contactcontroller' . $col, 'style' => ($rows ? null : 'display:none')),
674                      ($colprop['subtypes'] ? html::tag('legend', null, Q($colprop['label'])) : ' ') .
675                      $rows);
676                }
677            }
678
679            if (!$content)
680                continue;
681
682            // also render add-field selector
683            if ($edit_mode)
684                $content .= html::p('addfield', $select_add->show(null, array('style' => $select_add->_count ? null : 'display:none')));
685
686            $content = html::div(array('id' => 'contactsection' . $section), $content);
687        }
688        else {
689            $content = $fieldset['content'];
690        }
691
692        if ($content)
693            $out .= html::tag('fieldset', null, html::tag('legend', null, Q($fieldset['name'])) . $content) ."\n";
694    }
695
696    if ($edit_mode) {
697      $RCMAIL->output->set_env('coltypes', $coltypes + $coltype_labels);
698      $RCMAIL->output->set_env('delbutton', $del_button);
699      $RCMAIL->output->add_label('delete');
700    }
701
702    return $out;
703}
704
705
706function rcmail_contact_photo($attrib)
707{
708    global $SOURCE_ID, $CONTACTS, $CONTACT_COLTYPES, $RCMAIL, $CONFIG;
709
710    if ($result = $CONTACTS->get_result())
711        $record = $result->first();
712
713    $photo_img = $attrib['placeholder'] ? $CONFIG['skin_path'] . $attrib['placeholder'] : 'program/blank.gif';
714    $RCMAIL->output->set_env('photo_placeholder', $photo_img);
715    unset($attrib['placeholder']);
716
717    if (preg_match('!^https?://!i', $record['photo']))
718        $photo_img = $record['photo'];
719    else if ($record['photo'])
720        $photo_img = $RCMAIL->url(array('_action' => 'photo', '_cid' => $record['ID'], '_source' => $SOURCE_ID));
721    else
722        $ff_value = '-del-'; // will disable delete-photo action
723
724    $img = html::img(array('src' => $photo_img, 'border' => 1, 'alt' => ''));
725    $content = html::div($attrib, $img);
726
727    if ($CONTACT_COLTYPES['photo'] && ($RCMAIL->action == 'edit' || $RCMAIL->action == 'add')) {
728        $RCMAIL->output->add_gui_object('contactphoto', $attrib['id']);
729        $hidden = new html_hiddenfield(array('name' => '_photo', 'id' => 'ff_photo', 'value' => $ff_value));
730        $content .= $hidden->show();
731    }
732
733    return $content;
734}
735
736
737function rcmail_format_date_col($val)
738{
739    global $RCMAIL;
740    return format_date($val, $RCMAIL->config->get('date_format', 'Y-m-d'), false);
741}
742
743
744/**
745 * Returns contact ID(s) and source(s) from GET/POST data
746 *
747 * @return array List of contact IDs per-source
748 */
749function rcmail_get_cids()
750{
751    // contact ID (or comma-separated list of IDs) is provided in two
752    // forms. If _source is an empty string then the ID is a string
753    // containing contact ID and source name in form: <ID>-<SOURCE>
754
755    $cid    = get_input_value('_cid', RCUBE_INPUT_GPC);
756    $source = (string) get_input_value('_source', RCUBE_INPUT_GPC);
757
758    if (!preg_match('/^[a-zA-Z0-9\+\/=_-]+(,[a-zA-Z0-9\+\/=_-]+)*$/', $cid)) {
759        return array();
760    }
761
762    $cid        = explode(',', $cid);
763    $got_source = strlen($source);
764    $result     = array();
765
766    // create per-source contact IDs array
767    foreach ($cid as $id) {
768        // if _source is not specified we'll find it from decoded ID
769        if (!$got_source) {
770            if ($sep = strrpos($id, '-')) {
771                $contact_id = substr($id, 0, $sep);
772                $source_id  = substr($id, $sep+1);
773                if (strlen($source_id)) {
774                    $result[(string)$source_id][] = $contact_id;
775                }
776            }
777        }
778        else {
779            $result[$source][] = $id;
780        }
781    }
782
783    return $result;
784}
785
786// register UI objects
787$OUTPUT->add_handlers(array(
788    'directorylist' => 'rcmail_directory_list',
789//  'groupslist' => 'rcmail_contact_groups',
790    'addresslist' => 'rcmail_contacts_list',
791    'addressframe' => 'rcmail_contact_frame',
792    'recordscountdisplay' => 'rcmail_rowcount_display',
793    'searchform' => array($OUTPUT, 'search_form')
794));
795
796// register action aliases
797$RCMAIL->register_action_map(array(
798    'add' => 'edit.inc',
799    'photo' => 'show.inc',
800    'group-create' => 'groups.inc',
801    'group-rename' => 'groups.inc',
802    'group-delete' => 'groups.inc',
803    'group-addmembers' => 'groups.inc',
804    'group-delmembers' => 'groups.inc',
805    'search-create' => 'search.inc',
806    'search-delete' => 'search.inc',
807));
Note: See TracBrowser for help on using the repository browser.