* Addressbook/InfoLog/Filemanager/Tracker: added or fixed formatting of certain custom field types using a single shared method

This commit is contained in:
Ralf Becker 2012-08-09 12:41:47 +00:00
parent e83a7c1b8d
commit f7fa8ea37e
5 changed files with 141 additions and 86 deletions

View File

@ -277,24 +277,9 @@ class addressbook_tracking extends bo_tracking
break;
}
}
if ($this->contacts->customfields)
{
foreach($this->contacts->customfields as $name => $custom)
{
if (!$header_done)
{
$details['custom'] = array(
'value' => lang('Custom fields').':',
'type' => 'reply',
);
$header_done = true;
}
$details['#'.$name] = array(
'label' => $custom['label'],
'value' => $custom['type'] == 'select' ? $custom['values'][$data['#'.$name]] : $data['#'.$name],
);
}
}
// add custom fields for given type
$details += $this->get_customfields($data, $data['tid']);
return $details;
}
}

View File

@ -245,42 +245,11 @@ abstract class bo_merge
}
if ($name != 'photo') $replacements['$$'.($prefix ? $prefix.'/':'').$name.'$$'] = $value;
}
// set custom fields
// set custom fields, should probably go to a general method all apps can use
foreach($this->contacts->customfields as $name => $field)
{
$name = '#'.$name;
$value = (string)$contact[$name];
switch($field['type'])
{
case 'select-account':
if ($value) $value = common::grab_owner_name($value);
break;
case 'select':
if (count($field['values']) == 1 && isset($field['values']['@']))
{
$field['values'] = customfields_widget::_get_options_from_file($field['values']['@']);
}
$values = array();
foreach($field['rows'] > 1 ? explode(',',$value) : (array) $value as $value)
{
$values[] = $field['values'][$value];
}
$value = implode(', ',$values);
break;
case 'date':
case 'date-time':
if ($value)
{
$format = $field['len'] ? $field['len'] : ($field['type'] == 'date' ? 'Y-m-d' : 'Y-m-d H:i:s');
$date = array_combine(preg_split('/[\\/. :-]/',$format),preg_split('/[\\/. :-]/',$value));
$value = common::dateformatorder($date['Y'],$date['m'],$date['d'],true);
if (isset($date['H'])) $value .= ' '.common::formattime($date['H'],$date['i']);
}
break;
}
$replacements['$$'.($prefix ? $prefix.'/':'').$name.'$$'] = $value;
$replacements['$$'.($prefix ? $prefix.'/':'').$name.'$$'] = customfields_widget::format_customfield($field, (string)$contact[$name]);
}
// Add in extra cat field
@ -1578,7 +1547,7 @@ abstract class bo_merge
),true);
}
}
$dircount = array();
foreach($files as $key => $file)
{
@ -1635,7 +1604,7 @@ abstract class bo_merge
if ($GLOBALS['egw_info']['flags']['currentapp'] == 'addressbook') $current_level[$prefix.$file['name']]['confirm_multiple'] = lang('Do you want to send the message to all selected entries, WITHOUT further editing?');
}
break;
default:
if(!is_array($current_level[$prefix.$name_arr[$count]]))
{
@ -1687,7 +1656,7 @@ abstract class bo_merge
}
}
}
return array(
'icon' => 'etemplate/merge',
'caption' => $caption,

View File

@ -1,12 +1,12 @@
<?php
/**
* eGroupWare - abstract base class for tracking (history log, notifications, ...)
* EGroupware - abstract base class for tracking (history log, notifications, ...)
*
* @link http://www.egroupware.org
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @package etemplate
* @subpackage api
* @copyright (c) 2007-10 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @copyright (c) 2007-12 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @version $Id$
*/
@ -210,6 +210,8 @@ abstract class bo_tracking
/**
* Get the details of an entry
*
* You can/should call $this->get_customfields() to add custom fields.
*
* @param array|object $data
* @param int|string $receiver nummeric account_id or email address
* @return array of details as array with values for keys 'label','value','type'
@ -219,6 +221,43 @@ abstract class bo_tracking
return array();
}
/**
* Get custom fields of an entry of an entry
*
* @param array|object $data
* @param string $only_type2=null if given only return fields of type2 == $only_type2
* @return array of details as array with values for keys 'label','value','type'
*/
function get_customfields($data, $only_type2=null)
{
$details = array();
if (($cfs = config::get_customfields($this->app, $all_private_too=false, $only_type2)))
{
$header_done = false;
foreach($cfs as $name => $field)
{
if (in_array($field['type'], customfields_widget::$non_printable_fields)) continue;
if (!$header_done)
{
$details['custom'] = array(
'value' => lang('Custom fields').':',
'type' => 'reply',
);
$header_done = true;
}
//error_log(__METHOD__."() $name: data['#$name']=".array2string($data['#'.$name]).", field[values]=".array2string($field['values']));
$details['#'.$name] = array(
'label' => $field['label'],
'value' => customfields_widget::format_customfield($field, $data['#'.$name]),
);
//error_log("--> details['#$name']=".array2string($details['#'.$name]));
}
}
return $details;
}
/**
* Get a config value, which can depend on $data and $old
*

View File

@ -1,6 +1,6 @@
<?php
/**
* eGroupWare eTemplate Widget for custom fields
* EGroupware eTemplate Widget for custom fields
*
* @link http://www.egroupware.org
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
@ -514,7 +514,7 @@ class customfields_widget
* @param string $file file name inside the eGW server root, either relative to it or absolute
* @return array in case of an error we return a single option with the message
*/
function _get_options_from_file($file)
public static function _get_options_from_file($file)
{
if (!($path = realpath($file{0} == '/' ? $file : EGW_SERVER_ROOT.'/'.$file)) || // file does not exist
substr($path,0,strlen(EGW_SERVER_ROOT)+1) != EGW_SERVER_ROOT.'/' || // we are NOT inside the eGW root
@ -593,4 +593,89 @@ class customfields_widget
}
}
}
/**
* Non printable custom fields eg. UI elements
*
* @var array
*/
public static $non_printable_fields = array('button');
/**
* Format a single custom field value as string
*
* @param array $field field defintion incl. type
* @param string $value field value
* @return string formatted value
*/
public static function format_customfield(array $field, $value)
{
switch($field['type'])
{
case 'select-account':
if ($value)
{
$values = array();
foreach($field['rows'] > 1 ? explode(',', $value) : (array) $value as $value)
{
$values[] = common::grab_owner_name($value);
}
$value = implode(', ',$values);
}
break;
case 'checkbox':
$value = $value ? 'X' : '';
break;
case 'select':
case 'radio':
if (count($field['values']) == 1 && isset($field['values']['@']))
{
$field['values'] = customfields_widget::_get_options_from_file($field['values']['@']);
}
$values = array();
foreach($field['rows'] > 1 ? explode(',', $value) : (array) $value as $value)
{
$values[] = isset($field['values'][$value]) ? $field['values'][$value] : '#'.$value;
}
$value = implode(', ', $values);
break;
case 'date':
case 'date-time':
if ($value)
{
$format = $field['len'] ? $field['len'] : ($field['type'] == 'date' ? 'Y-m-d' : 'Y-m-d H:i:s');
$date = array_combine(preg_split('/[\\/. :-]/',$format), preg_split('/[\\/. :-]/',$value));
$value = common::dateformatorder($date['Y'], $date['m'], $date['d'],true);
if (isset($date['H'])) $value .= ' '.common::formattime($date['H'], $date['i']);
}
break;
case 'htmlarea': // ToDo: EMail probably has a nicer html2text method
if ($value) $value = strip_tags(preg_replace('/<(br|p)[^>]*>/i', "\r\n", str_replace(array("\r", "\n"), '', $value)));
break;
case 'ajax_select': // ToDo: returns unchanged value for now
break;
default:
// handling for several link types
if ($value && in_array($field['type'], self::get_customfield_link_types()))
{
if ($field['type'] == 'link-entry' || strpos($value, ':') !== false)
{
list($app, $value) = explode(':', $value);
}
else
{
$app = $field['type'];
}
if ($value) $value = egw_link::title($app, $value);
}
break;
}
return $value;
}
}

View File

@ -189,15 +189,13 @@ class infolog_tracking extends bo_tracking
/**
* Get the details of an entry
*
* @param array $data
* @param string $datetime_format of user to notify, eg. 'Y-m-d H:i'
* @param int $tz_offset_s offset in sec to be add to server-time to get the user-time of the user to notify
* @param array|object $data
* @param int|string $receiver nummeric account_id or email address
* @return array of details as array with values for keys 'label','value','type'
*/
function get_details($data)
function get_details($data,$receiver=null)
{
//error_log(__METHOD__.__LINE__.' Data:'.array2string($data));
$header_done = false;
$responsible = array();
if ($data['info_responsible'])
{
@ -247,30 +245,9 @@ class infolog_tracking extends bo_tracking
'value' => $data['info_des'],
'type' => 'multiline',
);
// should be moved to bo_tracking because auf the different custom field types
if ($this->infolog->customfields)
{
foreach($this->infolog->customfields as $name => $field)
{
if ($field['type2'] && !in_array($data['info_type'],explode(',',$field['type2']))) continue; // different type
// add custom fields for given type
$details += $this->get_customfields($data, $data['info_type']);
if (!$header_done)
{
$details['custom'] = array(
'value' => lang('Custom fields').':',
'type' => 'reply',
);
$header_done = true;
}
//error_log(__METHOD__."() $name: data['#$name']=".array2string($data['#'.$name]).", field[values]=".array2string($field['values']));
$details['#'.$name] = array(
'label' => $field['label'],
'value' => is_array($field['values']) && $field['values'] && isset($data['#'.$name]) &&
array_key_exists((string)$data['#'.$name],$field['values']) ? $field['values'][$data['#'.$name]] : $data['#'.$name],
);
//error_log("--> details['#$name']=".array2string($details['#'.$name]));
}
}
return $details;
}