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