<?php

/**
 * 'menu_class' table.
 *
 * @package model
 */

class menu_class {

  private $n_RoleId      ='UNKNOWN';
  private $n_StartTaskId ='UNKNOWN';
  private $n_GlobalAccess='N';

  protected $n_MenuInit    ='N';

  protected $n_TaskType    ='UNKNOWN';
  protected $n_TaskId      ='UNKNOWN';
  protected $n_TaskDesc    ='UNKNOWN';
  protected $n_ScriptId    ='UNKNOWN';
  protected $n_SubsysDir   ='UNKNOWN';
  protected $n_TaskPrefix  ='UNKNOWN';
  protected $n_IsDisabled  ='Y';
  protected $n_Access      =FALSE;

  /**
   * Inicializa los parametros de la clase
   *
   * @param String $role
   */
  public function getInit($role){
    /*
    $c = new Criteria();
    $c->add(mnu_rolePeer::ROLE_ID, $role);
    $db_MnuRole = mnu_rolePeer::doSelectOne($c);
    */
    $con = Conexion::getConexion();
    $sql = "SELECT * FROM mnu_role WHERE role_id='".$role."';";
    $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
    $db_MnuRole = $result ? mysql_fetch_object($result) : null;
    
    if ($db_MnuRole)
    {
      $this->n_RoleId = $db_MnuRole->role_id;
      $this->n_StartTaskId = $db_MnuRole->start_task_id;
      $this->n_GlobalAccess= $db_MnuRole->global_access;

      $this->n_MenuInit= 'Y';
    }
  }

  /**
   * Retorna la tarea inicial del role
   *
   * @return string
   */
  public function getStartTaskId()
  {
    if ($this->n_MenuInit == 'N')
      return;

    return $this->n_StartTaskId;
  }

  /**
   * Retorna un array de parametros con referencia a la tarea
   * consideracion:
   * si no es inicializado o
   * si tipo de tarea es <> PROC o
   * si la tarea no pertenece al
   * role devuelve un array = NULL
   *
   * @param string $task
   * @return un array
   */
  public function getTask($task)
  {
    if ($this->n_MenuInit == 'N')
        return Array();

    $this->getMnuRoleTask($task);

    if ($this->n_TaskType != 'proc')
        return Array();

    $eTask['TaskId']    = $this->n_TaskId;
    $eTask['TaskDesc']  = $this->n_TaskDesc;
    $eTask['ButtonText']= $this->n_ButtonText;
    $eTask['TaskType']  = $this->n_TaskType;
    $eTask['ScriptId']  = $this->n_ScriptId;
    $eTask['IsDisabled']= $this->n_IsDisabled;
    $eTask['SubsysDir'] = $this->n_SubsysDir;
    $eTask['TaskPrefix']= $this->n_TaskPrefix;
    $eTask['Access']    = $this->n_Access;
    $eTask['NavButton'] = 'UNKNOWN';

    if ($eTask['Access'] && $eTask['IsDisabled']=='N') {
        $eTask['PathScriptId'] = $eTask['SubsysDir'].$eTask['ScriptId'];
         $eTask['PathImageId']  = $this->getImagesDir2($eTask['SubsysDir'],$eTask['ScriptId']);
    }else{
        $eTask['TaskType']    = 'UNKNOWN';
        $eTask['PathScriptId']= 'UNKNOWN';
        $eTask['PathImageId'] = 'UNKNOWN';
    }

    if ($eTask['TaskType'] == 'proc') {
        $eTask['NavButton'] = $this->getMnuMenuNav($eTask['TaskId']);
        $eTask['TaskField'] = $this->getMnuRoleTaskField($eTask['TaskId']);
    } else {
        $eTask['NavButton'] = Array();
        $eTask['TaskField'] = Array();
    }

    return $eTask;
  }

  /**
   * Enter description here...
   *
   * @param unknown_type $task
   * @param unknown_type $IsLevel
   * @return unknown
   */
  public function getMenu($task,$IsLevel = 0){
    if ($this->n_MenuInit == 'N'){
        return Array();
    }
    return ($this->getMenuArray($task,$IsLevel));
  }

  /**
   * Enter description here...
   *
   * @param unknown_type $task
   * @return unknown
   */
  public function getTaskHelp($task)
  {
    if ($this->n_MenuInit == 'N')
      return;
    /*
    $c = new Criteria();
    $c->add(help_textPeer::TASK_ID, $task);
    $db_HelpText = help_textPeer::doSelectOne($c);
    */
    $con = Conexion::getConexion();
    $sql = "SELECT * FROM help_text WHERE task_id = '".$task."';";
    $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
    $db_HelpText = $result ? mysql_fetch_object($result) : null;
    
    if ($db_HelpText)
    {
      return $db_HelpText->help_text;
    }
    return;
  }

  /**
   * Retorna el menu
	 1-mnu_menuPeer::MENU_ID
	 2-mnu_menuPeer::TASK_ID_JNR
	 3-mnu_menuPeer::SORT_SEQ
	 4-mnu_menuPeer::BUTTON_TEXT
	 5-mnu_taskPeer::TASK_DESC
	 6-mnu_taskPeer::TASK_TYPE
	 7-mnu_taskPeer::SCRIPT_ID
	 8-mnu_taskPeer::IS_DISABLED
   *
   * @param string $menu
   * @return array
   */
  private function getMnuMenu($menu){
    /*
    $menu_lst = Array();
    $c = new Criteria();
    $c->add(mnu_menuPeer::MENU_ID, $menu);

    mnu_menuPeer::addSelectColumnsSome($c);
    $db_MnuMenu = mnu_menuPeer::doSelectRS($c);
    */
    $con = Conexion::getConexion();
    $sql = "SELECT mnu_menu.menu_id, mnu_menu.task_id_jnr, mnu_menu.sort_seq, mnu_menu.button_text, mnu_task.task_desc, 
                   mnu_task.task_type, mnu_task.script_id, mnu_task.is_disabled
            FROM mnu_menu
              Inner Join mnu_task ON mnu_menu.task_id_jnr = mnu_task.task_id
            WHERE menu_id = '".$menu."'";
    $result   = mysql_db_query(Conexion::getNameDb(), $sql, $con);
    $num_rows = mysql_num_rows($result);
    //$db_MnuMenu = $result ? mysql_fetch_object($result) : null;

    if ($num_rows>0) {
      while ($db_MnuMenu = mysql_fetch_object($result)) {
        $seq = $db_MnuMenu->sort_seq;

        $menu_lst[$seq]['TaskId']     = $db_MnuMenu->task_id_jnr;
        $menu_lst[$seq]['ButtonText'] = $db_MnuMenu->button_text;
        $menu_lst[$seq]['TaskDesc']   = $db_MnuMenu->task_desc;
        $menu_lst[$seq]['TaskType']   = $db_MnuMenu->task_type;
        $menu_lst[$seq]['IsDisable']  = $db_MnuMenu->is_disabled;

        $this->getMnuRoleTask($menu_lst[$seq]['TaskId']); //condicionar
        $menu_lst[$seq]['Access']       = $this->n_Access;
        $menu_lst[$seq]['PathScriptId'] = $this->n_SubsysDir.$db_MnuMenu->script_id;
      }

      if (count($menu_lst) > 0)
          return $menu_lst;
    }
    return Array();
  }

  /**
   * Retorna el menu_nav
   * $db_MnuMenu->getString(1)  task_id_snr
   * $db_MnuMenu->getString(3)  sort_seq
   * $db_MnuMenu->getString(2)  task_id_jnr
   * $db_MnuMenu->getString(4)  button_text
   *
   * @param string $menu
   * @return array
   */
  private function getMnuMenuNav($menu){
    /*
    $menu_lst = Array();
    $c = new Criteria();
    $c->add(mnu_nav_buttonPeer::TASK_ID_SNR, $menu);
    $db_MnuMenu = mnu_nav_buttonPeer::doSelectRS($c);
    *///falta
    $con = Conexion::getConexion();
    $sql = "SELECT * FROM mnu_nav_button Inner Join mnu_task ON mnu_nav_button.task_id_jnr = mnu_task.task_id WHERE task_id_snr = '".$menu."';";
    $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
    $num_rows = mysql_num_rows($result);
    //$db_HelpText = $result ? mysql_fetch_object($result) : null;
    
    if ($num_rows>0) {
      while ($ob_mnuNavButton = mysql_fetch_object($result)) {

        //$ob_mnuNavButton = new mnu_nav_button();
        //$ob_mnuNavButton->hydrate($db_MnuMenu);

        $seq = $ob_mnuNavButton->sort_seq;

        $menu_lst[$seq]['TaskId']		  = $ob_mnuNavButton->task_id_jnr;
        $menu_lst[$seq]['ButtonText']	= $ob_mnuNavButton->button_text;
        $menu_lst[$seq]['TaskDesc']		= $ob_mnuNavButton->task_desc; //TASK
        $menu_lst[$seq]['TaskType']		= $ob_mnuNavButton->task_type; //task
        $menu_lst[$seq]['IsDisable']	= $ob_mnuNavButton->is_disabled; //task
        $menu_lst[$seq]['BarType']		= $ob_mnuNavButton->bar_type;
		    $menu_lst[$seq]['Settings']		= $ob_mnuNavButton->settings; //task
		
        $this->getMnuRoleTask($menu_lst[$seq]['TaskId']);
        $menu_lst[$seq]['Access']       = $this->n_Access;
        $menu_lst[$seq]['PathScriptId'] = $this->n_SubsysDir.$ob_mnuNavButton->script_id; //task
         $eTask['PathImageId']  = $this->getImagesDir2($eTask['SubsysDir'],$eTask['ScriptId']);
      }
      if (count($menu_lst) > 0){
          ksort($menu_lst);
          return $menu_lst;
      }
    }

    return Array();
  }

  /**
   * Enter description here...
   *
   * @param unknown_type $taskId
   * @return unknown
   */
  private function getMnuRoleTaskField($taskId)
  {
    $FieldLst = Array();

    if ($this->n_GlobalAccess == 'N') {
        /*
        $c = new Criteria();
        $c->add(mnu_role_taskfieldPeer::ROLE_ID, $this->n_RoleId);
        $c->add(mnu_role_taskfieldPeer::TASK_ID, $taskId);
        $db_MnuTaskFields = mnu_role_taskfieldPeer::doSelectRS($c);
        */
        $con = Conexion::getConexion();
        $sql = "SELECT * FROM mnu_role_taskfield WHERE role_id = '".$this->n_RoleId."' AND task_id = '".$taskId."';";
        $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
        $num_rows = mysql_num_rows($result);
        //$db_HelpText = $result ? mysql_fetch_object($result) : null;
        
        if ($num_rows>0) {
            while ($ob_mnuTaskField = mysql_fetch_object($result)) {

                //$ob_mnuTaskField = new mnu_role_taskfield();
                //$ob_mnuTaskField->hydrate($db_MnuTaskFields);
                
                $FieldLst[$ob_mnuTaskField->field_id] = $ob_mnuTaskField->access_type;
                /*
                switch( $ob_mnuTaskField->getAccessType() ){
                    case 'FULL':
                        $FieldLst[$ob_mnuTaskField->getFieldId()]='';
                        break;
                    case 'NDI' :
                        $FieldLst[$ob_mnuTaskField->getFieldId()]='disabled';
                        break;
                    case 'NED' :
                        $FieldLst[$ob_mnuTaskField->getFieldId()]='readonly';
                        break;
                }
                */
            }
            //if (count($FieldLst) > 0)
            //    return $FieldLst;
        }
    }
    return $FieldLst;
  }

  /**
   * Enter description here...
   *
   * @param string $task
   * @return bool (TRUE/FALSE)
   */
  private function getMnuRoleTask($task)
  {
    $this->n_TaskId    = 'UNKNOWN';
    $this->n_TaskDesc  = 'UNKNOWN';
    $this->n_ButtonText= 'UNKNOWN';
    $this->n_TaskType  = 'UNKNOWN';
    $this->n_ScriptId  = 'UNKNOWN';
    $this->n_IsDisabled= 'Y';
    $this->n_SubsysDir = 'UNKNOWN';
    $this->n_TaskPrefix= 'UNKNOWN';
    $this->n_Access    = FALSE;

    if ( !$this->getMnuTask($task) ){
        return (FALSE);
    }
    
    if ($this->n_GlobalAccess == 'N') {
      /*
      $c = new Criteria();
      $c->add(mnu_role_taskPeer::ROLE_ID, $this->n_RoleId);
      $c->add(mnu_role_taskPeer::TASK_ID, $task);
      $db_MnuRoleTask = mnu_role_taskPeer::doSelectOne($c);
      */
      $con = Conexion::getConexion();
      $sql = "SELECT * FROM mnu_role_task WHERE role_id = '".$this->n_RoleId."' AND task_id = '".$task."';";
      $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
      $db_MnuRoleTask  = $result ? mysql_fetch_object($result) : null;

      if ($db_MnuRoleTask){
        $this->n_Access = TRUE;
      }
    }else{
      $this->n_Access = TRUE;
    }
    
    return (true);
  }

  /**
   * Retorno el nodo de la tarea
   * es llamado solo por getMnuRoleTask()
   * @param string $task
   * @return bool (TRUE/FALSE)
   */
  private function getMnuTask($task){
    //AQUI ME QUEDE------------------------------------------------------------------------
    /*
    $c = new Criteria();
    $c->add(mnu_taskPeer::TASK_ID, $task);
    $db_MnuTask = mnu_taskPeer::doSelectOne($c);
    */
    $con = Conexion::getConexion();
    $sql = "SELECT * FROM mnu_task Inner Join mnu_subsystem ON mnu_task.subsys_id = mnu_subsystem.subsys_id WHERE task_id = '".$task."';";
    $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
    $db_MnuTask = $result ? mysql_fetch_object($result) : null;
    
    if ($db_MnuTask) {
      $this->n_TaskId    = $db_MnuTask->task_id;
      $this->n_TaskDesc  = $db_MnuTask->task_desc;
      $this->n_ButtonText= $db_MnuTask->button_text;
      $this->n_TaskType  = $db_MnuTask->task_type;
      $this->n_ScriptId  = $db_MnuTask->script_id;
      $this->n_IsDisabled= $db_MnuTask->is_disabled;
      $this->n_SubsysDir = $db_MnuTask->subsys_dir;
      $this->n_TaskPrefix= $db_MnuTask->task_prefix;
      return (TRUE);
    }
    return (FALSE);
  }

  /**
   * Recupera la estructura del menu en un array()
   *
   * @param String $task
   * @return array
   */
  private function getMenuArray($task,$IsLevel)
  {
    $menu_lst=Array();

    $this->getMnuRoleTask($task);

    if ($this->n_TaskType == 'menu'){
      $menu = $this->getMnuMenu($this->n_TaskId);
    }else{
      $menu = Array();
    }

    ///return $this->n_Access;
    if ( count($menu) <= 0)
        ;
    else {

        ksort($menu);
        for (reset($menu); $key = key($menu); next($menu)) {
            $menu_lst[$key]['TaskId']      =$menu[$key]['TaskId'];
            $menu_lst[$key]['ButtonText']  =$menu[$key]['ButtonText'];
            $menu_lst[$key]['TaskDesc']    =$menu[$key]['TaskDesc'];
            $menu_lst[$key]['TaskType']    =$menu[$key]['TaskType'];
            $menu_lst[$key]['IsDisable']   =$menu[$key]['IsDisable'];
            $menu_lst[$key]['PathScriptId']=$menu[$key]['PathScriptId'];
            $menu_lst[$key]['Access']      =$menu[$key]['Access'];

            if ($IsLevel == 1 && $menu_lst[$key]['Access'] && $menu_lst[$key]['IsDisable'] && $menu_lst[$key]['TaskType']=='menu')
                $menu_lst[$key]['NextMenu']  =$this->getMenuArray($menu[$key]['TaskId'],$IsLevel);
            else
                $menu_lst[$key]['NextMenu']  =array();
        }
    }
    return $menu_lst;
  }

  /**
   * Enter description here...
   *
   * @param unknown_type $dir
   * @return unknown
   */
  private function getImagesDir($dir)
  {
		
    $a=strpos($dir,".php");
    $ap=substr($dir,0,$a);
    $a=strpos($dir,"/",1);
    $ap.=substr($dir,$a);
    
		
    return "/images".$ap;
  }
  
  private function getImagesDir2($dir,$modules)
  {
		$a_parser = explode("/",$dir);
		$apps_temp = explode(".",$a_parser[2]);
			
    return "/images/apps/".$apps_temp[0]."/modules/".$modules."/icono.gif";
  }
} // mnu_class
<<<TODO
 roleid de ser verificado por la clase
 el nav_buttom falta verificar(solo es en caso PROC)***confirmar***
 mostrar la opcion en disable para el usuario
 mostrar la opcion en disable para el sistema
 validar recursividad infinita
TODO
?>