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