source: github/program/steps/addressbook/func.inc @ 8253e7d

release-0.8
Last change on this file since 8253e7d was 8253e7d, checked in by Aleksander Machniak <alec@…>, 13 months ago

Fix PHP warning on PHP 5.4 (#1488469)

  • Property mode set to 100644
File size: 32.9 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-2012, The Roundcube Dev Team                       |
9 |                                                                       |
10 | Licensed under the GNU General Public License version 3 or            |
11 | any later version with exceptions for skins & plugins.                |
12 | See the README file for a full license statement.                     |
13 |                                                                       |
14 | PURPOSE:                                                              |
15 |   Provide addressbook functionality and GUI objects                   |
16 |                                                                       |
17 +-----------------------------------------------------------------------+
18 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
19 +-----------------------------------------------------------------------+
20
21 $Id$
22
23*/
24
25$SEARCH_MODS_DEFAULT = array('name'=>1, 'firstname'=>1, 'surname'=>1, 'email'=>1, '*'=>1);
26
27// general definition of contact coltypes
28$CONTACT_COLTYPES = array(
29  'name'         => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('name'), 'category' => 'main'),
30  'firstname'    => array('type' => 'text', 'size' => 19, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('firstname'), 'category' => 'main'),
31  'surname'      => array('type' => 'text', 'size' => 19, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('surname'), 'category' => 'main'),
32  'email'        => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => rcube_label('email'), 'subtypes' => array('home','work','other'), 'category' => 'main'),
33  'middlename'   => array('type' => 'text', 'size' => 19, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('middlename'), 'category' => 'main'),
34  'prefix'       => array('type' => 'text', 'size' => 8,  'maxlength' => 20, 'limit' => 1, 'label' => rcube_label('nameprefix'), 'category' => 'main'),
35  'suffix'       => array('type' => 'text', 'size' => 8,  'maxlength' => 20, 'limit' => 1, 'label' => rcube_label('namesuffix'), 'category' => 'main'),
36  'nickname'     => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('nickname'), 'category' => 'main'),
37  'jobtitle'     => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('jobtitle'), 'category' => 'main'),
38  'organization' => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('organization'), 'category' => 'main'),
39  'department'   => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('department'), 'category' => 'main'),
40  'gender'       => array('type' => 'select', 'limit' => 1, 'label' => rcube_label('gender'), 'options' => array('male' => rcube_label('male'), 'female' => rcube_label('female')), 'category' => 'personal'),
41  'maidenname'   => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('maidenname'), 'category' => 'personal'),
42  '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'),
43  'address'      => array('type' => 'composite', 'label' => rcube_label('address'), 'subtypes' => array('home','work','other'), 'childs' => array(
44    'street'     => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => rcube_label('street'), 'category' => 'main'),
45    'locality'   => array('type' => 'text', 'size' => 28, 'maxlength' => 50, 'label' => rcube_label('locality'), 'category' => 'main'),
46    'zipcode'    => array('type' => 'text', 'size' => 8,  'maxlength' => 15, 'label' => rcube_label('zipcode'), 'category' => 'main'),
47    'region'     => array('type' => 'text', 'size' => 12, 'maxlength' => 50, 'label' => rcube_label('region'), 'category' => 'main'),
48    'country'    => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => rcube_label('country'), 'category' => 'main'),
49  ), 'category' => 'main'),
50  'birthday'     => array('type' => 'date', 'size' => 12, 'maxlength' => 16, 'label' => rcube_label('birthday'), 'limit' => 1, 'render_func' => 'rcmail_format_date_col', 'category' => 'personal'),
51  'anniversary'  => array('type' => 'date', 'size' => 12, 'maxlength' => 16, 'label' => rcube_label('anniversary'), 'limit' => 1, 'render_func' => 'rcmail_format_date_col', 'category' => 'personal'),
52  'website'      => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => rcube_label('website'), 'subtypes' => array('homepage','work','blog','profile','other'), 'category' => 'main'),
53  'im'           => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => rcube_label('instantmessenger'), 'subtypes' => array('aim','icq','msn','yahoo','jabber','skype','other'), 'category' => 'main'),
54  'notes'        => array('type' => 'textarea', 'size' => 40, 'rows' => 15, 'maxlength' => 500, 'label' => rcube_label('notes'), 'limit' => 1),
55  'photo'        => array('type' => 'image', 'limit' => 1, 'category' => 'main'),
56  'assistant'    => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('assistant'), 'category' => 'personal'),
57  'manager'      => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('manager'), 'category' => 'personal'),
58  'spouse'       => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('spouse'), 'category' => 'personal'),
59  // TODO: define fields for vcards like GEO, KEY
60);
61
62$PAGE_SIZE = $RCMAIL->config->get('addressbook_pagesize', $RCMAIL->config->get('pagesize', 50));
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        $classes = array('person');  // org records will follow some day
343
344        // build contact ID with source ID
345        if (isset($row['sourceid'])) {
346            $row['ID'] = $row['ID'].'-'.$row['sourceid'];
347        }
348
349        // format each col
350        foreach ($a_show_cols as $col) {
351            $val = $col == 'name' ? rcube_addressbook::compose_list_name($row) : $row[$col];
352            $a_row_cols[$col] = Q($val);
353        }
354
355        if ($row['readonly'])
356            $classes[] = 'readonly';
357
358        $OUTPUT->command($prefix.'add_contact_row', $row['ID'], $a_row_cols, join(' ', $classes));
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
438    // Allow plugins to modify contact form content
439    $plugin = $RCMAIL->plugins->exec_hook('contact_form', array(
440        'form' => $form, 'record' => $record));
441
442    $form = $plugin['form'];
443    $record = $plugin['record'];
444    $edit_mode = $RCMAIL->action != 'show';
445    $del_button = $attrib['deleteicon'] ? html::img(array('src' => $CONFIG['skin_path'] . $attrib['deleteicon'], 'alt' => rcube_label('delete'))) : rcube_label('delete');
446    unset($attrib['deleteicon']);
447    $out = '';
448
449    // get default coltypes
450    $coltypes = $GLOBALS['CONTACT_COLTYPES'];
451    $coltype_labels = array();
452
453    foreach ($coltypes as $col => $prop) {
454        if ($prop['subtypes']) {
455            $subtype_names = array_map('rcmail_get_type_label', $prop['subtypes']);
456            $select_subtype = new html_select(array('name' => '_subtype_'.$col.'[]', 'class' => 'contactselectsubtype'));
457            $select_subtype->add($subtype_names, $prop['subtypes']);
458            $coltypes[$col]['subtypes_select'] = $select_subtype->show();
459        }
460        if ($prop['childs']) {
461            foreach ($prop['childs'] as $childcol => $cp)
462                $coltype_labels[$childcol] = array('label' => $cp['label']);
463        }
464    }
465
466    foreach ($form as $section => $fieldset) {
467        // skip empty sections
468        if (empty($fieldset['content']))
469            continue;
470
471        $select_add = new html_select(array('class' => 'addfieldmenu', 'rel' => $section));
472        $select_add->add(rcube_label('addfield'), '');
473
474        // render head section with name fields (not a regular list of rows)
475        if ($section == 'head') {
476            $content = '';
477
478            // unset display name if it is composed from name parts
479            if ($record['name'] == rcube_addressbook::compose_display_name(array('name' => '') + (array)$record))
480              unset($record['name']);
481
482            // group fields
483            $field_blocks = array(
484                'names'    => array('prefix','firstname','middlename','surname','suffix'),
485                'displayname' => array('name'),
486                'nickname' => array('nickname'),
487                'organization' => array('organization'),
488                'department' => array('department'),
489                'jobtitle' => array('jobtitle'),
490            );
491            foreach ($field_blocks as $blockname => $colnames) {
492                $fields = '';
493                foreach ($colnames as $col) {
494                    // skip cols unknown to the backend
495                    if (!$coltypes[$col])
496                        continue;
497
498                    // only string values are expected here
499                    if (is_array($record[$col]))
500                        $record[$col] = join(' ', $record[$col]);
501
502                    if ($RCMAIL->action == 'show') {
503                        if (!empty($record[$col]))
504                            $fields .= html::span('namefield ' . $col, Q($record[$col])) . " ";
505                    }
506                    else {
507                        $colprop = (array)$fieldset['content'][$col] + (array)$coltypes[$col];
508                        $colprop['id'] = 'ff_'.$col;
509                        if (empty($record[$col]) && !$colprop['visible']) {
510                            $colprop['style'] = 'display:none';
511                            $select_add->add($colprop['label'], $col);
512                        }
513                        $fields .= rcmail_get_edit_field($col, $record[$col], $colprop, $colprop['type']);
514                    }
515                }
516                $content .= html::div($blockname, $fields);
517            }
518
519            if ($edit_mode)
520                $content .= html::p('addfield', $select_add->show(null));
521
522            $out .= html::tag('fieldset', $attrib, (!empty($fieldset['name']) ? html::tag('legend', null, Q($fieldset['name'])) : '') . $content) ."\n";
523            continue;
524        }
525
526        $content = '';
527        if (is_array($fieldset['content'])) {
528            foreach ($fieldset['content'] as $col => $colprop) {
529                // remove subtype part of col name
530                list($field, $subtype) = explode(':', $col);
531                if (!$subtype) $subtype = 'home';
532                $fullkey = $col.':'.$subtype;
533
534                // skip cols unknown to the backend
535                if (!$coltypes[$field])
536                    continue;
537
538                // merge colprop with global coltype configuration
539                $colprop += $coltypes[$field];
540                $label = isset($colprop['label']) ? $colprop['label'] : rcube_label($col);
541
542                // prepare subtype selector in edit mode
543                if ($edit_mode && is_array($colprop['subtypes'])) {
544                    $subtype_names = array_map('rcmail_get_type_label', $colprop['subtypes']);
545                    $select_subtype = new html_select(array('name' => '_subtype_'.$col.'[]', 'class' => 'contactselectsubtype'));
546                    $select_subtype->add($subtype_names, $colprop['subtypes']);
547                }
548                else
549                    $select_subtype = null;
550
551                if (!empty($colprop['value'])) {
552                    $values = (array)$colprop['value'];
553                }
554                else {
555                    // iterate over possible subtypes and collect values with their subtype
556                    if (is_array($colprop['subtypes'])) {
557                        $values = $subtypes = array();
558                        foreach ($colprop['subtypes'] as $i => $st) {
559                            $newval = false;
560                            if ($record[$field.':'.$st]) {
561                                $subtypes[count($values)] = $st;
562                                $newval = $record[$field.':'.$st];
563                            }
564                            else if ($i == 0 && $record[$field]) {
565                                $subtypes[count($values)] = $st;
566                                $newval = $record[$field];
567                            }
568                            if ($newval !== false) {
569                                if (is_array($newval) && isset($newval[0]))
570                                    $values = array_merge($values, $newval);
571                                else
572                                    $values[] = $newval;
573                            }
574                        }
575                    }
576                    else {
577                        $values = $record[$fullkey] ? $record[$fullkey] : $record[$field];
578                        $subtypes = null;
579                    }
580                }
581
582                // hack: create empty values array to force this field to be displayed
583                if (empty($values) && $colprop['visible'])
584                    $values[] = '';
585
586                $rows = '';
587                foreach ((array)$values as $i => $val) {
588                    if ($subtypes[$i])
589                        $subtype = $subtypes[$i];
590
591                    // render composite field
592                    if ($colprop['type'] == 'composite') {
593                        $composite = array(); $j = 0;
594                        $template = $RCMAIL->config->get($col . '_template', '{'.join('} {', array_keys($colprop['childs'])).'}');
595                        foreach ($colprop['childs'] as $childcol => $cp) {
596                            if (!empty($val) && is_array($val)) {
597                                $childvalue = $val[$childcol] ? $val[$childcol] : $val[$j];
598                            }
599                            else {
600                                $childvalue = '';
601                            }
602
603                            if ($edit_mode) {
604                                if ($colprop['subtypes'] || $colprop['limit'] != 1) $cp['array'] = true;
605                                $composite['{'.$childcol.'}'] = rcmail_get_edit_field($childcol, $childvalue, $cp, $cp['type']) . " ";
606                            }
607                            else {
608                                $childval = $cp['render_func'] ? call_user_func($cp['render_func'], $childvalue, $childcol) : Q($childvalue);
609                                $composite['{'.$childcol.'}'] = html::span('data ' . $childcol, $childval) . " ";
610                            }
611                            $j++;
612                        }
613
614                        $coltypes[$field] += (array)$colprop;
615                        $coltypes[$field]['count']++;
616                        $val = preg_replace('/\{\w+\}/', '', strtr($template, $composite));
617                    }
618                    else if ($edit_mode) {
619                        // call callback to render/format value
620                        if ($colprop['render_func'])
621                            $val = call_user_func($colprop['render_func'], $val, $col);
622
623                        $coltypes[$field] = (array)$colprop + $coltypes[$field];
624
625                        if ($colprop['subtypes'] || $colprop['limit'] != 1)
626                            $colprop['array'] = true;
627
628                        // load jquery UI datepicker for date fields
629                        if ($colprop['type'] == 'date') {
630                            $colprop['class'] .= ($colprop['class'] ? ' ' : '') . 'datepicker';
631                            if (!$colprop['render_func'])
632                                $val = rcmail_format_date_col($val);
633                        }
634
635                        $val = rcmail_get_edit_field($col, $val, $colprop, $colprop['type']);
636                        $coltypes[$field]['count']++;
637                    }
638                    else if ($colprop['render_func'])
639                        $val = call_user_func($colprop['render_func'], $val, $col);
640                    else if (is_array($colprop['options']) && isset($colprop['options'][$val]))
641                        $val = $colprop['options'][$val];
642                    else
643                        $val = Q($val);
644
645                    // use subtype as label
646                    if ($colprop['subtypes'])
647                        $label = rcmail_get_type_label($subtype);
648
649                    // add delete button/link
650                    if ($edit_mode && !($colprop['visible'] && $colprop['limit'] == 1))
651                        $val .= html::a(array('href' => '#del', 'class' => 'contactfieldbutton deletebutton', 'title' => rcube_label('delete'), 'rel' => $col), $del_button);
652
653                    // display row with label
654                    if ($label) {
655                        $rows .= html::div('row',
656                            html::div('contactfieldlabel label', $select_subtype ? $select_subtype->show($subtype) : Q($label)) .
657                            html::div('contactfieldcontent '.$colprop['type'], $val));
658                    }
659                    else   // row without label
660                        $rows .= html::div('row', html::div('contactfield', $val));
661                }
662
663                // add option to the add-field menu
664                if (!$colprop['limit'] || $coltypes[$field]['count'] < $colprop['limit']) {
665                    $select_add->add($colprop['label'], $col);
666                    $select_add->_count++;
667                }
668
669                // wrap rows in fieldgroup container
670                if ($rows) {
671                    $content .= html::tag('fieldset', array('class' => 'contactfieldgroup ' . ($colprop['subtypes'] ? 'contactfieldgroupmulti ' : '') . 'contactcontroller' . $col, 'style' => ($rows ? null : 'display:none')),
672                      ($colprop['subtypes'] ? html::tag('legend', null, Q($colprop['label'])) : ' ') .
673                      $rows);
674                }
675            }
676
677            if (!$content && (!$edit_mode || !$select_add->_count))
678                continue;
679
680            // also render add-field selector
681            if ($edit_mode)
682                $content .= html::p('addfield', $select_add->show(null, array('style' => $select_add->_count ? null : 'display:none')));
683
684            $content = html::div(array('id' => 'contactsection' . $section), $content);
685        }
686        else {
687            $content = $fieldset['content'];
688        }
689
690        if ($content)
691            $out .= html::tag('fieldset', null, html::tag('legend', null, Q($fieldset['name'])) . $content) ."\n";
692    }
693
694    if ($edit_mode) {
695      $RCMAIL->output->set_env('coltypes', $coltypes + $coltype_labels);
696      $RCMAIL->output->set_env('delbutton', $del_button);
697      $RCMAIL->output->add_label('delete');
698    }
699
700    return $out;
701}
702
703
704function rcmail_contact_photo($attrib)
705{
706    global $SOURCE_ID, $CONTACTS, $CONTACT_COLTYPES, $RCMAIL, $CONFIG;
707
708    if ($result = $CONTACTS->get_result())
709        $record = $result->first();
710
711    $photo_img = $attrib['placeholder'] ? $CONFIG['skin_path'] . $attrib['placeholder'] : 'program/blank.gif';
712    $RCMAIL->output->set_env('photo_placeholder', $photo_img);
713    unset($attrib['placeholder']);
714
715    if (preg_match('!^https?://!i', $record['photo']))
716        $photo_img = $record['photo'];
717    else if ($record['photo'])
718        $photo_img = $RCMAIL->url(array('_action' => 'photo', '_cid' => $record['ID'], '_source' => $SOURCE_ID));
719    else
720        $ff_value = '-del-'; // will disable delete-photo action
721
722    $img = html::img(array('src' => $photo_img, 'border' => 1, 'alt' => ''));
723    $content = html::div($attrib, $img);
724
725    if ($CONTACT_COLTYPES['photo'] && ($RCMAIL->action == 'edit' || $RCMAIL->action == 'add')) {
726        $RCMAIL->output->add_gui_object('contactphoto', $attrib['id']);
727        $hidden = new html_hiddenfield(array('name' => '_photo', 'id' => 'ff_photo', 'value' => $ff_value));
728        $content .= $hidden->show();
729    }
730
731    return $content;
732}
733
734
735function rcmail_format_date_col($val)
736{
737    global $RCMAIL;
738    return format_date($val, $RCMAIL->config->get('date_format', 'Y-m-d'), false);
739}
740
741
742function rcmail_contact_key($row, $sort_col)
743{
744    $key = $row[$sort_col] . ':' . $row['sourceid'];
745
746    // add email to a key to not skip contacts with the same name (#1488375)
747    if (!empty($row['email'])) {
748         if (is_array($row['email'])) {
749             $key .= ':' . implode(':', $row['email']);
750         }
751         else {
752             $key .= ':' . $row['email'];
753         }
754     }
755
756     return $key;
757}
758
759
760/**
761 * Returns contact ID(s) and source(s) from GET/POST data
762 *
763 * @return array List of contact IDs per-source
764 */
765function rcmail_get_cids()
766{
767    // contact ID (or comma-separated list of IDs) is provided in two
768    // forms. If _source is an empty string then the ID is a string
769    // containing contact ID and source name in form: <ID>-<SOURCE>
770
771    $cid    = get_input_value('_cid', RCUBE_INPUT_GPC);
772    $source = (string) get_input_value('_source', RCUBE_INPUT_GPC);
773
774    if (!preg_match('/^[a-zA-Z0-9\+\/=_-]+(,[a-zA-Z0-9\+\/=_-]+)*$/', $cid)) {
775        return array();
776    }
777
778    $cid        = explode(',', $cid);
779    $got_source = strlen($source);
780    $result     = array();
781
782    // create per-source contact IDs array
783    foreach ($cid as $id) {
784        // if _source is not specified we'll find it from decoded ID
785        if (!$got_source) {
786            if ($sep = strrpos($id, '-')) {
787                $contact_id = substr($id, 0, $sep);
788                $source_id  = substr($id, $sep+1);
789                if (strlen($source_id)) {
790                    $result[(string)$source_id][] = $contact_id;
791                }
792            }
793        }
794        else {
795            $result[$source][] = $id;
796        }
797    }
798
799    return $result;
800}
801
802// register UI objects
803$OUTPUT->add_handlers(array(
804    'directorylist' => 'rcmail_directory_list',
805//  'groupslist' => 'rcmail_contact_groups',
806    'addresslist' => 'rcmail_contacts_list',
807    'addressframe' => 'rcmail_contact_frame',
808    'recordscountdisplay' => 'rcmail_rowcount_display',
809    'searchform' => array($OUTPUT, 'search_form')
810));
811
812// register action aliases
813$RCMAIL->register_action_map(array(
814    'add' => 'edit.inc',
815    'photo' => 'show.inc',
816    'group-create' => 'groups.inc',
817    'group-rename' => 'groups.inc',
818    'group-delete' => 'groups.inc',
819    'group-addmembers' => 'groups.inc',
820    'group-delmembers' => 'groups.inc',
821    'search-create' => 'search.inc',
822    'search-delete' => 'search.inc',
823));
Note: See TracBrowser for help on using the repository browser.