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

Documentation is available at TButton.php

  1. <?php
  2. namespace Adianti\Widget\Form;
  3.  
  4. use Adianti\Widget\Form\AdiantiWidgetInterface;
  5. use Adianti\Core\AdiantiCoreTranslator;
  6. use Adianti\Control\TAction;
  7. use Adianti\Widget\Base\TElement;
  8. use Adianti\Widget\Base\TScript;
  9. use Adianti\Widget\Form\TField;
  10. use Adianti\Widget\Form\TLabel;
  11. use Adianti\Widget\Util\TImage;
  12.  
  13. use Exception;
  14.  
  15. /**
  16.  * Button Widget
  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 TButton extends TField implements AdiantiWidgetInterface
  26. {
  27.     private $action;
  28.     private $image;
  29.     private $functions;
  30.     private $tagName;
  31.     protected $properties;
  32.     protected $label;
  33.     protected $formName;
  34.     
  35.     /**
  36.      * Create a button with icon and action
  37.      */
  38.     public static function create($name$callback$label$image)
  39.     {
  40.         $button new TButton$name );
  41.         $button->setAction(new TAction$callback )$label);
  42.         $button->setImage$image );
  43.         return $button;
  44.     }
  45.     
  46.     /**
  47.      * Add CSS class
  48.      */
  49.     public function addStyleClass($class)
  50.     {
  51.         $classes ['btn-primary''btn-secondary''btn-success''btn-danger''btn-warning''btn-info''btn-light''btn-dark''btn-link''btn-default'];
  52.         $found   false;
  53.         
  54.         foreach ($classes as $btnClass)
  55.         {
  56.             if (strpos($class$btnClass!== false)
  57.             {
  58.                 $found true;
  59.             }
  60.         }
  61.         
  62.         $this->{'class''btn '($found  '' 'btn-default ')$class;
  63.     }
  64.     
  65.     /**
  66.      * Define the action of the button
  67.      * @param  $action TAction object
  68.      * @param  $label  Button's label
  69.      */
  70.     public function setAction(TAction $action$label NULL)
  71.     {
  72.         $this->action $action;
  73.         $this->label  $label;
  74.     }
  75.     
  76.     /**
  77.      * Returns the buttona action
  78.      */
  79.     public function getAction()
  80.     {
  81.         return $this->action;
  82.     }
  83.     
  84.     /**
  85.      * Define the tag name
  86.      * @param  $name  tag name
  87.      */
  88.     public function setTagName($name)
  89.     {
  90.         $this->tagName $name;
  91.     }
  92.     
  93.     /**
  94.      * Define the icon of the button
  95.      * @param  $image  image path
  96.      */
  97.     public function setImage($image)
  98.     {
  99.         $this->image $image;
  100.     }
  101.     
  102.     /**
  103.      * Define the label of the button
  104.      * @param  $label button label
  105.      */
  106.     public function setLabel($label)
  107.     {
  108.         $this->label $label;
  109.     }
  110.     
  111.     /**
  112.      * Returns the button label
  113.      */
  114.     public function getLabel()
  115.     {
  116.         return $this->label;
  117.     }
  118.     
  119.     /**
  120.      * Add a JavaScript function to be executed by the button
  121.      * @param $function A piece of JavaScript code
  122.      * @ignore-autocomplete on
  123.      */
  124.     public function addFunction($function)
  125.     {
  126.         if ($function)
  127.         {
  128.             $this->functions $function.';';
  129.         }
  130.     }
  131.     
  132.     /**
  133.      * Define a field property
  134.      * @param $name  Property Name
  135.      * @param $value Property Value
  136.      */
  137.     public function setProperty($name$value$replace TRUE)
  138.     {
  139.         $this->properties[$name$value;
  140.     }
  141.     
  142.     /**
  143.      * Return field property
  144.      */
  145.     public function getProperty($name)
  146.     {
  147.         return isset($this->properties[$name]$this->properties[$namenull;
  148.     }
  149.     
  150.     /**
  151.      * Enable the field
  152.      * @param $form_name Form name
  153.      * @param $field Field name
  154.      */
  155.     public static function enableField($form_name$field)
  156.     {
  157.         TScript::create" tbutton_enable_field('{$form_name}', '{$field}'); );
  158.     }
  159.     
  160.     /**
  161.      * Disable the field
  162.      * @param $form_name Form name
  163.      * @param $field Field name
  164.      */
  165.     public static function disableField($form_name$field)
  166.     {
  167.         TScript::create" tbutton_disable_field('{$form_name}', '{$field}'); );
  168.     }
  169.     
  170.     /**
  171.      * Show the widget at the screen
  172.      */
  173.     public function show()
  174.     {
  175.         if ($this->action)
  176.         {
  177.             if (empty($this->formName))
  178.             {
  179.                 $label ($this->label instanceof TLabel$this->label->getValue($this->label;
  180.                 throw new Exception(AdiantiCoreTranslator::translate('You must pass the ^1 (^2) as a parameter to ^3'__CLASS__$label'TForm::setFields()') );
  181.             }
  182.             
  183.             // get the action as URL
  184.             $url $this->action->serialize(FALSE);
  185.             if ($this->action->isStatic())
  186.             {
  187.                 $url .= '&static=1';
  188.             }
  189.             $url htmlspecialchars($url);
  190.             $wait_message AdiantiCoreTranslator::translate('Loading');
  191.             // define the button's action (ajax post)
  192.             $action "Adianti.waitMessage = '$wait_message';";
  193.             $action.= "{$this->functions}";
  194.             $action.= "__adianti_post_data('{$this->formName}', '{$url}');";
  195.             $action.= "return false;";
  196.                         
  197.             $button new TElement!empty($this->tagName)$this->tagName 'button' );
  198.             $button->{'id'}      'tbutton_'.$this->name;
  199.             $button->{'name'}    $this->name;
  200.             $button->{'class'}   'btn btn-default btn-sm';
  201.             $button->{'onclick'$action;
  202.             $action '';
  203.         }
  204.         else
  205.         {
  206.             $action $this->functions;
  207.             // creates the button using a div
  208.             $button new TElement!empty($this->tagName)$this->tagName 'div' );
  209.             $button->{'id'}      'tbutton_'.$this->name;
  210.             $button->{'name'}    $this->name;
  211.             $button->{'class'}   'btn btn-default btn-sm';
  212.             $button->{'onclick'$action;
  213.         }
  214.         
  215.         if ($this->properties)
  216.         {
  217.             foreach ($this->properties as $property => $value)
  218.             {
  219.                 $button->$property $value;
  220.             }
  221.         }
  222.  
  223.         $span new TElement('span');
  224.         if ($this->image)
  225.         {
  226.             $image new TImage($this->image);
  227.             if (!empty($this->label))
  228.             {
  229.                 $image->{'style'.= ';padding-right:4px';
  230.             }
  231.             $span->add($image);
  232.         }
  233.         
  234.         if ($this->label)
  235.         {
  236.             $span->add($this->label);
  237.             $button->{'aria-label'$this->label;
  238.         }
  239.         
  240.         $button->add($span);
  241.         $button->show();
  242.     }
  243. }