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

Documentation is available at TDateTime.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\TElement;
  8. use Adianti\Widget\Base\TScript;
  9. use Adianti\Widget\Form\TEntry;
  10.  
  11. use DateTime;
  12. use Exception;
  13.  
  14. /**
  15.  * DateTimePicker Widget
  16.  *
  17.  * @version    7.4
  18.  * @package    widget
  19.  * @subpackage form
  20.  * @author     Pablo Dall'Oglio
  21.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  22.  * @license    http://www.adianti.com.br/framework-license
  23.  */
  24. class TDateTime extends TEntry implements AdiantiWidgetInterface
  25. {
  26.     private $mask;
  27.     private $dbmask;
  28.     protected $id;
  29.     protected $size;
  30.     protected $value;
  31.     protected $options;
  32.     protected $replaceOnPost;
  33.     protected $changeFunction;
  34.     protected $changeAction;
  35.  
  36.     /**
  37.      * Class Constructor
  38.      * @param $name Name of the widget
  39.      */
  40.     public function __construct($name)
  41.     {
  42.         parent::__construct($name);
  43.         $this->id   = 'tdatetime_' mt_rand(10000000001999999999);
  44.         $this->mask 'yyyy-mm-dd hh:ii';
  45.         $this->dbmask null;
  46.         $this->options = [];
  47.         $this->replaceOnPost = FALSE;
  48.  
  49.         $this->setOption('fontAwesome'true);
  50.  
  51.         $newmask $this->mask;
  52.         $newmask str_replace('dd',   '99',   $newmask);
  53.         $newmask str_replace('hh',   '99',   $newmask);
  54.         $newmask str_replace('ii',   '99',   $newmask);
  55.         $newmask str_replace('mm',   '99',   $newmask);
  56.         $newmask str_replace('yyyy''9999'$newmask);
  57.         parent::setMask($newmask);
  58.         $this->tag->{'widget''tdatetime';
  59.     }
  60.  
  61.     /**
  62.      * Store the value inside the object
  63.      */
  64.     public function setValue($value)
  65.     {
  66.         $value str_replace('T'' '$value);
  67.         if (!empty($this->dbmaskand ($this->mask !== $this->dbmask) )
  68.         {
  69.             return parent::setValueself::convertToMask($value$this->dbmask$this->mask) );
  70.         }
  71.         else
  72.         {
  73.             return parent::setValue($value);
  74.         }
  75.     }
  76.  
  77.     /**
  78.      * Return the post data
  79.      */
  80.     public function getPostData()
  81.     {
  82.         $value parent::getPostData();
  83.  
  84.         if (!empty($this->dbmaskand ($this->mask !== $this->dbmask) )
  85.         {
  86.             return self::convertToMask($value$this->mask$this->dbmask);
  87.         }
  88.         else
  89.         {
  90.             return $value;
  91.         }
  92.     }
  93.  
  94.     /**
  95.      * Convert from one mask to another
  96.      * @param $value original date
  97.      * @param $fromMask source mask
  98.      * @param $toMask target mask
  99.      */
  100.     public static function convertToMask($value$fromMask$toMask)
  101.     {
  102.         if (is_array($value)) // vector fields (field list)
  103.         {
  104.             foreach ($value as $key => $item)
  105.             {
  106.                 $value[$keyself::convertToMask($item$fromMask$toMask);
  107.             }
  108.  
  109.             return $value;
  110.         }
  111.         else if ($value)
  112.         {
  113.             $value substr($value,0,strlen($fromMask));
  114.  
  115.             $phpFromMask str_replace['dd','mm''yyyy''hh''ii''ss']['d','m','Y''H''i''s']$fromMask);
  116.             $phpToMask   str_replace['dd','mm''yyyy''hh''ii''ss']['d','m','Y''H''i''s']$toMask);
  117.  
  118.             $date DateTime::createFromFormat($phpFromMask$value);
  119.             if ($date)
  120.             {
  121.                 return $date->format($phpToMask);
  122.             }
  123.         }
  124.  
  125.         return $value;
  126.     }
  127.  
  128.     /**
  129.      * Set change function
  130.      */
  131.     public function setChangeFunction($function)
  132.     {
  133.         $this->changeFunction $function;
  134.     }
  135.  
  136.     /**
  137.      * Define the action to be executed when the user changes the field
  138.      * @param $action TAction object
  139.      */
  140.     public function setExitAction(TAction $action)
  141.     {
  142.         $this->setChangeAction($action);
  143.     }
  144.  
  145.     /**
  146.      * Define the action to be executed when the user changes the field
  147.      * @param $action TAction object
  148.      */
  149.     public function setChangeAction(TAction $action)
  150.     {
  151.         if ($action->isStatic())
  152.         {
  153.             $this->changeAction $action;
  154.         }
  155.         else
  156.         {
  157.             $string_action $action->toString();
  158.             throw new Exception(AdiantiCoreTranslator::translate('Action (^1) must be static to be used in ^2'$string_action__METHOD__));
  159.         }
  160.     }
  161.  
  162.     /**
  163.      * Define the field's mask
  164.      * @param $mask  Mask for the field (dd-mm-yyyy)
  165.      */
  166.     public function setMask($mask$replaceOnPost FALSE)
  167.     {
  168.         $this->mask $mask;
  169.         $this->replaceOnPost $replaceOnPost;
  170.  
  171.         $newmask $this->mask;
  172.         $newmask str_replace('dd',   '99',   $newmask);
  173.         $newmask str_replace('hh',   '99',   $newmask);
  174.         $newmask str_replace('ii',   '99',   $newmask);
  175.         $newmask str_replace('mm',   '99',   $newmask);
  176.         $newmask str_replace('ss',   '99',   $newmask);
  177.         $newmask str_replace('yyyy''9999'$newmask);
  178.  
  179.         parent::setMask($newmask);
  180.     }
  181.  
  182.     /**
  183.      * Set the mask to be used to colect the data
  184.      */
  185.     public function setDatabaseMask($mask)
  186.     {
  187.         $this->dbmask $mask;
  188.     }
  189.  
  190.     /**
  191.      * Set extra datepicker options
  192.      * @link https://www.malot.fr/bootstrap-datetimepicker/
  193.      */
  194.     public function setOption($option$value)
  195.     {
  196.         $this->options[$option$value;
  197.     }
  198.  
  199.     /**
  200.      * Enable the field
  201.      * @param $form_name Form name
  202.      * @param $field Field name
  203.      */
  204.     public static function enableField($form_name$field)
  205.     {
  206.         TScript::create" tdate_enable_field('{$form_name}', '{$field}'); );
  207.     }
  208.  
  209.     /**
  210.      * Disable the field
  211.      * @param $form_name Form name
  212.      * @param $field Field name
  213.      */
  214.     public static function disableField($form_name$field)
  215.     {
  216.         TScript::create" tdate_disable_field('{$form_name}', '{$field}'); );
  217.     }
  218.  
  219.     /**
  220.      * Shows the widget at the screen
  221.      */
  222.     public function show()
  223.     {
  224.         $js_mask str_replace('yyyy''yy'$this->mask);
  225.         $language strtolowerAdiantiCoreTranslator::getLanguage() );
  226.         $options json_encode($this->options);
  227.  
  228.         $outer_size 'undefined';
  229.         if (strstr((string) $this->size'%'!== FALSE)
  230.         {
  231.             $outer_size $this->size;
  232.             $this->size '100%';
  233.         }
  234.  
  235.         if (isset($this->changeAction))
  236.         {
  237.             if (!TForm::getFormByName($this->formNameinstanceof TForm)
  238.             {
  239.                 throw new Exception(AdiantiCoreTranslator::translate('You must pass the ^1 (^2) as a parameter to ^3'__CLASS__$this->name'TForm::setFields()') );
  240.             }
  241.  
  242.             $string_action $this->changeAction->serialize(FALSE);
  243.             $this->setProperty('changeaction'"__adianti_post_lookup('{$this->formName}', '{$string_action}', '{$this->id}', 'callback')");
  244.             $this->setProperty('onChange'$this->getProperty('changeaction'));
  245.         }
  246.  
  247.         if (isset($this->changeFunction))
  248.         {
  249.             $this->setProperty('changeaction'$this->changeFunctionFALSE);
  250.             $this->setProperty('onChange'$this->changeFunctionFALSE);
  251.         }
  252.  
  253.         parent::show();
  254.  
  255.         TScript::create"tdatetime_start( '#{$this->id}', '{$this->mask}', '{$language}', '{$outer_size}', '{$options}');");
  256.  
  257.         if (!parent::getEditable())
  258.         {
  259.             TScript::create" tdate_disable_field( '{$this->formName}', '{$this->name}' ); );
  260.         }
  261.     }
  262. }