source: github/program/steps/addressbook/func.inc @ 041c93c

HEADdev-browser-capabilitiespdo
Last change on this file since 041c93c was 041c93c, checked in by Aleksander Machniak <alec@…>, 12 months ago

Removed $Id$

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