Type = $Type;
$this->Name = $Name;
$this->Value = $Value;
}
// }}}
// {{{ generateControl
/**
* Generate a new control
*
* @param enum $Type Type of the control
* @param string $Name Name of the control
* @parem mixed $Value (optional) Default value for the control
*
* @remark May return other classes then twIf_Widget_Control in future
* @access public
* @return object
**/
public static function generateControl ($Type, $Name, $Value = null) {
return new twIf_Widget_Control ($Type, $Name, $Value);
}
// }}}
// {{{ setValue
/**
* Set the default value for the control
*
* @param mixed $Value
*
* @access public
* @return void
**/
public function setValue ($Value) {
$this->Value = $Value;
}
// }}}
// {{{ getValue
/**
* Retrive the assigned value for this control
*
* @access public
* @return mixed
**/
public function getValue () {
return $this->Value;
}
// }}}
// {{{ setItems
/**
* Set items for an enumeration-control
*
* @param array $Items key/value-pairs for control
*
* @access public
* @return void
**/
public function setItems ($Items) {
if (!is_array ($Items))
return false;
$this->Items = $Items;
}
// }}}
// {{{ setAutoSubmit
/**
* Auto-Submit the form if this control was changed
*
* @param bool $Value
*
* @access public
* @return void
**/
public function setAutoSubmit ($Value) {
$this->AutoSubmit = $Value;
}
// }}}
// {{{ onChange
/**
* Execute this JS-Code upon changes
*
* @param string $Value
*
* @access public
* @return void
**/
public function onChange ($Value) {
$this->onChange = $Value;
}
// }}}
// {{{ generate
/**
* Create HTML-Code for this control
*
* @access public
* @return string
**/
public function generate () {
$JS = '';
if ($this->AutoSubmit) {
#if ($this->Type == self::TYPE_ENUM)
# $ev = 'onchange';
#elseif ($this->Type == self::TYPE_ENUM_LIST)
# $ev = 'onclick';
#else
# $ev = ''; #onblur';
#
#if ($ev != '')
# $JS .= ' ' . $ev . '="this.form.submit();"';
if ($this->Type == self::TYPE_ENUM_LIST)
$JS .= ' onclick="this.form.submit();"';
elseif (($this->Type == self::TYPE_ENUM) && ($this->onChange == ''))
$JS .= ' onchange="this.form.submit();"';
}
if ($this->onChange != '')
$JS .= ' onchange="' . $this->onChange . ($AutoSubmit && ($this->Type == self::TYPE_ENUM) ? '; this.form.submit();' : '') . '"';
switch ($this->Type) {
case self::TYPE_BOOL_CHECKBOX:
return 'Value!= 0 ? ' checked="1"' : '') . $JS . ' />';
case self::TYPE_BOOL:
return 'Value != 0 ? ' checked="1"' : '') . $JS . ' /> ' .
' ' .
'Value == 0 ? ' checked="1"' : '') . $JS . ' /> ' .
'';
case self::TYPE_ENUM_LIST:
$buf = '';
foreach ($this->Items as $Key=>$Val)
$buf .= '
' .
'Value == $Key ? 'checked="1" ' : '') . '/> ' .
'' .
"
\n";
return $buf;
case self::TYPE_ENUM:
$buf = '';
case self::TYPE_SET:
require_once ('twIf/widget/table.php');
return twIf_Widget_Table::generateTable ($this->Items, array ('Element' => '__toString()'), array (), array (), false, true);
case self::TYPE_TEXTAREA:
return '';
case self::TYPE_UPLOAD:
return '';
case self::TYPE_UPDOWNLOAD:
return '';
case self::TYPE_PASSWORD:
return '';
case self::TYPE_DATETIME:
case self::TYPE_DATETIME_FUTURE:
if (!is_numeric ($this->Value))
$val = strtotime ($this->Value);
else
$val = $this->Value;
if (($val != false) && ($val != null)) {
$Hour = date ('H', $val);
$Minute = date ('i', $val);
$Second = date ('s', $val);
} else {
$Hour = null;
$Minute = null;
$Second = null;
}
$buf = ':: ' . twIf_i18n::getText ("o'clock") . '
';
case self::TYPE_DATE:
case self::TYPE_DATE_FUTURE:
case self::TYPE_DATE_YEARMONTH:
case self::TYPE_DATE_YEARMONTH_FUTURE:
if (!isset ($val)) {
if (!is_numeric ($this->Value))
$val = strtotime ($this->Value);
else
$val = $this->Value;
}
if (($val !== false) && ($val !== null) && ($val != 0)) {
$Day = date ('d', $val);
$Month = date ('m', $val);
$Year = date ('Y', $val);
} else {
$Day = 0;
$Month = 0;
$Year = 0;
}
if (!isset ($buf))
$buf = '';
if (in_array ($this->Type, array (self::TYPE_DATE, self::TYPE_DATE_FUTURE, self::TYPE_DATETIME, self::TYPE_DATETIME_FUTURE))) {
$buf .= '.';
}
$buf .= '.';
return $buf;
case self::TYPE_HIDDEN:
return '';
case self::TYPE_CURRENCY_EURO:
case self::TYPE_CURRENCY_CENT:
case self::TYPE_FLOAT:
case self::TYPE_NUMBER:
case self::TYPE_TEXT:
case self::TYPE_CUSTOM:
case self::TYPE_STATIC:
if ($this->Type == self::TYPE_CURRENCY_EURO)
$Value = sprintf ('%01.2f' ,$this->Value);
elseif ($this->Type == self::TYPE_FLOAT)
$Value = sprintf ('%f', $this->Value);
elseif (($this->Type == self::TYPE_NUMBER) || ($this->Type == self::TYPE_CURRENCY_CENT))
$Value = strval (intval ($this->Value));
else
$Value = htmlentities ($this->Value, ENT_COMPAT, 'utf-8');
return 'Type == self::TYPE_STATIC ? ' readonly="1" enabled="0"' : '') . $JS . ' />' . ($this->Type == self::TYPE_CURRENCY_EURO ? ' €' : ($this->Type == self::TYPE_CURRENCY_CENT ? ' Cent': ''));
default:
trigger_error (twIf_i18n::getText ('Could not generate control of type %s', $this->Type));
return '';
}
}
// }}}
// {{{ hasPostValue
/**
* Check if there was a value posted for this control
*
* @param bool $AllowEmpty (optional) Allow empty strings (default)
*
* @access public
* @return bool
**/
public function hasPostValue ($AllowEmpty = true) {
if ($this->IgnorePostValue)
return false;
switch ($this->Type) {
case self::TYPE_SET:
# We assume always true here - Improve this!
return true;
case self::TYPE_DATETIME:
case self::TYPE_DATETIME_FUTURE:
return
isset ($_POST [$this->Name . '_h']) &&
isset ($_POST [$this->Name . '_i']) &&
isset ($_POST [$this->Name . '_s']) &&
isset ($_POST [$this->Name . '_d']) &&
isset ($_POST [$this->Name . '_m']) &&
isset ($_POST [$this->Name . '_y']) &&
($_POST [$this->Name . '_h'] >= 0) &&
($_POST [$this->Name . '_h'] < 24) &&
($_POST [$this->Name . '_i'] >= 0) &&
($_POST [$this->Name . '_i'] < 60) &&
($_POST [$this->Name . '_s'] >= 0) &&
($_POST [$this->Name . '_s'] < 60) &&
($_POST [$this->Name . '_d'] > 0) &&
($_POST [$this->Name . '_d'] < 32) &&
($_POST [$this->Name . '_m'] > 0) &&
($_POST [$this->Name . '_m'] < 13) &&
($_POST [$this->Name . '_y'] > 1900);
case self::TYPE_DATE:
case self::TYPE_DATE_FUTURE:
return
isset ($_POST [$this->Name . '_d']) &&
isset ($_POST [$this->Name . '_m']) &&
isset ($_POST [$this->Name . '_y']) &&
($_POST [$this->Name . '_d'] > 0) &&
($_POST [$this->Name . '_d'] < 32) &&
($_POST [$this->Name . '_m'] > 0) &&
($_POST [$this->Name . '_m'] < 13) &&
($_POST [$this->Name . '_y'] > 1900);
case self::TYPE_DATE_YEARMONTH:
case self::TYPE_DATE_YEARMONTH_FUTURE:
return
isset ($_POST [$this->Name . '_m']) &&
isset ($_POST [$this->Name . '_y']) &&
($_POST [$this->Name . '_m'] > 0) &&
($_POST [$this->Name . '_m'] < 13) &&
($_POST [$this->Name . '_y'] > 1900);
case self::TYPE_UPLOAD:
return (isset ($_FILES [$this->Name]) && ($_FILES [$this->Name]['size'] > 0));
case self::TYPE_UPDOWNLOAD:
if (($this->downloadValue !== null) || (isset ($_FILES [$this->Name . 'up']) && ($_FILES [$this->Name . 'up']['size'] > 0)))
return true;
if ((strlen ($_POST [$this->Name . 'url']) == 0) || (strpos ($_POST [$this->Name . 'url'], '://') === false))
return false;
$curl = curl_init ();
curl_setopt ($curl, CURLOPT_FAILONERROR, true);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curl, CURLOPT_USERAGENT, 'twIf/widget-control');
curl_setopt ($curl, CURLOPT_URL, $_POST [$this->Name . 'url']);
if (!($Value = curl_exec ($curl))) {
trigger_error ('URL given, but download failed: ' . $_POST [$this->Name . 'url']);
return false;
}
curl_close ($curl);
$this->downloadValue = $Value;
return true;
case self::TYPE_BOOL_CHECKBOX:
return true; // Checkboxes are assumed as "always there" (but may be false)
}
return (isset ($_POST [$this->Name]) && (($_POST [$this->Name] != '') || $AllowEmpty));
}
// }}}
// {{{ ignorePostValue
/**
* Make the control claim to have no post-value
*
* @param bool $Set (optional)
*
* @access public
* @return void
**/
public function ignorePostValue ($Set = true) {
$this->IgnorePostValue = $Set;
}
// }}}
// {{{ getPostValue
/**
* Retrive the posted value for this control
*
* @access public
* @return string
**/
public function getPostValue () {
if (!$this->hasPostValue ())
return null;
if (isset ($_POST [$this->Name]))
$Value = stripslashes ($_POST [$this->Name]);
else
$Value = '';
switch ($this->Type) {
case self::TYPE_DATETIME:
case self::TYPE_DATETIME_FUTURE:
return mktime (intval ($_POST [$this->Name . '_h']), intval ($_POST [$this->Name . '_i']), intval ($_POST [$this->Name . '_s']), intval ($_POST [$this->Name . '_m']), intval ($_POST [$this->Name . '_d']), intval ($_POST [$this->Name . '_y']));
case self::TYPE_DATE:
case self::TYPE_DATE_FUTURE:
return mktime (0, 0, 0, intval ($_POST [$this->Name . '_m']), intval ($_POST [$this->Name . '_d']), intval ($_POST [$this->Name . '_y']));
case self::TYPE_DATE_YEARMONTH:
case self::TYPE_DATE_YEARMONTH_FUTURE:
return mktime (0, 0, 0, intval ($_POST [$this->Name . '_m']), 1, intval ($_POST [$this->Name . '_y']));
case self::TYPE_UPDOWNLOAD:
if ($this->downloadValue !== null)
return $this->downloadValue;
$SourceName = $this->Name . 'up';
case self::TYPE_UPLOAD:
if (!isset ($SourceName))
$SourceName = $this->Name;
// Try to read contents of uploaded file
if (is_resource ($f = @fopen ($_FILES [$SourceName]['tmp_name'], 'r'))) {
$Value = fread ($f, $_FILES [$SourceName]['size']);
fclose ($f);
}
// Always try to remove the uploaded file
@unlink ($_FILES [$SourceName]['tmp_name']);
return $Value;
case self::TYPE_SET:
require_once ('twIf/widget/table.php');
return twIf_Widget_Table::filterSelected ($this->Items);
}
return $this->processValue ($Value);
}
// }}}
// {{{ processValue
/**
* Try to convert a given value according to our type
*
* @param mixed $Value
*
* @access public
* @return mixed
**/
public function processValue ($Value) {
if (($this->Type == self::TYPE_ENUM_LIST) ||
($this->Type == self::TYPE_ENUM)) {
if (isset ($this->Items [$Value]))
return $Value;
return array_search ($Value, $this->Items);
}
return twIf_Object_Access::formatNativeType ($Value, $this->Type);
}
// }}}
// {{{ getName
/**
* Retrive the name of this control
*
* @access public
* @return string
**/
public function getName () {
return $this->Name;
}
// }}}
// {{{ getType
/**
* Retrive the type of this control
*
* @access public
* @return enum
**/
public function getType () {
return $this->Type;
}
// }}}
}
?>