<?php
class GnutecaUserMenu extends MDiv
{
	public function __construct()
	{
        parent::__construct('searchPanel', null,  'm-panel-body gUserMenu');
	}

	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';
        }

        //FIXME fazer base de dados ou configuração para isto no futuro
        $url                                    = array();
        $url['Home']->action                    = '/';
        $url['Home']->icon                      = 'gnuteca3-16x16.png';
		$url['My library']->action 	            = $MIOLO->getActionURL($module, 'main:myLibrary');
		$url['My library']->icon   	            = 'myLibrary-16x16.png';
		$url['Simple Search']->action           = $MIOLO->getActionURL($module, $location);
		$url['Simple Search']->icon  	        = 'search-16x16.png';
		$url['Clear']->action		            = 'javascript:gnuteca.clearForm()';
		$url['Clear']->icon   		            = 'clear-16x16.png';
		$url['Renew']->action		            = $MIOLO->getActionURL($module, 'main:myLibrary:myrenew');
		$url['Renew']->icon   		            = 'renew-16x16.png';
		$url[MYLIBRARY_REQUEST_TITLE]->action   = $MIOLO->getActionURL($module, 'main:myLibrary:congelado');
		$url[MYLIBRARY_REQUEST_TITLE]->icon     = 'congelado-16x16.png';
		$url['Help']->action		            = "javascript:miolo.doAjax('help', '', 'frm__mainForm')";
		$url['Help']->icon			            = 'help-16x16.png';


		$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);
				$button     = new MDiv(null, $content, 'gUserMenuButton');
				$buttons[]	= $button;
			}
		}

		$this->setInner( $buttons );

		return parent::generate();

		/*$div = new MDiv('searchPanel', $buttons ,  'm-panel-body gUserMenu');

		return $div;*/
	}
}
?>
