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

Documentation is available at TEntry.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\Util\TImage;
  9. use Adianti\Control\TAction;
  10. use Adianti\Core\AdiantiCoreTranslator;
  11. use Exception;
  12.  
  13. /**
  14.  * Entry Widget
  15.  *
  16.  * @version    7.4
  17.  * @package    widget
  18.  * @subpackage form
  19.  * @author     Pablo Dall'Oglio
  20.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  21.  * @license    http://www.adianti.com.br/framework-license
  22.  */
  23. class TEntry extends TField implements AdiantiWidgetInterface
  24. {
  25.     private $mask;
  26.     protected $completion;
  27.     protected $numericMask;
  28.     protected $decimals;
  29.     protected $decimalsSeparator;
  30.     protected $thousandSeparator;
  31.     protected $reverse;
  32.     protected $allowNegative;
  33.     protected $replaceOnPost;
  34.     protected $exitFunction;
  35.     protected $exitAction;
  36.     protected $id;
  37.     protected $formName;
  38.     protected $name;
  39.     protected $value;
  40.     protected $minLength;
  41.     protected $delimiter;
  42.     protected $exitOnEnterOn;
  43.     protected $innerIcon;
  44.     
  45.     /**
  46.      * Class Constructor
  47.      * @param  $name name of the field
  48.      */
  49.     public function __construct($name)
  50.     {
  51.         parent::__construct($name);
  52.         $this->id   = 'tentry_' mt_rand(10000000001999999999);
  53.         $this->numericMask = FALSE;
  54.         $this->replaceOnPost = FALSE;
  55.         $this->minLength = 1;
  56.         $this->exitOnEnterOn = FALSE;
  57.         $this->tag->{'type'}   'text';
  58.         $this->tag->{'widget''tentry';
  59.     }
  60.     
  61.     /**
  62.      * Define input type
  63.      */
  64.     public function setInputType($type)
  65.     {
  66.         $this->tag->{'type'}  $type;
  67.     }
  68.     
  69.     /**
  70.      * Define the Inner icon
  71.      */
  72.     public function setInnerIcon(TImage $image$side 'right')
  73.     {
  74.         $this->innerIcon $image;
  75.         $this->innerIcon->{'class'.= ' input-inner-icon ' $side;
  76.         
  77.         if ($side == 'left')
  78.         {
  79.             $this->setProperty('style'"padding-left:23px"false)//aggregate style info
  80.         }
  81.     }
  82.     
  83.     /**
  84.      * Turn on exit on enter
  85.      */
  86.     public function exitOnEnter()
  87.     {
  88.         $this->exitOnEnterOn true;
  89.     }
  90.     
  91.     /**
  92.      * Define the field's mask
  93.      * @param $mask A mask for input data
  94.      */
  95.     public function setMask($mask$replaceOnPost FALSE)
  96.     {
  97.         $this->mask $mask;
  98.         $this->replaceOnPost $replaceOnPost;
  99.     }
  100.     
  101.     /**
  102.      * Define the field's numeric mask (available just in web)
  103.      * @param $decimals Sets the number of decimal points.
  104.      * @param $decimalsSeparator Sets the separator for the decimal point.
  105.      * @param $thousandSeparator Sets the thousands separator.
  106.      * @param $allowNegative Sets negative allowed.
  107.      */
  108.     public function setNumericMask($decimals$decimalsSeparator$thousandSeparator$replaceOnPost FALSE$reverse FALSE$allowNegative TRUE)
  109.     {
  110.         if (empty($decimalsSeparator))
  111.         {
  112.             $decimals 0;
  113.         }
  114.         else if (empty($decimals))
  115.         {
  116.             $decimalsSeparator '';
  117.         }
  118.         
  119.         $this->setProperty('style'"text-align:right;"false)//aggregate style info
  120.         $this->numericMask TRUE;
  121.         $this->decimals $decimals;
  122.         $this->reverse $reverse;
  123.         $this->allowNegative $allowNegative;
  124.         $this->decimalsSeparator $decimalsSeparator;
  125.         $this->thousandSeparator $thousandSeparator;
  126.         $this->replaceOnPost $replaceOnPost;
  127.         
  128.         $dec_pattern $decimalsSeparator == '.' '\\.' $decimalsSeparator;
  129.         $tho_pattern $thousandSeparator == '.' '\\.' $thousandSeparator;
  130.         
  131.         //$this->tag->{'pattern'}   = '^\\$?(([1-9](\\d*|\\d{0,2}('.$tho_pattern.'\\d{3})*))|0)('.$dec_pattern.'\\d{1,2})?$';
  132.         $this->tag->{'pattern'}   '^\\$?(([1-9](\\d*|\\d{0,'.$decimals.'}('.$tho_pattern.'\\d{3})*))|0)('.$dec_pattern.'\\d{1,'.$decimals.'})?$';
  133.         $this->tag->{'inputmode''numeric';
  134.         $this->tag->{'data-nmask'}  $decimals.$decimalsSeparator.$thousandSeparator;
  135.     }
  136.     
  137.     /**
  138.      * Define the field's value
  139.      * @param $value A string containing the field's value
  140.      */
  141.     public function setValue($value)
  142.     {
  143.         if ($this->replaceOnPost)
  144.         {
  145.             if ($this->numericMask && is_numeric($value))
  146.             {
  147.                 parent::setValue(number_format($value$this->decimals$this->decimalsSeparator$this->thousandSeparator));
  148.             }
  149.             else if ($this->mask)
  150.             {
  151.                 parent::setValue($this->formatMask($this->mask$value));
  152.             }
  153.             else
  154.             {
  155.                 parent::setValue($value);
  156.             }
  157.         }
  158.         else
  159.         {
  160.             parent::setValue($value);
  161.         }
  162.     }
  163.     
  164.     /**
  165.      * Return the post data
  166.      */
  167.     public function getPostData()
  168.     {
  169.         $name str_replace(['[',']']['','']$this->name);
  170.         
  171.         if (isset($_POST[$name]))
  172.         {
  173.             if ($this->replaceOnPost)
  174.             {
  175.                 $value $_POST[$name];
  176.                 
  177.                 if ($this->numericMask)
  178.                 {
  179.                     $value str_replace$this->thousandSeparator''$value);
  180.                     $value str_replace$this->decimalsSeparator'.'$value);
  181.                     return $value;
  182.                 }
  183.                 else if ($this->mask)
  184.                 {
  185.                     return preg_replace('/[^a-z\d]+/i'''$value);
  186.                 }
  187.                 else
  188.                 {
  189.                     return $value;
  190.                 }
  191.             }
  192.             else
  193.             {
  194.                 return $_POST[$name];
  195.             }
  196.         }
  197.         else
  198.         {
  199.             return '';
  200.         }
  201.     }
  202.     
  203.     /**
  204.      * Define max length
  205.      * @param  $length Max length
  206.      */
  207.     public function setMaxLength($length)
  208.     {
  209.         if ($length 0)
  210.         {
  211.             $this->tag->{'maxlength'$length;
  212.         }
  213.     }
  214.     
  215.     /**
  216.      * Define options for completion
  217.      * @param $options array of options for completion
  218.      */
  219.     function setCompletion($options)
  220.     {
  221.         $this->completion $options;
  222.     }
  223.     
  224.     /**
  225.      * Define the action to be executed when the user leaves the form field
  226.      * @param $action TAction object
  227.      */
  228.     function setExitAction(TAction $action)
  229.     {
  230.         if ($action->isStatic())
  231.         {
  232.             $this->exitAction $action;
  233.         }
  234.         else
  235.         {
  236.             $string_action $action->toString();
  237.             throw new Exception(AdiantiCoreTranslator::translate('Action (^1) must be static to be used in ^2'$string_action__METHOD__));
  238.         }
  239.     }
  240.     
  241.     /**
  242.      * Define the javascript function to be executed when the user leaves the form field
  243.      * @param $function Javascript function
  244.      */
  245.     public function setExitFunction($function)
  246.     {
  247.         $this->exitFunction $function;
  248.     }
  249.     
  250.     /**
  251.      * Force lower case
  252.      */
  253.     public function forceLowerCase()
  254.     {
  255.         $this->tag->{'onKeyPress'"return tentry_lower(this)";
  256.         $this->tag->{'onBlur'"return tentry_lower(this)";
  257.         $this->tag->{'forcelower'"1";
  258.         $this->setProperty('style'"text-transform: lowercase;"false)//aggregate style info
  259.         
  260.     }
  261.     
  262.     /**
  263.      * Force upper case
  264.      */
  265.     public function forceUpperCase()
  266.     {
  267.         $this->tag->{'onKeyPress'"return tentry_upper(this)";
  268.         $this->tag->{'onBlur'"return tentry_upper(this)";
  269.         $this->tag->{'forceupper'"1";
  270.         $this->setProperty('style'"text-transform: uppercase;"false)//aggregate style info
  271.     }
  272.     
  273.     /**
  274.      * Set autocomplete delimiter
  275.      * @param $delimiter autocomplete delimiter
  276.      */
  277.     public function setDelimiter($delimiter)
  278.     {
  279.         $this->delimiter $delimiter;
  280.     }
  281.     
  282.     /**
  283.      * Define the minimum length for search
  284.      */
  285.     public function setMinLength($length)
  286.     {
  287.         $this->minLength $length;
  288.     }
  289.     
  290.     /**
  291.      * Reload completion
  292.      * 
  293.      * @param $field Field name or id
  294.      * @param $options array of options for autocomplete
  295.      */
  296.     public static function reloadCompletion($field$list$options null)
  297.     {
  298.         $list_json json_encode($list);
  299.         if (is_null($options))
  300.         {
  301.             $options [];
  302.         }
  303.         
  304.         $options_json json_encode$options );
  305.         TScript::create(" tentry_autocomplete_by_name( '{$field}', {$list_json}, '{$options_json}'); ");
  306.     }
  307.     
  308.     /**
  309.      * Apply mask
  310.      * 
  311.      * @param $mask  Mask
  312.      * @param $value Value
  313.      */
  314.     protected function formatMask($mask$value)
  315.     {
  316.         if ($value)
  317.         {
  318.             $value_index  0;
  319.             $clear_result '';
  320.         
  321.             $value preg_replace('/[^a-z\d]+/i'''$value);
  322.             
  323.             for ($mask_index=0$mask_index strlen($mask)$mask_index ++)
  324.             {
  325.                 $mask_char substr($mask$mask_index,  1);
  326.                 $text_char substr($value$value_index1);
  327.         
  328.                 if (in_array($mask_chararray('-''_''.''/''\\'':''|''('')''['']''{''}'' ')))
  329.                 {
  330.                     $clear_result .= $mask_char;
  331.                 }
  332.                 else
  333.                 {
  334.                     $clear_result .= $text_char;
  335.                     $value_index ++;
  336.                 }
  337.             }
  338.             return $clear_result;
  339.         }
  340.     }
  341.     
  342.     /**
  343.      * Change mask dynamically
  344.      */
  345.     public static function changeMask($formName$name$mask)
  346.     {
  347.         TScript::create("tentry_change_mask( '{$formName}', '{$name}', '{$mask}');");
  348.     }
  349.     
  350.     /**
  351.      * Shows the widget at the screen
  352.      */
  353.     public function show()
  354.     {
  355.         // define the tag properties
  356.         $this->tag->{'name'}  $this->name;    // TAG name
  357.         $this->tag->{'value'htmlspecialchars(string) $this->valueENT_QUOTES ENT_HTML5'UTF-8');   // TAG value
  358.         
  359.         if (!empty($this->size))
  360.         {
  361.             if (strstr((string) $this->size'%'!== FALSE)
  362.             {
  363.                 $this->setProperty('style'"width:{$this->size};"false)//aggregate style info
  364.             }
  365.             else
  366.             {
  367.                 $this->setProperty('style'"width:{$this->size}px;"false)//aggregate style info
  368.             }
  369.         }
  370.         
  371.         if ($this->id and empty($this->tag->{'id'}))
  372.         {
  373.             $this->tag->{'id'$this->id;
  374.         }
  375.         
  376.         if (isset($this->exitAction))
  377.         {
  378.             if (!TForm::getFormByName($this->formNameinstanceof TForm)
  379.             {
  380.                 throw new Exception(AdiantiCoreTranslator::translate('You must pass the ^1 (^2) as a parameter to ^3'__CLASS__$this->name'TForm::setFields()') );
  381.             }
  382.             $string_action $this->exitAction->serialize(FALSE);
  383.             $this->setProperty('exitaction'"__adianti_post_lookup('{$this->formName}', '{$string_action}', '{$this->id}', 'callback')");
  384.         }
  385.         
  386.         // verify if the widget is non-editable
  387.         if (parent::getEditable())
  388.         {
  389.             if (isset($this->exitAction))
  390.             {
  391.                 // just aggregate onBlur, if the previous one does not have return clause
  392.                 if (strstr((string) $this->getProperty('onBlur')'return'== FALSE)
  393.                 {
  394.                     $this->setProperty('onBlur'$this->getProperty('exitaction')FALSE);
  395.                 }
  396.                 else
  397.                 {
  398.                     $this->setProperty('onBlur'$this->getProperty('exitaction')TRUE);
  399.                 }
  400.             }
  401.             
  402.             if (isset($this->exitFunction))
  403.             {
  404.                 if (strstr($this->getProperty('onBlur')'return'== FALSE)
  405.                 {
  406.                     $this->setProperty('onBlur'$this->exitFunctionFALSE);
  407.                 }
  408.                 else
  409.                 {
  410.                     $this->setProperty('onBlur'$this->exitFunctionTRUE);
  411.                 }
  412.             }
  413.         }
  414.         else
  415.         {
  416.             $this->tag->{'readonly'"1";
  417.             $this->tag->{'class'.= ' tfield_disabled'// CSS
  418.             $this->tag->{'tabindex''-1';
  419.             $this->tag->{'onmouseover'"style.cursor='default'";
  420.         }
  421.         
  422.         if ($this->mask)
  423.         {
  424.             TScript::create"tentry_new_mask( '{$this->id}', '{$this->mask}'); ");
  425.         }
  426.         
  427.         if (!empty($this->innerIcon))
  428.         {
  429.             $icon_wrapper new TElement('div');
  430.             $icon_wrapper->add($this->tag);
  431.             $icon_wrapper->add($this->innerIcon);
  432.             $icon_wrapper->show();
  433.         }
  434.         else
  435.         {
  436.             // shows the tag
  437.             $this->tag->show();
  438.         }
  439.         
  440.         if (isset($this->completion))
  441.         {
  442.             $options 'minChars' => $this->minLength ];
  443.             if (!empty($this->delimiter))
  444.             {
  445.                 $options'delimiter'$this->delimiter;
  446.             }
  447.             $options_json json_encode$options );
  448.             $list json_encode($this->completion);
  449.             TScript::create(" tentry_autocomplete( '{$this->id}', $list, '{$options_json}'); ");
  450.         }
  451.         if ($this->numericMask)
  452.         {
  453.             $reverse $this->reverse 'true' 'false';
  454.             $allowNegative $this->allowNegative 'true' 'false';
  455.  
  456.             TScript::create"tentry_numeric_mask( '{$this->id}', {$this->decimals}, '{$this->decimalsSeparator}', '{$this->thousandSeparator}', {$reverse}, {$allowNegative}); ");
  457.         }
  458.         
  459.         if ($this->exitOnEnterOn)
  460.         {
  461.             TScript::create"tentry_exit_on_enter( '{$this->id}' ); ");
  462.         }
  463.     }
  464. }