<?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('sinergia');
        $this->setAttribute('UserId', $user->getUserId(), 'sinergia');
        $this->setAttribute('Name', $user->getUserName(), 'sinergia');
        $this->setAttribute('RoleId', $user->getUserRoleId(), 'sinergia');
        $this->setAttribute('IdColaborador', $user->getIdColaborador(), 'sinergia');
        $this->setAttribute('IdPadre', $user->getIdPadre(), 'sinergia');
        $this->setAttribute('IdParent', $user->getIdParent(), 'sinergia');
        
        
    }
    return $isLoginOk;
  }

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


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

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

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

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