<?php
//antes -> extends sfBasicSecurityUser
class myUser extends sfUser{
  
  protected static $instance = null;

  public static function getUser(){
    if (!isset(self::$instance))
    {
      $class = __CLASS__;
      self::$instance = new $class();
      //self::$instance->initialize();
    }
    return self::$instance;
  }
  
  public function signIn($login, $password){
    
    $isLoginOk = false;
    $user = new user_class();

    // nickname exists?
    if ($user->findUser($login)){
      if ($user->getUserPassword() == md5($password)){ // password is OK?
        $isLoginOk = true;
      }
    }

    if ($isLoginOk) {
        $this->setAuthenticated(true);
        $this->addCredential('credimas');
        $this->setAttribute('UserId', $user->getUserId(), 'credimas');
        $this->setAttribute('Name', $user->getUserName(), 'credimas');
        $this->setAttribute('RoleId', $user->getUserRoleId(), 'credimas');
        $this->setAttribute('IdColaborador', $user->getIdColaborador(), 'credimas');
        $this->setAttribute('IdPadre', $user->getIdPadre(), 'credimas');
        $this->setAttribute('IdParent', $user->getIdParent(), 'credimas');
        
        
    }
    return $isLoginOk;
  }

  public function signOut()
  {
    $this->setAuthenticated(false);
    $this->clearCredentials();
    $this->removeNamespace('credimas');
  }


  public function getUserId()
  {
    return $this->getAttribute('UserId','', 'credimas');
  }
  
  public function getIdColaborador()
  {
    return $this->getAttribute('IdColaborador','', 'credimas');
  }
  
  public function getIdPadre()
  {
    return $this->getAttribute('IdPadre','', 'credimas');
  }
  
  public function getIdParent()
  {
    return $this->getAttribute('IdParent','', 'credimas');
  }

  public function getName()
  {
    return $this->getAttribute('Name','', 'credimas');
  }

  public function getRoleId()
  {
    return $this->getAttribute('RoleId','', 'credimas');
  }

  public function getPerm($task)
  {
      $this->mnuMain = new menu_class;
      $this->mnuMain->getInit($this->getRoleId());

      $this->mnuTask = $this->mnuMain->getTask($task);
      if (count($this->mnuTask) > 0)
          return TRUE;
      return FALSE;

  }
  
  public function hasTask($task){
  	//return mnu_taskPeer::retrieveByPK($task);
    $con = Conexion::getConexion();
    $sql = "SELECT * FROM mnu_task WHERE task_id = '".$task."';";
    $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
    $db_result = $result ? mysql_fetch_object($result) : null;
    return $db_result;
  }
  
}
