Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
46.67% covered (warning)
46.67%
7 / 15
CRAP
24.51% covered (danger)
24.51%
25 / 102
TButton
0.00% covered (danger)
0.00%
0 / 1
46.67% covered (warning)
46.67%
7 / 15
800.87
24.51% covered (danger)
24.51%
25 / 102
 __construct
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
13 / 13
 show
0.00% covered (danger)
0.00%
0 / 1
210
0.00% covered (danger)
0.00%
0 / 49
 setAction
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getAction
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 3
 setOnClick
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getOnClick
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 setConfirmMessage
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getConfirmMessage
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setImage
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getImage
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 12
 setImageDisabled
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getImageDisabled
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 5
 setSubmitAction
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getSubmitAction
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 clearEvents
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 3
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*  Classe para criar botões
44*/
45class TButton extends TControl
46{
47    const CLASS_CSS = 'fwButton'; //FormDin 4
48    //const CLASS_CSS = 'btn btn-primary btn-sm'; //FormDin 5
49    const CLASS_CSS_IMG = 'fwButtonImg';
50
51    private $action;
52    private $onClick;
53    private $confirMessage;
54    private $imageEnabled;
55    private $imageDisabled;
56    private $submitAction;
57    
58    /***
59     *  Cria um botão 
60     * @param string $strName           - 1: Id do botão
61     * @param string $strValue          - 2: label do botão que irá aparecer para o usuário
62     * @param string $strAction         - 3: nome da ação que será executada
63     * @param string $strOnClick        - 4: 
64     * @param string $strConfirmMessage - 5: Mensagem de confirmação, para utilizar o confirme sem utilizar javaScript explicito.
65     * @param string $strImageEnabled   - 6: Imagem no botão. Evite usar no lugar procure usar a propriedade setClass. Busca pasta imagens do base ou no caminho informado
66     * @param string $strImageDisabled  - 7: Imagem no desativado. Evite usar no lugar procure usar a propriedade setClass. Busca pasta imagens do base ou no caminho informado
67     * @param string $strHint           - 8: Texto hint para explicar
68     * @param boolean $boolSubmitAction - 9: 
69     */
70    public function __construct($strName
71                               ,$strValue=null
72                               ,$strAction=null
73                               ,$strOnClick=null
74                               ,$strConfirmMessage=null
75                               ,$strImageEnabled=null
76                               ,$strImageDisabled=null
77                               ,$strHint=null
78                               ,$boolSubmitAction=null)
79    {
80        $strName = is_null( $strName ) ? $this->removeIllegalChars( $strValue ) : $strName;
81        parent::__construct('button',$strName,$strValue);
82        $this->setHint($strHint);
83        $this->setFieldType('button');
84        $this->setProperty('type','button');
85        $this->setAction($strAction);
86        $this->setOnClick($strOnClick);
87        $this->setConfirmMessage($strConfirmMessage);
88        $this->setSubmitAction($boolSubmitAction);
89        $this->setImage($strImageEnabled);
90        $this->setImageDisabled($strImageDisabled);
91        $this->setClass(self::CLASS_CSS);
92    }
93
94    public function show($print=true)
95    {
96        // ajustar as propriedades se o botão for uma imagem
97        $isImage=false;
98        if((string)$this->getImage()!="")
99        {
100            $this->setTagType('img');
101            $this->setFieldType('img');
102            $this->setProperty('src',$this->getImage());
103            if( $this->getClass() == self::CLASS_CSS )
104            {
105                $this->setProperty('class',null);
106                //$this->setClass(self::CLASS_CSS_IMG);
107                $this->setCss('background',null);
108                $this->setCss('cursor','pointer');
109                $this->setCss('background-color',null);
110                $this->setCss('font-family',null);
111                $this->setCss('font-size',null);
112                $this->setCss('border','none');
113                $this->setCss('color',null);
114                $this->setCss('vertical-align','top');                
115                if( is_null( $this->getProperty('alt'))){
116                    $this->setProperty('alt', $this->getvalue() );
117                }
118                if( !$this->getProperty('title')){
119                    $this->setProperty('title', $this->getValue() );
120                }
121                $this->setAttribute('type',null);
122                $this->setValue('');
123            }
124            $isImage=true;
125        }
126
127        // regra de acessibilidade
128        if( is_null( $this->getHint() ) ){
129            $this->setHint($this->getValue());
130        }
131
132//        $jsConfirm=null;
133        $jsConfirmBegin=null;
134        $jsConfirmEnd=null;
135        if((string)$this->getConfirmMessage() != '')
136        {
137            //Alterado para padronizar as mensagens utilizando o fwConfirm
138            //Por Diego Barreto e Felipe Colares
139//             $jsConfirm = 'if( !confirm("'.$this->getConfirmMessage().'")){return false;} ';
140            $msg = htmlentities($this->getConfirmMessage(),ENT_COMPAT,ENCODINGS);
141            $jsConfirmBegin = 'fwConfirm("'.$msg.'", function() { ';
142            $jsConfirmEnd    = '}, function() {})';
143        }
144
145        // o evento action tem precedencia sobre o evento onClick
146        if((string)$this->getAction()!='')
147        {
148            if( $this->getSubmitAction() )
149            {
150                $formAction = 'fwFazerAcao("'.$this->getAction().'")';
151//                 $this->addEvent('onclick',$jsConfirm.'if( typeof(btn'.ucwords($this->getId()).'OnClick)=="function"){btn'.ucwords($this->getId()).'OnClick(this);return true;};this.disabled=true;this.value="Aguarde";this.style.color="red";'.$formAction);
152                $this->addEvent('onclick',$jsConfirmBegin.'if( typeof(btn'.ucwords($this->getId()).'OnClick)=="function"){btn'.ucwords($this->getId()).'OnClick(this);return true;};jQuery(this).attr("disabled","true").val("Aguarde").css("color","red");'.$formAction.$jsConfirmEnd);
153            }
154            else
155            {
156//                 $this->addEvent('onclick',$jsConfirm.'if( typeof(btn'.ucwords($this->getId()).'OnClick)=="function"){btn'.ucwords($this->getId()).'OnClick(this);return true;};');
157                $this->addEvent('onclick',$jsConfirmBegin.'if( typeof(btn'.ucwords($this->getId()).'OnClick)=="function"){btn'.ucwords($this->getId()).'OnClick(this);return true;};.'.$jsConfirmEnd);
158            }
159            //$this->addEvent('onclick',$jsConfirm.'if( typeof(btn'.ucwords($this->getId()).'OnClick)=="function"){btn'.ucwords($this->getId()).'OnClick(this);return true;};this.disabled=true;this.value="Aguarde";this.style.color="red";fwFazerAcao("'.$this->getAction().'")');
160            //$this->addEvent('onclick','if(!'.$this->getId().'Click()) { return false } '.$jsConfirm.'fwFazerAcao("'.$this->getAction().'")');
161        }
162        else if($this->getOnClick())
163        {
164            $this->setEvent('onclick',$jsConfirmBegin.$this->getOnClick().$jsConfirmEnd,false);
165        }
166        if( ! $this->getEnabled() ){
167            $this->setCss('cursor','default');
168            if( ! $this->getProperty('title') ){
169                $property = htmlentities('Ação desabilitada',ENT_COMPAT,ENCODINGS);
170                $this->setProperty('title', $property);
171            }
172            $this->setAttribute('disabled','true');
173            if( $this->getOnClick() && $this->getImage() != '' ){
174                $this->setOnClick('if( jQuery(this).attr("disabled")){return false;};'.$this->getOnClick() ,false);
175                $this->setEvent('onClick',$this->getOnClick());
176            }
177        }
178        $this->add( $this->getValue() );
179        $this->setValue(null);
180        return parent::show($print);
181    }
182    //-----------------------------------------------------------------------------------------------
183    public function setAction($strNewValue=null)
184    {
185        $this->action = $strNewValue;
186    }
187    public function getAction()
188    {
189        if((string) $this->action =='' && (string)$this->getOnClick()=='')
190        {
191            $this->setAction($this->getValue());
192        }
193        return $this->action;
194    }
195    public function setOnClick($strFunctionJs=null)
196    {
197        $this->onClick = $strFunctionJs;
198    }
199    public function getOnClick()
200    {
201        if( !is_null($this->onClick))
202        {
203            return $this->onClick;
204        }
205        return $this->getEvent('onclick');
206    }
207    public function setConfirmMessage($strNewMessage=null)
208    {
209        $this->confirMessage = $strNewMessage;
210    }
211    public function getConfirmMessage()
212    {
213        return $this->confirMessage;
214    }
215    public function setImage($strNewImage=null)
216    {
217        $this->imageEnabled=$strNewImage;
218    }
219    public function getImage()
220    {
221        $path="";
222        if($this->getEnabled()){
223            $image = $this->imageEnabled;
224        }else{
225            $image = $this->getImageDisabled();
226        }
227        if($image){
228            // se não foi informado o endereço manualmente, encontrar na pasta base
229            if( strpos($image,'/')===false){
230                if( ! file_exists($image) ){
231                    $path = $this->getBase().'imagens/';
232                }
233            }
234        }
235        if( ! file_exists($path.$image) ){
236            $image = empty($image)?'':$image;
237            $image = str_replace('_disabled.','.',$image);
238        }
239        return $path.$image;
240    }
241    public function setImageDisabled($strNewImage=null)
242    {
243        $this->imageDisabled=$strNewImage;
244    }
245    public function getImageDisabled() {
246        if( !$this->imageDisabled ) {
247            if( !empty($this->imageDisable) && file_exists($this->getBase().'imagens/'.str_replace('.','_disabled.',$this->imageEnabled))) {
248                return str_replace('.','_disabled.',$this->imageEnabled );
249            } else {
250                return 'fwblank16x16.png';
251            }
252        }
253        return $this->imageDisabled;
254    }
255    public function setSubmitAction($boolNewValue=null)
256    {
257        $this->submitAction = $boolNewValue;
258    }
259    public function getSubmitAction()
260    {
261        return is_null($this->submitAction) ? true : $this->submitAction;
262    }
263    public function clearEvents()
264    {
265        $this->setOnClick(null);
266        parent::clearEvents();
267    }
268}
269
270/***
271 * Exemplo de como usar apenas o botão
272 */
273/***
274$btn = new TButton('btnGravar','Gravar','actGravar',null,'Confirma Gravação ?');
275$btn->setImage('btnCalendario.gif');
276$btn->show();
277echo '<br>';
278echo '<br>';
279$btn = new TButton('btnGravar','Gravar',null,'fwTeste()','Tem Certeza ?','../../imagens/search.gif','../../imagens/lixeira.gif');
280$btn->setEnabled(false);
281$btn->setVisible(false);
282$btn->show();
283 */
284?>