Location: PHPKode > scripts > Yet Another Pager > yet-another-pager/CYap_Control.php
<?PHP
/**
 * Yap_controls.php
 *
 * Base widgets base class.
 *
 * @copyright CYap_Control is part of Yap project {@link http://www.andrioli.com/en/yap.html} and it is LGPL
 * @author    Andrioli Darvin <darvin (inside) andrioli (dot) com>
 * @version   $Header: d:\cvs/classistd/yap/CYap_Control.php,v 1.24 2008/03/10 12:15:46 darvin Exp $
 */
/*
 * +-------------------------------------------------------------------------+
 * | Yap                                                                     |
 * +-------------------------------------------------------------------------+
 * | Copyright (c) 2003-2008 Andrioli Darvin                                 |
 * | Email         hide@address.com                                       |
 * | Web           http://www.andrioli.com/en/yap.html                       |
 * | Download      http://www.phpclasses.org/browse.html/package/1391.html   |
 * |                                                                         |
 * +-------------------------------------------------------------------------+
 * | This library is free software; you can redistribute it and/or modify    |
 * | it under the terms of the GNU Lesser General Public License as          |
 * | published by the Free Software Foundation; either version 2 of the      |
 * | License, or (at your option) any later version.                         |
 * |                                                                         |
 * | This library is distributed in the hope that it will be useful, but     |
 * | WITHOUT ANY WARRANTY; without even the implied warranty of              |
 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU        |
 * | Lesser General Public License for more details.                         |
 * |                                                                         |
 * | You should have received a copy of the GNU Lesser General Public        |
 * | License along with this library; if not, write to the Free Software     |
 * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
 * +-------------------------------------------------------------------------+
 */
/**
 * @package CYap
 */
class CYap_Control
{
/**
 * Control Name
 * @var string
 */
var $Name;
/**
 * Name of the database field binded to this control
 * @var string
 */
var $DbField;
/**
 * Information about the field binded to this control. Associative array.
 * Key:
 * 'autoincrement' -> Autoincrement flag
 * 'values'        ->
 * 'table'         -> table where the field is
 * 'type'          -> field type (database format)
 * 'apice'         -> should I use the single quote to enclose the field value
 * 'size'          -> field's max length
 * 'decimal'       -> No. decimal digits
 * 'name'          -> field's name
 * @var array
 */
var $DbFieldInfo;
/**
 * Expected key in $DbFieldInfo
 * @var array
 */
var $ValidIndex=array('autoincrement','values','table','type','apice','size','decimal','name');
/**
 * Cutrent value
 * @var mixed
 * @see SetValue()
 */
var $FieldValue;
/**
 * If true $FieldValue contains the current field's value
 * @var boolean
 * @see $FieldValue
 * @see SetValue()
 */
var $IHaveValue;
/**
 * Callback function to call to validate the inoput
 * @var string
 */
var $CallBackCheck;
/**
 * The current value is valid?
 * @var bool
 */
var $IsValidInput;
/**
 * @var string
 */
var $Label;

/**
 * Class constructor
 *
 * @param array this table contains the definition for the field binded to this control
 *
 */
function CYap_control($TableFieldDef=array())
{
$this->IHaveValue=false;
if(count($TableFieldDef)>0)
  $this->BindField($TableFieldDef);
$this->CallBackCheck='';
}

/**
 * Store the information about the field binded to this control
 * @var array
 * @access public
 */
function BindField($FieldDef)
{
$this->DbFieldInfo=$FieldDef;
if(array_key_exists('name',$this->DbFieldInfo))
  $this->SetControlName($this->DbFieldInfo['name']);
}

/**
 * Set the current value for the field
 * @param string mixed
 * @access public
 */
function SetValue($value)
{
$this->FieldValue=$value;
$this->IHaveValue=true;
}

/**
 * set the name for the control
 * @param string
 * @access public
 */
function SetControlName($name)
{
$this->Name=$name;
}
/**
 * set the Label for the control
 * @param string
 * @access public
 */
function SetControlLabel($name)
{
$this->Label=$name;
}
/**
 * Set tha callback function to check the input value
 * @param string
 * @access public
 */
function SetCalBackFunc($name)
{
$this->CallBackCheck=$name;
}
/**
 * Generic function to set the paramters without create many set function
 * as the property declared by each control
 * @param Property string
 * @param Value mixed
 * @access public
 */
function SetProperty($Property,$value)
{
// TODO use reflection to check whether the property exists
eval('$this->'.$Property.'=$value;');
}

/**
 * Load the input value from form answer
 *
 * @access public
 */
function ParseResponse()
{
if(array_key_exists($this->Name,$_SERVER))
   {
   $this->SetValue($_SERVER[$this->Name]);
   }
$this->_InternalCheck();
}

/**
 * Internal function to check the input value. Each control may develop som intrisic cchecks
 * i.e an integer inpu accepts only integer value
 * In the base class this function is empty
 * @access private
 */
function _InternalCheck()
{
$this->IsValidInput=true;
}


/**
 * The current value is valid?
 * @return bool
 * @access public
 */
function IsValid()
{
return($this->IsValidInput);
}
/**
 * Retrieve the value in the format suitable to build an update SQL query
 * @return string
 * @access public
 */
function GetSqlData()
{
if(!$this->HasValue())
  trigger_error('Field '.$this->Name.' unset',E_USER_ERROR);
return($this->DbFieldInfo['apici'].$this->FieldValue.$this->DbFieldInfo['apici']);
}

/**
 * Retrieve the value in the format suitable to build an update SQL query
 * @return string
 * @access public
 */
function GetSqlFieldName()
{
return($this->DbFieldInfo['name']);
}

/**
 * return the value of the index $InfoName of $this->DbFieldInfo
 * @param string $InfoName
 * @return string
 * @access public
 */
function GetInfo($InfoName)
{
if(!in_array($InfoName,$this->ValidIndex))
  trigger_error('Unexpected index '.$InfoName.' requested',E_USER_ERROR);
if(array_key_exists($InfoName,$this->DbFieldInfo))
  return($this->DbFieldInfo[$InfoName]);
else
  trigger_error('Iindex '.$InfoName.' doesn\'t set',E_USER_ERROR);
}

// Abstract function
function GetHeaderCode()
{
return('');
}

/**
 * Has been set the control's value?
 * @return boolean
 */
function HasValue()
{
return($this->IHaveValue);
}

function GetValue()
{
return($this->FieldValue);
}
/**
 * Return the Js code reuired by the control
 * @access public
 */
function GetJsCode()
{
return('');
}

/**
 * Return the JS code required to write the function to check the form before submit it
 * @access public
 */
function GetJsSubmitFunction()
{
return('');
}

/**
 * Draw the control
 * @param bool Should the function echo the code, or return it as string
 * @access public
 * @return string
 * @abstract
 */
function Draw($Show=true)
{
}
/**
 * Draw the control label. The base class provides standard way to draw the label.
 * @param boolean $Show
 * @return string
 * @access public
 * @since 2.1.0
 */
function DrawLabel($Show=true)
{
return($this->Label);
}
/**
 * The hidden fields are fields expected not to be requested (never show) in modify form.
 * The default are false, (show the field)
 * @return boolean
 * @access public
 */
function IsHidden()
{
return(false);
}

}  // class Yap_Controll

/********************************************************
 *    T E X T   B O X
 ********************************************************/

/**
 * Generic text box class
 * @package CYap
 */
class CYap_TextBox extends CYap_Control
{

function CYap_TextBox($TableFieldDef=array())
{
parent::CYap_Control($TableFieldDef);
}


/**
 * Draw the TextBox
 * @param bool Should the function echo the code, or return it as string
 * @access public
 * @return string
 */
function Draw($Show=true)
{
if($this->Name=='')
  trigger_error('Control without name!',E_USER_ERROR);
$width=$this->DbFieldInfo['size'];
$Text='<input type=text name='.$this->Name.' size='.$width.' ';
if($this->FieldValue!='')
  $Text.='value=\''.htmlentities($this->FieldValue,ENT_QUOTES).'\'';
$Text.='>';
if($Show)
  echo $Text;
return($Text);
}
} // CYap_TextBox

/**
 * @package CYap
 */
class CYAP_ViewTextBox extends CYAP_Control
{
var $Size;

function CYAP_ViewTextBox($TableFieldDef=array())
{
parent::CYap_Control($TableFieldDef);
}


//
function Draw($Show=true)
{
if($this->Name=='')
  trigger_error('Control without name!',E_USER_ERROR);
$Text='<input type=text name='.$this->Name.' size='.$this->Size.' ';
if($this->FieldValue!='')
  $Text.='Value=\''.htmlentities($this->FieldValue,ENT_QUOTES).'\'';
$Text.='>';
return($Text);
}
function DrawLabel($Show=true)
{
return($this->Label);
}

// The view control create only the HTML code, it doesn't manage the
// logic behind it.

function GetSubmitFunction()
{
return('');
}
}
/**
 * @package CYap
 */
class CYAP_ViewTextRange extends CYAP_ViewTextBox
{
var $FieldValueTo;
var $NameTo;

function CYAP_ViewTextRange($TableFieldDef=array())
{
parent::CYAP_ViewTextBox($TableFieldDef);
}
/**
 * Set the current value for the field
 * @param string mixed
 * @access public
 */
function SetValueTo($value)
{
$this->FieldValueTo=$value;
}
/**
 * set the name for the control, To field
 * @param string
 * @access public
 */
function SetControlNameTo($name)
{
$this->NameTo=$name;
}
//
function Draw($Show=true)
{
if($this->Name=='')
  trigger_error('Control without name!',E_USER_ERROR);
$Text='<input type=text name='.$this->Name.' size='.$this->Size.' ';
if($this->FieldValue!='')
  $Text.='Value=\''.htmlentities($this->FieldValue,ENT_QUOTES).'\'';
$Text.='>';
$Text.=' to ';
$Text.='<input type=text name='.$this->NameTo.' size='.$this->Size.' ';
if($this->FieldValueTo!='')
  $Text.='Value=\''.htmlentities($this->FieldValueTo,ENT_QUOTES).'\'';
$Text.='>';
return($Text);
}
function DrawLabel($Show=true)
{
return($this->Label);
}

// The view control create only the HTML code, it doesn't manage the
// logic behind it.

function GetSubmitFunction()
{
return('');
}
}

/********************************************************
 *    S E L E C T   B O X
 ********************************************************/
/**
 * @package CYap
 */
class CYap_SelectBox extends CYap_Control
{
/**
 * Array of all choices available
 * @var array
 */
var $Choices;
function CYap_SelectBox($TableFieldDef=array())
{
parent::CYap_Control($TableFieldDef);
$this->Choices=array();
}

/**
 * Set the choices to show into the select box
 * @param array
 * @access public
 */
function SetChoices($tblChoices=array())
{
$this->Choices=$tblChoices;
}

/**
 * Draw the TextBox
 * @param bool Should the function echo the code, or return it as string
 * @access public
 * @return string
 */
function Draw($Show=true)
{
// $width=$this->DbFieldInfo['size'];
if($this->Name=='')
  trigger_error('Control without name!',E_USER_ERROR);
$HtmlCode='';
$HtmlCode.='<SELECT name='.$this->Name.' >'.chr(13);
foreach($this->Choices as $key => $Value)
    {
    $HtmlCode.="<option ";
    if ($this->FieldValue==(string)$key) {
        $HtmlCode.="SELECTED";
        }
    $HtmlCode.=" value='".$key."'";
    $HtmlCode.=" >".$this->Choices[$key]."</option>".chr(13);
    }
$HtmlCode.="</SELECT>".chr(13);
if($Show)
  echo $HtmlCode;
return($HtmlCode);
}

}

/**
 * Preset input object. This object obtains its value
 * from the configuration file and don't ask to the user
 * its value (it is never be shown)
 *
 * @package CYap
 */
class CYap_Preset extends CYap_Control
{

function CYap_Preset($TableFieldDef=array())
{
parent::CYap_Control($TableFieldDef);
}

/**
 * Load the input value from form answer. It will never shown,
 * so there is nothing to check from the response
 *
 * @access public
 */
function ParseResponse()
{
$this->IsValidInput=true;
}

/**
 * Draw the TextBox
 * @param bool Should the function echo the code, or return it as string
 * @access public
 * @return string
 */
function Draw($Show=true)
{
if($this->Name=='')
  trigger_error('Control without name!',E_USER_ERROR);
$Text='<input type=hidden name='.$this->Name.' ';
if($this->FieldValue!='')
  $Text.='value=\''.htmlentities($this->FieldValue,ENT_QUOTES).'\'';
$Text.='>';
if($Show)
  echo $Text;
return($Text);
}

/**
 * The hidden fields are fields expected not to be requested (never show) in modify form.
 * The preset field, should never requested (they have the value from the configuration)
 * @return boolean
 * @access public
 */
function IsHidden()
{
return(true);
}

} // CYap_Preset


/********************************************************
 *    S U B M I T   B U T T O N
 ********************************************************/
/**
 * @package CYap
 */
class CYAP_ViewSubmitButton extends CYAP_Control
{
var $Size;

function CYAP_ViewSubmitButton($TableFieldDef=array())
{
parent::CYap_Control($TableFieldDef);
}


//
function Draw($Show=true)
{
$Text="<input type=submit value='".$this->Label."'>";
return($Text);
}

function DrawLabel($Show=true)
{
return('');
}

// The view control create only the HTML code, it doesn't manage the
// logic behind it.

function GetSubmitFunction()
{
return('');
}
}
/**
 * @package CYap
 */
class CYap_ViewDate extends CYap_Control
{
/**
 * Date format used to ask the date. It uses the
 * same format string as Php date statement
 * @var string
 */
var $ShowFormat;
var $JsFormat;

function CYap_ViewDate($TableFieldDef=array())
{
/**
 * Date format used to ask the date
 */
parent::CYap_Control($TableFieldDef);
}
/**
 * Generic function to set the paramters without create many set function
 * as the property declared by each control
 * @param Property string
 * @param Value mixed
 * @access public
 */
function SetProperty($Property,$value)
{
parent::SetProperty($Property,$value);
/*
 * Format validation.
 * Supported format:
 * d.m.Y
 * m.d.Y
 * Y.m.d
 */
if(strtolower($Property)=='showformat')
  {
  if(preg_match('/^d.m.Y$/',$value))
    {
    $cond='/^d(.*)m(.*)Y$/';
    $replace='dd${1}mm${2}yyyy';
    $this->JsFormat=preg_replace($cond,$replace,$value);
    }
  elseif(preg_match('/^Y.m.d$/',$value))
    {
    $cond='/^Y(.*)m(.*)d$/';
    $replace='yyyy${1}mm${2}dd';
    $this->JsFormat=preg_replace($cond,$replace,$value);
    }
  elseif(preg_match('/^m.d.Y$/',$value))
    {
    $cond='/^m(.*)d(.*)Y$/';
    $replace='mm${1}dd${2}yyyy';
    $this->JsFormat=preg_replace($cond,$replace,$value);
    }
  else
    {
    $this->JsFormat='dd/mm/yyyy';
    }
  }
}
/**
 * Draw the TextBox
 * This module require to add the following rows to the head section
 *   <link rel="stylesheet" href="dhtmlgoodies_calendar/dhtmlgoodies_calendar.css" media="screen"></LINK>
 *   <SCRIPT type="text/javascript" src="dhtmlgoodies_calendar/dhtmlgoodies_calendar.js"></script>
 *
 * @param bool Should the function echo the code, or return it as string
 * @access public
 * @return string
 */
function Draw($Show=true)
{
if($this->Name=='')
  trigger_error('Control without name!',E_USER_ERROR);
$Text='<input type=text name='.$this->Name.'  id=viewdate_'.$this->Name.' ';
if($this->FieldValue!='')
  $Text.='value=\''.htmlentities(date($this->ShowFormat,$this->FieldValue),ENT_QUOTES).'\'';
$Text.='>';
$Text.='<input type="button" value="Cal" onclick="displayCalendar(\'viewdate_'.$this->Name.'\',\''.$this->JsFormat.'\',this)">';
if($Show)
  echo $Text;
return($Text);
}
function GetJsCode()
{
$txt='
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar/dhtmlgoodies_calendar.js"></script>
<script type="text/javascript">
         var pathToImages = \'dhtmlgoodies_calendar/images/\';	// Relative to your HTML file
 </script>
';
return($txt);
}
}
/**
 * @package CYap
 */
class CYap_ViewOptRegex extends CYap_Control
{
/**
 * "1" = Allow pattern maching, checkbox not checked
 * "0" = Match exactly the given text, checkbox checked
 *
 * @var string
 */
var $UseRegex;
function CYap_ViewOptRegex($TableFieldDef=array())
{
parent::CYap_Control($TableFieldDef);
}

function Draw($Show=true)
{
if($this->Name=='')
  trigger_error('Control without name!',E_USER_ERROR);
$Text='<input type=text name='.$this->Name.' ';
if($this->FieldValue!='')
  $Text.='value=\''.htmlentities($this->FieldValue,ENT_QUOTES).'\'';
$Text.='>';
$Text.='&nbsp;&nbsp; <input type="checkbox" name='.$this->Name.'_compWord ';
if($this->UseRegex=='0')
   $Text.=' CHECKED ';
$Text.='> Exact match';
if($Show)
  echo $Text;
return($Text);
}

}
?>
Return current item: Yet Another Pager