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

Documentation is available at TDate.php

  1. <?php
  2. namespace Adianti\Widget\Form;
  3.  
  4. use Adianti\Control\TAction;
  5. use Adianti\Core\AdiantiCoreTranslator;
  6. use Adianti\Widget\Form\AdiantiWidgetInterface;
  7. use Adianti\Widget\Base\TScript;
  8. use Adianti\Widget\Form\TEntry;
  9.  
  10. use DateTime;
  11. use Exception;
  12.  
  13. /**
  14.  * DatePicker 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 TDate extends TEntry implements AdiantiWidgetInterface
  24. {
  25.     protected $mask;
  26.     protected $dbmask;
  27.     protected $id;
  28.     protected $size;
  29.     protected $options;
  30.     protected $value;
  31.     protected $replaceOnPost;
  32.     protected $changeFunction;
  33.     protected $changeAction;
  34.  
  35.     /**
  36.      * Class Constructor
  37.      * @param $name Name of the widget
  38.      */
  39.     public function __construct($name)
  40.     {
  41.         parent::__construct($name);
  42.         $this->id   = 'tdate_' mt_rand(10000000001999999999);
  43.         $this->mask = 'yyyy-mm-dd';
  44.         $this->dbmask = null;
  45.         $this->options = [];
  46.         $this->replaceOnPost = FALSE;
  47.  
  48.         $newmask $this->mask;
  49.         $newmask str_replace('dd',   '99',   $newmask);
  50.         $newmask str_replace('mm',   '99',   $newmask);
  51.         $newmask str_replace('yyyy''9999'$newmask);
  52.         parent::setMask($newmask);
  53.         $this->tag->{'widget''tdate';
  54.         $this->tag->{'autocomplete''off';
  55.     }
  56.  
  57.     /**
  58.      * Store the value inside the object
  59.      */
  60.     public function setValue($value)
  61.     {
  62.         if (!empty($this->dbmaskand ($this->mask !== $this->dbmask) )
  63.         {
  64.             return parent::setValueself::convertToMask($value$this->dbmask$this->mask) );
  65.         }
  66.         else
  67.         {
  68.             return parent::setValue($value);
  69.         }
  70.     }
  71.  
  72.     /**
  73.      * Return the post data
  74.      */
  75.     public function getPostData()
  76.     {
  77.         $value parent::getPostData();
  78.  
  79.         if (!empty($this->dbmaskand ($this->mask !== $this->dbmask) )
  80.         {
  81.             return self::convertToMask($value$this->mask$this->dbmask);
  82.         }
  83.         else
  84.         {
  85.             return $value;
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * Convert from one mask to another
  91.      * @param $value original date
  92.      * @param $fromMask source mask
  93.      * @param $toMask target mask
  94.      */
  95.     public static function convertToMask($value$fromMask$toMask)
  96.     {
  97.         if (is_array($value)) // vector fields (field list)
  98.         {
  99.             foreach ($value as $key => $item)
  100.             {
  101.                 $value[$keyself::convertToMask($item$fromMask$toMask);
  102.             }
  103.  
  104.             return $value;
  105.         }
  106.         else if ($value)
  107.         {
  108.             $value substr($value,0,strlen($fromMask));
  109.  
  110.             $phpFromMask str_replace['dd','mm''yyyy']['d','m','Y']$fromMask);
  111.             $phpToMask   str_replace['dd','mm''yyyy']['d','m','Y']$toMask);
  112.  
  113.             $date DateTime::createFromFormat($phpFromMask$value);
  114.             if ($date)
  115.             {
  116.                 return $date->format($phpToMask);
  117.             }
  118.         }
  119.  
  120.         return $value;
  121.     }
  122.  
  123.     /**
  124.      * Define the field's mask
  125.      * @param $mask  Mask for the field (dd-mm-yyyy)
  126.      */
  127.     public function setMask($mask$replaceOnPost FALSE)
  128.     {
  129.         $this->mask $mask;
  130.         $this->replaceOnPost $replaceOnPost;
  131.  
  132.         $newmask $this->mask;
  133.         $newmask str_replace('dd',   '99',   $newmask);
  134.         $newmask str_replace('mm',   '99',   $newmask);
  135.         $newmask str_replace('yyyy''9999'$newmask);
  136.  
  137.         parent::setMask($newmask);
  138.     }
  139.  
  140.     /**
  141.      * Return mask
  142.      */
  143.     public function getMask()
  144.     {
  145.         return $this->mask;
  146.     }
  147.  
  148.     /**
  149.      * Set the mask to be used to colect the data
  150.      */
  151.     public function setDatabaseMask($mask)
  152.     {
  153.         $this->dbmask $mask;
  154.     }
  155.  
  156.     /**
  157.      * Return database mask
  158.      */
  159.     public function getDatabaseMask()
  160.     {
  161.         return $this->dbmask;
  162.     }
  163.  
  164.     /**
  165.      * Set extra datepicker options
  166.      * @link https://bootstrap-datepicker.readthedocs.io/en/latest/options.html
  167.      */
  168.     public function setOption($option$value)
  169.     {
  170.         $this->options[$option$value;
  171.     }
  172.  
  173.     /**
  174.      * Define the action to be executed when the user changes the field
  175.      * @param $action TAction object
  176.      */
  177.     public function setExitAction(TAction $action)
  178.     {
  179.         $this->setChangeAction($action);
  180.     }
  181.  
  182.     /**
  183.      * Define the action to be executed when the user changes the field
  184.      * @param $action TAction object
  185.      */
  186.     public function setChangeAction(TAction $action)
  187.     {
  188.         if ($action->isStatic())
  189.         {
  190.             $this->changeAction $action;
  191.         }
  192.         else
  193.         {
  194.             $string_action $action->toString();
  195.             throw new Exception(AdiantiCoreTranslator::translate('Action (^1) must be static to be used in ^2'$string_action__METHOD__));
  196.         }
  197.     }
  198.  
  199.     /**
  200.      * Set change function
  201.      */
  202.     public function setChangeFunction($function)
  203.     {
  204.         $this->changeFunction $function;
  205.     }
  206.  
  207.     /**
  208.      * Shortcut to convert a date to format yyyy-mm-dd
  209.      * @param $date = date in format dd/mm/yyyy
  210.      */
  211.     public static function date2us($date)
  212.     {
  213.         if ($date)
  214.         {
  215.             // get the date parts
  216.             $day  substr($date,0,2);
  217.             $mon  substr($date,3,2);
  218.             $year substr($date,6,4);
  219.             return "{$year}-{$mon}-{$day}";
  220.         }
  221.     }
  222.  
  223.     /**
  224.      * Shortcut to convert a date to format dd/mm/yyyy
  225.      * @param $date = date in format yyyy-mm-dd
  226.      */
  227.     public static function date2br($date)
  228.     {
  229.         if ($date)
  230.         {
  231.             // get the date parts
  232.             $year substr($date,0,4);
  233.             $mon  substr($date,5,2);
  234.             $day  substr($date,8,2);
  235.             return "{$day}/{$mon}/{$year}";
  236.         }
  237.     }
  238.  
  239.     /**
  240.      * Enable the field
  241.      * @param $form_name Form name
  242.      * @param $field Field name
  243.      */
  244.     public static function enableField($form_name$field)
  245.     {
  246.         TScript::create" tdate_enable_field('{$form_name}', '{$field}'); );
  247.     }
  248.  
  249.     /**
  250.      * Disable the field
  251.      * @param $form_name Form name
  252.      * @param $field Field name
  253.      */
  254.     public static function disableField($form_name$field)
  255.     {
  256.         TScript::create" tdate_disable_field('{$form_name}', '{$field}'); );
  257.     }
  258.  
  259.     /**
  260.      * Shows the widget at the screen
  261.      */
  262.     public function show()
  263.     {
  264.         $js_mask str_replace('yyyy''yy'$this->mask);
  265.         $language strtolowerAdiantiCoreTranslator::getLanguage() );
  266.         $options json_encode($this->options);
  267.  
  268.         $outer_size 'undefined';
  269.         if (strstr(string) $this->size'%'!== FALSE)
  270.         {
  271.             $outer_size $this->size;
  272.             $this->size '100%';
  273.         }
  274.  
  275.         if (isset($this->changeAction))
  276.         {
  277.             if (!TForm::getFormByName($this->formNameinstanceof TForm)
  278.             {
  279.                 throw new Exception(AdiantiCoreTranslator::translate('You must pass the ^1 (^2) as a parameter to ^3'__CLASS__$this->name'TForm::setFields()') );
  280.             }
  281.  
  282.             $string_action $this->changeAction->serialize(FALSE);
  283.             $this->setProperty('changeaction'"__adianti_post_lookup('{$this->formName}', '{$string_action}', '{$this->id}', 'callback');");
  284.             $this->setProperty('onChange'$this->getProperty('changeaction'));
  285.         }
  286.  
  287.         if (isset($this->changeFunction))
  288.         {
  289.             $this->setProperty('changeaction'$this->changeFunctionFALSE);
  290.             $this->setProperty('onChange'$this->changeFunctionFALSE);
  291.         }
  292.  
  293.         parent::show();
  294.  
  295.         TScript::create"tdate_start( '#{$this->id}', '{$this->mask}', '{$language}', '{$outer_size}', '{$options}');");
  296.  
  297.         if (!parent::getEditable())
  298.         {
  299.             TScript::create" tdate_disable_field( '{$this->formName}', '{$this->name}' ); );
  300.         }
  301.     }
  302. }