<?php
/**
 * <--- Copyright 2005-2011 de Solis - Cooperativa de Soluções Livres Ltda. e
 * Univates - Centro Universitário.
 *
 * Este arquivo é parte do programa Gnuteca.
 *
 * O Gnuteca é um software livre; você pode redistribuí-lo e/ou modificá-lo
 * dentro dos termos da Licença Pública Geral GNU como publicada pela Fundação
 * do Software Livre (FSF); na versão 2 da Licença.
 *
 * Este programa é distribuído na esperança que possa ser útil, mas SEM
 * NENHUMA GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO
 * ou APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/GPL em
 * português para maiores detalhes.
 *
 * Você deve ter recebido uma cópia da Licença Pública Geral GNU, sob o título
 * "LICENCA.txt", junto com este programa, se não, acesse o Portal do Software
 * Público Brasileiro no endereço www.softwarepublico.gov.br ou escreva para a
 * Fundação do Software Livre (FSF) Inc., 51 Franklin St, Fifth Floor, Boston,
 * MA 02110-1301, USA --->
 *
 * Class
 *
 * @author Lucas Gerhardt [lucas_gerhardt@solis.coop.br]
 *
 * @version $Id$
 *
 * \b Maintainers: \n
 * Jader Osvino Fiegenbaum [jader@solis.coop.br]
 * Jamiel Spezia [jamiel@solis.coop.br]
 * Lucas Gerhardt [lucas_gerhardt@solis.coop.br]
 *
 * @since
 * Class created on 06/10/2015
 *
 **/

$MIOLO = MIOLO::getInstance();
$MIOLO->page->addScript("gresponsivemenu.js", "gnuteca3");

class GResponsiveMenu extends MDiv
{
    /**
     * Identificador do trigger do menu
     *
     * @final {String} Id da div que conterá o menu
     */
    const ID_MENU_RESPONSIVE = "gResponsiveMenu";

    public $orientation;

    public function __construct( $orientation = 'horizontal' )
    {
        $class = $orientation ==  'horizontal' ? 'gResponsiveMenu' : 'gResponsiveMenuVertical';
        parent::__construct('searchResposiveMenu', null, $class );
        $this->orientation = $orientation;

        $this->page->addJsCode("
            expandRetractContainer = function(ContainerID)
            {
                var st = dojo.byId(ContainerID).style.display;

                try
                {
                    dojo.byId(ContainerID).style.display  = (st == 'none') ? 'block' : 'none';
                }
                catch(e){}
            }
        ");
    }

    public function getUrl()
    {
        $MIOLO           = MIOLO::getInstance();
        $module          = MIOLO::getCurrentModule();
        $busAuthenticate = $MIOLO->getBusiness('gnuteca3', 'BusAuthenticate');

        //Se tiver um operador logado, pega a pesquisa simples configurada por ele
        if (GOperator::isLogged())
        {
            $location = 'main:search:simpleSearch&formContentId=1&formContentTypeId=1';
        }
        else
        {
            $location = 'main:search:simpleSearch';
        }

        $url = array();
        $txt = explode("\n", GNUTECA_USER_MENU_LIST );

        //cria o array com as urls
        foreach ( $txt as $line => $info )
        {
            if ( $info )
            {
                $explode = explode(';',$info);
                $url[ trim( $explode[ 0 ] ) ]->action = trim( $explode[1] ) ;

                if ( $explode[2])
                {
                    $url[ trim( $explode[ 0 ] ) ]->icon   = trim( $explode[2] );
                }
            }
        }

        return $url;
    }

    public function getSearchMenu()
    {
        $MIOLO  = MIOLO::getInstance();
        $module = MIOLO::getCurrentModule();

        $busFormContent = $MIOLO->getBusiness($module, 'BusFormContent');
        $busFormContent->formContentType = FORM_CONTENT_TYPE_ADMINISTRATOR;
        $search = $busFormContent->searchFormContent(TRUE);

        if ( is_array( $search ) )
        {
            foreach ( $search as $line => $info )
            {
                if ($info->name != 'materialMovement')
                {
                    $url = 'index.php?module='.$module.'&action=main:search:simpleSearch&formContentId='.$info->formContentId.'&formContentTypeId='.$info->formContentType;
                    $info->action = $url;
                    $inner[$info->name] = $info;
                }
            }
        }

        if ( MUtil::getBooleanValue(GB_INTEGRATION ) )
        {
            $url = $url = 'index.php?module='.$module.'&action=main:search:simpleSearch&subForm=GoogleBook';
            $info->action = $url;
            $inner['GoogleBook'] = $info;
        }

        if ( GPerms::checkAccess('gtcZ3950', null, false) )
        {
            $url = $url = 'index.php?module='.$module.'&action=main:search:simpleSearch&subForm=Z3950';
            $info->action = $url;
            $inner['Z3950'] = $info;
        }

        if ( MUtil::getBooleanValue(FBN_INTEGRATION) )
        {
            $url = 'index.php?module='.$module.'&action=main:search:simpleSearch&subForm=FBN';
            $info->action = $url;
            $inner['B. Nacional'] = $info;
        }

        return $inner;
    }


    public function generate()
    {
        $MIOLO  = MIOLO::getInstance();
        $module = MIOLO::getCurrentModule();
        $ui 	= $MIOLO->getUi();
        $url 	= self::getUrl();
        $search = $this->getSearchMenu();

        $menuButton = new MImage('buttonMenuResponsive', '', GUtil::getImageTheme('menuResponsivo.png'));

        //MENU PESQUISA
        if ( is_array($search) )
        {
            foreach ( $search as $linha => $inf )
            {
                $labelLine = new MLabel($linha);
                $labelLine->addStyle('width', '100% !important');
                $divLine = new MDiv('divLine' . $linha , $labelLine);
                $divLine->setClass('gResponsiveMenuOptions');
                $content    = null;
                $content[]  = $mLink = new MLink('linkResponsiveMenu' . $linha, $divLine, $inf->action);
                $class      = $this->orientation ==  'horizontal' ? 'gResponsiveMenuButton' : 'gResponsiveMenuButtonVertical';
                $classEx    = $this->orientation ==  'horizontal' ? 'gResponsiveMenuButtonEx' : 'gResponsiveMenuButtonExVertical';
                $inner      = new MDiv(null, $content, $classEx);
                $button     = new MDiv(null, $inner, $class);
                $buttons[]	= $button;
            }
        }

        //SEPARADOR
        $buttons[] = new MDiv('divHrMenuResponsivo', "<hr>");

        //MINHA BIBIOTECA
        $labelLine = new MLabel('MINHA BIBIOTECA');
        $labelLine->addStyle('width', '100% !important');
        $divLine = new MDiv('divLineMinhaBiblioteca' , $labelLine);
        $divLine->setClass('gResponsiveMenuOptions');
        $content = $mLink = new MLink('linkMyLibrary', $divLine, 'javascript:' . GUtil::getAjax('subForm', 'MyLibrary'));
        $class      = $this->orientation ==  'horizontal' ? 'gResponsiveMenuButton' : 'gResponsiveMenuButtonVertical';
        $classEx    = $this->orientation ==  'horizontal' ? 'gResponsiveMenuButtonEx' : 'gResponsiveMenuButtonExVertical';
        $inner      = new MDiv(null, $content, $classEx);
        $buttons[]     = new MDiv(null, $inner, $class);

        //MENU PERSONALIZADO
        if ( is_array( $url ) )
        {
            foreach ( $url as $line => $info )
            {
                $labelLine = new MLabel($line);
                $labelLine->addStyle('width', '100% !important');
                $divLine = new MDiv('divLine' . $line , $labelLine);
                $divLine->setClass('gResponsiveMenuOptions');
                $content    = null;
                $content[]  = $mLink = new MLink('linkResponsiveMenu' . $line, $divLine, $info->action.';');
                $class      = $this->orientation ==  'horizontal' ? 'gResponsiveMenuButton' : 'gResponsiveMenuButtonVertical';
                $classEx    = $this->orientation ==  'horizontal' ? 'gResponsiveMenuButtonEx' : 'gResponsiveMenuButtonExVertical';
                $inner      = new MDiv(null, $content, $classEx);
                $button     = new MDiv(null, $inner, $class);
                $buttons[]	= $button;
            }
        }

        $divMenu[] = $contMenu = new GContainer('contMenuResponsive', array($menuButton));
        $contMenu->addAttribute('onclick', "expandRetractContainer('divButtonsResponsive')");
        $contMenu->addStyle('cursor', 'pointer');
        $contMenu->addStyle('float', 'left');

        $divMenu[] = $divButtons = new MDiv('divButtonsResponsive', $buttons);
        $divButtons->addStyle('display', 'none');

        $this->setInner( $divMenu );

        return parent::generate();
    }
}
?>
