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 TForm.php

Documentation is available at TForm.php

  1. <?php
  2. namespace Adianti\Widget\Form;
  3.  
  4. use Adianti\Widget\Form\AdiantiWidgetInterface;
  5. use Adianti\Widget\Base\TElement;
  6. use Adianti\Widget\Base\TScript;
  7. use Adianti\Widget\Form\TField;
  8. use Adianti\Widget\Form\TButton;
  9. use Adianti\Core\AdiantiCoreTranslator;
  10. use Adianti\Util\AdiantiStringConversion;
  11.  
  12. use Exception;
  13. use ReflectionClass;
  14.  
  15. /**
  16.  * Wrapper class to deal with forms
  17.  *
  18.  * @version    7.4
  19.  * @package    widget
  20.  * @subpackage form
  21.  * @author     Pablo Dall'Oglio
  22.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  23.  * @license    http://www.adianti.com.br/framework-license
  24.  */
  25. class TForm implements AdiantiFormInterface
  26. {
  27.     protected $fields// array containing the form fields
  28.     protected $name;   // form name
  29.     protected $children;
  30.     protected $js_function;
  31.     protected $element;
  32.     protected $silent_fields;
  33.     private static $forms;
  34.     
  35.     /**
  36.      * Class Constructor
  37.      * @param $name Form Name
  38.      */
  39.     public function __construct($name 'my_form')
  40.     {
  41.         if ($name)
  42.         {
  43.             $this->setName($name);
  44.         }
  45.         $this->children = [];
  46.         $this->silent_fields = [];
  47.         $this->element  = new TElement('form');
  48.     }
  49.     
  50.     /**
  51.      * Change tag name
  52.      */
  53.     public function setTagName($name)
  54.     {
  55.         $this->element->setName($name);
  56.     }
  57.     
  58.     /**
  59.      * Intercepts whenever someones assign a new property's value
  60.      * @param $name     Property Name
  61.      * @param $value    Property Value
  62.      */
  63.     public function __set($name$value)
  64.     {
  65.         $rc new ReflectionClass$this );
  66.         $classname $rc->getShortName();
  67.         
  68.         if (in_array($classnamearray('TForm''TQuickForm''TQuickNotebookForm')))
  69.         {
  70.             // objects and arrays are not set as properties
  71.             if (is_scalar($value))
  72.             {              
  73.                 // store the property's value
  74.                 $this->element->$name $value;
  75.             }
  76.         }
  77.         else
  78.         {
  79.             $this->$name $value;
  80.         }
  81.     }
  82.     
  83.     /**
  84.      * Silent field
  85.      */
  86.     public function silentField($name)
  87.     {
  88.         $this->silent_fields[$name;
  89.     }
  90.     
  91.     /**
  92.      * Define a form property
  93.      * @param $name  Property Name
  94.      * @param $value Property Value
  95.      */
  96.     public function setProperty($name$value$replace TRUE)
  97.     {
  98.         if ($replace)
  99.         {
  100.             // delegates the property assign to the composed object
  101.             $this->element->$name $value;
  102.         }
  103.         else
  104.         {
  105.             if ($this->element->$name)
  106.             {
  107.             
  108.                 // delegates the property assign to the composed object
  109.                 $this->element->$name $this->element->$name ';' $value;
  110.             }
  111.             else
  112.             {
  113.                 // delegates the property assign to the composed object
  114.                 $this->element->$name $value;
  115.             }
  116.         }
  117.     }
  118.     
  119.     /**
  120.      * Unset form property
  121.      */
  122.     public function unsetProperty($name)
  123.     {
  124.         unset($this->element->$name);
  125.     }
  126.     
  127.     /**
  128.      * Returns the form object by its name
  129.      */
  130.     public static function getFormByName($name)
  131.     {
  132.         if (isset(self::$forms[$name]))
  133.         {
  134.             return self::$forms[$name];
  135.         }
  136.     }
  137.     
  138.     /**
  139.      * Define the form name
  140.      * @param $name A string containing the form name
  141.      */
  142.     public function setName($name)
  143.     {
  144.         $this->name = $name;
  145.         // register this form
  146.         self::$forms[$this->name$this;
  147.     }
  148.     
  149.     /**
  150.      * Returns the form name
  151.      */
  152.     public function getName()
  153.     {
  154.         return $this->name;
  155.     }
  156.     
  157.     /**
  158.      * Send data for a form located in the parent window
  159.      * @param $form_name  Form Name
  160.      * @param $object     An Object containing the form data
  161.      */
  162.     public static function sendData($form_name$object$aggregate FALSE$fireEvents TRUE$timeout 0)
  163.     {
  164.         $fire_param $fireEvents 'true' 'false';
  165.         // iterate the object properties
  166.         if ($object)
  167.         {
  168.             foreach ($object as $field => $value)
  169.             {
  170.                 if (is_array($value))
  171.                 {
  172.                     $value json_encode($value);
  173.                 }
  174.                 
  175.                 $value addslashes((string) $value);
  176.                 $value AdiantiStringConversion::assureUnicode($value);
  177.                 
  178.                 $value str_replace(array("\n""\r")array'\n''\r')$value );
  179.                 
  180.                 // send the property value to the form
  181.                 if ($aggregate)
  182.                 {
  183.                     TScript::create" tform_send_data_aggregate('{$form_name}', '{$field}', '$value', $fire_param); );
  184.                 }
  185.                 else
  186.                 {
  187.                     TScript::create" tform_send_data('{$form_name}', '{$field}', '$value', $fire_param, '$timeout'); );
  188.                     TScript::create" tform_send_data_by_id('{$form_name}', '{$field}', '$value', $fire_param, '$timeout'); );
  189.                 }
  190.             }
  191.         }
  192.     }
  193.     
  194.     /**
  195.      * Define if the form will be editable
  196.      * @param $bool A Boolean
  197.      */
  198.     public function setEditable($bool)
  199.     {
  200.         if ($this->fields)
  201.         {
  202.             foreach ($this->fields as $object)
  203.             {
  204.                 $object->setEditable($bool);
  205.             }
  206.         }
  207.     }
  208.     
  209.     /**
  210.      * Add a Form Field
  211.      * @param $field Object
  212.      */
  213.     public function addField(AdiantiWidgetInterface $field)
  214.     {
  215.         $name $field->getName();
  216.         if (isset($this->fields[$name]AND substr($name,-2!== '[]')
  217.         {
  218.             throw new Exception(AdiantiCoreTranslator::translate('You have already added a field called "^1" inside the form'$name));
  219.         }
  220.         
  221.         if ($name)
  222.         {
  223.             $this->fields[$name$field;
  224.             $field->setFormName($this->name);
  225.             
  226.             if ($field instanceof TButton)
  227.             {
  228.                 $field->addFunction($this->js_function);
  229.             }
  230.         }
  231.     }
  232.     
  233.     /**
  234.      * Remove a form field
  235.      * @param $field Object
  236.      */
  237.     public function delField(AdiantiWidgetInterface $field)
  238.     {
  239.         if ($this->fields)
  240.         {
  241.             foreach($this->fields as $name => $object)
  242.             {
  243.                 if ($field === $object)
  244.                 {
  245.                     unset($this->fields[$name]);
  246.                 }
  247.             }
  248.         }
  249.     }
  250.     
  251.     /**
  252.      * Remove all form fields
  253.      */
  254.     public function delFields()
  255.     {
  256.         $this->fields = array();
  257.     }
  258.     
  259.     /**
  260.      * Define wich are the form fields
  261.      * @param $fields An array containing a collection of TField objects
  262.      */
  263.     public function setFields($fields)
  264.     {
  265.         if (is_array($fields))
  266.         {
  267.             $this->fields = array();
  268.             $this->js_function = '';
  269.             // iterate the form fields
  270.             foreach ($fields as $field)
  271.             {
  272.                 $this->addField($field);
  273.             }
  274.         }
  275.         else
  276.         {
  277.             throw new Exception(AdiantiCoreTranslator::translate('Method ^1 must receive a parameter of type ^2'__METHOD__'Array'));
  278.         }
  279.     }
  280.     
  281.     /**
  282.      * Returns a form field by its name
  283.      * @param $name  A string containing the field's name
  284.      * @return       The Field object
  285.      */
  286.     public function getField($name)
  287.     {
  288.         if (isset($this->fields[$name]))
  289.         {
  290.             return $this->fields[$name];
  291.         }
  292.     }
  293.     
  294.     /**
  295.      * Returns an array with the form fields
  296.      * @return Array of form fields
  297.      */
  298.     public function getFields()
  299.     {
  300.         return $this->fields;
  301.     }
  302.     
  303.     /**
  304.      * clear the form Data
  305.      */
  306.     public function clear($keepDefaults FALSE)
  307.     {
  308.         // iterate the form fields
  309.         foreach ($this->fields as $name => $field)
  310.         {
  311.             // labels don't have name
  312.             if ($name AND !$keepDefaults)
  313.             {
  314.                 $field->setValue(NULL);
  315.             }
  316.         }
  317.     }
  318.     
  319.     /**
  320.      * Define the data of the form
  321.      * @param $object An Active Record object
  322.      */
  323.     public function setData($object)
  324.     {
  325.         // iterate the form fields
  326.         foreach ($this->fields as $name => $field)
  327.         {
  328.             $name str_replace(['[',']']['','']$name);
  329.             
  330.             if ($name// labels don't have name
  331.             {
  332.                 if (isset($object->$name))
  333.                 {
  334.                     $field->setValue($object->$name);
  335.                 }
  336.             }
  337.         }
  338.     }
  339.     
  340.     /**
  341.      * Returns the form POST data as an object
  342.      * @param $class A string containing the class for the returning object
  343.      */
  344.     public function getData($class 'StdClass')
  345.     {
  346.         if (!class_exists($class))
  347.         {
  348.             throw new Exception(AdiantiCoreTranslator::translate('Class ^1 not found in ^2'$class__METHOD__));
  349.         }
  350.         
  351.         $object new $class;
  352.         foreach ($this->fields as $key => $fieldObject)
  353.         {
  354.             $key str_replace(['[',']']['','']$key);
  355.             
  356.             if (!$fieldObject instanceof TButton && !in_array($key$this->silent_fields))
  357.             {
  358.                 $object->$key $fieldObject->getPostData();
  359.             }
  360.         }
  361.         
  362.         return $object;
  363.     }
  364.     
  365.     /**
  366.      * Returns the form start values as an object
  367.      * @param $class A string containing the class for the returning object
  368.      */
  369.     public function getValues($class 'StdClass'$withOptions false)
  370.     {
  371.         if (!class_exists($class))
  372.         {
  373.             throw new Exception(AdiantiCoreTranslator::translate('Class ^1 not found in ^2'$class__METHOD__));
  374.         }
  375.         
  376.         $object new $class;
  377.         if ($this->fields)
  378.         {
  379.             foreach ($this->fields as $key => $field)
  380.             {
  381.                 $key str_replace(['[',']']['','']$key);
  382.                 
  383.                 if (!$field instanceof TButton)
  384.                 {
  385.                     if ($withOptions AND method_exists($field'getItems'))
  386.                     {
  387.                         $items $field->getItems();
  388.                         
  389.                         if (is_array($field->getValue()))
  390.                         {
  391.                             $value [];
  392.                             foreach ($field->getValue(as $field_value)
  393.                             {
  394.                                 if ($field_value)
  395.                                 {
  396.                                     $value[$items[$field_value];
  397.                                 }
  398.                             }
  399.                             $object->$key $value;
  400.                         }
  401.                     }
  402.                     else
  403.                     {
  404.                         $object->$key $field->getValue();
  405.                     }
  406.                 }
  407.             }
  408.         }
  409.         
  410.         return $object;
  411.     }
  412.     
  413.     /**
  414.      * Validate form
  415.      */
  416.     public function validate()
  417.     {
  418.         // assign post data before validation
  419.         // validation exception would prevent
  420.         // the user code to execute setData()
  421.         $this->setData($this->getData());
  422.         
  423.         $errors array();
  424.         foreach ($this->fields as $fieldObject)
  425.         {
  426.             try
  427.             {
  428.                 $fieldObject->validate();
  429.             }
  430.             catch (Exception $e)
  431.             {
  432.                 $errors[$e->getMessage('.';
  433.             }
  434.         }
  435.         
  436.         if (count($errors0)
  437.         {
  438.             throw new Exception(implode("<br>"$errors));
  439.         }
  440.     }
  441.     
  442.     /**
  443.      * Add a container to the form (usually a table or panel)
  444.      * @param $object Any Object that implements the show() method
  445.      */
  446.     public function add($object)
  447.     {
  448.         if (!in_array($object$this->children))
  449.         {
  450.             $this->children[$object;
  451.         }
  452.     }
  453.     
  454.     /**
  455.      * Pack a container to the form (usually a table or panel)
  456.      * @param mixed $object, ...Any Object that implements the show() method
  457.      */
  458.     public function pack()
  459.     {
  460.         $this->children = func_get_args();
  461.     }
  462.     
  463.     /**
  464.      * Returns the child object
  465.      */
  466.     public function getChild()
  467.     {
  468.         return $this->children[0];
  469.     }
  470.     
  471.     /**
  472.      * Shows the form at the screen
  473.      */
  474.     public function show()
  475.     {
  476.         // define form properties
  477.         $this->element->{'enctype'"multipart/form-data";
  478.         $this->element->{'name'}    $this->name// form name
  479.         $this->element->{'id'}      $this->name// form id
  480.         $this->element->{'method'}  'post';      // transfer method
  481.         
  482.         // add the container to the form
  483.         if (isset($this->children))
  484.         {
  485.             foreach ($this->children as $child)
  486.             {
  487.                 $this->element->add($child);
  488.             }
  489.         }
  490.         // show the form
  491.         $this->element->show();
  492.     }
  493. }