**/ class twIf_Widget_Filter { const EMPTY_VALUE = 'twIfFilterUnset'; private $Filters = array (); private $Class = null; private $UUID = ''; // {{{ __construct /** * Create a new filter-widget * * @param string $Class Create filter-widget for this class * * @access friendly * @return void **/ function __construct ($Class, $UUID = '') { if (!class_exists ($Class)) { trigger_error (twIf_i18n::getText ('Class %s does not exist, filter-widget will not work', $Class)); $Class = null; } $this->Class = $Class; $this->UUID = $UUID; } // }}} // {{{ addFilter /** * Append a filter to this widget * * @param string $Caption Caption for this filter * @param string $Field Destination-filed for this filter * @param enum $Type Control-Type of this filter * @param mixed $Default (optional) * @param mixed $Data (optional) TODO! Document/use this ;) * * @access public * @return **/ public function addFilter ($Caption, $Field, $Type, $Default = null, $Data = null) { $Name = 'twIfFilter' . sha1 ($this->UUID . $Field); $this->Filters [$Field] = array ( $Control = twIf_Widget_Control::generateControl ($Type, $Name, $Default), $Caption, $Field, $Type, $Default, $Data, ); if (is_array ($Data)) { $Items = array (self::EMPTY_VALUE => twIf_i18n::getText ('Show all')); foreach ($Data as $k=>$v) $Items [$k] = $v; $Control->setItems ($Items); } } // }}} // {{{ setFilter /** * Set the value for a filter-field * * @param string $Field * @param mixed $Value * * @access public * @return bool **/ public function setFilter ($Field, $Value) { if (!isset ($this->Filters [$Field])) return false; $this->Filters [$Field][0]->setValue ($Value); if ($this->Filters [$Field][0]->hasPostValue ()) $this->Filters [$Field][0]->ignorePostValue (); return true; } // }}} // {{{ generateFilter /** * Generate HTML-Code from this filter-definition * * @access public * @return string **/ public function generateFilter () { // Check if there are any filters defined or return directly if (count ($this->Filters) == 0) return ""; // Start the filter-code $buf = "
\n"; $AutoSubmit = (count ($this->Filters) < 2); $currentFilter = $this->getFilter (); foreach ($this->Filters as $Filter) { $Control = $Filter [0]; $Caption = $Filter [1]; $Field = $Filter [2]; $Type = $Filter [3]; $Default = $Filter [4]; $Data = $Filter [5]; $buf .= // Start new Filter Control '
' . '
' . // Output the caption '
' . twIf_i18n::getText ($Caption) . '
' . // Start the control '
'; $Control->setAutoSubmit ($AutoSubmit); $Control->setValue ($currentFilter [$Field]); $buf .= $Control->generate (); // Finish the Filter Control $buf .= "
\n"; } // Append submit-button $buf .= "
\n"; $buf .= "
\n"; return $buf; } // }}} // {{{ getFilter /** * Retrive filter-definition from this widget * * @access public * @return array **/ public function getFilter () { if (isset ($_SESSION ['twIfFilter' . $this->UUID])) $Params = $_SESSION ['twIfFilter' . $this->UUID]; else $Params = array (); foreach ($this->Filters as $Filter) { $Control = $Filter [0]; $Field = $Filter [2]; if ($Control->hasPostValue ()) { if (($v = $Control->getPostValue ()) == self::EMPTY_VALUE) unset ($Params [$Field]); else $Params [$Field] = $Control->getPostValue (); } elseif (($v = $Control->getValue ()) !== null) $Params [$Field] = $v; } return $_SESSION ['twIfFilter' . $this->UUID] = $Params; } // }}} } ?>