<?php
/**
* _Z class provide static methods for generating the most common input fields and html tags
* @copyright Copyright (c) 2012, PWF
* @license http://phpwebframework.com/License
* @version PWF_0.3.0
*/
class _Z
{
/**
* Generates <b>submit button</b> for PWF html form
* @param <b>$action</b> The action of submit as PWF use it (eg. Controller?action:actionData ,
* or action:actionData (the action url in this case will be the current) or action â actionData
* is in get format e.g. id=1&name=Hello)
* @param <b>$label</b> The label value of the button
* @return string
*/
public static function submit($action,$label)
{
$action = _String::replace($action, array("\?","\:","\=","\&"), array("\\\?","\\\:","\\\=","\\\&"));
return "<input type=\"submit\" onclick=\"javascript:submit('$action');\" value=\"$label\" />";
}
/**
* Generates submit button for posting using ajax
* @param <b>$action</b> The action of submit as PWF use it (eg. Controller?action:actionData ,
* or action:actionData (the action url in this case will be the current) or action â actionData
* is in get format e.g. id=1&name=Hello)
* @param <b>$label</b> The label value of the button
* @param <b>$copyPostData :optional</b> If true, all the form post data will be copied to the ajax post. False by default
* @return string
*/
public static function ajaxSubmit($action,$label,$copyPostData = false)
{
$action = _String::replace($action, array("\?","\:","\=","\&"), array("\\\?","\\\:","\\\=","\\\&"));
return "<input type=\"submit\" onclick=\"javascript:ajaxSubmit('$action',".($copyPostData?"true":"false").");\" value=\"$label\" />";
}
/**
* Generates <b>input text</b> form field
* @param <b>$name</b> The name of the text field
* @param <b>$value :optional</b> The value of the text field
* @param <b>$size :optional</b> The size of the text field
* @param <b>$maxlenght :optional</b> The maxlength of the text field
* @param <b>$attributes :optional</b> Extra attributes of the field and their values (eg. id='something'
* or onclick='javascript:doSomething();' )
* @return string
*/
public static function input($name,$value = null,$size = null,$maxlength = null, $attributes = null)
{
return "<input type='text' name='$name' ".($value==null?"":"value=\"$value\" ").($size==null?"":"size=\"$size\" ").($maxlength==null?"":"maxlength=\"$maxlength\" ").($attributes==null?"":$attributes." ")."/>";
}
/**
* Generates <b>hidden</b> form field
* @param <b>$name</b> The name of the hidden field
* @param <b>$value</b> The value of the hidden field
* @return string
*/
public static function hidden($name,$value)
{
return "<input type='hidden' name='$name' value='$value' />";
}
/**
* Generates <b>select</b> form field
* @param <b>$name</b> The name of the text field
* @param <b>$value</b> The key of the $options array that will be the selected option
* @param <b>$options</b> The array for the options of the select. Eg. $options[$key] = $value
* means <option value=â$keyâ>$value</option> . You can also use multidimensional arrays
* for option groups e.g. $options[$label][$key] = $value means <optgroup label=â$labelâ>
* <option value=â$keyâ>$value</option> </optgroup>
* @param <b>$attributes :optional</b> Extra attributes of the select field and their values (eg. id='something'
* or onclick='javascript:doSomething();' )
* @return string
*/
public static function select($name,$value,$options = array(),$atrributes = null)
{
$result = "<select name='$name' ";
if(!is_null($atrributes))
{
$atrributes .= $atrributes." ";
}
$result .= ">";
foreach($options as $key=>$val)
{
if(is_array($val))
{
$result .= "<optgroup label='$key'>";
foreach($val as $key2=>$val2)
{
$result .= "<option value='$key2' ";
if($value == $key2)
{
$result .= "selected ";
}
$result .=">$val2</option>";
}
$result .= "</optgroup>";
}
else
{
$result .= "<option value='$key' ";
if($value == $key)
{
$result .= "selected ";
}
$result .=">$val</option>";
}
}
$result .= "</select>";
return $result;
}
/**
* Generates <b>a href</b> link tag
* @param <b>$link</b> The link url
* @param <b>$label</b> The label for the link
* @param <b>$attributes :optional</b> Extra attributes of the link tag and their values (eg. id='something'
* or onclick='javascript:doSomething();' )
* @return string
*/
public static function href($link,$label,$attributes = null)
{
return "<a href='$link' ".($attributes==null?"":$attributes).">$label</a>";
}
/**
* Generates <b>image</b> tag
* @param <b>$src</b> The url source of the image tag
* @param <b>$alt</b> The alt attribute of the image tag
* @param <b>$attributes :optional</b> Extra attributes of the image tag and their values (eg. id='something'
* or onclick='javascript:doSomething();' )
* @return string
*/
public static function image($src,$alt,$attributes = null)
{
return "<img src='$src' alt='$alt' ".($attributes==null?"":$attributes)."/>";
}
/**
* Generate <b>textarea</b> input field
* @param <b>$name</b> The name of the textarea field
* @param <b>$value</b> The content of the textarea field
* @param <b>$rows</b> The number of rows of the textarea
* @param <b>$cols</b> The nubmer of collumns of the textarea
* @param <b>$attributes :optional</b> Extra attributes of the textarea input field and their values (eg. id='something'
* or onclick='javascript:doSomething();' )
*/
public static function textarea($name,$value,$rows,$cols,$attributes = null)
{
return "<textarea name=\"".$name."\" rows=\"".$rows."\" cols=\"".$cols."\" ".($attributes==null?"":$attributes).">".$value."</textarea>";
}
/**
* Generate <b>radio</b> input field.
* @param <b>$name</b> The name of the radio field
* @param <b>$value</b> The value of the radio field
* @param <b>$checked boolean :optional</b> If true checked="checked". False by default
* @param <b>$attributes :optional</b> Extra attributes of the radio input field and their values (eg. id='something'
* or onclick='javascript:doSomething();' )
* @return string
*/
public static function radio($name,$value,$checked=false,$attributes = null)
{
return "<input type=\"radio\" name=\"$name\" id=\"$name\" value=\"$value\" ".($checked?"checked=\"checked\" ":"").($attributes==null?"":$attributes)."/>";
}
/**
* Generates <b>checkbox</b> input field
* @param <b>$name</b> The name of the checkbox field
* @param <b>$value</b> The value of the checkbox field
* @param <b>$checked boolean :optional</b> If true checked="checked". False by default
* @param <b>$attributes :optional</b> Extra attributes of the checkbox input field and their values (eg. id='something'
* or onclick='javascript:doSomething();' )
* @return string
*/
public static function checkbox($name,$value,$checked=false,$attributes = null)
{
return "<input type=\"checkbox\" name=\"$name\" value=\"$value\" ".($checked?"checked=\"checked\" ":"").($attributes==null?"":$attributes)."/>";
}
/**
* Generates <b>file</b> input field
* @param <b>$name</b> The name of the file field
* @param <b>$attributes :optional</b> Extra attributes of the file input field and their values (eg. id='something'
* or onclick='javascript:doSomething();' )
* @return string
*/
public static function file($name,$attributes = null)
{
return "<input type=\"file\" name=\"$name\" id=\"$name\" ".($attributes==null?"":$attributes)."/>";
}
}
?>