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