alecpl
2012-03-05 0203f16312bc949a5cf46ce90fa43de9119e23a8
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                       |
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  $Id$
22
23 */
24
3cacf9 25 $SEARCH_MODS_DEFAULT = array('name'=>1, 'firstname'=>1, 'surname'=>1, 'email'=>1, '*'=>1);
A 26
0501b6 27 // general definition of contact coltypes
T 28 $CONTACT_COLTYPES = array(
57ce50 29   'name'         => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('name'), 'category' => 'main'),
T 30   'firstname'    => array('type' => 'text', 'size' => 19, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('firstname'), 'category' => 'main'),
31   'surname'      => array('type' => 'text', 'size' => 19, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('surname'), 'category' => 'main'),
32   'email'        => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => rcube_label('email'), 'subtypes' => array('home','work','other'), 'category' => 'main'),
33   'middlename'   => array('type' => 'text', 'size' => 19, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('middlename'), 'category' => 'main'),
34   'prefix'       => array('type' => 'text', 'size' => 8,  'maxlength' => 20, 'limit' => 1, 'label' => rcube_label('nameprefix'), 'category' => 'main'),
35   'suffix'       => array('type' => 'text', 'size' => 8,  'maxlength' => 20, 'limit' => 1, 'label' => rcube_label('namesuffix'), 'category' => 'main'),
36   'nickname'     => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('nickname'), 'category' => 'main'),
37   'jobtitle'     => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('jobtitle'), 'category' => 'main'),
38   'organization' => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('organization'), 'category' => 'main'),
39   'department'   => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('department'), 'category' => 'main'),
e9a9f2 40   'gender'       => array('type' => 'select', 'limit' => 1, 'label' => rcube_label('gender'), 'options' => array('male' => rcube_label('male'), 'female' => rcube_label('female')), 'category' => 'personal'),
57ce50 41   'maidenname'   => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('maidenname'), 'category' => 'personal'),
T 42   'phone'        => array('type' => 'text', 'size' => 40, 'maxlength' => 20, 'label' => rcube_label('phone'), 'subtypes' => array('home','home2','work','work2','mobile','main','homefax','workfax','car','pager','video','assistant','other'), 'category' => 'main'),
0501b6 43   'address'      => array('type' => 'composite', 'label' => rcube_label('address'), 'subtypes' => array('home','work','other'), 'childs' => array(
57ce50 44     'street'     => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => rcube_label('street'), 'category' => 'main'),
T 45     'locality'   => array('type' => 'text', 'size' => 28, 'maxlength' => 50, 'label' => rcube_label('locality'), 'category' => 'main'),
46     'zipcode'    => array('type' => 'text', 'size' => 8,  'maxlength' => 15, 'label' => rcube_label('zipcode'), 'category' => 'main'),
47     'region'     => array('type' => 'text', 'size' => 12, 'maxlength' => 50, 'label' => rcube_label('region'), 'category' => 'main'),
48     'country'    => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => rcube_label('country'), 'category' => 'main'),
e9a9f2 49   ), 'category' => 'main'),
57ce50 50   'birthday'     => array('type' => 'date', 'size' => 12, 'maxlength' => 16, 'label' => rcube_label('birthday'), 'limit' => 1, 'render_func' => 'rcmail_format_date_col', 'category' => 'personal'),
T 51   'anniversary'  => array('type' => 'date', 'size' => 12, 'maxlength' => 16, 'label' => rcube_label('anniversary'), 'limit' => 1, 'render_func' => 'rcmail_format_date_col', 'category' => 'personal'),
dbb0c2 52   'website'      => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => rcube_label('website'), 'subtypes' => array('homepage','work','blog','profile','other'), 'category' => 'main'),
57ce50 53   'im'           => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => rcube_label('instantmessenger'), 'subtypes' => array('aim','icq','msn','yahoo','jabber','skype','other'), 'category' => 'main'),
T 54   'notes'        => array('type' => 'textarea', 'size' => 40, 'rows' => 15, 'maxlength' => 500, 'label' => rcube_label('notes'), 'limit' => 1),
e9a9f2 55   'photo'        => array('type' => 'image', 'limit' => 1, 'category' => 'main'),
57ce50 56   'assistant'    => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('assistant'), 'category' => 'personal'),
T 57   'manager'      => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('manager'), 'category' => 'personal'),
58   'spouse'       => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('spouse'), 'category' => 'personal'),
0501b6 59   // TODO: define fields for vcards like GEO, KEY
T 60 );
61
b611d3 62 $PAGE_SIZE = $RCMAIL->config->get('addressbook_pagesize', $RCMAIL->config->get('pagesize', 50));
ecf295 63
A 64 // Addressbook UI
65 if (!$RCMAIL->action && !$OUTPUT->ajax_call) {
66     // add list of address sources to client env
67     $js_list = $RCMAIL->get_address_sources();
68
ca7dda 69     $source = get_input_value('_source', RCUBE_INPUT_GPC);
A 70
ecf295 71     // use first directory by default
ca7dda 72     if (!strlen($source) || !isset($js_list[$source]))
A 73         $source = $js_list[key($js_list)]['id'];
ecf295 74
5c9d1f 75     // count all/writeable sources
5b3ac3 76     $writeable = 0;
5c9d1f 77     $count = 0;
A 78     foreach ($js_list as $sid => $s) {
79         $count++;
ecf295 80         if (!$s['readonly']) {
5b3ac3 81             $writeable++;
5c9d1f 82         }
A 83         // unset hidden sources
84         if ($s['hidden']) {
85             unset($js_list[$sid]);
ecf295 86         }
0501b6 87     }
ecf295 88
A 89     $search_mods = $RCMAIL->config->get('addressbook_search_mods', $SEARCH_MODS_DEFAULT);
90     $OUTPUT->set_env('search_mods', $search_mods);
91     $OUTPUT->set_env('address_sources', $js_list);
5b3ac3 92     $OUTPUT->set_env('writable_source', $writeable);
ecf295 93
A 94     $OUTPUT->set_pagetitle(rcube_label('addressbook'));
5c9d1f 95     $_SESSION['addressbooks_count'] = $count;
5b3ac3 96     $_SESSION['addressbooks_count_writeable'] = $writeable;
5c9d1f 97
fa5996 98     if (!strlen($source))
T 99       $source = strval(key($js_list));
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
163 function rcmail_default_source($writable=false)
164 {
165     global $RCMAIL;
166
167     // get list of address sources
34854b 168     $first = reset($RCMAIL->get_address_sources($writable));
ecf295 169
A 170     // use first directory by default
34854b 171     return $first['id'];
ecf295 172 }
0501b6 173
da89cf 174 function rcmail_set_sourcename($abook)
T 175 {
176     global $OUTPUT;
70c311 177
da89cf 178     // get address book name (for display)
T 179     if ($abook && $_SESSION['addressbooks_count'] > 1) {
180         $name = $abook->get_name();
181         if (!$name && $source == 0) {
182             $name = rcube_label('personaladrbook');
183         }
184         $OUTPUT->set_env('sourcename', $name);
185     }
186 }
187
0501b6 188
f11541 189 function rcmail_directory_list($attrib)
T 190 {
a79417 191     global $RCMAIL, $OUTPUT;
f11541 192
a79417 193     if (!$attrib['id'])
A 194         $attrib['id'] = 'rcmdirectorylist';
cc97ea 195
a79417 196     $out = '';
A 197     $local_id = '0';
198     $jsdata = array();
ca7dda 199
a79417 200     $line_templ = html::tag('li', array(
b0689b 201         'id' => 'rcmli%s', 'class' => '%s'),
a79417 202         html::a(array('href' => '%s',
ce988a 203             'rel' => '%s',
a79417 204             'onclick' => "return ".JS_OBJECT_NAME.".command('list','%s',this)"), '%s'));
cc97ea 205
ca7dda 206     $sources = (array) $OUTPUT->env['address_sources'];
A 207     reset($sources);
cc97ea 208
ca7dda 209     // currently selected source
A 210     $current = get_input_value('_source', RCUBE_INPUT_GPC);
211
212     foreach ($sources as $j => $source) {
64ebc9 213         $id = strval(strlen($source['id']) ? $source['id'] : $j);
a79417 214         $js_id = JQ($id);
b0689b 215
A 216         // set class name(s)
217         $class_name = 'addressbook';
218         if ($current === $id)
219             $class_name .= ' selected';
220         if ($source['readonly'])
221             $class_name .= ' readonly';
44143c 222         if ($source['class_name'])
A 223             $class_name .= ' ' . $source['class_name'];
b0689b 224
ce988a 225         $out .= sprintf($line_templ,
T 226             html_identifier($id),
b0689b 227             $class_name,
a79417 228             Q(rcmail_url(null, array('_source' => $id))),
ce988a 229             $source['id'],
a79417 230             $js_id, (!empty($source['name']) ? Q($source['name']) : Q($id)));
0501b6 231
T 232         $groupdata = array('out' => $out, 'jsdata' => $jsdata, 'source' => $id);
233         if ($source['groups'])
234             $groupdata = rcmail_contact_groups($groupdata);
a79417 235         $jsdata = $groupdata['jsdata'];
A 236         $out = $groupdata['out'];
237     }
238
f8e48d 239     $line_templ = html::tag('li', array(
A 240         'id' => 'rcmliS%s', 'class' => '%s'),
241         html::a(array('href' => '#', 'rel' => 'S%s',
242             'onclick' => "return ".JS_OBJECT_NAME.".command('listsearch', '%s', this)"), '%s'));
243
244     // Saved searches
245     $sources = $RCMAIL->user->list_searches(rcube_user::SEARCH_ADDRESSBOOK);
246     foreach ($sources as $j => $source) {
247         $id = $source['id'];
248         $js_id = JQ($id);
249
250         // set class name(s)
251         $class_name = 'contactsearch';
252         if ($current === $id)
253             $class_name .= ' selected';
254         if ($source['class_name'])
255             $class_name .= ' ' . $source['class_name'];
256
257         $out .= sprintf($line_templ,
258             html_identifier($id),
259             $class_name,
260             $id,
261             $js_id, (!empty($source['name']) ? Q($source['name']) : Q($id)));
262     }
263
264     $OUTPUT->set_env('contactgroups', $jsdata);
a79417 265     $OUTPUT->add_gui_object('folderlist', $attrib['id']);
5731d6 266     // add some labels to client
2c77f5 267     $OUTPUT->add_label('deletegroupconfirm', 'groupdeleting', 'addingmember', 'removingmember');
a79417 268
A 269     return html::tag('ul', $attrib, $out, html::$common_attrib);
a61bbb 270 }
T 271
272
bb8012 273 function rcmail_contact_groups($args)
a61bbb 274 {
a79417 275     global $RCMAIL;
a61bbb 276
a79417 277     $groups = $RCMAIL->get_address_book($args['source'])->list_groups();
bb8012 278
a79417 279     if (!empty($groups)) {
A 280         $line_templ = html::tag('li', array(
f6c7af 281             'id' => 'rcmliG%s', 'class' => 'contactgroup'),
a79417 282             html::a(array('href' => '#',
ce988a 283                 'rel' => '%s:%s',
a79417 284                 'onclick' => "return ".JS_OBJECT_NAME.".command('listgroup',{'source':'%s','id':'%s'},this)"), '%s'));
bb8012 285
a79417 286         $jsdata = array();
A 287         foreach ($groups as $group) {
ce988a 288             $args['out'] .= sprintf($line_templ,
T 289                 html_identifier($args['source'] . $group['ID']),
290                 $args['source'], $group['ID'],
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
a79417 299     return $args;
f11541 300 }
T 301
4e17e6 302
fa5996 303 // return the contacts list as HTML table
4e17e6 304 function rcmail_contacts_list($attrib)
a79417 305 {
A 306     global $CONTACTS, $OUTPUT;
4e17e6 307
0501b6 308     // define list of cols to be displayed
T 309     $a_show_cols = array('name');
310
a79417 311     // add id to message list table if not specified
A 312     if (!strlen($attrib['id']))
313         $attrib['id'] = 'rcmAddressList';
314
315     // create XHTML table
fa5996 316     $out = rcube_table_output($attrib, array(), $a_show_cols, $CONTACTS->primary_key);
a79417 317
A 318     // set client env
319     $OUTPUT->add_gui_object('contactslist', $attrib['id']);
320     $OUTPUT->set_env('current_page', (int)$CONTACTS->list_page);
321     $OUTPUT->include_script('list.js');
322
323     // add some labels to client
b17539 324     $OUTPUT->add_label('deletecontactconfirm', 'copyingcontact', 'contactdeleting');
a79417 325
A 326     return $out;
327 }
4e17e6 328
T 329
f11541 330 function rcmail_js_contacts_list($result, $prefix='')
a79417 331 {
A 332     global $OUTPUT;
4e17e6 333
a79417 334     if (empty($result) || $result->count == 0)
A 335         return;
4e17e6 336
a79417 337     // define list of cols to be displayed
A 338     $a_show_cols = array('name');
e9a9f2 339
a79417 340     while ($row = $result->next()) {
A 341         $a_row_cols = array();
e9a9f2 342
ecf295 343         // build contact ID with source ID
A 344         if (isset($row['sourceid'])) {
345             $row['ID'] = $row['ID'].'-'.$row['sourceid'];
346         }
347
a79417 348         // format each col
71e8cc 349         foreach ($a_show_cols as $col) {
f9a967 350             $val = $col == 'name' ? rcube_addressbook::compose_list_name($row) : $row[$col];
71e8cc 351             $a_row_cols[$col] = Q($val);
f9a967 352         }
a79417 353
A 354         $OUTPUT->command($prefix.'add_contact_row', $row['ID'], $a_row_cols);
f11541 355     }
a79417 356 }
4e17e6 357
T 358
359 // similar function as /steps/settings/identities.inc::rcmail_identity_frame()
360 function rcmail_contact_frame($attrib)
a79417 361 {
A 362     global $OUTPUT;
4e17e6 363
a79417 364     if (!$attrib['id'])
A 365         $attrib['id'] = 'rcmcontactframe';
ecf295 366
a79417 367     $attrib['name'] = $attrib['id'];
f11541 368
a79417 369     $OUTPUT->set_env('contentframe', $attrib['name']);
A 370     $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
e2c610 371
a79417 372     return html::iframe($attrib);
A 373 }
4e17e6 374
T 375
376 function rcmail_rowcount_display($attrib)
a79417 377 {
A 378     global $OUTPUT;
4e17e6 379
a79417 380     if (!$attrib['id'])
A 381         $attrib['id'] = 'rcmcountdisplay';
4e17e6 382
a79417 383     $OUTPUT->add_gui_object('countdisplay', $attrib['id']);
4e17e6 384
faaa1b 385     if ($attrib['label'])
T 386       $_SESSION['contactcountdisplay'] = $attrib['label'];
387
fa5996 388     return html::span($attrib, rcube_label('loading'));
a79417 389 }
4e17e6 390
T 391
ecf295 392 function rcmail_get_rowcount_text($result=null)
a79417 393 {
08ffd9 394     global $CONTACTS, $PAGE_SIZE;
ecf295 395
a79417 396     // read nr of contacts
ecf295 397     if (!$result) {
A 398         $result = $CONTACTS->get_result();
a79417 399     }
4e17e6 400
a79417 401     if ($result->count == 0)
A 402         $out = rcube_label('nocontactsfound');
403     else
404         $out = rcube_label(array(
faaa1b 405             'name'  => $_SESSION['contactcountdisplay'] ? $_SESSION['contactcountdisplay'] : 'contactsfromto',
a79417 406             'vars'  => array(
A 407             'from'  => $result->first + 1,
08ffd9 408             'to'    => min($result->count, $result->first + $PAGE_SIZE),
a79417 409             'count' => $result->count)
A 410         ));
411
412     return $out;
413 }
414
415
29aab5 416 function rcmail_get_type_label($type)
T 417 {
418     $label = 'type'.$type;
8703b0 419     if (rcube_label_exists($label, '*', $domain))
A 420         return rcube_label($label, $domain);
29aab5 421     else if (preg_match('/\w+(\d+)$/', $label, $m)
T 422             && ($label = preg_replace('/(\d+)$/', '', $label))
8703b0 423             && rcube_label_exists($label, '*', $domain))
A 424         return rcube_label($label, $domain) . ' ' . $m[1];
ecf295 425
29aab5 426     return ucfirst($type);
T 427 }
428
429
0501b6 430 function rcmail_contact_form($form, $record, $attrib = null)
a79417 431 {
0501b6 432     global $RCMAIL, $CONFIG;
a79417 433
A 434     // Allow plugins to modify contact form content
435     $plugin = $RCMAIL->plugins->exec_hook('contact_form', array(
436         'form' => $form, 'record' => $record));
437
438     $form = $plugin['form'];
439     $record = $plugin['record'];
0501b6 440     $edit_mode = $RCMAIL->action != 'show';
T 441     $del_button = $attrib['deleteicon'] ? html::img(array('src' => $CONFIG['skin_path'] . $attrib['deleteicon'], 'alt' => rcube_label('delete'))) : rcube_label('delete');
442     unset($attrib['deleteicon']);
a79417 443     $out = '';
89c30a 444
0501b6 445     // get default coltypes
T 446     $coltypes = $GLOBALS['CONTACT_COLTYPES'];
e9a9f2 447     $coltype_labels = array();
ecf295 448
0501b6 449     foreach ($coltypes as $col => $prop) {
T 450         if ($prop['subtypes']) {
c4e555 451             $subtype_names = array_map('rcmail_get_type_label', $prop['subtypes']);
0501b6 452             $select_subtype = new html_select(array('name' => '_subtype_'.$col.'[]', 'class' => 'contactselectsubtype'));
c4e555 453             $select_subtype->add($subtype_names, $prop['subtypes']);
0501b6 454             $coltypes[$col]['subtypes_select'] = $select_subtype->show();
T 455         }
456         if ($prop['childs']) {
457             foreach ($prop['childs'] as $childcol => $cp)
e9a9f2 458                 $coltype_labels[$childcol] = array('label' => $cp['label']);
0501b6 459         }
T 460     }
a79417 461
0501b6 462     foreach ($form as $section => $fieldset) {
T 463         // skip empty sections
a79417 464         if (empty($fieldset['content']))
A 465             continue;
466
0501b6 467         $select_add = new html_select(array('class' => 'addfieldmenu', 'rel' => $section));
T 468         $select_add->add(rcube_label('addfield'), '');
469
470         // render head section with name fields (not a regular list of rows)
471         if ($section == 'head') {
472             $content = '';
e84818 473
ec4b59 474             // unset display name if it is composed from name parts
445a4c 475             if ($record['name'] == rcube_addressbook::compose_display_name(array('name' => '') + (array)$record))
0501b6 476               unset($record['name']);
T 477
478             // group fields
479             $field_blocks = array(
480                 'names'    => array('prefix','firstname','middlename','surname','suffix'),
481                 'displayname' => array('name'),
482                 'nickname' => array('nickname'),
317372 483                 'organization' => array('organization'),
T 484                 'department' => array('department'),
485                 'jobtitle' => array('jobtitle'),
0501b6 486             );
T 487             foreach ($field_blocks as $blockname => $colnames) {
488                 $fields = '';
489                 foreach ($colnames as $col) {
490                     // skip cols unknown to the backend
491                     if (!$coltypes[$col])
492                         continue;
ecf295 493
0fbade 494                     // only string values are expected here
T 495                     if (is_array($record[$col]))
496                         $record[$col] = join(' ', $record[$col]);
0501b6 497
T 498                     if ($RCMAIL->action == 'show') {
499                         if (!empty($record[$col]))
500                             $fields .= html::span('namefield ' . $col, Q($record[$col])) . " ";
501                     }
502                     else {
503                         $colprop = (array)$fieldset['content'][$col] + (array)$coltypes[$col];
504                         $colprop['id'] = 'ff_'.$col;
505                         if (empty($record[$col]) && !$colprop['visible']) {
506                             $colprop['style'] = 'display:none';
507                             $select_add->add($colprop['label'], $col);
508                         }
509                         $fields .= rcmail_get_edit_field($col, $record[$col], $colprop, $colprop['type']);
510                     }
511                 }
512                 $content .= html::div($blockname, $fields);
513             }
ecf295 514
0501b6 515             if ($edit_mode)
T 516                 $content .= html::p('addfield', $select_add->show(null));
517
518             $out .= html::tag('fieldset', $attrib, (!empty($fieldset['name']) ? html::tag('legend', null, Q($fieldset['name'])) : '') . $content) ."\n";
519             continue;
520         }
521
a79417 522         $content = '';
A 523         if (is_array($fieldset['content'])) {
524             foreach ($fieldset['content'] as $col => $colprop) {
0501b6 525                 // remove subtype part of col name
T 526                 list($field, $subtype) = explode(':', $col);
527                 if (!$subtype) $subtype = 'home';
528                 $fullkey = $col.':'.$subtype;
a79417 529
0501b6 530                 // skip cols unknown to the backend
T 531                 if (!$coltypes[$field])
532                     continue;
533
534                 // merge colprop with global coltype configuration
535                 $colprop += $coltypes[$field];
536                 $label = isset($colprop['label']) ? $colprop['label'] : rcube_label($col);
537
538                 // prepare subtype selector in edit mode
539                 if ($edit_mode && is_array($colprop['subtypes'])) {
29aab5 540                     $subtype_names = array_map('rcmail_get_type_label', $colprop['subtypes']);
0501b6 541                     $select_subtype = new html_select(array('name' => '_subtype_'.$col.'[]', 'class' => 'contactselectsubtype'));
29aab5 542                     $select_subtype->add($subtype_names, $colprop['subtypes']);
0501b6 543                 }
T 544                 else
545                     $select_subtype = null;
a79417 546
A 547                 if (!empty($colprop['value'])) {
0501b6 548                     $values = (array)$colprop['value'];
a79417 549                 }
A 550                 else {
0501b6 551                     // iterate over possible subtypes and collect values with their subtype
T 552                     if (is_array($colprop['subtypes'])) {
553                         $values = $subtypes = array();
554                         foreach ($colprop['subtypes'] as $i => $st) {
555                             $newval = false;
556                             if ($record[$field.':'.$st]) {
557                                 $subtypes[count($values)] = $st;
558                                 $newval = $record[$field.':'.$st];
559                             }
560                             else if ($i == 0 && $record[$field]) {
561                                 $subtypes[count($values)] = $st;
562                                 $newval = $record[$field];
563                             }
564                             if ($newval !== false) {
565                                 if (is_array($newval) && isset($newval[0]))
566                                     $values = array_merge($values, $newval);
567                                 else
568                                     $values[] = $newval;
569                             }
570                         }
571                     }
572                     else {
573                         $values = $record[$fullkey] ? $record[$fullkey] : $record[$field];
574                         $subtypes = null;
575                     }
a79417 576                 }
A 577
0501b6 578                 // hack: create empty values array to force this field to be displayed
T 579                 if (empty($values) && $colprop['visible'])
580                     $values[] = '';
581
582                 $rows = '';
583                 foreach ((array)$values as $i => $val) {
584                     if ($subtypes[$i])
585                         $subtype = $subtypes[$i];
586
587                     // render composite field
588                     if ($colprop['type'] == 'composite') {
589                         $composite = array(); $j = 0;
590                         $template = $RCMAIL->config->get($col . '_template', '{'.join('} {', array_keys($colprop['childs'])).'}');
591                         foreach ($colprop['childs'] as $childcol => $cp) {
592                             $childvalue = $val[$childcol] ? $val[$childcol] : $val[$j];
593
594                             if ($edit_mode) {
595                                 if ($colprop['subtypes'] || $colprop['limit'] != 1) $cp['array'] = true;
596                                 $composite['{'.$childcol.'}'] = rcmail_get_edit_field($childcol, $childvalue, $cp, $cp['type']) . " ";
597                             }
598                             else {
599                                 $childval = $cp['render_func'] ? call_user_func($cp['render_func'], $childvalue, $childcol) : Q($childvalue);
600                                 $composite['{'.$childcol.'}'] = html::span('data ' . $childcol, $childval) . " ";
601                             }
602                             $j++;
603                         }
604
605                         $coltypes[$field] += (array)$colprop;
606                         $coltypes[$field]['count']++;
3ac5cd 607                         $val = preg_replace('/\{\w+\}/', '', strtr($template, $composite));
0501b6 608                     }
T 609                     else if ($edit_mode) {
610                         // call callback to render/format value
611                         if ($colprop['render_func'])
612                             $val = call_user_func($colprop['render_func'], $val, $col);
613
614                         $coltypes[$field] = (array)$colprop + $coltypes[$field];
615
616                         if ($colprop['subtypes'] || $colprop['limit'] != 1)
617                             $colprop['array'] = true;
618
537c39 619                         // load jquery UI datepickert for date fields
T 620                         if ($colprop['type'] == 'date') {
621                             $colprop['class'] .= ($colprop['class'] ? ' ' : '') . 'datepicker';
89c30a 622                             $val = rcmail_format_date_col($val);
537c39 623                         }
T 624
0501b6 625                         $val = rcmail_get_edit_field($col, $val, $colprop, $colprop['type']);
T 626                         $coltypes[$field]['count']++;
627                     }
628                     else if ($colprop['render_func'])
629                         $val = call_user_func($colprop['render_func'], $val, $col);
630                     else if (is_array($colprop['options']) && isset($colprop['options'][$val]))
631                         $val = $colprop['options'][$val];
632                     else
633                         $val = Q($val);
634
635                     // use subtype as label
636                     if ($colprop['subtypes'])
29aab5 637                         $label = rcmail_get_type_label($subtype);
0501b6 638
T 639                     // add delete button/link
640                     if ($edit_mode && !($colprop['visible'] && $colprop['limit'] == 1))
641                         $val .= html::a(array('href' => '#del', 'class' => 'contactfieldbutton deletebutton', 'title' => rcube_label('delete'), 'rel' => $col), $del_button);
642
643                     // display row with label
644                     if ($label) {
645                         $rows .= html::div('row',
646                             html::div('contactfieldlabel label', $select_subtype ? $select_subtype->show($subtype) : Q($label)) .
647                             html::div('contactfieldcontent '.$colprop['type'], $val));
648                     }
649                     else   // row without label
650                         $rows .= html::div('row', html::div('contactfield', $val));
651                 }
ecf295 652
0501b6 653                 // add option to the add-field menu
T 654                 if (!$colprop['limit'] || $coltypes[$field]['count'] < $colprop['limit']) {
655                     $select_add->add($colprop['label'], $col);
656                     $select_add->_count++;
657                 }
ecf295 658
0501b6 659                 // wrap rows in fieldgroup container
7772ae 660                 if ($rows) {
A 661                     $content .= html::tag('fieldset', array('class' => 'contactfieldgroup ' . ($colprop['subtypes'] ? 'contactfieldgroupmulti ' : '') . 'contactcontroller' . $col, 'style' => ($rows ? null : 'display:none')),
662                       ($colprop['subtypes'] ? html::tag('legend', null, Q($colprop['label'])) : ' ') .
663                       $rows);
664                 }
a79417 665             }
0501b6 666
01169b 667             if (!$content)
2df4a9 668                 continue;
A 669
0501b6 670             // also render add-field selector
T 671             if ($edit_mode)
672                 $content .= html::p('addfield', $select_add->show(null, array('style' => $select_add->_count ? null : 'display:none')));
673
674             $content = html::div(array('id' => 'contactsection' . $section), $content);
a79417 675         }
A 676         else {
677             $content = $fieldset['content'];
678         }
679
01169b 680         if ($content)
A 681             $out .= html::tag('fieldset', null, html::tag('legend', null, Q($fieldset['name'])) . $content) ."\n";
a79417 682     }
0501b6 683
T 684     if ($edit_mode) {
e9a9f2 685       $RCMAIL->output->set_env('coltypes', $coltypes + $coltype_labels);
0501b6 686       $RCMAIL->output->set_env('delbutton', $del_button);
T 687       $RCMAIL->output->add_label('delete');
688     }
689
a79417 690     return $out;
0501b6 691 }
T 692
693
694 function rcmail_contact_photo($attrib)
695 {
c3dabf 696     global $SOURCE_ID, $CONTACTS, $CONTACT_COLTYPES, $RCMAIL, $CONFIG;
e50551 697
0501b6 698     if ($result = $CONTACTS->get_result())
T 699         $record = $result->first();
e50551 700
0501b6 701     $photo_img = $attrib['placeholder'] ? $CONFIG['skin_path'] . $attrib['placeholder'] : 'program/blank.gif';
e50551 702     $RCMAIL->output->set_env('photo_placeholder', $photo_img);
0501b6 703     unset($attrib['placeholder']);
e50551 704
6a6168 705     if (preg_match('!^https?://!i', $record['photo']))
e50551 706         $photo_img = $record['photo'];
A 707     else if ($record['photo'])
c3dabf 708         $photo_img = $RCMAIL->url(array('_action' => 'photo', '_cid' => $record['ID'], '_source' => $SOURCE_ID));
e50551 709     else
A 710         $ff_value = '-del-'; // will disable delete-photo action
711
712     $img = html::img(array('src' => $photo_img, 'border' => 1, 'alt' => ''));
713     $content = html::div($attrib, $img);
714
faaa1b 715     if ($CONTACT_COLTYPES['photo'] && ($RCMAIL->action == 'edit' || $RCMAIL->action == 'add')) {
e50551 716         $RCMAIL->output->add_gui_object('contactphoto', $attrib['id']);
A 717         $hidden = new html_hiddenfield(array('name' => '_photo', 'id' => 'ff_photo', 'value' => $ff_value));
718         $content .= $hidden->show();
719     }
720
721     return $content;
0501b6 722 }
T 723
724
725 function rcmail_format_date_col($val)
726 {
727     global $RCMAIL;
77153b 728     return format_date($val, $RCMAIL->config->get('date_format', 'Y-m-d'), false);
a79417 729 }
A 730
731
0203f1 732 function rcmail_contact_key($row, $sort_col)
A 733 {
734     $key = $row[$sort_col] . ':' . $row['sourceid'];
735
736     // add email to a key to not skip contacts with the same name (#1488375)
737     if (!empty($row['email'])) {
738          if (is_array($row['email'])) {
739              $key .= ':' . implode(':', $row['email']);
740          }
741          else {
742              $key .= ':' . $row['email'];
743          }
744      }
745
746      return $key;
747 }
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  */
755 function rcmail_get_cids()
756 {
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
A 764     if (!preg_match('/^[a-zA-Z0-9\+\/=_-]+(,[a-zA-Z0-9\+\/=_-]+)*$/', $cid)) {
765         return array();
766     }
767
768     $cid        = explode(',', $cid);
769     $got_source = strlen($source);
770     $result     = array();
771
772     // create per-source contact IDs array
773     foreach ($cid as $id) {
774         // if _source is not specified we'll find it from decoded ID
775         if (!$got_source) {
179b39 776             if ($sep = strrpos($id, '-')) {
A 777                 $contact_id = substr($id, 0, $sep);
778                 $source_id  = substr($id, $sep+1);
779                 if (strlen($source_id)) {
780                     $result[(string)$source_id][] = $contact_id;
781                 }
ecf295 782             }
A 783         }
784         else {
785             $result[$source][] = $id;
786         }
787     }
788
789     return $result;
790 }
791
f11541 792 // register UI objects
T 793 $OUTPUT->add_handlers(array(
a79417 794     'directorylist' => 'rcmail_directory_list',
a61bbb 795 //  'groupslist' => 'rcmail_contact_groups',
a79417 796     'addresslist' => 'rcmail_contacts_list',
A 797     'addressframe' => 'rcmail_contact_frame',
798     'recordscountdisplay' => 'rcmail_rowcount_display',
799     'searchform' => array($OUTPUT, 'search_form')
f11541 800 ));
68d2d5 801
A 802 // register action aliases
803 $RCMAIL->register_action_map(array(
804     'add' => 'edit.inc',
0501b6 805     'photo' => 'show.inc',
68d2d5 806     'group-create' => 'groups.inc',
A 807     'group-rename' => 'groups.inc',
808     'group-delete' => 'groups.inc',
809     'group-addmembers' => 'groups.inc',
810     'group-delmembers' => 'groups.inc',
f8e48d 811     'search-create' => 'search.inc',
A 812     'search-delete' => 'search.inc',
68d2d5 813 ));