<?php
class rolActions extends wtActions
{
    public function executeIndex()
    {
        
    }
    
    public function executeList()
    {
    	$this->block = $this->getRequestParameter( 'block' );
    	$this->s_Mensaje = "";
    	
    	$s_Id = $this->getRequestParameter( 's_Id' );
    	$tipo = $this->getRequestParameter( 'tipo' );
    	
    	if( $tipo )
    	{
    	    $o_UsuarioFN = new SEG_UsuarioFN();
    	    $o_UsuarioFN->setIdRol( $s_Id );
    	    $a_Usuario = $o_UsuarioFN->select();
    	    if( !$a_Usuario )
    	    {
    	        $b_Result = SEG_RolDP::eliminarRolesAsoc( $s_Id );
    	        
    	        $o_RolDP = new SEG_RolDP();
    	        $o_RolDP->setIdRol( $s_Id );
    	        $b_Result2 = $o_RolDP->delete();
    	    }
    	    else 
    	    {
    	        $this->s_Mensaje = "No se puede eliminar por tener asociado un usuario.";
    	    }
    	}
    }
    
    public function executeGrid()
    {
        $block = $this->getRequestParameter( 'block' );
        $page  = $this->getRequestParameter( 'page' );
        
         set_header_utf8();
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        $o_GridAction = new GridAction();
        $o_GridAction->setClassFind( new SEG_RolFN() );
        
        $o_GridAction->addConditionFilter( "RolDescripcion", SEG_RolFN::RolDescripcion, "LIKE", "%%s%" );
        $o_GridAction->addConditionFilter( "RolId", SEG_RolFN::IdRol, "LIKE", "%%s%" );
        
        $o_GridAction->addOrder( "RolDescripcion", SEG_RolFN::RolDescripcion);
        $o_GridAction->addOrder( "IdRol", SEG_RolFN::IdRol );
        
        //$o_GridAction->addOrderDefault( ALM_MarcaFN::IdMarca , true );
        
        $o_GridAction->addRowsToShow( "IdRol", "IdRol", "RolDescripcion" );
        
        $o_GridAction->setNumberRecordsShow( 15 );
        
        $o_GridAction->initialize();
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        $a_ResultJson['page']    = $o_GridAction->getPage();
        $a_ResultJson['total']   = $o_GridAction->getTotalPage();
        $a_ResultJson['records'] = $o_GridAction->getTotalRecords();
        $a_ResultJson['rows']    = $o_GridAction->getResultArray();
        //pr( $a_ResultJson );
        echo json_encode( $a_ResultJson );

        return ViewResponse::NONE;   
        /*
        $block = $this->getRequestParameter( 'block' );
        $page  = $this->getRequestParameter( 'page' );
        
        $Codigo      = $this->getRequestParameter( 'Codigo' );
        $Descripcion = $this->getRequestParameter( 'Descripcion' );
        
        try
        {
            $o_ObjClass = new SEG_RolFN();

            if( !is_null( $Descripcion ) && $Descripcion != "" ){ $o_ObjClass->addCondition( SEG_RolFN::RolDescripcion, "LIKE", "%$Descripcion%" ); }
            if( !is_null( $Codigo ) && $Codigo != "" ){ $o_ObjClass->addCondition( SEG_RolFN::IdRol, "LIKE", "%$Codigo%" ); }

            
            $tamanioPag = 15;
            if( !$page ) $page = 1;
            $limitInf   = ( $page - 1 ) * $tamanioPag;
            
            $a_ObjClass = $o_ObjClass->select( $limitInf, $tamanioPag );
            $i_NumReg   = $o_ObjClass->selectNumRows();
            
            $o_navigation = new navigation();
            $o_navigation->setPaginaActual( $page );
            $o_navigation->setTamanioPagina( $tamanioPag );
            $o_navigation->setTotalRegistros( $i_NumReg );
            
            $i_TotalPagina = $o_navigation->getTotalPagina();
            
            $a_ResultJson['page']    = $page;
            $a_ResultJson['total']   = $i_TotalPagina;
            $a_ResultJson['records'] = $i_NumReg;
            
            foreach ( $a_ObjClass as $iKey => $o_ObjClassBN )
            {
                $a_Rs[] = array(
                                    "IdRol"        		=> ( string ) $o_ObjClassBN->getIdRol(),
                                    "Codigo"            => $o_ObjClassBN->getIdRol(),
                                    "RolDescripcion"  	=> $o_ObjClassBN->getRolDescripcion()
                               );
            }
            
            $a_ResultJson['rows'] = $a_Rs;
            echo json_encode( $a_ResultJson );
        }
        catch ( Exception $o_Exception )
        {
            pr( $o_Exception );
        }
        //ViewResponse::getInstance()->setMethodAjax();
        return ViewResponse::NONE;
        */
    }
    
    public function executeAdd()
    {
        $this->block = $this->getRequestParameter( 'block' );
        $this->i_Id  = $this->getRequestParameter( 'i_Id' );
        $this->s_TipoProc  = $this->getRequestParameter( 's_TipoProc' );
        
        $this->o_RolBN = new SEG_RolBN();
        $this->s_Titulo = "Nuevo Rol";
        if( $this->s_TipoProc == "upd" )
        {
            $this->s_Titulo = "Editar Rol";
            $o_SEG_RolFN = new SEG_RolFN();
            $this->o_RolBN = $o_SEG_RolFN->selectByPK( $this->i_Id );
        }
    }
    
    public function executeSaveRol()
    {
        $block      = $this->getRequestParameter( 'block' );
        $TypeProc   = $this->getRequestParameter( 's_TipoProc' );
        $i_Id       = $this->getRequestParameter( 'i_Id' );
        
        $RolId           = $this->getRequestParameter( 'RolId' );
        $RolDescripcion  = $this->getRequestParameter( 'RolDescripcion' );
        $AccesoGlobal    = $this->getRequestParameter( 'AccesoGlobal' );
        $EsAdministrador = $this->getRequestParameter( 'EsAdministrador' );

        try 
        {
            $o_RolDP = new SEG_RolDP();
            
            $o_RolDP->setRolDescripcion( $RolDescripcion );
            $o_RolDP->setAccesoGlobal( $AccesoGlobal );
            $o_RolDP->setEsAdministrador( $EsAdministrador );
            $o_RolDP->setIdtareaInicio( "mnu.main" );
            
            if( $TypeProc == "nvo" )
            {
                $o_RolDP->setIdRol( $RolId );
                
                $o_RolDP->setUsuarioCreacion( myUser::getUser()->getUserId() );
                $o_RolDP->setFechaCreacion( date( "Y-m-d H:i:s" ) );
                $b_Result = $o_RolDP->save();
                $i_Id = $RolId;
            }
            else 
            {
                $o_RolDP->setIdRol( $i_Id );
                
                $o_RolDP->setUsuarioModificacion( myUser::getUser()->getUserId() );
                $o_RolDP->setFechaModificacion( date( "Y-m-d H:i:s" ) );
                $b_Result = $o_RolDP->update();
            }
            $a_Rs['type_sub'] = $TypeProc;
            $a_Rs['error']   = false;
            $a_Rs['id']      = $i_Id;
        }
        catch ( Exception $o_Exception )
        {
            $a_Rs['error']        = true;
            $a_Rs['mensaje_error'] = $o_Exception->getMessage();
        }
        echo json_encode( $a_Rs );
        return ViewResponse::NONE;
    }
    
    public function executedetail()
    {
        set_header_utf8();
        
        $this->block = $this->getRequestParameter( 'block' );
        $this->i_Id  = $this->getRequestParameter( 'i_Id' );
        $this->s_TipoProc  = $this->getRequestParameter( 's_TipoProc' );
        
        $this->s_Titulo = "Editar Rol";
        $o_SEG_RolFN = new SEG_RolFN();
        $this->o_RolBN = $o_SEG_RolFN->selectByPK( $this->i_Id );
        
        $o_MenuFN = new SEG_MenuFN();
        $o_MenuFN->setIdMenu( "mnu.main" );
        $o_MenuFN->addOrderBy( SEG_MenuFN::Secuencia );
        $this->a_Menu = $o_MenuFN->select();
    }
    
    public function executeCargarHijos()
    {
        set_header_utf8();
        
        $this->block = $this->getRequestParameter( 'block' );
        $this->i_Id  = $this->getRequestParameter( 'i_Id' );
        
        $s_IdHijo    = $this->getRequestParameter( 'IdHijo' );
        $this->s_Secuencia = $this->getRequestParameter( 'secuencia' );
        //pr( $s_IdHijo );
        $o_MenuFN = new SEG_MenuFN();
        //$o_MenuFN->setEstado( 1 );
        $o_MenuFN->setIdMenu( $s_IdHijo );
        $o_MenuFN->addOrderBy( SEG_MenuFN::Secuencia );
        $this->a_Menu  = $o_MenuFN->select();
        $this->a_Tarea = null;
        
        if( !$this->a_Menu )
        {
            $a_Fuente = explode( ".", $s_IdHijo );
            
            $s_WhereIdtarea  = $a_Fuente[0].".".$a_Fuente[1].".".$a_Fuente[2];
            $s_WhereIdtarea2 = $a_Fuente[0].".".$a_Fuente[1];
            $o_TareaFN = new SEG_TareaFN();
            $o_TareaFN->addCondition( SEG_TareaFN::IdTarea, "LIKE", $s_WhereIdtarea."%" );
            $o_TareaFN->addCondition( SEG_TareaFN::IdTarea, "NOT IN", "( (SELECT IdtareaHijo FROM SEG_Menu WHERE IdMenu='$s_WhereIdtarea2' ) )", false );
            $this->a_Tarea = $o_TareaFN->select();
        }
        ViewResponse::getInstance()->setMethodAjax();
    }
   
    public function executeSaveRolTareas()
    {
        $block      = $this->getRequestParameter( 'block' );
        $TypeProc   = ( boolean ) $this->getRequestParameter( 's_TipoProc' );
        $i_Id       = $this->getRequestParameter( 'i_Id' );
        
        $tareas          = $this->getRequestParameter( 'tareas' );
        $tareas = array_unique( $tareas );

        try 
        {
            SEG_TareaRolDP::deletePorRol( $i_Id );
            
            if( $tareas )
            {
                $o_TareaRolDP = new SEG_TareaRolDP();
                $o_TareaRolDP->setIdRol( $i_Id );
                $o_TareaRolDP->setIdTarea( "mnu.main" );
                $o_TareaRolDP->setUsuarioCreacion( myUser::getUser()->getUserId() );
                $o_TareaRolDP->setFechaCreacion( date( "Y-m-d H:i:s" ) );
                $o_TareaRolDP->setUsuarioModificacion( myUser::getUser()->getUserId() );
                $o_TareaRolDP->setFechaModificacion( date( "Y-m-d H:i:s" ) );
                $b_Result = $o_TareaRolDP->save();
                foreach ( $tareas as $i_Key => $s_Value )
                {
                    $o_TareaRolDP = new SEG_TareaRolDP();
                    $o_TareaRolDP->setIdRol( $i_Id );
                    $o_TareaRolDP->setIdTarea( $s_Value );
                    $o_TareaRolDP->setUsuarioCreacion( myUser::getUser()->getUserId() );
                    $o_TareaRolDP->setFechaCreacion( date( "Y-m-d H:i:s" ) );
                    $o_TareaRolDP->setUsuarioModificacion( myUser::getUser()->getUserId() );
                    $o_TareaRolDP->setFechaModificacion( date( "Y-m-d H:i:s" ) );
                    $b_Result = $o_TareaRolDP->save();
                }
            }

            $a_Rs['type_sub'] = $TypeProc;
            $a_Rs['error']   = false;
            $a_Rs['id']      = $i_Id;
        }
        catch ( Exception $o_Exception )
        {
            $a_Rs['error']        = true;
            $a_Rs['mensaje_error'] = $o_Exception->getMessage();
        }
        echo json_encode( $a_Rs );
        return ViewResponse::NONE;
    }
}