* @copyright 2002-9 by RalfBecker@outdoor-training.de
* @version $Id$
*/
/**
* eTemplate Extension: several select-boxes with predefined eGW specific content
*
* This widgets replaces the not longer exiting phpgwapi.sbox class. The widgets are independent of the UI,
* as they only uses etemplate-widgets and therefore have no render-function.
*/
class select_widget
{
/**
* exported methods of this class
* @var array
*/
var $public_functions = array(
'pre_process' => True,
'post_process' => True,
);
/**
* availible extensions and their names for the editor
* @var array
*/
var $human_name = array(
'select-percent' => 'Select Percentage',
'select-priority' => 'Select Priority',
'select-access' => 'Select Access',
'select-country' => 'Select Country',
'select-state' => 'Select State', // US-states
'select-cat' => 'Select Category', // Category-Selection, size: -1=Single+All, 0=Single, >0=Multiple with size lines
'select-account' => 'Select Account', // label=accounts(default),groups,both
// size: -1=Single+not assigned, 0=Single, >0=Multiple
'select-year' => 'Select Year',
'select-month' => 'Select Month',
'select-day' => 'Select Day',
'select-dow' => 'Select Day of week',
'select-hour' => 'Select Hour', // either 0-23 or 12am,1am-11am,12pm,1pm-11pm
'select-number' => 'Select Number',
'select-app' => 'Select Application',
'select-lang' => 'Select Language',
'select-bool' => 'Select yes or no',
'select-timezone' => 'Select timezone', // select timezone
);
/**
* @var array
*/
var $monthnames = array(
0 => '',
1 => 'January',
2 => 'February',
3 => 'March',
4 => 'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 => 'September',
10 => 'October',
11 => 'November',
12 => 'December'
);
/**
* Constructor of the extension
*
* @param string $ui '' for html
*/
function select_widget($ui='')
{
foreach($this->monthnames as $k => $name)
{
if ($name)
{
$this->monthnames[$k] = lang($name);
}
}
$this->ui = $ui;
}
/**
* pre-processing of the extension
*
* This function is called before the extension gets rendered
*
* @param string $name form-name of the control
* @param mixed &$value value / existing content, can be modified
* @param array &$cell array with the widget, can be modified for ui-independent widgets
* @param array &$readonlys names of widgets as key, to be made readonly
* @param mixed &$extension_data data the extension can store persisten between pre- and post-process
* @param object &$tmpl reference to the template we belong too
* @return boolean true if extra label is allowed, false otherwise
*/
function pre_process($name,&$value,&$cell,&$readonlys,&$extension_data,&$tmpl)
{
list($rows,$type,$type2,$type3,$type4,$type5,$type6,$enhance) = explode(',',$cell['size']);
echo "$name ($rows,$type,$type2,$type3,$type4,$type5,$type6,$enhance)
";
$extension_data['type'] = $cell['type'];
$readonly = $cell['readonly'] || $readonlys;
switch ($cell['type'])
{
case 'select-percent': // options: #row,decrement(default=10)
$decr = $type > 0 ? $type : 10;
for ($i=0; $i <= 100; $i += $decr)
{
$cell['sel_options'][intval($i)] = intval($i).'%';
}
$cell['sel_options'][100] = '100%';
if (!$rows || !empty($value))
{
$value = intval(($value+($decr/2)) / $decr) * $decr;
}
$cell['no_lang'] = True;
break;
case 'select-priority':
$cell['sel_options'] = array('','low','normal','high');
break;
case 'select-bool': // equal to checkbox, can be used with nextmatch-customfilter to filter a boolean column
$cell['sel_options'] = array(0 => 'no',1 => 'yes');
break;
case 'select-access':
$cell['sel_options'] = array(
'private' => 'Private',
'public' => 'Global public',
'group' => 'Group public'
);
break;
case 'select-country': // #Row|Extralabel,1=use country name, 0=use 2 letter-code,custom country field name
if($type == 0 && $type2)
{
$custom_label = is_numeric($type2) ? 'Custom' : $type2;
$cell['sel_options'] = array('-custom-' => lang($custom_label)) + $GLOBALS['egw']->country->countries();
}
else
{
$cell['sel_options'] = $GLOBALS['egw']->country->countries();
}
if (($extension_data['country_use_name'] = $type) && $value)
{
$value = $GLOBALS['egw']->country->country_code($value);
if (!isset($cell['sel_options'][$value]))
{
if($type2)
{
$cell['sel_options'][$value] = $value;
}
}
}
$cell['no_lang'] = True;
break;
case 'select-state':
$cell['sel_options'] = $GLOBALS['egw']->country->us_states();
$cell['no_lang'] = True;
break;
case 'select-cat': // !$type == globals cats too, $type2: extraStyleMultiselect, $type3: application, if not current-app, $type4: parent-id, $type5=owner (-1=global),$type6=show missing
if ($readonly) // for readonly we dont need to fetch all cat's, nor do we need to indent them by level
{
$cell['no_lang'] = True;
if ($value)
{
if (!is_array($value)) $value = explode(',',$value);
foreach($value as $key => $id)
{
if ($id && ($name = stripslashes($GLOBALS['egw']->categories->id2name($id))) && $name != '--')
{
$cell['sel_options'][$id] = $name;
}
else
{
if(!$type6)
{
unset($value[$key]); // remove not (longer) existing or inaccessible cats
}
elseif ($id) // Display id of no longer existing cat
{
$cell['sel_options'][$id] = $type6 == '2' ? $id : lang('Missing: %1',$id);
}
}
}
}
else
{
$value = '';
}
break;
}
if ((!$type3 || $type3 === $GLOBALS['egw']->categories->app_name) &&
(!$type5 || $type5 == $GLOBALS['egw']->categories->account_id))
{
$categories = $GLOBALS['egw']->categories;
}
else // we need to instanciate a new cat object for the correct application
{
$categories = new categories($type5,$type3);
}
// Allow text for global
$type = ($type && strlen($type) > 1 ? $type : !$type);
// we cast $type4 (parent) to int, to get default of 0 if omitted
foreach((array)$categories->return_sorted_array(0,False,'','','',$type,(int)$type4,true) as $cat)
{
$s = str_repeat(' ',$cat['level']) . stripslashes($cat['name']);
if (categories::is_global($cat))
{
$s .= ' ♦';
}
$cell['sel_options'][$cat['id']] = empty($cat['description']) ? $s : array(
'label' => $s,
'title' => $cat['description'],
);
}
// preserv unavailible cats (eg. private user-cats)
if ($value && ($unavailible = array_diff(is_array($value) ? $value : explode(',',$value),array_keys((array)$cell['sel_options']))))
{
$extension_data['unavailible'] = $unavailible;
}
$cell['size'] = $rows.($type2 ? ','.$type2 : '');
$cell['no_lang'] = True;
break;
case 'select-account': // options: #rows,{accounts(default)|both|groups|owngroups},{0(=lid)|1(default=name)|2(=lid+name),expand-multiselect-rows,not-to-show-accounts,...)}
//echo "
select-account widget: name=$cell[name], type='$type', rows=$rows, readonly=".(int)($cell['readonly'] || $readonlys)."
\n"; if($type == 'owngroups') { $type = 'groups'; $owngroups = true; foreach($GLOBALS['egw']->accounts->membership() as $group) $mygroups[] = $group['account_id']; } // in case of readonly, we read/create only the needed entries, as reading accounts is expensive if ($readonly) { $cell['no_lang'] = True; if (!is_array($value) && strpos($value,',') !== false) $value = explode(',',$value); foreach(is_array($value) ? $value : array($value) as $id) { $cell['sel_options'][$id] = !$id && !is_numeric($rows) ? lang($rows) : $this->accountInfo($id,$acc,$type2,$type=='both'); } break; } if ($this->ui == 'html' && $type != 'groups') // use eGW's new account-selection (html only) { $not = array_slice(explode(',',$cell['size']),4); $help = (int)$cell['no_lang'] < 2 ? lang($cell['help']) : $cell['help']; $onFocus = "self.status='".addslashes(htmlspecialchars($help))."'; return true;"; $onBlur = "self.status=''; return true;"; if ($cell['noprint']) { foreach(is_array($value) ? $value : (strpos($value,',') !== false ? explode(',',$value) : array($value)) as $id) { if ($id) $onlyPrint[] = $this->accountInfo($id,$acc,$type2,$type=='both'); } $onlyPrint = $onlyPrint ? implode('select_widget::post_process('$name',...,'$value_in'): value='$value'
\n"; break; case 'select-country': if ($extension_data['country_use_name'] && $value_in) { if (($value = $GLOBALS['egw']->country->get_full_name($value_in))) { break; } } // fall through default: $value = $value_in; break; } //echo "select_widget::post_process('$name',,'$extension_data',,,'$value_in'): value='$value', is_null(value)=".(int)is_null($value)."
\n"; return true; } }