Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 89
TDate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 12
2550
0.00% covered (danger)
0.00%
0 / 89
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 7
 show
0.00% covered (danger)
0.00%
0 / 1
72
0.00% covered (danger)
0.00%
0 / 32
 setMask
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 7
 getMaskType
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setButtonVisible
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 getButtonVisible
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 validate
0.00% covered (danger)
0.00%
0 / 1
756
0.00% covered (danger)
0.00%
0 / 27
 setMaxValue
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getMaxValue
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setMinValue
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getMinValue
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getYMD
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 5
1<?php
2/*
3 * Formdin Framework
4 * Copyright (C) 2012 Ministério do Planejamento
5 * Criado por Luís Eugênio Barbosa
6 * Essa versão é um Fork https://github.com/bjverde/formDin
7 *
8 * ----------------------------------------------------------------------------
9 * This file is part of Formdin Framework.
10 *
11 * Formdin Framework is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License version 3
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License version 3
21 * along with this program; if not,  see <http://www.gnu.org/licenses/>
22 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
23 * Fifth Floor, Boston, MA  02110-1301, USA.
24 * ----------------------------------------------------------------------------
25 * Este arquivo é parte do Framework Formdin.
26 *
27 * O Framework Formdin é um software livre; você pode redistribuí-lo e/ou
28 * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
29 * do Software Livre (FSF).
30 *
31 * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
32 * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
33 * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
34 * para maiores detalhes.
35 *
36 * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
37 * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
38 * ou escreva para a Fundação do Software Livre (FSF) Inc.,
39 * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
40 */
41
42  /**
43 * Campo para edição de data, dia/mes e mes/ano definindo o parametro
44 * maskType como dmy, dm, my
45 *
46 * <!--<style type="text/css">@import url(base/css/calendar-win2k-1.css);</style>-->
47 */
48class TDate extends TMask
49{
50    private $maskType;
51    private $minValue;
52    private $maxValue;
53    private $buttonVisible;
54    /**
55     * @param string $name
56     * @param string $value
57     * @param boolean $required
58     * @param string $minValue
59     * @param string $maxValue
60     * @param string $maskType
61     */
62    public function __construct($strName,$strValue=null,$boolRequired=null,$strMinValue=null,$strMaxValue=null,$strMaskType=null,$boolButtonVisible=null)
63    {
64        parent::__construct($strName,$strValue,null,$boolRequired);
65        $this->setFieldType('date');
66        $this->setMask($strMaskType);
67        $this->setMinValue($strMinValue);
68        $this->setMaxValue($strMaxValue);
69        $this->setButtonVisible($boolButtonVisible);
70    }
71    //-------------------------------------------------------------------------------
72    public function show($print=true)
73    {
74        // não colocar a imagem no campo data se ele estiver desabilitado
75        if( $this->getEnabled())
76        {
77            $imgCalendar = new TElement('img');
78            $imgCalendar->id = $this->getName().'_calendar';
79            $imgCalendar->src         = self::$base."imagens/btnCalendario.gif";
80            $imgCalendar->width        = '15x';
81            $imgCalendar->height    = '12px';
82            $imgCalendar->setCss('vertical-align','middle');
83            $imgCalendar->setCss('cursor','pointer');
84            if($this->getButtonVisible())
85            {
86                $js = new TElement('script');
87                $js->setProperty('type',"text/javascript");
88                $js->add('Calendar.setup({inputField : "'.$this->getName().'", // ID of the input field');
89                if($this->getMaskType()=='dmy')
90                {
91                    $js->add('ifFormat : "%d/%m/%Y",');
92                }
93                else if($this->getMaskType()=='dm')
94                {
95                    $js->add('ifFormat : "%d/%m",');
96                    if( is_null( $this->getExempleText() ) )
97                    {
98                        $this->setExampleText('dd/mm');
99                    }
100                }
101                else if($this->getMaskType()=='my')
102                {
103                    $js->add('ifFormat : "%m/%y",');
104                    if( is_null( $this->getExampleText() ) )
105                    {
106                        $this->setExampleText('mm/yyyy');
107                    }
108                }
109                $js->add('electric : false,');
110                $js->add('position : false,');
111                $js->add('cache : true,');
112                $js->add('onUpdate : fwCalendarioUpdate,');
113                $js->add('button : "'.$imgCalendar->id.'" // ID of the button');
114                $js->add('});');
115                $imgCalendar->add($js);
116                $this->add($imgCalendar);
117            }
118            $this->addEvent('onBlur' ,'fwValidarData(this,event,"'.$this->getMaskType().'","'.$this->getMinValue().'","'.$this->getMaxValue().'")');
119        }
120        return parent::show($print);
121    }
122    //--------------------------------------------------------------------------
123    public function setMask($strNewMaskType=null)
124    {
125        $strNewMaskType = isset($strNewMaskType)?strtolower($strNewMaskType):null;
126        $arrMasksTypes = array(
127             'dmy'    => '99/99/9999'
128            ,'dm'    => '99/99'
129            ,'my'    => '99/9999');
130        if (!array_key_exists($strNewMaskType,$arrMasksTypes) ){
131            $strNewMaskType = 'dmy';
132        }
133        $this->maskType = $strNewMaskType;
134        parent::setMask($arrMasksTypes[$strNewMaskType]);
135    }
136    //--------------------------------------------------------------------------
137    public function getMaskType()
138    {
139        return $this->maskType;
140    }
141    //--------------------------------------------------------------------------
142    public function setButtonVisible($boolValue=null)
143    {
144        $boolValue = $boolValue===null ? true : (bool)$boolValue;
145        $this->buttonVisible = $boolValue;
146    }
147    public function getButtonVisible()
148    {
149        return $this->buttonVisible;
150    }
151    /**
152    * validar campo dmy, dm, my
153    *
154    */
155    public function validate()
156    {
157        if(parent::validate())
158        {
159            $date = preg_replace('/[^0-9]/','',$this->getValue());
160            if($this->getMaskType() =='dm')
161            {
162                $date .= '2000';
163            }
164            elseif($this->getMaskType() == 'my')
165            {
166                $date = '01'.$date;
167            }
168            $tam = strlen($date);
169            if ( $tam == 8 or $tam==6 )
170            {
171                $day = substr($date,0,2);
172                $month = substr($date,2,2);
173                $year = substr($date,4,4)+0;
174
175                if( $year < 60 )
176                {
177                    $year += 2000;
178                }
179                else if( $year > 59 and $year < 100)
180                {
181                    $year += 1900;
182                }
183
184                if ( ($day<1) or ($day>31)  || ($month<1) || ($month>12)  || ( ($day>29) && ($month==2) ) || ( ($day>30) && ( ($month==4) || ($month==6) || ($month==9) || ($month==11)  ) ) )
185                {
186                    if( $this->getMaskType() == 'dm')
187                    {
188                        $this->setError('DIA/MÊS está inválido');
189                    }
190                    else if( $this->getMaskType() == 'my')
191                    {
192                        $this->setError('MÊS/ANO está inválido');
193                    }
194                    else
195                    {
196                        $this->setError('Valor inválido');
197                    }
198                }
199                else
200                {
201                    // validar ano bissexto
202                    if ( $month == 2 )
203                    {
204                        if (($day==29) && ((($year % 4) != 0) || ((($year % 100) == 0) && (($year % 400) != 0))))
205                        {
206                            $this->setError('Dia inválido. Ano não é bissexto.');
207                        }
208                    }
209                }
210            }
211            else
212            {
213                $this->setValue('');
214                parent::validate();
215            }
216        }
217        return ( (string)$this->getError()==="" );
218    }
219
220
221//---------------------------------------------------------------------------
222    public function setMaxValue($strNewValue=null)
223    {
224        $this->maxValue = $strNewValue;
225    }
226    //---------------------------------------------------------------------------
227    public function getMaxValue()
228    {
229        return $this->maxValue;
230    }
231    //---------------------------------------------------------------------------
232    public function setMinValue($strNewValue=null)
233    {
234        $this->minValue = $strNewValue;
235    }
236    //---------------------------------------------------------------------------
237    public function getMinValue()
238    {
239        return $this->minValue;
240    }
241    //----------------------------------------------------------------------------
242    /**
243    * Retorna a data invertida
244    * O parametro strNewDelim altera a barra para o novo valor passado
245    *
246    * @param bool $strNewDelim
247    */
248
249    public function getYMD( $strNewDelim = null )
250    {
251        $strNewDelim = is_null( $strNewDelim ) ? '/' : $strNewDelim;
252        if($this->getValue())
253        {
254            $aData = explode('/',$this->getValue());
255            return $aData[2].$strNewDelim.$aData[1].$strNewDelim.$aData[0];
256        }
257    }
258}
259?>