Lançado Adianti Framework 7.6!
Clique aqui para saber mais
menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file TField.php

Documentation is available at TField.php

  1. <?php
  2. namespace Adianti\Widget\Form;
  3.  
  4. use Adianti\Core\AdiantiCoreTranslator;
  5. use Adianti\Widget\Base\TElement;
  6. use Adianti\Widget\Base\TScript;
  7. use Adianti\Validator\TFieldValidator;
  8. use Adianti\Validator\TRequiredValidator;
  9. use Adianti\Validator\TEmailValidator;
  10. use Adianti\Validator\TMinLengthValidator;
  11. use Adianti\Validator\TMaxLengthValidator;
  12.  
  13. use Exception;
  14. use ReflectionClass;
  15. use Closure;
  16.  
  17. /**
  18.  * Base class to construct all the widgets
  19.  *
  20.  * @version    7.4
  21.  * @package    widget
  22.  * @subpackage form
  23.  * @author     Pablo Dall'Oglio
  24.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  25.  * @license    http://www.adianti.com.br/framework-license
  26.  */
  27. abstract class TField
  28. {
  29.     protected $id;
  30.     protected $name;
  31.     protected $size;
  32.     protected $value;
  33.     protected $editable;
  34.     protected $tag;
  35.     protected $formName;
  36.     protected $label;
  37.     protected $properties;
  38.     protected $valueCallback;
  39.     private   $validations;
  40.     
  41.     /**
  42.      * Class Constructor
  43.      * @param  $name name of the field
  44.      */
  45.     public function __construct($name)
  46.     {
  47.         $rc new ReflectionClass$this );
  48.         $classname $rc->getShortName();
  49.         
  50.         if (empty($name))
  51.         {
  52.             throw new Exception(AdiantiCoreTranslator::translate('The parameter (^1) of ^2 constructor is required''name'$classname));
  53.         }
  54.         
  55.         // define some default properties
  56.         self::setEditable(TRUE);
  57.         self::setName(trim($name));
  58.         
  59.         // initialize validations array
  60.         $this->validations [];
  61.         $this->properties  = [];
  62.         
  63.         // creates a <input> tag
  64.         $this->tag = new TElement('input');
  65.         $this->tag->{'class''tfield';   // classe CSS
  66.         $this->tag->{'widget'strtolower($classname);
  67.     }
  68.     
  69.     /**
  70.      * Intercepts whenever someones assign a new property's value
  71.      * @param $name     Property Name
  72.      * @param $value    Property Value
  73.      */
  74.     public function __set($name$value)
  75.     {
  76.         // objects and arrays are not set as properties
  77.         if (is_scalar($value))
  78.         {              
  79.             // store the property's value
  80.             $this->setProperty($name$value);
  81.         }
  82.     }
  83.     
  84.     /**
  85.      * Returns a property value
  86.      * @param $name     Property Name
  87.      */
  88.     public function __get($name)
  89.     {
  90.         return $this->getProperty($name);
  91.     }
  92.     
  93.     /**
  94.      * Returns if the property is set
  95.      * @param $name     Property Name
  96.      */
  97.     public function __isset($name)
  98.     {
  99.         return isset($this->tag->$name);
  100.     }
  101.     
  102.     /**
  103.      * Clone the object
  104.      */
  105.     function __clone()
  106.     {
  107.         $this->tag clone $this->tag;
  108.     }
  109.     
  110.     /**
  111.      * Redirects function call
  112.      * @param $method Method name
  113.      * @param $param  Array of parameters
  114.      */
  115.     public function __call($method$param)
  116.     {
  117.         if (method_exists($this->tag$method))
  118.         {
  119.             return call_user_func_arrayarray($this->tag$method)$param );
  120.         }
  121.         else
  122.         {
  123.             throw new Exception(AdiantiCoreTranslator::translate("Method ^1 not found"$method.'()'));
  124.         }
  125.     }
  126.     
  127.     /**
  128.      * Set callback for setValue method
  129.      */
  130.     public function setValueCallback($callback)
  131.     {
  132.         $this->valueCallback $callback;
  133.     }
  134.     
  135.     /**
  136.      * Define the field's label
  137.      * @param $label   A string containing the field's label
  138.      */
  139.     public function setLabel($label)
  140.     {
  141.         $this->label $label;
  142.     }
  143.  
  144.     /**
  145.      * Returns the field's label
  146.      */
  147.     public function getLabel()
  148.     {
  149.         return $this->label;
  150.     }
  151.     
  152.     /**
  153.      * Define the field's name
  154.      * @param $name   A string containing the field's name
  155.      */
  156.     public function setName($name)
  157.     {
  158.         $this->name $name;
  159.     }
  160.  
  161.     /**
  162.      * Returns the field's name
  163.      */
  164.     public function getName()
  165.     {
  166.         return $this->name;
  167.     }
  168.     
  169.     /**
  170.      * Define the field's id
  171.      * @param $id A string containing the field's id
  172.      */
  173.     public function setId($id)
  174.     {
  175.         $this->id $id;
  176.     }
  177.  
  178.     /**
  179.      * Returns the field's id
  180.      */
  181.     public function getId()
  182.     {
  183.         return $this->id;
  184.     }
  185.     
  186.     /**
  187.      * Define the field's value
  188.      * @param $value A string containing the field's value
  189.      */
  190.     public function setValue($value)
  191.     {
  192.         $this->value $value;
  193.         
  194.         if (!empty($this->valueCallback&& ($this->valueCallback instanceof Closure))
  195.         {
  196.             $callback $this->valueCallback;
  197.             $callback($this$value);
  198.         }
  199.     }
  200.     
  201.     /**
  202.      * Returns the field's value
  203.      */
  204.     public function getValue()
  205.     {
  206.         return $this->value;
  207.     }
  208.     
  209.     /**
  210.      * Define the name of the form to wich the field is attached
  211.      * @param $name    A string containing the name of the form
  212.      * @ignore-autocomplete on
  213.      */
  214.     public function setFormName($name)
  215.     {
  216.         $this->formName $name;
  217.     }
  218.     
  219.     /**
  220.      * Return the name of the form to wich the field is attached
  221.      */
  222.     public function getFormName()
  223.     {
  224.         return $this->formName;
  225.     }
  226.     
  227.     /**
  228.      * Define the field's tooltip
  229.      * @param $name   A string containing the field's tooltip
  230.      */
  231.     public function setTip($tip)
  232.     {
  233.         $this->tag->{'title'$tip;
  234.     }
  235.     
  236.     /**
  237.      * Return the post data
  238.      */
  239.     public function getPostData()
  240.     {
  241.         if (isset($_POST[$this->name]))
  242.         {
  243.             return $_POST[$this->name];
  244.         }
  245.         else
  246.         {
  247.             return '';
  248.         }
  249.     }
  250.     
  251.     /**
  252.      * Define if the field is editable
  253.      * @param $editable A boolean
  254.      */
  255.     public function setEditable($editable)
  256.     {
  257.         $this->editable$editable;
  258.     }
  259.  
  260.     /**
  261.      * Returns if the field is editable
  262.      * @return boolean
  263.      */
  264.     public function getEditable()
  265.     {
  266.         return $this->editable;
  267.     }
  268.     
  269.     /**
  270.      * Define a field property
  271.      * @param $name  Property Name
  272.      * @param $value Property Value
  273.      */
  274.     public function setProperty($name$value$replace TRUE)
  275.     {
  276.         if ($replace)
  277.         {
  278.             // delegates the property assign to the composed object
  279.             $this->tag->$name $value;
  280.         }
  281.         else
  282.         {
  283.             if ($this->tag->$name)
  284.             {
  285.             
  286.                 // delegates the property assign to the composed object
  287.                 $this->tag->$name $this->tag->$name ';' $value;
  288.             }
  289.             else
  290.             {
  291.                 // delegates the property assign to the composed object
  292.                 $this->tag->$name $value;
  293.             }
  294.         }
  295.         
  296.         $this->properties$name $this->tag->$name;
  297.     }
  298.     
  299.     /**
  300.      * Get properties as string
  301.      */
  302.     public function getPropertiesAsString($filter null)
  303.     {
  304.         $content '';
  305.         
  306.         if ($this->properties)
  307.         {
  308.             foreach ($this->properties as $name => $value)
  309.             {
  310.                 if empty($filter|| ($filter && strpos($name$filter!== false))
  311.                 {
  312.                     $value str_replace('"''&quot;'$value);
  313.                     $content .= " {$name}=\"{$value}\"";
  314.                 }
  315.             }
  316.         }
  317.         
  318.         return $content;
  319.     }
  320.     
  321.     /**
  322.      * Return a field property
  323.      * @param $name  Property Name
  324.      * @param $value Property Value
  325.      */
  326.     public function getProperty($name)
  327.     {
  328.         return $this->tag->$name;
  329.     }
  330.     
  331.     /**
  332.      * Define the Field's width
  333.      * @param $width Field's width in pixels
  334.      */
  335.     public function setSize($width$height NULL)
  336.     {
  337.         $this->size $width;
  338.     }
  339.     
  340.     /**
  341.      * Returns the field size
  342.      */
  343.     public function getSize()
  344.     {
  345.         return $this->size;
  346.     }
  347.     
  348.     /**
  349.      * Add a field validator
  350.      * @param $label Field name
  351.      * @param $validator TFieldValidator object
  352.      * @param $parameters Aditional parameters
  353.      */
  354.     public function addValidation($labelTFieldValidator $validator$parameters NULL)
  355.     {
  356.         $this->validations[array($label$validator$parameters);
  357.         
  358.         if ($validator instanceof TRequiredValidator)
  359.         {
  360.             $this->tag->{'required''';
  361.         }
  362.         
  363.         if ($validator instanceof TEmailValidator)
  364.         {
  365.             $this->tag->{'type''email';
  366.         }
  367.         
  368.         if ($validator instanceof TMinLengthValidator)
  369.         {
  370.             $this->tag->{'minlength'$parameters[0];
  371.         }
  372.         
  373.         if ($validator instanceof TMaxLengthValidator)
  374.         {
  375.             $this->tag->{'maxlength'$parameters[0];
  376.         }
  377.     }
  378.     
  379.     /**
  380.      * Returns field validations
  381.      */
  382.     public function getValidations()
  383.     {
  384.         return $this->validations;
  385.     }
  386.     
  387.     /**
  388.      * Returns if the field is required
  389.      */
  390.     public function isRequired()
  391.     {
  392.         if ($this->validations)
  393.         {
  394.             foreach ($this->validations as $validation)
  395.             {
  396.                 $validator $validation[1];
  397.                 if ($validator instanceof TRequiredValidator)
  398.                 {
  399.                     return TRUE;
  400.                 }
  401.             }
  402.         }
  403.         return FALSE;
  404.     }
  405.     
  406.     /**
  407.      * Validate a field
  408.      */
  409.     public function validate()
  410.     {
  411.         if ($this->validations)
  412.         {
  413.             foreach ($this->validations as $validation)
  414.             {
  415.                 $label      $validation[0];
  416.                 $validator  $validation[1];
  417.                 $parameters $validation[2];
  418.                 
  419.                 $validator->validate($label$this->getValue()$parameters);
  420.             }
  421.         }
  422.     }
  423.     
  424.     /**
  425.      * Converts the object into a string
  426.      */
  427.     public function __toString()
  428.     {
  429.         return $this->getContents();
  430.     }
  431.     
  432.     /**
  433.      * Returns the element content as a string
  434.      */
  435.     public function getContents()
  436.     {
  437.         ob_start();
  438.         $this->show();
  439.         $content ob_get_contents();
  440.         ob_end_clean();
  441.         return $content;
  442.     }
  443.     
  444.     /**
  445.      * Enable the field
  446.      * @param $form_name Form name
  447.      * @param $field Field name
  448.      */
  449.     public static function enableField($form_name$field)
  450.     {
  451.         TScript::create" tfield_enable_field('{$form_name}', '{$field}'); );
  452.     }
  453.     
  454.     /**
  455.      * Disable the field
  456.      * @param $form_name Form name
  457.      * @param $field Field name
  458.      */
  459.     public static function disableField($form_name$field)
  460.     {
  461.         TScript::create" tfield_disable_field('{$form_name}', '{$field}'); );
  462.     }
  463.     
  464.     /**
  465.      * Clear the field
  466.      * @param $form_name Form name
  467.      * @param $field Field name
  468.      */
  469.     public static function clearField($form_name$field)
  470.     {
  471.         TScript::create" tfield_clear_field('{$form_name}', '{$field}'); );
  472.     }
  473. }