egroupware_official/etemplate/inc/class.customfields_widget.inc.php

194 lines
6.3 KiB
PHP
Raw Normal View History

<?php
/**************************************************************************\
* eGroupWare - eTemplate Widget for custom fields *
* http://www.egroupware.org *
* Written by Ralf Becker <RalfBecker@outdoor-training.de> and *
* Cornelius Weiss <egw@von-und-zu-weiss.de> *
* -------------------------------------------- *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation; either version 2 of the License, or (at your *
* option) any later version. *
\**************************************************************************/
/* $Id$ */
/**
* This widget handles customfields completely
*
* It generates a template based on definitioins in phpgw_config table
*
* You have to provide the main id in $value
*
* The customfield table name is assumed as appname_custom.
* If you use a differnt name you have to provide it in option (1)
*
* The customfields table must have the following structure: main_id | main_owner | custom_owner | custrom_field | custom_value
* If you use different column names you have to provide them in option (2)
*
*
* @package eTemplate
* @author RalfBecker-At-outdoor-training.de
* @author Cornelius Weiss <egw@von-und-zu-weiss.de>
* @copyright GPL - GNU General Public License
*/
class customfields_widget
{
var $public_functions = array(
'pre_process' => True,
/* 'post_process' => True*/
);
var $human_name = 'custom fields';
/**
* @var $prefix string Prefix for every custiomfield name returned in $content (# for general (admin) customfields)
*/
var $prefix = '#';
function customfields_widget($ui)
{
$this->appname = $GLOBALS['egw_info']['flags']['currentapp'];
$this->config =& CreateObject('phpgwapi.config',$this->appname);
$this->config->appname = $this->appname;
$config = $this->config->read_repository();
//merge old config_name in phpgw_config table
$config_name = isset($config['customfields']) ? 'customfields' : 'custom_fields';
$this->customfields = $config[$config_name];
}
function pre_process($name,&$value,&$cell,&$readonlys,&$extension_data,&$tmpl)
{
// list($table,$cols) = explode(',',$cell['size']);
// list($main_id,$main_owner,$custom_owner,$custom_field,$custom_value) = explode('|',$cols);
// echo $custom_value;
//
// infolog compability
if ($this->appname == 'infolog')
{
$typ = $value['###typ###'];
unset($value['###typ###']);
$this->customfields = $value;
}
if(empty($this->customfields))
{
$cell['type'] = 'label';
return True;
}
$tpl =& new etemplate;
$tpl->init('*** generated custom fields','','',0,'',0,0); // make an empty template
//echo '<pre style="text-aling: left;">'; print_r($value); echo "</pre>\n";
foreach($this->customfields as $name => $field)
{
if (!empty($field['typ']) && $field['typ'] != $typ)
{
continue; // not for our typ
}
if(empty($field['type']))
{
if (count($field['values'])) $field['type'] = 'select'; // selectbox
elseif ($field['rows'] > 1) $field['type'] = 'textarea'; // textarea
elseif (intval($field['len']) > 0) $field['type'] = 'text'; // regular input field
else $field['type'] = 'label'; // header-row
}
$row_class = 'row';
$label = &$tpl->new_cell(++$n,'label',$field['label'],'',array(
'no_lang' => substr(lang($field['label']),-1) == '*' ? 2 : 0
));
switch ($field['type'])
{
case 'select' :
foreach($field['values'] as $key => $val)
{
if (substr($val = lang($val),-1) != '*')
{
$field['values'][$key] = $val;
}
}
$input = &$tpl->new_cell($n,'select','',$this->prefix.$name,array(
'sel_options' => $field['values'],
'size' => $field['rows'],
'no_lang' => True
));
break;
case 'label' :
$label['span'] = 'all';
$tpl->new_cell($n); // is needed even if its over-span-ed
$row_class = 'th';
break;
case 'checkbox' :
$input = &$tpl->new_cell($n,'checkbox','',$this->prefix.$name);
break;
case 'radio' :
$input = &$tpl->new_cell($n,'groupbox','','',array(
'size' => count($field['values']),
));
$m = 0;
foreach ($field['values'] as $key => $val)
{
$input[++$m] = &$tpl->new_cell($m,'radio',$val,'',array(
'size' => $key,
));
$tpl->set_row_attributes($m,0,$row_class);
}
/* _debug_array($input);*/
// to be continued ...
break;
case 'text' :
case 'textarea' :
default :
$field['len'] = $field['len'] ? $field['len'] : 20;
if($field['rows'] < 1)
{
list($max,$shown) = explode(',',$field['len']);
$input = &$tpl->new_cell($n,'text','',$this->prefix.$name,array(
'size' => intval($shown > 0 ? $shown : $max).','.intval($max)
));
}
else
{
$input = &$tpl->new_cell($n,'textarea','',$this->prefix.$name,array(
'size' => $field['rows'].($field['len'] > 0 ? ','.intval($field['len']) : '')
));
}
break;
}
if (!empty($field['help']) && $row_class != 'th')
{
$input['help'] = $field['help'];
$input['no_lang'] = substr(lang($help),-1) == '*' ? 2 : 0;
}
$tpl->set_row_attributes($n,0,$row_class);
}
// create an empty line which (should) take all the remaining height
$tpl->new_cell(++$n,'label','','',array(
'span' => 'all'
));
$tpl->set_row_attributes($n,'99%','row');
// set width of 1. (label) column to 100
$tpl->set_column_attributes(0,'100');
$tpl->set_rows_cols(); // msie (at least 5.5 shows nothing with div overflow=auto)
$tpl->size = '100%,100%'.($tpl->html->user_agent != 'msie' ? ',,,,,auto' : '');
//echo '<pre style="text-align: left;">'; print_r($tpl); echo "</pre>\n";
if (count($tpl->data) < 2)
{
$cell['type'] = 'label';
return True;
}
$cell['size'] = ''; // no separate namespace
$cell['type'] = 'template';
$cell['name'] = $tpl->name;
$cell['obj'] = &$tpl;
return True; // extra Label is ok
}
}