Advertisement
petschko

FormField Class

Dec 2nd, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 31.85 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Author: Peter Dragicevic [peter-91@hotmail.de]
  4.  * Authors-Website: http://petschko.org/
  5.  * Date: 25.11.2015
  6.  * Time: 16:03
  7.  * Update: 09.04.2016
  8.  * Version: 2.1.4 (Changed Website)
  9.  * 2.1.3 (Reformat Code)
  10.  * 2.1.2 (Fixed TextArea-HTML Output-Bug)
  11.  * 2.1.1 (Added minLength to TextArea and Input)
  12.  * 2.1.0 (Added TEXTAREA constructor, Values & Output)
  13.  * 2.0.1 (Added trim to value to remove spaces at begin/end)
  14.  * 2.0.0 (Added auto-max/minLength and auto-dataType and html5 features)
  15.  *
  16.  * Notes: Easily create Forms (Generates HTML)
  17.  */
  18.  
  19. /**
  20.  * Class FormField
  21.  */
  22. class FormField {
  23.     const TYPE_BOOL = 'bool';
  24.     const TYPE_STRING = 'string';
  25.     const TYPE_INT = 'int';
  26.     const TYPE_DOUBLE = 'double';
  27.     const TYPE_NUMBER = 'number';
  28.     const TYPE_PHONE = 'phone';
  29.     const TYPE_EMAIL = 'email';
  30.     const TYPE_TIME = 'time';
  31.     const TYPE_DATE = 'date';
  32.     const TYPE_DATETIME = 'datetime';
  33.     const TYPE_URL = 'url';
  34.     const TYPE_COLOR = 'color';
  35.     const TYPE_TEXT = 'text';
  36.     const TYPE_ZIP = 'zip';
  37.  
  38.     private static $xhtml = false;
  39.     private static $html5 = true;
  40.     private $name;
  41.     private $value;
  42.     private $type;
  43.     private $enabled;
  44.     private $readOnly = false;
  45.     private $cssIds;
  46.     private $cssClasses;
  47.     private $otherHTMLAttr;
  48.     private $checked = false;
  49.     private $selectList = null;
  50.     private $multipleSelect = false;
  51.     private $size = null;
  52.     private $isPostVar = true;
  53.     private $required = true;
  54.     private $minLen = 0;
  55.     private $maxLen = 0;
  56.     private $currentLen;
  57.     private $dataType = self::TYPE_STRING;
  58.     private $rows;
  59.     private $cols;
  60.  
  61.     /**
  62.      * Creates a new instance of a form field
  63.      *
  64.      * @param string $name - name of the form field object
  65.      * @param string|array $value - current value of the object - it can be an array for select list - multiple
  66.      * @param string $type - type of the current object
  67.      * @param bool $isPost - is the form var saved to a post or get var
  68.      * @param array|null $cssId - css ID(s) of the object
  69.      * @param array|null $cssClasses - css Class(es) of the object
  70.      * @param bool $enabled - disable/enable this object (false - disabled | true - enabled)
  71.      * @param string|null $other - all other not listed HTML-Attr write it as an normal HTML-String
  72.      */
  73.     private function __construct($name, $value = '', $type = 'text', $isPost = true, $cssId = null, $cssClasses = null, $enabled = true, $other = null) {
  74.         $this->setName($name);
  75.         $this->setValue($value);
  76.  
  77.         if(self::isHtml5())
  78.             $this->setType($type);
  79.         else
  80.             $this->setType(self::removeHtml5Type($type));
  81.  
  82.         $this->setIsPostVar($isPost);
  83.         $this->setCssIds($cssId);
  84.         $this->setCssClasses($cssClasses);
  85.         $this->setEnabled($enabled);
  86.         $this->setOtherHTMLAttr($other);
  87.  
  88.         // If is submit field is not required
  89.         if(mb_strtolower($this->getType()) == 'submit')
  90.             $this->setRequired(false);
  91.  
  92.         // Auto assign DataType
  93.         $this->autoSetDataType($type);
  94.     }
  95.  
  96.     /**
  97.      * Clears Memory
  98.      */
  99.     public function __destruct() {
  100.         unset($this->name);
  101.         unset($this->value);
  102.         unset($this->type);
  103.         unset($this->isPostVar);
  104.         unset($this->enabled);
  105.         unset($this->readOnly);
  106.         unset($this->cssIds);
  107.         unset($this->cssClasses);
  108.         unset($this->otherHTMLAttr);
  109.         unset($this->checked);
  110.         unset($this->selectList);
  111.         unset($this->multipleSelect);
  112.         unset($this->size);
  113.         unset($this->required);
  114.         unset($this->minLen);
  115.         unset($this->maxLen);
  116.         unset($this->currentLen);
  117.         unset($this->dataType);
  118.         unset($this->rows);
  119.         unset($this->cols);
  120.     }
  121.  
  122.     /**
  123.      * Returns true if you use XHTML
  124.      *
  125.      * @return boolean - is this XHTML code
  126.      */
  127.     public static function isXhtml() {
  128.         return self::$xhtml;
  129.     }
  130.  
  131.     /**
  132.      * Set the value of XHTML use
  133.      *
  134.      * @param boolean $xhtml - enable/disable XHTML (true = enable | false = disable)
  135.      */
  136.     public static function setXhtml($xhtml) {
  137.         self::$xhtml = $xhtml;
  138.     }
  139.  
  140.     /**
  141.      * Returns true if HTML5 is enabled
  142.      *
  143.      * @return boolean - is HTML5 enabled (true = yes | false = no)
  144.      */
  145.     public static function isHtml5() {
  146.         return self::$html5;
  147.     }
  148.  
  149.     /**
  150.      * Set HTML5 on/off
  151.      *
  152.      * @param boolean $html5 - enable/disable HTML5 (true = enable | false = disable)
  153.      */
  154.     public static function setHtml5($html5) {
  155.         self::$html5 = $html5;
  156.     }
  157.  
  158.     /**
  159.      * Removes HTML5 types to input type text other non-HTML5 types will return normal
  160.      *
  161.      * @param string $type - Type to check
  162.      * @return string - New type (text) or the other non HTML5 Type
  163.      */
  164.     private static function removeHtml5Type($type) {
  165.         switch(mb_strtolower($type)) {
  166.             case 'color':
  167.             case 'date':
  168.             case 'datetime':
  169.             case 'datetime-local':
  170.             case 'email':
  171.             case 'month':
  172.             case 'number':
  173.             case 'range':
  174.             case 'search':
  175.             case 'tel':
  176.             case 'time':
  177.             case 'url':
  178.             case 'week':
  179.                 $type = 'text';
  180.         }
  181.  
  182.         return $type;
  183.     }
  184.  
  185.     /**
  186.      * Like the constructor but only make select fields
  187.      *
  188.      * @param string $name - name of the form field object
  189.      * @param array $selectList - the list of the select options
  190.      * @param string|array|null $value - current value of the object - it can be an array for select list - multiple - null detect value itself
  191.      * @param bool $isPost - is the form var saved to a post or get var
  192.      * @param bool $multiSelect - is multiSelect on the select field is allowed
  193.      * @param array|null $cssId - css ID(s) of the object
  194.      * @param array|null $cssClasses - css Class(es) of the object
  195.      * @param bool $enabled - disable/enable this object (false - disabled | true - enabled)
  196.      * @param string|null $other - all other not listed HTML-Attr write it as an normal HTML-String
  197.      * @return FormField - new instance
  198.      * @throws Exception - bad value
  199.      */
  200.     public static function createNewSelect($name, $selectList, $value = null, $isPost = true, $multiSelect = false, $cssId = null, $cssClasses = null, $enabled = true, $other = null) {
  201.         if($value === null)
  202.             $value = self::detectValue($name, $isPost);
  203.  
  204.         $obj = new self($name, $value, 'select', $isPost, $cssId, $cssClasses, $enabled, $other);
  205.         $obj->setSelectList($selectList);
  206.         $obj->setMultipleSelect($multiSelect);
  207.  
  208.         return $obj;
  209.     }
  210.  
  211.     /**
  212.      * Like the constructor but only make radio buttons
  213.      *
  214.      * @param string $name - name of the form field object
  215.      * @param string|array $value - current value of the object - it can be an array for select list - multiple
  216.      * @param bool|null $checked - is the radio button selected - null detect it itself
  217.      * @param bool $isPost - is the form var saved to a post or get var
  218.      * @param array|null $cssId - css ID(s) of the object
  219.      * @param array|null $cssClasses - css Class(es) of the object
  220.      * @param bool $enabled - disable/enable this object (false - disabled | true - enabled)
  221.      * @param string|null $other - all other not listed HTML-Attr write it as an normal HTML-String
  222.      * @return FormField - new instance
  223.      */
  224.     public static function createNewRadioButton($name, $value, $checked = null, $isPost = true, $cssId = null, $cssClasses = null, $enabled = true, $other = null) {
  225.         $tmpReturn = self::detectValue($name, $isPost);
  226.  
  227.         // Check if value is returned if none is given
  228.         if($checked === null) {
  229.             if($tmpReturn == $value)
  230.                 $checked = true;
  231.             else
  232.                 $checked = false;
  233.         }
  234.  
  235.         $obj = new self($name, $value, 'radio', $isPost, $cssId, $cssClasses, $enabled, $other);
  236.         $obj->setChecked($checked);
  237.  
  238.         return $obj;
  239.     }
  240.  
  241.     /**
  242.      * Like the constructor but only make checkboxes
  243.      *
  244.      * @param string $name - name of the form field object
  245.      * @param string|array $value - current value of the object - it can be an array for select list - multiple
  246.      * @param bool|null $checked - is the radio button selected - null detect it itself
  247.      * @param bool $isPost - is the form var saved to a post or get var
  248.      * @param array|null $cssId - css ID(s) of the object
  249.      * @param array|null $cssClasses - css Class(es) of the object
  250.      * @param bool $enabled - disable/enable this object (false - disabled | true - enabled)
  251.      * @param string|null $other - all other not listed HTML-Attr write it as an normal HTML-String
  252.      * @return FormField - new instance
  253.      */
  254.     public static function createNewCheckBox($name, $value, $checked = null, $isPost = true, $cssId = null, $cssClasses = null, $enabled = true, $other = null) {
  255.         $tmpReturn = self::detectValue($name, $isPost);
  256.  
  257.         // Check if value is returned if none is given
  258.         if($checked === null) {
  259.             if($tmpReturn == $value)
  260.                 $checked = true;
  261.             else
  262.                 $checked = false;
  263.         }
  264.  
  265.         $obj = new self($name, $value, 'checkbox', $isPost, $cssId, $cssClasses, $enabled, $other);
  266.         $obj->setChecked($checked);
  267.  
  268.         return $obj;
  269.     }
  270.  
  271.     /**
  272.      * Like the constructor but only make TextAreas
  273.      *
  274.      * @param string $name - Name of the TextArea-Field
  275.      * @param null|int $cols - Cols of the TextArea
  276.      * @param null|int  $rows - Rows of the TextArea
  277.      * @param string|null $value - current value of the object - null detect value itself
  278.      * @param bool $isPost - is the form var saved to a post or get var
  279.      * @param array|null $cssId - css ID(s) of the object
  280.      * @param array|null $cssClasses - css Class(es) of the object
  281.      * @param bool $enabled - disable/enable this object (false - disabled | true - enabled)
  282.      * @param string|null $other - all other not listed HTML-Attr write it as an normal HTML-String
  283.      * @return FormField - new instance
  284.      */
  285.     public static function createNewTextArea($name, $cols = null, $rows = null, $value = null, $isPost = true, $cssId = null, $cssClasses = null, $enabled = true, $other = null) {
  286.         if($value === null)
  287.             $value = self::detectValue($name, $isPost);
  288.  
  289.         $obj = new self($name, $value, 'textarea', $isPost, $cssId, $cssClasses, $enabled, $other);
  290.         $obj->setRows($rows);
  291.         $obj->setCols($cols);
  292.  
  293.         return $obj;
  294.     }
  295.  
  296.     /**
  297.      * The new constructor - creates a new instance
  298.      *
  299.      * @param string $name - name of the form field object
  300.      * @param string $type - type of the current object
  301.      * @param string|null $value - current value of the object - null detect value itself
  302.      * @param bool $isPost - is the form var saved to a post or get var
  303.      * @param array|null $cssId - css ID(s) of the object
  304.      * @param array|null $cssClasses - css Class(es) of the object
  305.      * @param bool $enabled - disable/enable this object (false - disabled | true - enabled)
  306.      * @param string|null $other - all other not listed HTML-Attr write it as an normal HTML-String
  307.      * @return FormField - new instance
  308.      * @throws Exception - bad function
  309.      */
  310.     public static function createNewField($name, $type = 'text', $value = null, $isPost = true, $cssId = null, $cssClasses = null, $enabled = true, $other = null) {
  311.         if(in_array(mb_strtolower($type), array('radio', 'checkbox', 'select', 'textarea')))
  312.             throw new Exception(__CLASS__ . '::' . __FUNCTION__ . ': Please use the special create functions for textarea, select, radio & checkbox!');
  313.  
  314.         if($value === null)
  315.             $value = self::detectValue($name, $isPost);
  316.  
  317.         return new self($name, $value, $type, $isPost, $cssId, $cssClasses, $enabled, $other);
  318.     }
  319.  
  320.     /**
  321.      * Auto detects Value for the specified field
  322.      *
  323.      * @param string $name - Field name
  324.      * @param bool $isPost - Is it a POST or GET Variable
  325.      * @return mixed - Value
  326.      */
  327.     private static function detectValue($name, $isPost) {
  328.         // PreCheck var and assign
  329.         if($isPost) {
  330.             if(! isset($_POST[$name]))
  331.                 $_POST[$name] = false;
  332.  
  333.             return $_POST[$name];
  334.         } else {
  335.             if(! isset($_GET[$name]))
  336.                 $_GET[$name] = false;
  337.  
  338.             return $_GET[$name];
  339.         }
  340.     }
  341.  
  342.     /**
  343.      * Returns the value of the Rows for a TextArea
  344.      *
  345.      * @return null|int - Rows of the TextArea or null if none is set
  346.      */
  347.     private function getRows() {
  348.         return $this->rows;
  349.     }
  350.  
  351.     /**
  352.      * Set the Rows of the TextArea
  353.      *
  354.      * @param null|int $rows - Rows of the TextArea or null to unset
  355.      */
  356.     private function setRows($rows) {
  357.         $this->rows = $rows;
  358.     }
  359.  
  360.     /**
  361.      * Returns the value of the Cols for a TextArea
  362.      *
  363.      * @return null|int - Cols of the TextArea or null if none is set
  364.      */
  365.     private function getCols() {
  366.         return $this->cols;
  367.     }
  368.  
  369.     /**
  370.      * Set the Cols of the TextArea
  371.      *
  372.      * @param null|int $cols - Cols of the TextArea or null to unset
  373.      */
  374.     private function setCols($cols) {
  375.         $this->cols = $cols;
  376.     }
  377.  
  378.     /**
  379.      * Returns the allowed DataType of the value
  380.      *
  381.      * @return string - Allowed DataType
  382.      */
  383.     public function getDataType() {
  384.         return $this->dataType;
  385.     }
  386.  
  387.     /**
  388.      * Set the allowed DataType of the value
  389.      *
  390.      * @param string $dataType - Allowed DataType
  391.      */
  392.     public function setDataType($dataType) {
  393.         $this->dataType = $dataType;
  394.     }
  395.  
  396.  
  397.  
  398.     /**
  399.      * Returns true if the object is checked else false
  400.      *
  401.      * @return boolean - is the object checked
  402.      */
  403.     public function isChecked() {
  404.         return $this->checked;
  405.     }
  406.  
  407.     /**
  408.      * Set if the object is checked
  409.      *
  410.      * @param boolean $checked - is the object checked (true = checked | false = not checked)
  411.      */
  412.     public function setChecked($checked) {
  413.         $this->checked = $checked;
  414.     }
  415.  
  416.     /**
  417.      * Returns true if the object is required
  418.      *
  419.      * @return boolean - is this field required
  420.      */
  421.     public function isRequired() {
  422.         return $this->required;
  423.     }
  424.  
  425.     /**
  426.      * Set if the object is required
  427.      *
  428.      * @param boolean $required - is this field required (true = yes | false = no)
  429.      */
  430.     public function setRequired($required) {
  431.         $this->required = $required;
  432.     }
  433.  
  434.  
  435.     /**
  436.      * Returns true if the var is send via POST else false
  437.      *
  438.      * @return bool - is the form value saved to a post or get var
  439.      */
  440.     public function isPostVar() {
  441.         return $this->isPostVar;
  442.     }
  443.  
  444.     /**
  445.      * Set is var send via POST
  446.      *
  447.      * @param bool $isPostVar - is the form var saved to a post or get var (true = yes | false = no)
  448.      */
  449.     private function setIsPostVar($isPostVar) {
  450.         $this->isPostVar = $isPostVar;
  451.     }
  452.  
  453.     /**
  454.      * Returns the list of a select object
  455.      *
  456.      * @return null|array - the list of an select input field - null means no list
  457.      */
  458.     public function getSelectList() {
  459.         return $this->selectList;
  460.     }
  461.  
  462.     /**
  463.      * Set the list of a select object
  464.      *
  465.      * @param null|array $selectList - the list of an selected input field - null means no list
  466.      * @throws Exception - wrong type
  467.      */
  468.     public function setSelectList($selectList) {
  469.         if($selectList === null || is_array($selectList))
  470.             $this->selectList = $selectList;
  471.         else
  472.             throw new Exception(__CLASS__ . '->' . __FUNCTION__ . ': selectList has to be an array or null! ' . gettype($selectList) . ' given...');
  473.     }
  474.  
  475.     /**
  476.      * Returns true if the user can select more than 1 option in this select object else false
  477.      *
  478.      * @return boolean - can user select multiple options
  479.      */
  480.     public function isMultipleSelect() {
  481.         return $this->multipleSelect;
  482.     }
  483.  
  484.     /**
  485.      * Set if the user can select multiple options in this select object
  486.      *
  487.      * @param boolean $multipleSelect - can user select multiple options (true = yes | false = no)
  488.      */
  489.     public function setMultipleSelect($multipleSelect) {
  490.         $this->multipleSelect = $multipleSelect;
  491.     }
  492.  
  493.  
  494.     /**
  495.      * Returns the name of this object
  496.      *
  497.      * @return string - the name of this object
  498.      */
  499.     public function getName() {
  500.         return $this->name;
  501.     }
  502.  
  503.     /**
  504.      * Set the name of this object
  505.      *
  506.      * @param string $name - the name of this object
  507.      */
  508.     private function setName($name) {
  509.         $this->name = $name;
  510.     }
  511.  
  512.     /**
  513.      * Returns the current value of this object
  514.      *
  515.      * @return string|array - the current value of this object
  516.      */
  517.     public function getValue() {
  518.         return $this->value;
  519.     }
  520.  
  521.     /**
  522.      * Set the current value of this object
  523.      *
  524.      * @param string|array $value - the current value of this object
  525.      */
  526.     public function setValue($value) {
  527.         $this->value = trim($value);
  528.     }
  529.  
  530.     /**
  531.      * Returns the size of the object - null means no limit
  532.      *
  533.      * @return null|int - size of the object - null means no limit
  534.      */
  535.     public function getSize() {
  536.         return $this->size;
  537.     }
  538.  
  539.     /**
  540.      * Set the size of the object - null means no limit
  541.      *
  542.      * @param null|int $size - size of the object - null means no limit
  543.      */
  544.     public function setSize($size) {
  545.         if(is_numeric($size) || $size === null)
  546.             $this->size = $size;
  547.     }
  548.  
  549.     /**
  550.      * Get the current type of this object
  551.      *
  552.      * @return string - the current type of this object
  553.      */
  554.     public function getType() {
  555.         return $this->type;
  556.     }
  557.  
  558.     /**
  559.      * Set the current type of this object
  560.      *
  561.      * @param string $type - the current type of this object
  562.      */
  563.     private function setType($type) {
  564.         $this->type = mb_strtolower($type);
  565.     }
  566.  
  567.     /**
  568.      * Returns true if the object is enabled else false
  569.      *
  570.      * @return boolean - is this object enabled
  571.      */
  572.     public function isEnabled() {
  573.         return $this->enabled;
  574.     }
  575.  
  576.     /**
  577.      * Set if the object is enabled
  578.      *
  579.      * @param boolean $enabled - enable disable this object (true = enabled | false = disabled)
  580.      */
  581.     public function setEnabled($enabled) {
  582.         $this->enabled = $enabled;
  583.     }
  584.  
  585.     /**
  586.      * Returns true if you can only read the field
  587.      *
  588.      * @return boolean - Can field only read? true = readOnly | false = write/read
  589.      */
  590.     public function isReadOnly() {
  591.         return $this->readOnly;
  592.     }
  593.  
  594.     /**
  595.      * Set if field can only read
  596.      *
  597.      * @param boolean $readOnly - Can field only read? true = readOnly | false = write/read
  598.      */
  599.     public function setReadOnly($readOnly) {
  600.         $this->readOnly = $readOnly;
  601.     }
  602.  
  603.     /**
  604.      * Returns the value of chars that this field must have at least
  605.      *
  606.      * @return int - min length of the field
  607.      */
  608.     public function getMinLen() {
  609.         return $this->minLen;
  610.     }
  611.  
  612.     /**
  613.      * Set the value of chars that this field must have at least
  614.      *
  615.      * @param int $minLen - min length of the field.
  616.      */
  617.     public function setMinLen($minLen) {
  618.         $this->minLen = $minLen;
  619.     }
  620.  
  621.     /**
  622.      * Returns the value of chars that this field can have max
  623.      *
  624.      * @return int - max length 0 means no limit
  625.      */
  626.     public function getMaxLen() {
  627.         return $this->maxLen;
  628.     }
  629.  
  630.     /**
  631.      * Set the value of chars that this field can have max
  632.      *
  633.      * @param int $maxLen - max length 0 means no limit
  634.      */
  635.     public function setMaxLen($maxLen) {
  636.         $this->maxLen = $maxLen;
  637.     }
  638.  
  639.     /**
  640.      * Returns the current length of the value and set it if not set before
  641.      *
  642.      * @return int - length of the value
  643.      */
  644.     public function getCurrentLen() {
  645.         if(! isset($this->currentLen))
  646.             $this->setCurrentLen();
  647.  
  648.         return $this->currentLen;
  649.     }
  650.  
  651.     /**
  652.      * Set the current length of the value
  653.      */
  654.     private function setCurrentLen() {
  655.         $this->currentLen = mb_strlen($this->getValue());
  656.     }
  657.  
  658.     /**
  659.      * Returns the CSS-ID(s) or null
  660.      *
  661.      * @return array|null - CSS-ID(s) as array or null if there are none
  662.      */
  663.     private function getCssIds() {
  664.         return $this->cssIds;
  665.     }
  666.  
  667.     /**
  668.      * Set the CSS-ID(s)
  669.      *
  670.      * @param array|null $cssIds - CSS-ID(s) as array or null if there are none
  671.      */
  672.     private function setCssIds($cssIds) {
  673.         $this->cssIds = $cssIds;
  674.     }
  675.  
  676.     /**
  677.      * Returns CSS-ID(s) as HTML-string
  678.      *
  679.      * @return string - HTML-string
  680.      */
  681.     private function cssIdsHTML() {
  682.         if($this->getCssIds() === null)
  683.             return '';
  684.  
  685.         $code = '';
  686.         foreach($this->getCssIds() as $cssId) {
  687.             $code .= ' ' . $cssId;
  688.         }
  689.  
  690.         return ' id="' . trim($code) . '"';
  691.     }
  692.  
  693.     /**
  694.      * Returns CSS-Class(es) or null
  695.      *
  696.      * @return array|null - CSS-Class(es) as array or null if there are none
  697.      */
  698.     private function getCssClasses() {
  699.         return $this->cssClasses;
  700.     }
  701.  
  702.     /**
  703.      * Set CSS-Class(es)
  704.      *
  705.      * @param array|null $cssClasses - CSS-Class(es) as array or null if there are none
  706.      */
  707.     private function setCssClasses($cssClasses) {
  708.         $this->cssClasses = $cssClasses;
  709.     }
  710.  
  711.     /**
  712.      * Returns CSS-Class(es) as HTML-string
  713.      *
  714.      * @return string - HTML-String
  715.      */
  716.     private function cssClassesHTML() {
  717.         if($this->getCssClasses() === null)
  718.             return '';
  719.  
  720.         $code = '';
  721.         foreach($this->getCssClasses() as $cssClass) {
  722.             $code .= ' ' . $cssClass;
  723.         }
  724.  
  725.         return ' class="' . trim($code) . '"';
  726.     }
  727.  
  728.     /**
  729.      * Add a CSS-Class to the object
  730.      *
  731.      * @param string $cssClassName - the name of a css class, no "," needed!!!
  732.      */
  733.     public function addCssClass($cssClassName) {
  734.         $this->cssClasses[] = $cssClassName;
  735.     }
  736.  
  737.     /**
  738.      * Remove a CSS-Class from the object
  739.      *
  740.      * @param string $cssClassName - the name of a css class
  741.      */
  742.     public function removeCssClass($cssClassName) {
  743.         // Create a new array and include all values to it except the remove css class
  744.         $tmpNew = array();
  745.         $i = 0;
  746.         foreach($this->getCssClasses() as $cssClass) {
  747.             if($cssClassName != $cssClass) {
  748.                 $tmpNew[$i] = $cssClass;
  749.                 $i++;
  750.             }
  751.         }
  752.  
  753.         $this->setCssClasses($tmpNew);
  754.     }
  755.  
  756.     /**
  757.      * Add a CSS-ID to the object
  758.      *
  759.      * @param string $cssIdName - the name of a css ID, no "," needed!!!
  760.      */
  761.     public function addCssId($cssIdName) {
  762.         $this->cssIds[] = $cssIdName;
  763.     }
  764.  
  765.     /**
  766.      * Remove a CSS-ID from the object
  767.      *
  768.      * @param string $cssIdName - the name of a css ID
  769.      */
  770.     public function removeCssId($cssIdName) {
  771.         // Create a new array and include all values to it except the remove css class
  772.         $tmpNew = array();
  773.         $i = 0;
  774.         foreach($this->getCssIds() as $cssId) {
  775.             if($cssIdName != $cssId) {
  776.                 $tmpNew[$i] = $cssId;
  777.                 $i++;
  778.             }
  779.         }
  780.  
  781.         $this->setCssIds($tmpNew);
  782.     }
  783.  
  784.     /**
  785.      * Returns all other HTML-Stuff or null
  786.      *
  787.      * @return string|null - all other html attributes - null if there are none
  788.      */
  789.     public function getOtherHTMLAttr() {
  790.         return $this->otherHTMLAttr;
  791.     }
  792.  
  793.     /**
  794.      * Set all other CSS stuff
  795.      *
  796.      * @param string|null $otherHTMLAttr - all other html attributes - null if there are none
  797.      */
  798.     public function setOtherHTMLAttr($otherHTMLAttr) {
  799.         $this->otherHTMLAttr = $otherHTMLAttr;
  800.     }
  801.  
  802.     /**
  803.      * Check if the value dataType is the same as required on this field and convert them to the correct type
  804.      *
  805.      * @return bool - true on success else false
  806.      */
  807.     public function checkDataType() {
  808.         $value = $this->getValue();
  809.  
  810.         // Go to the right dataType to check
  811.         switch($this->getDataType()) {
  812.  
  813.             case self::TYPE_BOOL:
  814.                 // Convert value to bool
  815.                 if(is_string($value))
  816.                     $this->setValue((boolean) $value);
  817.                 else if(! is_bool($value))
  818.                     return false;
  819.                 break;
  820.  
  821.             case self::TYPE_INT:
  822.                 // Check if string contains only numbers
  823.                 if(is_string($value)) {
  824.                     if(! ctype_digit($value)) {
  825.  
  826.                         // May its an negative value check it out
  827.                         if(! mb_substr($value, 0, 1) == '-') {
  828.                             if(! ctype_digit(mb_substr($value, 1)))
  829.                                 return false;
  830.                         } else
  831.                             return false;
  832.                     }
  833.                 } else if(! is_int($value))
  834.                     return false;
  835.  
  836.                 // Convert to int
  837.                 $this->setValue((int) $value);
  838.                 break;
  839.  
  840.             case self::TYPE_DOUBLE:
  841.                 // Check if string is numeric
  842.                 if(is_string($value)) {
  843.                     if(! is_numeric($value))
  844.                         return false;
  845.                 } else if(! (is_int($value) || is_float($value)))
  846.                     return false;
  847.  
  848.                 // Convert to float
  849.                 $this->setValue((float) $value);
  850.                 break;
  851.  
  852.             case self::TYPE_NUMBER:
  853.                 if(is_string($value)) {
  854.                     if(! is_numeric($value))
  855.                         return false;
  856.                 } else
  857.                     return false;
  858.                 break;
  859.  
  860.             case self::TYPE_PHONE:
  861.                 if(! is_string($value))
  862.                     return false;
  863.  
  864.                 // Verify that its a number (with special chars) of typical numbers
  865.                 if(! preg_match('/([^0-9-+ \/\(\)])$/', $value))
  866.                     return false;
  867.                 break;
  868.  
  869.             case self::TYPE_STRING:
  870.                 // Check if its a string
  871.                 if(! is_string($value))
  872.                     return false;
  873.  
  874.                 //todo regex remove htmlEntities
  875.                 break;
  876.  
  877.             case self::TYPE_EMAIL:
  878.                 if(! is_string($value))
  879.                     return false;
  880.  
  881.                 // Check E-Mail pattern
  882.                 $atPos = mb_strpos($value, '@');
  883.                 $lastPointPos = mb_strrpos($value, '.');
  884.  
  885.                 if(! $atPos || ! $lastPointPos)
  886.                     return false;
  887.  
  888.                 if(! ($atPos > 0 && $lastPointPos > ($atPos + 1) && mb_strlen($value) > ($lastPointPos + 1)))
  889.                     return false;
  890.                 break;
  891.  
  892.             case self::TYPE_COLOR:
  893.                 //todo
  894.             case self::TYPE_URL:
  895.                 if(! is_string($value))
  896.                     return false;
  897.  
  898.                 // Only allow specific Characters to URLs
  899.                 if(! preg_match('/([^0-9a-zA-Z+_ #~&@=,;:\|\-\?\.\/])$/', $value)) // todo test
  900.                     return false;
  901.  
  902.                 // Correct URL
  903.                 //$this->setValue(); //todo
  904.  
  905.                 break;
  906.             case self::TYPE_TIME:
  907.                 //todo
  908.             case self::TYPE_DATE:
  909.                 //todo
  910.             case self::TYPE_DATETIME:
  911.                 //todo
  912.             case self::TYPE_ZIP:
  913.                 //todo
  914.  
  915.             case self::TYPE_TEXT:
  916.             default:
  917.                 if(! is_string($value))
  918.                     return false;
  919.  
  920.         }
  921.  
  922.         return true;
  923.     }
  924.  
  925.     /**
  926.      * Autodetect dataType for every FieldType and assign it (can changed later)
  927.      * Set also auto lengths can changed later too :)
  928.      *
  929.      * @param string|null $type - Current Field-Type
  930.      */
  931.     private function autoSetDataType($type = null) {
  932.         // Is a Type set? If not use object field type
  933.         if($type === null)
  934.             $type = $this->getType();
  935.  
  936.         switch(mb_strtolower($type)) {
  937.             case 'color':
  938.                 $this->setDataType(self::TYPE_COLOR);
  939.                 $this->setMinLen(3);
  940.                 $this->setMaxLen(7);
  941.                 break;
  942.  
  943.             case 'time':
  944.                 $this->setDataType(self::TYPE_TIME);
  945.                 $this->setMinLen(3);
  946.                 $this->setMaxLen(5);
  947.                 break;
  948.  
  949.             case 'date':
  950.                 $this->setDataType(self::TYPE_DATE);
  951.                 $this->setMinLen(6);
  952.                 $this->setMaxLen(10);
  953.                 break;
  954.  
  955.             case 'datetime':
  956.             case 'datetime-local':
  957.                 $this->setDataType(self::TYPE_DATETIME);
  958.                 $this->setMaxLen(16);
  959.                 break;
  960.  
  961.             case 'email':
  962.                 $this->setDataType(self::TYPE_EMAIL);
  963.                 $this->setMinLen(6);
  964.                 $this->setMaxLen(256);
  965.                 break;
  966.  
  967.             case 'tel':
  968.                 $this->setDataType(self::TYPE_PHONE);
  969.                 $this->setMaxLen(30);
  970.                 break;
  971.  
  972.             case 'url':
  973.                 $this->setDataType(self::TYPE_URL);
  974.                 $this->setMinLen(11);
  975.                 break;
  976.  
  977.             case 'range':
  978.             case 'number':
  979.                 $this->setDataType(self::TYPE_NUMBER);
  980.                 break;
  981.         }
  982.     }
  983.  
  984.     /**
  985.      * Output this object
  986.      *
  987.      * @return string - HTML-Code
  988.      */
  989.     public function output() {
  990.         switch($this->getType()) {
  991.             case 'select':
  992.                 return $this->selectHTML();
  993.             case 'radio':
  994.             case 'checkbox':
  995.                 return $this->radioButtonHTML();
  996.             case 'textarea':
  997.                 return $this->textAreaHTML();
  998.             default:
  999.                 // Returns input by default declare extra fields as case
  1000.                 return $this->inputHTML();
  1001.         }
  1002.     }
  1003.  
  1004.     /**
  1005.      * Returns a SELECT object as HTML-Code
  1006.      *
  1007.      * @return string - SELECT-HTML-Code
  1008.      */
  1009.     private function selectHTML() {
  1010.         if($this->getSelectList() === null)
  1011.             return '[SELECT - Info at line ' . __LINE__ . ' in file ' . __FILE__ . ']: Please use (this)->setSelectList(array()); to set the list... It\'s empty yet!';
  1012.  
  1013.         // Create SELECT field
  1014.         $code = '<select name="' . $this->getName() . (($this->isMultipleSelect()) ? '[]' : '') . '"' . $this->cssIdsHTML() . $this->cssClassesHTML();
  1015.  
  1016.         // Add size if its not the default size
  1017.         if($this->getSize() !== null && $this->getSize() != 1)
  1018.             $code .= ' size="' . $this->getSize() . '"';
  1019.  
  1020.         // Include other HTML attr
  1021.         if($this->getOtherHTMLAttr() !== null)
  1022.             $code .= ' ' . $this->getOtherHTMLAttr();
  1023.  
  1024.         // Is this field readOnly?
  1025.         if($this->isReadOnly()) {
  1026.             $code .= ' readonly';
  1027.             if(self::isXhtml())
  1028.                 $code .= '="readonly"';
  1029.         }
  1030.  
  1031.         // Is this field required AND is HTML5 allowed?
  1032.         if(self::isHtml5() && $this->isRequired()) {
  1033.             $code .= ' required';
  1034.             if(self::isXhtml())
  1035.                 $code .= '="required"';
  1036.         }
  1037.  
  1038.         if(! $this->isEnabled()) {
  1039.             $code .= ' disabled';
  1040.             if(self::isXhtml())
  1041.                 $code .= '="disabled"';
  1042.         } else {
  1043.             if($this->isMultipleSelect()) {
  1044.                 $code .= 'multiple';
  1045.                 if(self::isXhtml())
  1046.                     $code .= '="multiple"';
  1047.             }
  1048.         }
  1049.         // Close StartTag
  1050.         $code .= '>';
  1051.  
  1052.         // Show List
  1053.         foreach($this->getSelectList() as $listName => $listValue) {
  1054.             $code .= '<option value="' . $listValue . '"';
  1055.             if(is_array($this->getValue()) && $this->isMultipleSelect()) {
  1056.                 foreach($this->getValue() as $value) {
  1057.                     if($value == $listValue) {
  1058.                         $code .= ' selected';
  1059.                         if(self::isXhtml())
  1060.                             $code .= '="selected"';
  1061.                         break;
  1062.                     }
  1063.                 }
  1064.             } else if($listValue == $this->getValue()) {
  1065.                 $code .= ' selected';
  1066.                 if(self::isXhtml())
  1067.                     $code .= '="selected"';
  1068.             }
  1069.             $code .= '>' . $listName . '</option>';
  1070.         }
  1071.  
  1072.         // Close Select field
  1073.         $code .= '</select>';
  1074.  
  1075.         return $code;
  1076.     }
  1077.  
  1078.     /**
  1079.      * Returns the object as HTML-Code
  1080.      *
  1081.      * @return string - INPUT-HTML-Code
  1082.      */
  1083.     private function inputHTML() {
  1084.         $code = '<input type="' . $this->getType() . '" name="' . $this->getName() . '" value="' . $this->getValue() . '"';
  1085.         $code .= $this->cssIdsHTML() . $this->cssClassesHTML();
  1086.  
  1087.         // Add size if its not the default size
  1088.         if($this->getSize() !== null)
  1089.             $code .= ' size="' . $this->getSize() . '"';
  1090.  
  1091.         // Add min length if is set
  1092.         if($this->getMinLen() > 0 && self::isHtml5())
  1093.             $code .= ' minlength="' . $this->getMinLen() . '"';
  1094.  
  1095.         // Add max length if is set
  1096.         if($this->getMaxLen() > 0 && self::isHtml5())
  1097.             $code .= ' maxlength="' . $this->getMaxLen() . '"';
  1098.  
  1099.         // Include other HTML attr
  1100.         if($this->getOtherHTMLAttr() !== null)
  1101.             $code .= ' ' . $this->getOtherHTMLAttr();
  1102.  
  1103.         // Is this field readOnly?
  1104.         if($this->isReadOnly()) {
  1105.             $code .= ' readonly';
  1106.             if(self::isXhtml())
  1107.                 $code .= '="readonly"';
  1108.         }
  1109.  
  1110.         // Is this field required AND is HTML5 allowed?
  1111.         if(self::isHtml5() && $this->isRequired()) {
  1112.             $code .= ' required';
  1113.             if(self::isXhtml())
  1114.                 $code .= '="required"';
  1115.         }
  1116.  
  1117.         // Is enabled
  1118.         if(! $this->isEnabled()) {
  1119.             $code .= ' disabled';
  1120.             if(self::isXhtml())
  1121.                 $code .= '="disabled"';
  1122.         }
  1123.  
  1124.         // Close input-field
  1125.         if(self::isXhtml())
  1126.             $code .= ' />';
  1127.         else
  1128.             $code .= '>';
  1129.  
  1130.         return $code;
  1131.     }
  1132.  
  1133.     /**
  1134.      * Returns the RadioButton/CheckBox as HTML-Code
  1135.      *
  1136.      * @return string - RadioButton/CheckBox HTML-Code
  1137.      */
  1138.     private function radioButtonHTML() {
  1139.         $code = '<input type="' . $this->getType() . '" name="' . $this->getName() . '" value="' . $this->getValue() . '"';
  1140.         $code .= $this->cssIdsHTML() . $this->cssClassesHTML();
  1141.  
  1142.         // Include other HTML attr
  1143.         if($this->getOtherHTMLAttr() !== null)
  1144.             $code .= ' ' . $this->getOtherHTMLAttr();
  1145.  
  1146.         // Is this field readOnly?
  1147.         if($this->isReadOnly()) {
  1148.             $code .= ' readonly';
  1149.             if(self::isXhtml())
  1150.                 $code .= '="readonly"';
  1151.         }
  1152.  
  1153.         // Is this field required AND is HTML5 allowed?
  1154.         if(self::isHtml5() && $this->isRequired()) {
  1155.             $code .= ' required';
  1156.             if(self::isXhtml())
  1157.                 $code .= '="required"';
  1158.         }
  1159.  
  1160.         // Is checked
  1161.         if($this->isChecked()) {
  1162.             $code .= ' checked';
  1163.             if(self::isXhtml())
  1164.                 $code .= '="checked"';
  1165.         }
  1166.  
  1167.         // Is enabled
  1168.         if(! $this->isEnabled()) {
  1169.             $code .= ' disabled';
  1170.             if(self::isXhtml())
  1171.                 $code .= '="disabled"';
  1172.         }
  1173.  
  1174.         // Close input-field
  1175.         if(self::isXhtml())
  1176.             $code .= ' />';
  1177.         else
  1178.             $code .= '>';
  1179.  
  1180.         return $code;
  1181.     }
  1182.  
  1183.     /**
  1184.      * Returns the TextArea as HTML-Code
  1185.      *
  1186.      * @return string - TextArea HTML-Code
  1187.      */
  1188.     private function textAreaHTML() {
  1189.         $code = '<textarea name="' . $this->getName() . '"';
  1190.  
  1191.         // Add Cols && Rows
  1192.         if($this->getCols() !== null)
  1193.             $code .= ' cols="' . $this->getCols() . '"';
  1194.         if($this->getRows() !== null)
  1195.             $code .= ' rows="' . $this->getRows() . '"';
  1196.  
  1197.         $code .= $this->cssIdsHTML() . $this->cssClassesHTML();
  1198.  
  1199.         if($this->getMinLen() > 0 && self::isHtml5())
  1200.             $code .= ' minlength="' . $this->getMinLen() . '"';
  1201.  
  1202.         // Add max length if is set
  1203.         if($this->getMaxLen() > 0 && self::isHtml5())
  1204.             $code .= ' maxlength="' . $this->getMaxLen() . '"';
  1205.  
  1206.         // Include other HTML attr
  1207.         if($this->getOtherHTMLAttr() !== null)
  1208.             $code .= ' ' . $this->getOtherHTMLAttr();
  1209.  
  1210.         // Is this field readOnly?
  1211.         if($this->isReadOnly()) {
  1212.             $code .= ' readonly';
  1213.             if(self::isXhtml())
  1214.                 $code .= '="readonly"';
  1215.         }
  1216.  
  1217.         // Is this field required AND is HTML5 allowed?
  1218.         if(self::isHtml5() && $this->isRequired()) {
  1219.             $code .= ' required';
  1220.             if(self::isXhtml())
  1221.                 $code .= '="required"';
  1222.         }
  1223.  
  1224.         // Is enabled
  1225.         if(! $this->isEnabled()) {
  1226.             $code .= ' disabled';
  1227.             if(self::isXhtml())
  1228.                 $code .= '="disabled"';
  1229.         }
  1230.  
  1231.         // Close open TextArea-Tag and insert value
  1232.         $code .= '>' . $this->getValue() . '</textarea>';
  1233.  
  1234.         return $code;
  1235.     }
  1236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement