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