<?php

/**-----------------------------------------------
*
* Rooty, 2018 <rooty@rooty.me>
*
*
* This software is protected by copyright, please
* read the file COPYRIGHT.
* This program is distributed in the hope that it
* will be useful, but without any warranty; without
* even the implied warranty of merchantability or
* fitness for a particular purpose. Please
* read the file LICENCE.
*
* Fichier NavigationPrint.php
*
* Ce fichier contient la classe NavigationPrint
*<BR>
* @author Initiance <www.initiance.com|martial@initiance.com>
* @copyright Copyright &copy; 2009-2018, Rooty 
* @since 2004/06/07
* @version 0.3
* @link www.rooty.me
* @package object_printing
* @subpackage NavigationPrint
*/
if ( !defined('SYSTEM_IN') )
{
 die("Hacking attempt");
}

/**
* Classe NavigationPrint
*<p>cette classe permet la recuperation de l'NavigationPrint</p>
* @package object_printing
* @subpackage NavigationPrint
*/
class NavigationPrint extends Printing
{

	//
	// Constructor
	//
/**
* Constructeur de la classe NavigationPrint
* @access public
*/
	function NavigationPrint()
	{
		$this->__construct();
	}

	function __construct()
	{
		parent::__construct("1.1", "Rooty <rooty@rooty.me>", "Affichage de la NavigationNode");
	}

/**
* Remplace dans la template principale
* @access public
*/
	function __print()
	{
        global $SERVER_NAME, $SCRIPT_NAME, $CONF_AUTH_SECURE;
        
		/* gestion du lib en fonction de la langue courante */
		$currentCode=$GLOBALS["SYSTEM_USER_SESSION"]->langue->getCode();
		$defaultCode=$GLOBALS["SYSTEM_DEFAULT_LANGUAGE_CODE"];
		/* fin récupération des codes par defaut */

		$MaTemplate = new ModeliXeQware($GLOBALS['SYSTEM_TEMPLATE_PATH'].$GLOBALS['SYSTEM_TEMPLATE_DIR']."object_printing/navigationprint/main.mxt");
		$MaTemplate->SetModeliXe(true);
		$MaTemplate->MxText("NavigationPrintName", getMessage("NavigationPrintName"));
		$Node	=$GLOBALS["SYSTEM_USER_SESSION"]->getNode($GLOBALS["SYSTEM_USER_SESSION"]->entryNodeId);

		/* recuperation de l'image de la langue courante */
		if (file_exists($GLOBALS['SYSTEM_TEMPLATE_PATH'].$GLOBALS['SYSTEM_TEMPLATE_DIR']."media/picto/node/".strtolower($Node->getIcone())) && ($Node->getIcone()!=""))
		{
			$img=$GLOBALS['SYSTEM_TEMPLATE_PATH'].$GLOBALS['SYSTEM_TEMPLATE_DIR']."media/picto/node/".strtolower($Node->getIcone());
		}else{
			$img=$GLOBALS['SYSTEM_TEMPLATE_PATH'].$GLOBALS['SYSTEM_TEMPLATE_DIR']."media/picto/node/defaultnode.gif";
		}
		
		// BLOC INFOS UTILISATEUR
		if ($GLOBALS["SYSTEM_USER_SESSION"]->user->getId()==$GLOBALS["SYSTEM_DEFAULT_USER_GUEST"])
		{
            $nodeId = $GLOBALS["SYSTEM_USER_SESSION"]->navigationNodeId;
			
			/* création de l'url de login */
			$url =$_SERVER['SCRIPT_NAME'];
			$url.="?system=authSession";
			$url=addIdToUrl($url);

			$tabObject	=$this->getObjectSon(eid());
			for ($i=0; $i<count($tabObject);$i++)
			{
				if ($tabObject[$i][0]->getClassName()=="Home")
				{
					$urlArray["id"]=$tabObject[$i][1]->getId();
				}
			}
			
			/* Création de l'URL pour nouveau compte */
			$urlNewAccount = $_SERVER['SCRIPT_NAME'];
			$urlNewAccount.= "?system=authNewAccount";
			$urlNewAccount = addIdToUrl($urlNewAccount);
			$MaTemplate->MxAttribut("urlActionNewAccount", formatUrl($urlArray["id"],"Home","formLecture","",recupCommunity()));
			$MaTemplate->MxAttribut("urlActionNewSubscription", formatUrl($urlArray["id"],"Home","formLectureAdh","",recupCommunity()));
			
			/* Création de l'URL pour rappel du mot de passe */
			$urlForgotPassword = $_SERVER['SCRIPT_NAME'];
			$urlForgotPassword.= "?system=authForgotPassword";
			$urlForgotPassword = addIdToUrl($urlForgotPassword);
			
			$MaTemplate->MxText("blocGUEST".".auth", getMessage("UserPrintAuth"));
			$MaTemplate->MxText("blocGUEST".".LOGIN", getMessage("UserPrintLogin"));
			$MaTemplate->MxFormField("blocGUEST".".login", "text", "login", "", "title='".getMessage("tipUserPrintLogin")."'");
			$MaTemplate->MxText("blocGUEST".".PASSWORD", getMessage("UserPrintPassword"));
			$MaTemplate->MxFormField("blocGUEST".".password", "password", "password", "", "title='".getMessage("tipUserPrintPassword")."'");
			$MaTemplate->MxAttribut("blocGUEST".".urlActionNewAccount", $urlNewAccount);
			$MaTemplate->MxText("blocGUEST".".NEWACCOUNT", getMessage("UserPrintNewAccount"));
			$MaTemplate->MxText("blocGUEST".".NEWSUBSCRIPTION", getMessage("UserPrintNewSubscription"));
			$MaTemplate->MxAttribut("blocGUEST".".urlActionForgotPassword", $urlForgotPassword);
			$MaTemplate->MxText("blocGUEST".".FORGOTPASSWORD", getMessage("UserPrintForgotPassword"));
			$MaTemplate->MxAttribut("blocGUEST".".urlAction", $url);
			$MaTemplate->MxAttribut("blocGUEST".".BtnEnter", getMessage("UserPrintEnter"));
			$MaTemplate->MxAttribut("blocGUEST".".titleButton", getMessage("tipUserPrintEnter"));

            $MaTemplate->MxBloc("blocAUTH", "delete");
        } else {
            /* création de l'url de déconnexion */
			$urlDisco =$_SERVER['SCRIPT_NAME'];
			$urlDisco.="?system=disconnectSession";
			$urlDisco=addIdToUrl($urlDisco);
			
			// On récupère la liste des objets pour l'EID courant (racine)
			$tabObject	=$this->getObjectSon(eid());
			
			/* création de l'url de l'outil de gestion du compte utilisateur */
			$nodeId = $GLOBALS["SYSTEM_USER_SESSION"]->navigationNodeId;
			$tabObjectNode	=$this->getObjectSon($nodeId);

			for ($j=0; $j<count($tabObjectNode);$j++)
			{
				if ($tabObjectNode[$j][0]->getClassName()=="UserModif")
				{
					$objID=$tabObjectNode[$j][1]->getId();
					$objCN=$tabObjectNode[$j][0]->getClassName();
					$objMN=$tabObjectNode[$j][0]->getMethodName();
				}
			}
			
// 			var_dump($objID."\n".$objCN."\n".$objMN);
			
			if( isset($objID) AND isset($objCN) AND isset($objMN) )
                $MaTemplate->MxAttribut("blocAUTH".".urlAccount", formatUrl($objID, $objCN, $objMN, "", ""));
            
			$MaTemplate->MxAttribut("blocAUTH".".disconnectURL", $urlDisco);
			$MaTemplate->MxText("blocAUTH".".firstName", $GLOBALS["SYSTEM_USER_SESSION"]->user->getFirstName());
			$MaTemplate->MxText("blocAUTH".".lastName", $GLOBALS["SYSTEM_USER_SESSION"]->user->getLastName());
// 			$MaTemplate->MxText("id", $GLOBALS["SYSTEM_USER_SESSION"]->user->getId());
// 			$MaTemplate->MxText("mail", $GLOBALS["SYSTEM_USER_SESSION"]->user->getMail());
// 			$MaTemplate->MxText("organisation", $GLOBALS["SYSTEM_USER_SESSION"]->user->getOrganisation());
// 			$MaTemplate->MxText("title", $GLOBALS["SYSTEM_USER_SESSION"]->user->getTitle());
// 			$MaTemplate->MxText("postalAddress", $GLOBALS["SYSTEM_USER_SESSION"]->user->getPostalAddress());
// 			$MaTemplate->MxText("postalCode", $GLOBALS["SYSTEM_USER_SESSION"]->user->getPostalCode());
// 			$MaTemplate->MxText("state", $GLOBALS["SYSTEM_USER_SESSION"]->user->getState());
// 			$MaTemplate->MxText("country", $GLOBALS["SYSTEM_USER_SESSION"]->user->getCountry());
			$MaTemplate->MxText("blocAUTH".".login", $GLOBALS["SYSTEM_USER_SESSION"]->user->getLogin());
// 			$MaTemplate->MxText("tel", $GLOBALS["SYSTEM_USER_SESSION"]->user->getTel());
			$MaTemplate->MxText("blocAUTH".".decon", getMessage("UserPrintDecon"));
// 			$MaTemplate->MxAttribut("decon", getMessage("UserPrintDecon"));
// 			$MaTemplate->MxText("fax", $GLOBALS["SYSTEM_USER_SESSION"]->user->getFax());
// 			$MaTemplate->MxText("TxtWelcome", getMessage("TxtWelcome"));

            $MaTemplate->MxBloc("blocGUEST", "delete");
        }

		$urlToGo = formatUrlNode("navigationNode", $Node->getId());

		/* pour l'url */
		$MaTemplate->MxAttribut("nodeUrl", htmlentitiesconv($urlToGo));
		
		if ( $Node->getLibelle($currentCode) != "" )
		{
			if( !strstr("0.", $Node->getLibelle($currentCode)) )
			{
				$lib=$Node->getLibelle($currentCode);
			}
		} else {
				$lib = "";
		}

		if ($lib=="")
		{
			if ( $Node->getLibelle($currentCode) != "" )
			{
				if( !strstr("0.", $Node->getLibelle($currentCode)) )
					$lib=$Node->getLibelle($defaultCode);
			}
		}

		$MaTemplate->MxImage("pictoNode", $img , htmlentitiesconv($lib),"");
		$MaTemplate->MxText("nodeName", htmlentitiesconv($lib));

		/* On affiche les noeuds suivants */
		$forNode=$this->printForNode($GLOBALS["SYSTEM_USER_SESSION"]->entryNodeId);
		$MaTemplate->MxText("Navigation", $forNode);
		$data=$MaTemplate->MxWrite();

		if ($forNode!="")
		{
			$GLOBALS["SYSTEM_TEMPLATE"]->MxText("navigationprint", $data);
		}else{
			$GLOBALS["SYSTEM_TEMPLATE"]->MxText("navigationprint", "");
		}
	}

/**
* affichage d'un noeud et de tous ses sous noeuds
* @access public
*/
	function printForNode($nodeId, $modelixeMxt="navigationnode.mxt")
	{
		global $SERVER_NAME, $SCRIPT_NAME, $CONF_PERSISTANT_SECURE;

		$MaTemplate = new ModeliXeQware($GLOBALS['SYSTEM_TEMPLATE_PATH'].$GLOBALS['SYSTEM_TEMPLATE_DIR']."object_printing/navigationprint/".$modelixeMxt);
		$MaTemplate->SetModeliXe(true);

		/* gestion du lib en fonction de la langue courante */
		$currentCode=$GLOBALS["SYSTEM_USER_SESSION"]->langue->getCode();
		$defaultCode=$GLOBALS["SYSTEM_DEFAULT_LANGUAGE_CODE"];
		/* fin récupération des codes par defaut */

		/**
		* recuperation des "tools"
		* recuperation des "reps"
		* + merge des 2 tableaux
		*/
		$tabObj=array_merge($this->getNodeSon($nodeId), $this->getObjectSon($nodeId));

		/* Pour le Tri/Nom */
		$tabName=array();

		for ($i=0; $i<count($tabObj);$i++)
		{
			if(!is_object($tabObj[$i]))	// c un obj!!!!
			{
				if( strpos( $tabObj[$i][1]->getName($currentCode), "zz. ") === false )
				{
					if( $GLOBALS["SYSTEM_USER_SESSION"]->user->getId() != 1 )
					{
						if( !in_array($tabObj[$i][1]->getName($currentCode), $GLOBALS['EXCLUDETOOLSFROMUSERMENU']) )
						{
							$lib=$tabObj[$i][1]->getName($currentCode);
						} else {
							$lib = false;
                        }
					} else {
						if( !in_array($tabObj[$i][1]->getName($currentCode), $GLOBALS['EXCLUDETOOLSFROMGUESTMENU']) )
						{
							$lib=$tabObj[$i][1]->getName($currentCode);
						} else {
							$lib = false;
                        }
					}
				} else {
					$lib = false;
				}
			}else{
				if( strpos( $tabObj[$i]->getLibelle($currentCode), "zz. ") === false )
				{
					if( $GLOBALS["SYSTEM_USER_SESSION"]->user->getId() != 1 )
						$lib=$tabObj[$i]->getLibelle($currentCode);
					else
						if( !in_array($tabObj[$i][1]->getName($currentCode), $GLOBALS['EXCLUDETOOLSFROMGUESTMENU']) )
							$lib=$tabObj[$i][1]->getName($currentCode);
						else
							$lib = false;
				} else {
					$lib = false;
				}
			}

			if( $lib && !empty($lib) )
				$tabName[$i]=$lib;
		}
		asort($tabName);
		reset($tabName);
		while (list($i, $lib) = each($tabName))
		{
			/* en fonction de l'obj */
			if(is_object($tabObj[$i]))	// c un dir!!!!
			{
				/* recuperation de l'image de la langue courante */
				if (file_exists("data/image/icons/node/".strtolower($tabObj[$i]->getIcone())) && ($tabObj[$i]->getIcone()!=""))
				{
					$img="data/image/icons/node/".strtolower($tabObj[$i]->getIcone());
				}else{
					$img="data/image/icons/node/defaultnode.gif";
				}

				/* on prepare l'url */
				$urlToGo = formatUrlNode("navigationNode", $tabObj[$i]->getId());

				/* pour l'url */
				$MaTemplate->MxAttribut("bloc1.nodeUrl", $urlToGo);
				$MaTemplate->MxAttribut("bloc1.blocDropdown.nodeUrl", $urlToGo);
				$MaTemplate->MxAttribut("bloc1.blocNoDropdown.nodeUrl", $urlToGo);
				$lib=$tabObj[$i]->getLibelle($currentCode);

				if ($lib=="")
				{
					$lib=$tabObj[$i]->getLibelle($defaultCode);
				}

				/*Fonction permettant de supprimer les numeros, le point et l'espace à l'affichage*/
                $lib = ltrim(substr( $lib, strpos($lib, ". "), strlen($lib) ), ". ");
                $MaTemplate->MxImage("bloc1.pictoNode", $img , htmlentitiesconv($lib), "");
				$MaTemplate->MxImage("bloc1.blocDropdown.pictoNode", $img , htmlentitiesconv($lib), "");
				$MaTemplate->MxImage("bloc1.blocNoDropdown.pictoNode", $img , htmlentitiesconv($lib), "");

				/* objet en cours */
				$bool=false;
				$tabSon=$GLOBALS["SYSTEM_USER_SESSION"]->listUserNode->getSonOfNodeAllowed(eid(),$tabObj[$i]->getId());
				for($l=0;$l<count($tabSon);$l++)
				{
					if ($tabSon[$l]->getId()==nid())
					{
						$bool=true;
						break;
					}
				}
                
				if ($tabObj[$i]->getId()==nid() || $bool==true) {
                    $classBoostrapMenu = " active";
				} else {
                    $classBoostrapMenu = "";
                }
				/* Fin des objets en cours */

                $MaTemplate->MxText("bloc1.nodeName", htmlentitiesconv($lib));
				$MaTemplate->MxText("bloc1.blocDropdown.nodeName", htmlentitiesconv($lib));
				$MaTemplate->MxText("bloc1.blocNoDropdown.nodeName", htmlentitiesconv($lib));
				// on récupére les sous niveau...

				$ssLevel=$this->printForNode($tabObj[$i]->getId(), ( (($modelixeMxt=="navigationsub.mxt") || ($modelixeMxt=="navigationdiv.mxt")) && (($modelixeMxt!="navigationnode.mxt"))?"navigationdiv.mxt":"navigationsub.mxt"));
				$MaTemplate->MxText("bloc1.blocDropdown.nodeDir.nodeDir", $ssLevel);

				if( $ssLevel ) {
                    $classBoostrapMenu = " dropdown".$classBoostrapMenu;
                    $MaTemplate->MxBloc("bloc1.blocNoDropdown", "delete");
                } else {
                    $classBoostrapMenu = $classBoostrapMenu;
                    $MaTemplate->MxBloc("bloc1.blocDropdown", "delete");
                }
                
                $MaTemplate->MxAttribut("bloc1.class", $classBoostrapMenu);
			}else{
				/* recuperation de l'image de la langue courante */
				if (file_exists("data/image/icons/object/".strtolower($tabObj[$i][1]->getIcone())) && ($tabObj[$i][1]->getIcone()!=""))
				{
					$img="data/image/icons/object/".strtolower($tabObj[$i][1]->getIcone());
				}else{
					$img="data/image/icons/object/defaultobject.gif";
				}

				$urlToGo = formatUrl($tabObj[$i][1]->getId(), $tabObj[$i][0]->getClassName(), $tabObj[$i][0]->getMethodName(),"","",$tabObj[$i][0]->getNodeId());

				/* pour l'url */
				$MaTemplate->MxAttribut("bloc1.nodeUrl", $urlToGo);
				$MaTemplate->MxAttribut("bloc1.blocDropdown.nodeUrl", $urlToGo);
				$MaTemplate->MxAttribut("bloc1.blocNoDropdown.nodeUrl", $urlToGo);
				$lib=$tabObj[$i][1]->getName($currentCode);

				if ($lib=="")
				{
					$lib=$tabObj[$i][1]->getName($defaultCode);
				}
				
                $lib = ltrim(substr( $lib, strpos($lib, ". "), strlen($lib) ), ". ");

                $MaTemplate->MxImage("bloc1.pictoNode", $img ,htmlentitiesconv($lib), "");
				$MaTemplate->MxImage("bloc1.blocDropdown.pictoNode", $img ,htmlentitiesconv($lib), "");
				$MaTemplate->MxImage("bloc1.blocNoDropdown.pictoNode", $img ,htmlentitiesconv($lib), "");

				/* objet en cours */
				if ( ($tabObj[$i][1]->getId()==oid() ) && ( strtolower($tabObj[$i][0]->getClassName())==strtolower(className())) && (!(strpos(strtolower(methodName()), strtolower(str_replace("action","",$tabObj[$i][0]->getMethodName())))===false)) ) {
                    $MaTemplate->MxAttribut("bloc1.class", "active");
					$MaTemplate->MxAttribut("bloc1.blocDropdown.class", "nav-item active");
					$MaTemplate->MxAttribut("bloc1.blocNoDropdown.class", " nav-item active");
				} else {
                    $MaTemplate->MxAttribut("bloc1.class", "");
					$MaTemplate->MxAttribut("bloc1.blocDropdown.class", "nav-item");
					$MaTemplate->MxAttribut("bloc1.blocNoDropdown.class", "nav-item");
                }
				/* Fin des objets en cours */

				/*Fonction permettant de supprimer les numeros, le point et l'espace à l'affichage*/
                $lib = preg_replace("/^[[:space:]]*[a-z]+[.)".chr(176)."][[:space:]]+/i", "", $lib);
				$MaTemplate->MxText("bloc1.nodeName", htmlentitiesconv($lib));
				$MaTemplate->MxText("bloc1.blocNoDropdown.nodeName", htmlentitiesconv($lib));
				$MaTemplate->MxText("bloc1.blocDropdown.nodeName", htmlentitiesconv($lib));
				$MaTemplate->MxBloc("bloc1.blocDropdown.nodeDir", "delete");

				if ( $lib && ($GLOBALS["SYSTEM_USER_SESSION"]->user->getId() == 1) )
					$MaTemplate->MxBloc("bloc1.blocDropdown", "delete");
			}
			$MaTemplate->MxBloc("bloc1", "loop");
		}
		
		if (count($tabName)!=0)
			$data=$MaTemplate->MxWrite();
		else
			$data="";
		return $data;
	}

/**
* recupere les repertoires d'un noeud
* @access public
*/
	private function getNodeSon($nodeId)
	{
		$tab=$GLOBALS["SYSTEM_USER_SESSION"]->getSonOfNodeAllowed($nodeId);
		return $tab;
	}

/**
* recupere les fils d'un noeud depuis le noeud principal
* @access public
*/
	private function getObjectSon($nodeId)
	{
		$tab=$GLOBALS["SYSTEM_CONTROL"]->getListObjectForNavigation($nodeId);
		return $tab;
	}
}
?>
