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 / 35
CRAP
0.00% covered (danger)
0.00%
0 / 154
TMenuDhtmlx
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 35
6162
0.00% covered (danger)
0.00%
0 / 154
 __construct
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 add
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 19
 addMenu
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 4
 setText
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 3
 setId
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 2
 setIdParent
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 2
 setUrl
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setToolTip
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setImg
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setImgDisabled
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setDisabled
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setHotKey
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setJsonParams
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getText
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getId
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getIdParent
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 3
 getUrl
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getToolTip
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getImg
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getImgDisabled
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getDisabled
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getHotKey
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getMenuById
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 13
 getOrphans
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 ignoreOrphans
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 4
 clearOrphans
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setSeparator
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getSeparator
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getJsonParams
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getOrphanById
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 8
 addOrphan
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 6
 getOrphanByIdParent
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 9
 getStructure
0.00% covered (danger)
0.00%
0 / 1
306
0.00% covered (danger)
0.00%
0 / 36
 addOrphanItems
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 6
 getXml
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 7
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 gerar o xml para criação de menus verticais utilizando a bibliotaca DHTMLX
44* Link: http://www.dhtmlx.com/docs/products/docsExplorer/index.shtml?node=dhtmlxmenu
45* Ex:
46*     $menu = new TMenuDhtmlx();
47*    $menu->add(1,0,'Cadastro');
48*    $menu->add(2,0,'Consulta');
49*    $menu->add(21,2,'Consulta 2.1');
50*    $menu->add(22,2,'Consulta 2.2');
51*    $menu->add(221,22,'Consulta 2.2.1');
52*     echo $menu->getStructure();
53*     ou echo $menu->getXml()
54*
55*/
56class TMenuDhtmlx {
57    private $arrMenu;
58    private $arrOrphan;
59    private $strId;
60    private $strIdParent;
61    private $strText;
62    private $strUrl;
63    private $boolDisabled;
64    private $strImg;
65    private $strImgDisabled;
66    private $strHotKey;
67    private $strTooltip;
68    private $boolIgnoreOrphans;
69    private $boolSeparator;
70    private $jsonParams;
71
72    public function __construct($data=null,$boolIgnoreOrphans=null) {
73        $this->boolIgnoreOrphans = $boolIgnoreOrphans === null ? true : $boolIgnoreOrphans;
74        $this->arrMenu=null;
75    }
76    
77    /**
78    * Método para adicionar itens de menu
79    *
80    * @param string $strId          - 1: id do Menu
81    * @param string $strIdParent    - 2: id do Item pai.
82    * @param string $strText        - 3: Texto que irá aparecer para o usuário
83    * @param string $strUrl         - 4: Caminho do módulos
84    * @param string $strToolTip     - 5: uma explicação maior sobre o item
85    * @param string $strImg         - 6: Caminho da imagem será utilizada como ícone
86    * @param string $strImgDisabled - 7: Caminho da imagem para o menu desabilitado
87    * @param boolean $boolDisabled  - 8: Menu desabilitado
88    * @param string $strHotKey      - 9: Tecla de Atalho
89    * @param boolean $boolSeparator
90    * @param string $jsonParams     - 11: json como parametro de entrada
91    * @return TMenuDhtmlx
92    */
93    public function add($strId
94                      , $strIdParent
95                      , $strText
96                      , $strUrl=null
97                      , $strToolTip=null
98                      , $strImg=null
99                      , $strImgDisabled=null
100                      , $boolDisabled=null
101                      , $strHotKey=null
102                      , $boolSeparator=null
103                      , $jsonParams=null) {
104        $menu = new TMenuDhtmlx();
105        $menu->setText($strText);
106        $menu->setId($strId);
107        $menu->setIdParent($strIdParent);
108        $menu->setUrl($strUrl);
109        $menu->setToolTip($strToolTip);
110        $menu->setImg($strImg);
111        $menu->setImgDisabled($strImgDisabled);
112        $menu->setDisabled($boolDisabled);
113        $menu->setHotKey($strHotKey);
114        $menu->setSeparator($boolSeparator);
115        $menu->setJsonParams($jsonParams);
116
117        if( $menu->getIdParent() ) {
118            // verificar se o pai já está adicionado
119            $objMenu = $this->getMenuById($strIdParent);
120            if( ! is_null( $objMenu ) )
121            {
122                $objMenu->addMenu($menu);
123            }
124            else
125            {
126                $this->addOrphan($menu);
127            }
128        }
129        else
130        {
131            $this->addMenu( $menu );
132        }
133        /*
134        // verificar se o pai já está adicionado
135        $objMenu = $this->getMenuById($strIdParent);
136        if( ! is_null( $objMenu ) )
137        {
138            $objMenu->addMenu($menu);
139        }
140        else
141        {
142            $this->addOrphan($menu);
143            // se tiver idparent então é orfão
144            if( !$strIdParent )
145            {
146                // item pai ou filho
147                $this->addMenu($menu);
148            }
149            else
150            {
151                // Orphan
152                $this->addOrphan($menu);
153            }
154        }
155        */
156        return $menu;
157    }
158    protected function addMenu(TMenuDhtmlx $objMenu)
159    {
160        if( $Orphan = $this->getOrphanByIdParent($objMenu->getId()))
161        {
162            $objMenu->addMenu($Orphan);
163        }
164        $this->arrMenu[] = $objMenu;
165    }
166    public function setText($strNewValue)
167    {
168        $this->text = preg_replace('/</','&lt;',$strNewValue);
169        $this->text = preg_replace('/"/','&quot;',$this->text);
170    }
171    public function setId($strNewValue)
172    {
173        $this->id = is_null($strNewValue) ? 0 : $strNewValue;
174    }
175    public function setIdParent($strNewValue)
176    {
177        $this->idParent = is_null($strNewValue) ? 0 : $strNewValue;
178    }
179    public function setUrl($strNewValue)
180    {
181        $this->strUrl = $strNewValue;
182    }
183    public function setToolTip($strNewValue)
184    {
185        $this->strTooltip = $strNewValue;
186    }
187    public function setImg($strNewValue)
188    {
189        $this->strImg = $strNewValue;
190    }
191    public function setImgDisabled($strNewValue)
192    {
193        $this->strImgDisabled = $strNewValue;
194    }
195    public function setDisabled($boolNewValue)
196    {
197        $this->boolDisabled = $boolNewValue;
198    }
199    public function setHotKey($strNewValue)
200    {
201        $this->strHotKey = $strNewValue;
202    }
203    public function setJsonParams($strNewValue)
204    {
205        $this->jsonParams = $strNewValue;
206    }
207
208    public function getText()
209    {
210        return $this->text;
211    }
212    public function getId()
213    {
214        return $this->id;
215    }
216    public function getIdParent()
217    {
218        if( (string) $this->idParent != '' && (int) $this->idParent != 0 )
219        {
220            return $this->idParent;
221        }
222        return null;
223    }
224    function getUrl()
225    {
226        return $this->strUrl;
227    }
228    public function getToolTip()
229    {
230        return $this->strTooltip;
231    }
232    public function getImg()
233    {
234        return $this->strImg;
235    }
236    public function getImgDisabled()
237    {
238        return $this->strImgDisabled;
239    }
240    public function getDisabled()
241    {
242        return $this->boolDisabled;
243    }
244    public function getHotKey()
245    {
246        return $this->strHotKey;
247    }
248    public function getMenuById($strId)
249    {
250        static $o = null;
251        if( (string) $strId == '0' )
252        {
253            return null;
254        }
255        if( $this->arrMenu )
256        {
257            foreach($this->arrMenu as $k=>$objMenu)
258            {
259                if( is_null( $o ))
260                {
261                    if( $objMenu->getId() == $strId )
262                    {
263                       $o = $objMenu;
264                       break;
265
266                    }
267                    else
268                    {
269                         $o = $objMenu->getMenuById($strId);
270                    }
271                }
272            }
273        }
274        // ver como resolver esse problema, se retornar o $o diretamente não funciona
275        $oo=$o;
276        $o=null;
277        return $oo;
278    }
279
280    public function getOrphans()
281    {
282        return $this->arrOrphan;
283    }
284    //--------------------------------------------------------------------------------------
285    public function ignoreOrphans($boolNewValue=null)
286    {
287        if( $boolNewValue === null)
288        {
289            return $this->boolIgnoreOrphans;
290        }
291        $this->boolIgnoreOrphans = $boolNewValue;
292    }
293    //--------------------------------------------------------------------------------------
294    public function clearOrphans()
295    {
296        $this->arrOrphan = null;
297    }
298    //--------------------------------------------------------------------------------------
299    public function setSeparator($boolNewValue=null)
300    {
301        $this->boolSeparator = $boolNewValue;
302    }
303    public function getSeparator()
304    {
305        return $this->boolSeparator;
306    }
307    //--------------------------------------------------------------------------------------
308    function getJsonParams()
309    {
310        return $this->jsonParams;
311    }
312    //--------------------------------------------------------------------------------------
313    public function getOrphanById($strId)
314    {
315        $result = null;
316        if($this->arrOrphan)
317        {
318            foreach($this->arrOrphan as $k=>$objMenu)
319            {
320                if( $objMenu->getId() == $strId)
321                {
322                    $result=$objMenu;
323                    break;
324                }
325                $result = $objMenu->getOrphanById($strId);
326            }
327        }
328        return $result;
329    }
330    public function addOrphan(TMenuDhtmlx $menu)
331    {
332        // se existir filhos orfão, adicionar todos os filhos
333        while( $objMenu = $this->getOrphanByIdParent($menu->getId()) )
334        {
335            $menu->addMenu($objMenu);
336        }
337        // adicionar ao pai se existir
338        if( $objMenu = $this->getOrphanById($menu->getIdParent()))
339        {
340            $objMenu->addMenu($menu);
341        }
342        else
343        {
344            // adicionar a lista de orfãos
345            $this->arrOrphan[] = $menu;
346        }
347    }
348    public function getOrphanByIdParent($strId)
349    {
350        $result = null;
351        if($this->arrOrphan )
352        {
353            foreach($this->arrOrphan as $k=>$objMenu)
354            {
355                if( $objMenu->getIdParent() == $strId)
356                {
357                    $result=$objMenu;
358                    // remover o registro órfão
359                    array_splice($this->arrOrphan,$k,1);
360                    break;
361                }
362                $result = $objMenu->getOrphanByIdParent($strId);
363            }
364        }
365        return $result;
366    }
367    public function getStructure()
368    {
369        static $level = 0;
370        $xml=null;
371        if( $level == 0 )
372        {
373            // processar o itens que ficaram sem pai e coloca-los no nivel 0 para aparecer no menu principal
374            if($this->ignoreOrphans())
375            {
376                $this->clearOrphans();
377            }
378            else
379            {
380                if( $this->getOrphans())
381                {
382                    foreach($this->getOrphans() as $k=>$objMenu)
383                    {
384                        // não adicionar o item 2 vezes
385                        if( ! $this->getMenuById( $objMenu->getId() ) )
386                        {
387                            $objMenu->setIdParent(0);
388                               $this->addMenu($objMenu);
389                        }
390                    }
391                }
392            }
393            $xml ='<menu>'."\n";
394        }
395        else
396        {
397            if( $this->getSeparator() )
398            {
399                $xml = "<item id='{$this->getId()}' type='separator'";
400            }
401            else
402            {
403                $xml = "<item id='{$this->getId()}' text='{$this->getText()}'";
404            }
405            if($this->getDisabled())
406            {
407                $xml.= " enabled='false'";
408            }
409            if($this->getImg())
410            {
411                $xml.= " img='{$this->getImg()}'";
412            }
413            if($this->getImgDisabled())
414            {
415                $xml.= " imgDis='{$this->getImgDisabled()}'";
416            }
417            $xml.= ">\n";
418            if($this->getHotKey())
419            {
420                $xml.= "   <hotkey>{$this->getHotKey()}</hotkey>\n";
421            }
422            if($this->getToolTip())
423            {
424                $xml.= "   <tooltip>{$this->getToolTip()}</tooltip>\n";
425            }
426            if($this->getUrl())
427            {
428                $xml.= "   <userdata name='url'>{$this->getUrl()}</userdata>\n";
429            }
430            if($this->jsonParams)
431            {
432                $xml.= "   <userdata name='jsonParams'>{$this->jsonParams}</userdata>\n";
433                // este squi
434            }
435        }
436        if( is_array($this->arrMenu))
437        {
438            foreach($this->arrMenu as $k => $objMenu)
439            {
440                $level++;
441                $xml .= $objMenu->getStructure();
442                $level--;
443            }
444        }
445        $xml.= ($level > 0 ) ? '</item>' :"\n</menu>";
446        return $xml;
447    }
448    //--------------------------------------------------------------------------------------
449    public function addOrphanItems()
450    {
451        // adicionar os itens orfãos
452        if ($this->getOrphans()){
453            foreach( $this->getOrphans() as $k=>$objMenu){
454                $objMenuPai =  $this->getMenuById( $objMenu->getIdParent() );
455                if ( $objMenuPai ){
456                    $objMenuPai->addMenu($objMenu);
457                }
458            }
459        }
460    }
461    //--------------------------------------------------------------------------------------
462    /**
463     * Gerar um XML do Menu no formato do FormDin 4
464     * @param boolean $print
465     * @return string
466     */
467    public function getXml($print=true)
468    {
469        $this->addOrphanItems();
470        $print = $print === null ? true : $print;
471        if( $print) {
472            header ("content-type: text/xml; charset=".ENCODINGS);
473            echo str_replace("'",'"',$this->getStructure());
474        }else {
475            return str_replace("'",'"',$this->getStructure());
476        }
477        /*
478        // ------  Exemplo de XML Gerado ------
479        echo '<menu>
480        <item id="file" text="Administração">
481            <item id="new" text="New" img="new.gif"/>
482            <item id="file_sep_1" type="separator"/>
483            <item id="open" text="Open" img="open.gif">
484                <userdata name="url">app_server_action("limparCache",false, "script")</userdata>
485            </item>
486            <item id="save" text="Save" img="save.gif"/>
487            <item id="saveAs" text="Save As..." imgdis="save_as_dis.gif" enabled="false"/>
488            <item id="file_sep_2" type="separator"/>
489            <item id="print" text="Print" img="print.gif"/>
490            <item id="pageSetup" text="Page Setup" imgdis="page_setup_dis.gif" enabled="false"/> <item id="file_sep_3" type="separator"/> <item id="close" text="Close" img="close.gif"/>
491        </item>
492        <item id="edit" text="Edit">
493            <item id="undo" text="Undo" img="undo.gif"/>
494            <item id="redo" text="Redo" img="redo.gif"/>
495            <item id="edit_sep_1" type="separator"/>
496            <item id="selectAll" text="Select All" img="select_all.gif"/>
497            <item id="edit_sep_2" type="separator"/>
498            <item id="cut" text="Cut" img="cut.gif"/>
499            <item id="copy" text="Copy" img="copy.gif"/>
500            <item id="paste" text="Paste" img="paste.gif"/>
501        </item>
502        <item id="help" text="Help">
503            <item id="about" text="About..." img="about.gif"/>
504            <item id="needhelp" text="Help" img="help.gif"/>
505            <item id="bugReporting" text="Bug Reporting" img="bug_reporting.gif"/>
506        </item>
507        </menu>';
508        */
509    }
510}
511?>