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

Documentation is available at TQuickForm.php

  1. <?php
  2. namespace Adianti\Widget\Wrapper;
  3.  
  4. use Adianti\Widget\Form\AdiantiWidgetInterface;
  5. use Adianti\Core\AdiantiCoreTranslator;
  6. use Adianti\Control\TAction;
  7. use Adianti\Widget\Base\TScript;
  8. use Adianti\Widget\Form\TForm;
  9. use Adianti\Widget\Form\TLabel;
  10. use Adianti\Widget\Form\THidden;
  11. use Adianti\Widget\Form\TButton;
  12. use Adianti\Widget\Form\TCheckGroup;
  13. use Adianti\Widget\Form\TRadioGroup;
  14. use Adianti\Widget\Container\TTable;
  15. use Adianti\Widget\Container\THBox;
  16. use Adianti\Validator\TFieldValidator;
  17. use Adianti\Validator\TRequiredValidator;
  18.  
  19. use Exception;
  20.  
  21. /**
  22.  * Create quick forms for input data with a standard container for elements
  23.  *
  24.  * @version    7.4
  25.  * @package    widget
  26.  * @subpackage wrapper
  27.  * @author     Pablo Dall'Oglio
  28.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  29.  * @license    http://www.adianti.com.br/framework-license
  30.  */
  31. class TQuickForm extends TForm
  32. {
  33.     protected $fields// array containing the form fields
  34.     protected $name;   // form name
  35.     protected $actionButtons;
  36.     protected $inputRows;
  37.     protected $currentRow;
  38.     protected $table;
  39.     protected $actionsContainer;
  40.     protected $hasAction;
  41.     protected $fieldsByRow;
  42.     protected $titleCell;
  43.     protected $actionCell;
  44.     protected $fieldPositions;
  45.     protected $client_validation;
  46.     
  47.     /**
  48.      * Class Constructor
  49.      * @param $name Form Name
  50.      */
  51.     public function __construct($name 'my_form')
  52.     {
  53.         parent::__construct($name);
  54.         
  55.         // creates a table
  56.         $this->table = new TTable;
  57.         $this->hasAction = FALSE;
  58.         $this->client_validation = FALSE;
  59.         
  60.         $this->fieldsByRow = 1;
  61.         
  62.         $this->setProperty('novalidate','');
  63.         
  64.         // add the table to the form
  65.         parent::add($this->table);
  66.     }
  67.     
  68.     /**
  69.      * Turn on/off client validation
  70.      */
  71.     public function setClientValidation($bool)
  72.     {
  73.         if ($bool)
  74.         {
  75.             $this->unsetProperty('novalidate');
  76.         }
  77.         else
  78.         {
  79.             $this->setProperty('novalidate','');
  80.         }
  81.     }
  82.     
  83.     /**
  84.      * Returns the actions container
  85.      */
  86.     public function getActionsContainer()
  87.     {
  88.         return $this->actionsContainer;
  89.     }
  90.     
  91.     /**
  92.      * Returns the inner table
  93.      */
  94.     public function getTable()
  95.     {
  96.         return $this->table;
  97.     }
  98.     
  99.     /**
  100.      * Define the field quantity per row
  101.      * @param $count Field count
  102.      */
  103.     public function setFieldsByRow($count)
  104.     {
  105.         if (is_int($countAND $count >=AND $count <=3)
  106.         {
  107.             $this->fieldsByRow = $count;
  108.             if (!empty($this->titleCell))
  109.             {
  110.                 $this->titleCell->{'colspan'}  $this->fieldsByRow;
  111.             }
  112.             if (!empty($this->actionCell))
  113.             {
  114.                 $this->actionCell->{'colspan'$this->fieldsByRow;
  115.             }
  116.         }
  117.         else
  118.         {
  119.             throw new Exception(AdiantiCoreTranslator::translate('The method (^1) just accept values of type ^2 between ^3 and ^4'__METHOD__'integer'13));
  120.         }
  121.     }
  122.     
  123.     /**
  124.      * Return the fields by row count
  125.      */
  126.     public function getFieldsByRow()
  127.     {
  128.         return $this->fieldsByRow;
  129.     }
  130.     
  131.     /**
  132.      * Intercepts whenever someones assign a new property's value
  133.      * @param $name     Property Name
  134.      * @param $value    Property Value
  135.      */
  136.     public function __set($name$value)
  137.     {
  138.         if ($name == 'class')
  139.         {
  140.             $this->table->{'width''100%';
  141.         }
  142.         
  143.         if (method_exists('TForm''__set'))
  144.         {
  145.             parent::__set($name$value);
  146.         }
  147.     }
  148.     
  149.     /**
  150.      * Returns the form container
  151.      */
  152.     public function getContainer()
  153.     {
  154.         return $this->table;
  155.     }
  156.     
  157.     /**
  158.      * Add a form title
  159.      * @param $title     Form title
  160.      */
  161.     public function setFormTitle($title)
  162.     {
  163.         // add the field to the container
  164.         $row $this->table->addRow();
  165.         $row->{'class''tformtitle';
  166.         $this->table->{'width''100%';
  167.         $this->titleCell $row->addCellnew TLabel($title) );
  168.         $this->titleCell->{'colspan'$this->fieldsByRow;
  169.     }
  170.     
  171.     /**
  172.      * Returns the input groups
  173.      */
  174.     public function getInputRows()
  175.     {
  176.         return $this->inputRows;
  177.     }
  178.     
  179.     /**
  180.      * Add a form field
  181.      * @param $label     Field Label
  182.      * @param $object    Field Object
  183.      * @param $size      Field Size
  184.      * @param $validator Field Validator
  185.      */
  186.     public function addQuickField($labelAdiantiWidgetInterface $object$size 200TFieldValidator $validator NULL$label_size NULL)
  187.     {
  188.         if ($size && !$object instanceof TRadioGroup && !$object instanceof TCheckGroup)
  189.         {
  190.             $object->setSize($size);
  191.         }
  192.         parent::addField($object);
  193.         
  194.         if ($label instanceof TLabel)
  195.         {
  196.             $label_field $label;
  197.             $label_value $label->getValue();
  198.         }
  199.         else
  200.         {
  201.             $label_field new TLabel($label);
  202.             $label_value $label;
  203.         }
  204.         
  205.         $object->setLabel($label_value);
  206.         
  207.         if empty($this->currentRowOR $this->fieldPositions $this->fieldsByRow == )
  208.         {
  209.             // add the field to the container
  210.             $this->currentRow $this->table->addRow();
  211.             $this->currentRow->{'class''tformrow';
  212.         }
  213.         $row $this->currentRow;
  214.         
  215.         if ($validator instanceof TRequiredValidator)
  216.         {
  217.             $label_field->setFontColor('#FF0000');
  218.         }
  219.         
  220.         if ($label_size)
  221.         {
  222.             $label_field->setSize($label_size);
  223.         }
  224.         if ($object instanceof THidden)
  225.         {
  226.             $row->addCell'' );
  227.             $row->{'style''display:none';
  228.         }
  229.         else
  230.         {
  231.             $cell $row->addCell$label_field );
  232.             $cell->{'width''30%';
  233.         }
  234.         $row->addCell$object );
  235.         
  236.         if ($validator)
  237.         {
  238.             $object->addValidation($label_value$validator);
  239.         }
  240.         
  241.         $this->inputRows[array($label_fieldarray($object)$validator instanceof TRequiredValidator$row);
  242.         $this->fieldPositions ++;
  243.         return $row;
  244.     }
  245.     
  246.     /**
  247.      * Add a form field
  248.      * @param $label     Field Label
  249.      * @param $objects   Array of Objects
  250.      * @param $required  Boolean TRUE if required
  251.      */
  252.     public function addQuickFields($label$objects$required FALSE)
  253.     {
  254.         if empty($this->currentRowOR $this->fieldPositions $this->fieldsByRow == )
  255.         {
  256.             // add the field to the container
  257.             $this->currentRow $this->table->addRow();
  258.             $this->currentRow->{'class''tformrow';
  259.         }
  260.         $row $this->currentRow;
  261.         
  262.         if ($label instanceof TLabel)
  263.         {
  264.             $label_field $label;
  265.             $label_value $label->getValue();
  266.         }
  267.         else
  268.         {
  269.             $label_field new TLabel($label);
  270.             $label_value $label;
  271.         }
  272.         
  273.         if ($required)
  274.         {
  275.             $label_field->setFontColor('#FF0000');
  276.         }
  277.         
  278.         $row->addCell$label_field );
  279.         
  280.         $hbox new THBox;
  281.         foreach ($objects as $object)
  282.         {
  283.             parent::addField($object);
  284.             
  285.             if (!$object instanceof TButton)
  286.             {
  287.                 $object->setLabel($label_value);
  288.             }
  289.             $hbox->add($object);
  290.         }
  291.         $row->addCell$hbox );
  292.         
  293.         $this->fieldPositions ++;
  294.         
  295.         $this->inputRows[array($label_field$objects$required$row);
  296.         return $row;
  297.     }
  298.     
  299.     /**
  300.      * Add a form action
  301.      * @param $label  Action Label
  302.      * @param $action TAction Object
  303.      * @param $icon   Action Icon
  304.      */
  305.     public function addQuickAction($labelTAction $action$icon 'fa:save')
  306.     {
  307.         $name   'btn_'.strtolower(str_replace(' ''_'$label));
  308.         $button new TButton($name);
  309.         parent::addField($button);
  310.         
  311.         // define the button action
  312.         $button->setAction($action$label);
  313.         $button->setImage($icon);
  314.         
  315.         if (!$this->hasAction)
  316.         {
  317.             $this->actionsContainer new THBox;
  318.             
  319.             $row  $this->table->addRow();
  320.             $row->{'class''tformaction';
  321.             $this->actionCell $row->addCell$this->actionsContainer );
  322.             $this->actionCell->{'colspan'$this->fieldsByRow;
  323.         }
  324.         
  325.         // add cell for button
  326.         $this->actionsContainer->add($button);
  327.         
  328.         $this->hasAction TRUE;
  329.         $this->actionButtons[$button;
  330.         
  331.         return $button;
  332.     }
  333.     
  334.     /**
  335.      * Add a form button
  336.      * @param $label  Action Label
  337.      * @param $action Javascript action
  338.      * @param $icon   Action Icon
  339.      */
  340.     public function addQuickButton($label$action$icon 'fa:save')
  341.     {
  342.         $name   strtolower(str_replace(' ''_'$label));
  343.         $button new TButton($name);
  344.         parent::addField($button);
  345.         
  346.         // define the button action
  347.         $button->addFunction($action);
  348.         $button->setLabel($label);
  349.         $button->setImage($icon);
  350.         
  351.         if (!$this->hasAction)
  352.         {
  353.             $this->actionsContainer new THBox;
  354.             
  355.             $row  $this->table->addRow();
  356.             $row->{'class''tformaction';
  357.             $this->actionCell $row->addCell$this->actionsContainer );
  358.             $this->actionCell->{'colspan'$this->fieldsByRow;
  359.         }
  360.         
  361.         // add cell for button
  362.         $this->actionsContainer->add($button);
  363.         $this->hasAction TRUE;
  364.         
  365.         return $button;
  366.     }
  367.     
  368.     /**
  369.      * Clear actions row
  370.      */
  371.     public function delActions()
  372.     {
  373.         if ($this->actionsContainer)
  374.         {
  375.             foreach ($this->actionButtons as $key => $button)
  376.             {
  377.                 parent::delField($button);
  378.                 unset($this->actionButtons[$key]);
  379.             }
  380.             $this->actionsContainer->clearChildren();
  381.         }
  382.     }
  383.     
  384.     /**
  385.      * Return an array with action buttons
  386.      */
  387.     public function getActionButtons()
  388.     {
  389.         return $this->actionButtons;
  390.     }
  391.     
  392.     /**
  393.      * Detach action buttons
  394.      */
  395.     public function detachActionButtons()
  396.     {
  397.         $buttons $this->getActionButtons();
  398.         $this->delActions();
  399.         return $buttons;
  400.     }
  401.     
  402.     /**
  403.      * Add a row
  404.      */
  405.     public function addRow()
  406.     {
  407.         return $this->table->addRow();
  408.     }
  409.     
  410.     /**
  411.      *
  412.      */
  413.     public static function showField($form$field$speed 0)
  414.     {
  415.         TScript::create("tform_show_field('{$form}', '{$field}', {$speed})");
  416.     }
  417.     
  418.     /**
  419.      *
  420.      */
  421.     public static function hideField($form$field$speed 0)
  422.     {
  423.         TScript::create("tform_hide_field('{$form}', '{$field}', {$speed})");
  424.     }
  425. }