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