+{
+ global $OUTPUT;
+
+ if (!$attrib['id'])
+ $attrib['id'] = 'rcmcountdisplay';
+
+ $OUTPUT->add_gui_object('countdisplay', $attrib['id']);
+
+ return html::span($attrib, rcube_label('loading'));
+}
+
+
+function rcmail_get_rowcount_text($result=null)
+{
+ global $CONTACTS, $CONFIG;
+
+ // read nr of contacts
+ if (!$result) {
+ $result = $CONTACTS->get_result();
+ }
+
+ if ($result->count == 0)
+ $out = rcube_label('nocontactsfound');
+ else
+ $out = rcube_label(array(
+ 'name' => 'contactsfromto',
+ 'vars' => array(
+ 'from' => $result->first + 1,
+ 'to' => min($result->count, $result->first + $CONFIG['pagesize']),
+ 'count' => $result->count)
+ ));
+
+ return $out;
+}
+
+
+function rcmail_get_type_label($type)
+{
+ $label = 'type'.$type;
+ if (rcube_label_exists($label, '*', $domain))
+ return rcube_label($label, $domain);
+ else if (preg_match('/\w+(\d+)$/', $label, $m)
+ && ($label = preg_replace('/(\d+)$/', '', $label))
+ && rcube_label_exists($label, '*', $domain))
+ return rcube_label($label, $domain) . ' ' . $m[1];
+
+ return ucfirst($type);
+}
+
+
+function rcmail_contact_form($form, $record, $attrib = null)
+{
+ global $RCMAIL, $CONFIG;
+ static $jqueryui_loaded = 0;
+
+ // Allow plugins to modify contact form content
+ $plugin = $RCMAIL->plugins->exec_hook('contact_form', array(
+ 'form' => $form, 'record' => $record));
+
+ $form = $plugin['form'];
+ $record = $plugin['record'];
+ $edit_mode = $RCMAIL->action != 'show';
+ $del_button = $attrib['deleteicon'] ? html::img(array('src' => $CONFIG['skin_path'] . $attrib['deleteicon'], 'alt' => rcube_label('delete'))) : rcube_label('delete');
+ unset($attrib['deleteicon']);
+ $out = '';
+
+ // get default coltypes
+ $coltypes = $GLOBALS['CONTACT_COLTYPES'];
+ $coltype_labels = array();
+
+ foreach ($coltypes as $col => $prop) {
+ if ($prop['subtypes']) {
+ $subtype_names = array_map('rcmail_get_type_label', $prop['subtypes']);
+ $select_subtype = new html_select(array('name' => '_subtype_'.$col.'[]', 'class' => 'contactselectsubtype'));
+ $select_subtype->add($subtype_names, $prop['subtypes']);
+ $coltypes[$col]['subtypes_select'] = $select_subtype->show();
+ }
+ if ($prop['childs']) {
+ foreach ($prop['childs'] as $childcol => $cp)
+ $coltype_labels[$childcol] = array('label' => $cp['label']);
+ }
+ }
+
+ foreach ($form as $section => $fieldset) {
+ // skip empty sections
+ if (empty($fieldset['content']))
+ continue;
+
+ $select_add = new html_select(array('class' => 'addfieldmenu', 'rel' => $section));
+ $select_add->add(rcube_label('addfield'), '');
+
+ // render head section with name fields (not a regular list of rows)
+ if ($section == 'head') {
+ $content = '';
+
+ // unset display name if it is composed from name parts
+ if ($record['name'] == rcube_addressbook::compose_display_name(array('name' => '') + (array)$record))
+ unset($record['name']);
+
+ // group fields
+ $field_blocks = array(
+ 'names' => array('prefix','firstname','middlename','surname','suffix'),
+ 'displayname' => array('name'),
+ 'nickname' => array('nickname'),
+ 'organization' => array('organization'),
+ 'department' => array('department'),
+ 'jobtitle' => array('jobtitle'),
+ );
+ foreach ($field_blocks as $blockname => $colnames) {
+ $fields = '';
+ foreach ($colnames as $col) {
+ // skip cols unknown to the backend
+ if (!$coltypes[$col])
+ continue;
+
+ // only string values are expected here
+ if (is_array($record[$col]))
+ $record[$col] = join(' ', $record[$col]);
+
+ if ($RCMAIL->action == 'show') {
+ if (!empty($record[$col]))
+ $fields .= html::span('namefield ' . $col, Q($record[$col])) . " ";
+ }
+ else {
+ $colprop = (array)$fieldset['content'][$col] + (array)$coltypes[$col];
+ $colprop['id'] = 'ff_'.$col;
+ if (empty($record[$col]) && !$colprop['visible']) {
+ $colprop['style'] = 'display:none';
+ $select_add->add($colprop['label'], $col);
+ }
+ $fields .= rcmail_get_edit_field($col, $record[$col], $colprop, $colprop['type']);
+ }
+ }
+ $content .= html::div($blockname, $fields);
+ }
+
+ if ($edit_mode)
+ $content .= html::p('addfield', $select_add->show(null));
+
+ $out .= html::tag('fieldset', $attrib, (!empty($fieldset['name']) ? html::tag('legend', null, Q($fieldset['name'])) : '') . $content) ."\n";
+ continue;
+ }
+
+ $content = '';
+ if (is_array($fieldset['content'])) {
+ foreach ($fieldset['content'] as $col => $colprop) {
+ // remove subtype part of col name
+ list($field, $subtype) = explode(':', $col);
+ if (!$subtype) $subtype = 'home';
+ $fullkey = $col.':'.$subtype;
+
+ // skip cols unknown to the backend
+ if (!$coltypes[$field])
+ continue;
+
+ // merge colprop with global coltype configuration
+ $colprop += $coltypes[$field];
+ $label = isset($colprop['label']) ? $colprop['label'] : rcube_label($col);
+
+ // prepare subtype selector in edit mode
+ if ($edit_mode && is_array($colprop['subtypes'])) {
+ $subtype_names = array_map('rcmail_get_type_label', $colprop['subtypes']);
+ $select_subtype = new html_select(array('name' => '_subtype_'.$col.'[]', 'class' => 'contactselectsubtype'));
+ $select_subtype->add($subtype_names, $colprop['subtypes']);
+ }
+ else
+ $select_subtype = null;
+
+ if (!empty($colprop['value'])) {
+ $values = (array)$colprop['value'];
+ }
+ else {
+ // iterate over possible subtypes and collect values with their subtype
+ if (is_array($colprop['subtypes'])) {
+ $values = $subtypes = array();
+ foreach ($colprop['subtypes'] as $i => $st) {
+ $newval = false;
+ if ($record[$field.':'.$st]) {
+ $subtypes[count($values)] = $st;
+ $newval = $record[$field.':'.$st];
+ }
+ else if ($i == 0 && $record[$field]) {
+ $subtypes[count($values)] = $st;
+ $newval = $record[$field];
+ }
+ if ($newval !== false) {
+ if (is_array($newval) && isset($newval[0]))
+ $values = array_merge($values, $newval);
+ else
+ $values[] = $newval;
+ }
+ }
+ }
+ else {
+ $values = $record[$fullkey] ? $record[$fullkey] : $record[$field];
+ $subtypes = null;
+ }
+ }
+
+ // hack: create empty values array to force this field to be displayed
+ if (empty($values) && $colprop['visible'])
+ $values[] = '';
+
+ $rows = '';
+ foreach ((array)$values as $i => $val) {
+ if ($subtypes[$i])
+ $subtype = $subtypes[$i];
+
+ // render composite field
+ if ($colprop['type'] == 'composite') {
+ $composite = array(); $j = 0;
+ $template = $RCMAIL->config->get($col . '_template', '{'.join('} {', array_keys($colprop['childs'])).'}');
+ foreach ($colprop['childs'] as $childcol => $cp) {
+ $childvalue = $val[$childcol] ? $val[$childcol] : $val[$j];
+
+ if ($edit_mode) {
+ if ($colprop['subtypes'] || $colprop['limit'] != 1) $cp['array'] = true;
+ $composite['{'.$childcol.'}'] = rcmail_get_edit_field($childcol, $childvalue, $cp, $cp['type']) . " ";
+ }
+ else {
+ $childval = $cp['render_func'] ? call_user_func($cp['render_func'], $childvalue, $childcol) : Q($childvalue);
+ $composite['{'.$childcol.'}'] = html::span('data ' . $childcol, $childval) . " ";
+ }
+ $j++;
+ }
+
+ $coltypes[$field] += (array)$colprop;
+ $coltypes[$field]['count']++;
+ $val = strtr($template, $composite);
+ }
+ else if ($edit_mode) {
+ // call callback to render/format value
+ if ($colprop['render_func'])
+ $val = call_user_func($colprop['render_func'], $val, $col);
+
+ $coltypes[$field] = (array)$colprop + $coltypes[$field];
+
+ if ($colprop['subtypes'] || $colprop['limit'] != 1)
+ $colprop['array'] = true;
+
+ // load jquery UI datepickert for date fields
+ if ($colprop['type'] == 'date') {
+ if (!$jqueryui_loaded++) {
+ $RCMAIL->plugins->load_plugin('jqueryui');
+ $RCMAIL->output->set_env('date_format', strtr($RCMAIL->config->get('date_format', 'Y-m-d'), array('y'=>'y', 'Y'=>'yy', 'm'=>'mm', 'n'=>'m', 'd'=>'dd', 'j'=>'d')));
+ foreach (array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec') as $month)
+ $month_names[] = rcube_label($month);
+ $RCMAIL->output->set_env('month_names', $month_names);
+ }
+ $colprop['class'] .= ($colprop['class'] ? ' ' : '') . 'datepicker';
+ $val = format_date($val, $RCMAIL->config->get('date_format', 'Y-m-d'), false);
+ }
+
+ $val = rcmail_get_edit_field($col, $val, $colprop, $colprop['type']);
+ $coltypes[$field]['count']++;
+ }
+ else if ($colprop['render_func'])
+ $val = call_user_func($colprop['render_func'], $val, $col);
+ else if (is_array($colprop['options']) && isset($colprop['options'][$val]))
+ $val = $colprop['options'][$val];
+ else
+ $val = Q($val);
+
+ // use subtype as label
+ if ($colprop['subtypes'])
+ $label = rcmail_get_type_label($subtype);
+
+ // add delete button/link
+ if ($edit_mode && !($colprop['visible'] && $colprop['limit'] == 1))
+ $val .= html::a(array('href' => '#del', 'class' => 'contactfieldbutton deletebutton', 'title' => rcube_label('delete'), 'rel' => $col), $del_button);
+
+ // display row with label
+ if ($label) {
+ $rows .= html::div('row',
+ html::div('contactfieldlabel label', $select_subtype ? $select_subtype->show($subtype) : Q($label)) .
+ html::div('contactfieldcontent '.$colprop['type'], $val));
+ }
+ else // row without label
+ $rows .= html::div('row', html::div('contactfield', $val));
+ }
+
+ // add option to the add-field menu
+ if (!$colprop['limit'] || $coltypes[$field]['count'] < $colprop['limit']) {
+ $select_add->add($colprop['label'], $col);
+ $select_add->_count++;
+ }
+
+ // wrap rows in fieldgroup container
+ if ($rows) {
+ $content .= html::tag('fieldset', array('class' => 'contactfieldgroup ' . ($colprop['subtypes'] ? 'contactfieldgroupmulti ' : '') . 'contactcontroller' . $col, 'style' => ($rows ? null : 'display:none')),
+ ($colprop['subtypes'] ? html::tag('legend', null, Q($colprop['label'])) : ' ') .
+ $rows);
+ }
+ }
+
+ if (!$content)
+ continue;
+
+ // also render add-field selector
+ if ($edit_mode)
+ $content .= html::p('addfield', $select_add->show(null, array('style' => $select_add->_count ? null : 'display:none')));
+
+ $content = html::div(array('id' => 'contactsection' . $section), $content);
+ }
+ else {
+ $content = $fieldset['content'];
+ }
+
+ if ($content)
+ $out .= html::tag('fieldset', null, html::tag('legend', null, Q($fieldset['name'])) . $content) ."\n";
+ }
+
+ if ($edit_mode) {
+ $RCMAIL->output->set_env('coltypes', $coltypes + $coltype_labels);
+ $RCMAIL->output->set_env('delbutton', $del_button);
+ $RCMAIL->output->add_label('delete');
+ }
+
+ return $out;
+}
+
+
+function rcmail_contact_photo($attrib)
+{
+ global $SOURCE_ID, $CONTACTS, $CONTACT_COLTYPES, $RCMAIL, $CONFIG;
+
+ if (!$CONTACT_COLTYPES['photo'])
+ return '';
+
+ if ($result = $CONTACTS->get_result())
+ $record = $result->first();
+
+ $photo_img = $attrib['placeholder'] ? $CONFIG['skin_path'] . $attrib['placeholder'] : 'program/blank.gif';
+ $RCMAIL->output->set_env('photo_placeholder', $photo_img);
+ unset($attrib['placeholder']);
+
+ if (preg_match('!^https?://!i', $record['photo']))
+ $photo_img = $record['photo'];
+ else if ($record['photo'])
+ $photo_img = $RCMAIL->url(array('_action' => 'photo', '_cid' => $record['ID'], '_source' => $SOURCE_ID));
+ else
+ $ff_value = '-del-'; // will disable delete-photo action
+
+ $img = html::img(array('src' => $photo_img, 'border' => 1, 'alt' => ''));
+ $content = html::div($attrib, $img);
+
+ if ($RCMAIL->action == 'edit' || $RCMAIL->action == 'add') {
+ $RCMAIL->output->add_gui_object('contactphoto', $attrib['id']);
+ $hidden = new html_hiddenfield(array('name' => '_photo', 'id' => 'ff_photo', 'value' => $ff_value));
+ $content .= $hidden->show();
+ }
+
+ return $content;
+}
+
+
+function rcmail_format_date_col($val)
+{
+ global $RCMAIL;
+ return format_date($val, $RCMAIL->config->get('date_format', 'Y-m-d'), false);
+}
+
+
+/**
+ * Returns contact ID(s) and source(s) from GET/POST data
+ *
+ * @return array List of contact IDs per-source
+ */
+function rcmail_get_cids()
+{
+ // contact ID (or comma-separated list of IDs) is provided in two
+ // forms. If _source is an empty string then the ID is a string
+ // containing contact ID and source name in form: <ID>-<SOURCE>
+
+ $cid = get_input_value('_cid', RCUBE_INPUT_GPC);
+ $source = (string) get_input_value('_source', RCUBE_INPUT_GPC);
+
+ if (!preg_match('/^[a-zA-Z0-9\+\/=_-]+(,[a-zA-Z0-9\+\/=_-]+)*$/', $cid)) {
+ return array();
+ }
+
+ $cid = explode(',', $cid);
+ $got_source = strlen($source);
+ $result = array();
+
+ // create per-source contact IDs array
+ foreach ($cid as $id) {
+ // if _source is not specified we'll find it from decoded ID
+ if (!$got_source) {
+ if ($sep = strrpos($id, '-')) {
+ $contact_id = substr($id, 0, $sep);
+ $source_id = substr($id, $sep+1);
+ if (strlen($source_id)) {
+ $result[(string)$source_id][] = $contact_id;
+ }
+ }
+ }
+ else {
+ $result[$source][] = $id;
+ }
+ }
+
+ return $result;
+}
+