<?php
class GnutecaUserMenu extends MDiv
{
	public $orientation;

	public function __construct( $orientation = 'horizontal' )
	{
		$class = $orientation ==  'horizontal' ? 'gUserMenu' : 'gUserMenuVertical';
        parent::__construct('searchPanel', null,  'm-panel-body ' . $class );
        $this->orientation = $orientation;
	}

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

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

        $url = array();

        //1 Menu para as Pesquisas e outro para Minha Biblioteca
        $local = (MIOLO::_REQUEST('action'));
        if ( ereg("myLibrary", $local) )
        {
            $txt = explode("\n", GNUTECA_USER_MENU_MYLIBRARY );
        }
        else
        {
            $txt = explode("\n", GNUTECA_USER_MENU_LIST );
        }
        

        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] );
	        	}
        	}
        }

		/*$actionUrl = '&redirect_action=' . MIOLO::getCurrentAction() . '&goto_direct=1';

		if ( $busAuthenticate->getUserCode() )
		{
            $url['Logoff']->action = $MIOLO->getActionURL($module, 'main:myLibrary:logoff') . $actionUrl;
            $url['Logoff']->icon   = 'exit-16x16.png';
 		}
		else
		{
            $url['Login']->action = $MIOLO->getActionURL($module, 'main:myLibrary:login') . $actionUrl;
            $url['Login']->icon   = 'login-16x16.png';
		}*/

		return $url;
	}


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

		if ( is_array( $url ) )
		{
			foreach ( $url as $line => $info )
			{
				$content    = null;
				$imageUrl	= $ui->getImage('gnuteca3', $info->icon);
				$content[]  = new MImageButton( null,  _M($line, $module), $info->action, $imageUrl );
				$content[]  = new MLink(null, _M($line, $module), $info->action);
				$class = $this->orientation ==  'horizontal' ? 'gUserMenuButton' : 'gUserMenuButtonVertical';
				$classEx = $this->orientation ==  'horizontal' ? 'gUserMenuButtonEx' : 'gUserMenuButtonExVertical';
                $inner      = new MDiv(null, $content, $classEx);
				$button     = new MDiv(null, $inner, $class);
				$buttons[]	= $button;
			}
		}

		$this->setInner( $buttons );

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