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 / 32
CRAP
0.00% covered (danger)
0.00%
0 / 94
TAutoComplete
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 32
2652
0.00% covered (danger)
0.00%
0 / 94
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 14
 setFieldName
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getFieldName
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setTablePackageFuncion
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getTablePackageFuncion
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setSearchField
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getSearchField
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setUpdateFields
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getUpdateFields
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setDisableUpdateFields
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getDisableUpdateFields
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setExtraSearchFields
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getExtraSearchFields
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setCallBackFunctionJs
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getCallBackFunctionJs
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setMinChars
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getMinChars
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 setDelay
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getDelay
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 setMaxItensToShow
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getMaxItensToShow
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 setCacheTime
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getCacheTime
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 setRemoveMask
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getRemoveMask
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 setUrl
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getUrl
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setCallBackParameters
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getCallBackParameters
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setHint
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getHint
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 getJs
0.00% covered (danger)
0.00%
0 / 1
182
0.00% covered (danger)
0.00%
0 / 33
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
42class TAutoComplete
43{
44    private $strFieldName;
45    private $strTablePackageFuncion;
46    private $strSearchField;
47    private $mixUpdateFields;
48    private $boolDisableUpdateFields;
49    private $mixExtraSearchFields;
50    private $strCallBackFunctionJs;
51    private $intMinChars;
52    private $intDelay;
53    private $intMaxItensToShow;
54    private $intCacheTime;
55    private $boolRemoveMask;
56    private $strUrl;
57    private $callBackParams;
58    private $hint;
59    /**
60    * Implementa o recurso de autosugestão ao um campo
61    * @strUrl - se for informada a url que devolverá os dados para o autocomplete, o retorno deverá ser no seguinte formato:
62    * "descriçao|chave\n"  exemplo: echo "Abacate|123\n"
63    *
64    * @param string $strFieldName
65    * @param string $strPackageFunction
66    * @param string $strSearchField
67    * @param mixed $mixUpdateFields
68    * @param boolean $boolDisableUpdateFields
69    * @param mixed $mixExtraSearchFields
70    * @param string $strCallBackFunctionJs
71    * @param integer $intMinChars
72    * @param integer $intDelay
73    * @param integer $intMaxItensToShow
74    * @param integer $intCacheTime
75    * @param integer $boolRemoveMask
76    * @param string $strUrl
77    */
78    public function __construct($strFieldName,$strTablePackageFuncion,$strSearchField,$mixUpdateFields=null,$boolDisableUpdateFields=null,$mixExtraSearchFields=null,$strCallBackFunctionJs=null,$intMinChars=null,$intDelay=null,$intMaxItensToShow=null,$intCacheTime=null,$boolRemoveMask=null,$strUrl=null)
79    {
80         $this->setFieldName($strFieldName);
81         $this->setTablePackageFuncion($strTablePackageFuncion);
82         $this->setSearchField($strSearchField);
83         $this->setUpdateFields($mixUpdateFields);
84         $this->setDisableUpdateFields($boolDisableUpdateFields);
85         $this->setExtraSearchFields($mixExtraSearchFields);
86         $this->setCallBackFunctionJs($strCallBackFunctionJs);
87         $this->setMinChars($intMinChars);
88         $this->setDelay($intDelay);
89         $this->setMaxItensToShow($intMaxItensToShow);
90         $this->setCacheTime($intCacheTime);
91         $this->setRemoveMask($boolRemoveMask);
92         $this->setUrl($strUrl);
93    }
94    //---------------------------------------------------------------------------------------------------
95    public function setFieldName($strNewValue)
96    {
97        $this->strFieldName = $strNewValue;
98    }
99    public function getFieldName()
100    {
101        return $this->strFieldName;
102    }
103    //---------------------------------------------------------------------------------------------------
104    public function setTablePackageFuncion($strNewValue)
105    {
106        $this->strTablePackageFuncion = $strNewValue;
107    }
108    public function getTablePackageFuncion()
109    {
110        return $this->strTablePackageFuncion;
111    }
112    //---------------------------------------------------------------------------------------------------
113    public function setSearchField($strNewValue)
114    {
115        $this->strSearchField = $strNewValue;
116    }
117    public function getSearchField()
118    {
119        return $this->strSearchField;
120    }
121    //---------------------------------------------------------------------------------------------------
122    public function setUpdateFields($mixNewValue=null)
123    {
124        $this->mixUpdateFields = $mixNewValue;
125    }
126    public function getUpdateFields()
127    {
128        return $this->mixUpdateFields;
129    }
130    //---------------------------------------------------------------------------------------------------
131    public function setDisableUpdateFields($boolNewValue=null)
132    {
133        $this->boolDisableUpdateFields = $boolNewValue;
134    }
135    public function getDisableUpdateFields()
136    {
137        return $this->boolDisableUpdateFields;
138    }
139    //---------------------------------------------------------------------------------------------------
140    public function setExtraSearchFields($mixNewValue=null)
141    {
142        $this->mixExtraSearchFields = $mixNewValue;
143    }
144    public function getExtraSearchFields()
145    {
146        return $this->mixExtraSearchFields;
147    }
148    //---------------------------------------------------------------------------------------------------
149    public function setCallBackFunctionJs($strNewValue=null)
150    {
151        $this->strCallBackFunctionJs = $strNewValue;
152    }
153    public function getCallBackFunctionJs()
154    {
155        return $this->strCallBackFunctionJs;
156    }
157    //---------------------------------------------------------------------------------------------------
158    public function setMinChars($intNewValue=null)
159    {
160        $this->intMinChars = $intNewValue;
161    }
162    public function getMinChars()
163    {
164        return is_null($this->intMinChars) ? 3 : $this->intMinChars;
165    }
166    //---------------------------------------------------------------------------------------------------
167    public function setDelay($intNewValue=null)
168    {
169        $this->intDelay = $intNewValue;
170    }
171    public function getDelay()
172    {
173        return is_null($this->intDelay) ? 1000 : $this->intDelay;
174    }
175    //---------------------------------------------------------------------------------------------------
176    public function setMaxItensToShow($intNewValue=null)
177    {
178        $this->intMaxItensToShow = $intNewValue;
179    }
180    public function getMaxItensToShow()
181    {
182        return is_null($this->intMaxItensToShow) ? 20 : $this->intMaxItensToShow;
183    }
184    //---------------------------------------------------------------------------------------------------
185    public function setCacheTime($intNewValue=null)
186    {
187        $this->intCacheTime = $intNewValue;
188    }
189    public function getCacheTime()
190    {
191        return is_null($this->intCacheTime) ? 60 : $this->intCacheTime;
192    }
193    //---------------------------------------------------------------------------------------------------
194    public function setRemoveMask($boolNewValue=null)
195    {
196        $this->boolRemoveMask = $boolNewValue;
197    }
198    public function getRemoveMask()
199    {
200        return is_null($this->boolRemoveMask) ? 'false' : ( ($this->boolRemoveMask)?'true':'false');
201    }
202    //---------------------------------------------------------------------------------------------------
203    public function setUrl($strNewValue=null)
204    {
205        $this->strUrl = $strNewValue;
206    }
207    public function getUrl()
208    {
209        //return is_null($this->strUrl) ? 'base/callbacks/autocomplete.php' :$this->strUrl;
210        //return is_null($this->strUrl) ? 'modulo=base/callbacks/autocomplete.php&ajax=1' :$this->strUrl;
211        return $this->strUrl;
212    }
213    //---------------------------------------------------------------------------------------------------
214    public function setCallBackParameters($strParameters=null)
215    {
216        $this->callBackParams=$strParameters;
217    }
218    public function getCallBackParameters()
219    {
220        return $this->callBackParams;
221    }
222    //---------------------------------------------------------------------------------------------------
223    public function setHint($strNewValue=null)
224    {
225        $this->hint = $strNewValue;
226    }
227    public function getHint()
228    {
229        if( is_null($this->hint))
230        {
231            return 'Autocompletar - Inicie a digitação e interrompa por alguns segundos, para que a lista de sugestões, com os registros coincidentes, seja apresentada logo abaixo!';
232        }
233        return $this->hint;
234    }
235    //---------------------------------------------------------------------------------------------------
236
237    //---------------------------------------------------------------------------------------------------
238    //---------------------------------------------------------------------------------------------------
239
240    public function getJs()
241    {
242        // criar array com dados para passar como extraparams via json
243        $strExtraParams = '';
244        $strFillFields     = '';
245        $aTemp=array();
246        $aTemp['tablePackageFunction']     = $this->getTablePackageFuncion();
247        $aTemp['searchField']            = $this->getSearchField();
248        $aTemp['cacheTime']                = $this->getCacheTime();
249        if( $this->getCallBackFunctionJs() )
250        {
251            $aTemp['functionJs'] = $this->getCallBackFunctionJs();
252            if( ! strpos($aTemp['functionJs'],'(') )
253            {
254                $aTemp['functionJs'].="()";
255            }
256            if( strpos($aTemp['functionJs'],'()') > 0 && $this->getCallBackParameters() )
257            {
258                $aTemp['functionJs'] =  str_replace('()','('.$this->getCallBackParameters().')',$aTemp['functionJs']);
259            }
260        }
261        if( $this->getUpdateFields() )
262         {
263             $mixUpdateFields = $this->getUpdateFields();
264             if(!is_array($mixUpdateFields))
265             {
266                 $mixUpdateFields = explode(',',$mixUpdateFields);
267            }
268            foreach($mixUpdateFields as $k=>$v)
269            {
270                if( is_int($k))
271                {
272                    $aField = explode('|',$v);
273                    if( !isset($aField[1]))
274                    {
275                        $aField[1] = strtolower($aField[0]);
276                    }
277                    $k=strtoupper($aField[0]);
278                    $v=$aField[1];
279                }
280                // quando o autocomplete estiver sendo utilizado em campos do gride, o nome dos campos
281                // virão em formato de array ( NUM_PESSOA%1 ). Utilize explode('%'...) para extrari o nome do campo
282                $k = explode('%',$k);
283                $strExtraParams .= $strExtraParams=="" ? "" : ",";
284                $strFillFields     .= $strFillFields=="" ? "" : ",";
285                $strExtraParams    .= '"'.$k[0].'":"'.str_replace('%','_',$v).'"'; //"COD_UF":"cod_uf"
286                $aTemp['_u_'.$k[0]] = str_replace('%','_',$v);
287                $strFillFields     .= str_replace('%','_',$v);
288            }
289        }
290           $strExtraParams = str_replace(array('{','}',':"$',').value"','"jQuery("#', '\").get', '"{', '}"'),array('','',':$',').value','jQuery("#','").get','{','}'),stripcslashes(json_encode($aTemp)));
291           if($this->getUrl())
292           {
293            return 'jQuery("#'.$this->getFieldName().'").autocomplete("'.$this->getUrl().'", { delay:'.$this->getDelay().', minChars:'.$this->getMinChars().', matchSubset:1, matchContains:1, cacheLength:10, onItemSelect:fwAutoCompleteSelectItem, onFindValue:fwAutoCompleteFindValue, matchCase:false, maxItemsToShow:'.$this->getMaxItensToShow().', autoFill:true, selectFirst:true, mustMatch:false, selectOnly:true,removeMask:'.$this->getRemoveMask().',extraParams:{'.$strExtraParams.'} });';
294        }
295        else
296        {
297            return 'jQuery("#'.$this->getFieldName().'").autocomplete(app_url+app_index_file+"?modulo=base/callbacks/autocomplete.php&ajax=1", { delay:'.$this->getDelay().', minChars:'.$this->getMinChars().', matchSubset:1, matchContains:1, cacheLength:10, onItemSelect:fwAutoCompleteSelectItem, onFindValue:fwAutoCompleteFindValue, matchCase:false, maxItemsToShow:'.$this->getMaxItensToShow().', autoFill:true, selectFirst:true, mustMatch:false, selectOnly:true,removeMask:'.$this->getRemoveMask().',extraParams:{'.$strExtraParams.'} });';
298        }
299    }
300}
301//$ac = new TAutoComplete('nom_pessoa','PESSOA','NOM_PESSOA','NUM_PESSOA');
302//print $ac->getJs();
303?>