IESA
Programa de Feedback Gerencial y Liderazgo
 Todo Estructuras de Datos Archivos Funciones Variables
Usuario.class.php
Ir a la documentación de este archivo.
00001 <?php
00002 
00012 class Usuario{
00013 
00014         protected $usu_id;
00015         protected $usu_nombres;
00016         protected $usu_apellidos;
00017         protected $usu_correo_electronico;
00018         protected $usu_clave;
00019         protected $usu_rol_id;
00020         protected $usu_status = 1;
00021 
00022         private $rol_obj;
00023 
00034         function __construct( $id = NULL ){
00035 
00036                 $this->rol_obj = new stdClass( );
00037                 $this->rol_obj->nombre = '';
00038                 $this->rol_obj->descripcion = '';
00039                 $this->rol_obj->permisos = 0;
00040                 $this->rol_obj->status = 0;
00041 
00042                 if( $id != NULL )
00043                         $this->getUsuarioPorId( $id );
00044 
00045         }
00046 
00053         private function limpiar(  ){
00054                 foreach( get_object_vars($this) as $name => $value )
00055                         switch( $name ){
00056                                 case 'rol_obj': 
00057                                         $this->{$name}->nombre = '';
00058                                         $this->{$name}->descripcion = '';
00059                                         $this->{$name}->permisos = 0;
00060                                         $this->{$name}->status = 0;
00061                                         break;
00062                                 case 'usu_status': $this->{$name} = 1; break;
00063                                 default: $this->{$name} = NULL; break;
00064                         }
00065         }
00066 
00078         public function getUsuario( $data ){
00079 
00080                 $this->limpiar(  );
00081 
00082                 try{
00083 
00084                         $db = Database::getInstance(  );
00085 
00086                         $attr  = array(  );
00087 
00088                         foreach( get_object_vars($this) as $name => $val )
00089                                 switch( $name ){
00090                                         case 'rol_obj': break;
00091                                         default:
00092                                                 array_push( $attr, $name );
00093                                 }
00094 
00095                         $query = 'SELECT '.implode( $attr, ', ' ).' FROM usuarios WHERE '.$data['where'];
00096                         
00097                         $consulta = $db->prepare( $query );
00098                         $consulta->execute( $data['data'] );
00099 
00100                         $resultado = $consulta->fetch( PDO::FETCH_ASSOC );
00101                         unset( $consulta );
00102 
00103                         
00104                         if( !$resultado )
00105                                 return false;
00106                         else
00107                                 foreach( $resultado as $name => $value )
00108                                         $this->{$name} = $value;
00109 
00110                         $query = 'SELECT rol_nombre, rol_descripcion, rol_permisos, rol_status FROM roles WHERE rol_id = :id';
00111 
00112                         $consulta = $db->prepare( $query );
00113                         $consulta->execute( array( ':id' => $this->usu_rol_id ) );
00114 
00115                         $resultado = $consulta->fetch( PDO::FETCH_ASSOC );
00116                         unset( $consulta );
00117 
00118                         if( $resultado ){
00119                                 $this->rol_obj->id = $this->usu_rol_id;
00120                                 $this->rol_obj->nombre = $resultado['rol_nombre'];
00121                                 $this->rol_obj->descripcion = $resultado['rol_descripcion'];
00122                                 $this->rol_obj->permisos = $resultado['rol_permisos'];
00123                                 $this->rol_obj->status = $resultado['rol_status'];
00124                         }
00125 
00126                         return true;
00127 
00128                 }catch( PDOException $e ){ unset($consulta); throw $e; }
00129         }
00130 
00143         public function getUsuarioPorId( $id ){
00144                 return $this->getUsuario( array( 
00145                         'where' => 'usu_id = :id',
00146                         'data'=> array(
00147                                 ':id'=>$id
00148                         )
00149                 ));
00150         }
00151 
00164         public function getUsuarioPorCorreoElectronico( $correo ){
00165                 return $this->getUsuario( array( 
00166                         'where' => 'usu_correo_electronico = :correo',
00167                         'data'=> array(
00168                                 ':correo'=>$correo
00169                         )
00170                 ));
00171         }
00172 
00188         public function getUsuarioPorCredenciales( $correo, $clave ){
00189                 return $this->getUsuario(
00190                         array(
00191                                 'where' => 'usu_correo_electronico = :correo AND usu_clave = :clave ',
00192                                 'data'  => array(
00193                                 ':correo' => $correo,
00194                                 ':clave' => $clave
00195                                 )
00196                         )
00197                 );
00198         }
00199 
00209         public function guardar(  ){
00210 
00211                 $data = array(  );
00212 
00213                 $field = array(  );
00214                 $value = array(  );
00215 
00216                 if( !$this->getId(  ) ){
00217 
00218                         foreach( get_object_vars($this) as $name => $val )
00219                                 if( !is_null( $val ) )
00220                                         switch( $name ){
00221                                                 case 'usu_id':
00222                                                 case 'rol_obj': break;
00223                                                 default:
00224                                                         array_push( $field, $name );
00225                                                         array_push( $value, ':'.$name );
00226                                                         $data[':'.$name]=$val;
00227                                         }
00228 
00229                         $query = 'INSERT INTO usuarios ( '.implode( $field, ', ' ).' )VALUES( '.implode( $value, ', ' ).' )';
00230 
00231                 }else{
00232 
00233                         foreach( get_object_vars($this) as $name => $val )
00234                                 if( !is_null( $val ) )
00235                                         switch( $name ){
00236                                                 case 'usu_id':
00237                                                 case 'rol_obj': break;
00238                                                 default:
00239                                                         array_push( $field,"{$name}=:{$name}" );
00240                                                         $data[':'.$name]=$val;
00241                                         }
00242 
00243                         $data[':usu_id']=$this->usu_id;
00244 
00245                         $query = 'UPDATE usuarios SET '.implode( $field, ', ' ).' WHERE usu_id = :usu_id';
00246 
00247                 }
00248 
00249                 $db = Database::getInstance(  );
00250                 $consulta = $db->prepare( $query );
00251                 $consulta->execute( $data );
00252 
00253                 if( !$this->getId(  ) ) $this->getUsuarioPorId( Database::lastInsertId( 'usuarios_seq' ) );
00254 
00255         }
00256 
00266         public function getNombreCompleto(  ){
00267 
00268                 $Nombres = $this->usu_nombres;
00269                 $Apellidos = $this->usu_apellidos ? ', '.$this->usu_apellidos : '' ;
00270 
00271                 return ucwords( strtolower( "{$Nombres}{$Apellidos}" ) );
00272         }
00273 
00281         public function eliminar(  ){
00282                 if( !$this->getId(  ) )
00283                         return 0;
00284 
00285                 $db = Database::getInstance(  );
00286                 $rt = $db->exec( 'DELETE FROM usuarios WHERE usu_id = '.$this->getId(  ) );
00287 
00288                 $this->limpiar(  );
00289 
00290                 return $rt ? $rt : 0;
00291         }
00292 
00300         public function getId(  ){ return $this->usu_id ? $this->usu_id : NULL; }
00301 
00309         public function setNombres( $val ){ $this->usu_nombres = $val; }
00310 
00318         public function getNombres(  ){ return $this->usu_nombres; }
00319 
00327         public function setApellidos( $val ){ $this->usu_apellidos = $val; }
00328 
00336         public function getApellidos(  ){ return $this->usu_apellidos; }
00337 
00345         public function setCorreoElectronico( $val ){ $this->usu_correo_electronico = $val; }
00346 
00354         public function getCorreoElectronico(  ){ return $this->usu_correo_electronico; }
00355 
00363         public function setRolId( $val ){ $this->usu_rol_id = $val; }
00364 
00372         public function getRolId(  ){ return $this->usu_rol_id; }
00373 
00383         public function getRol(  ){ return $this->rol_obj; }
00384 
00407         public function permite( $val ){ return ( $this->rol_obj->permisos & $val ) == 0 ? false : true ; }
00408 
00416         public function setClave( $val ){ $this->usu_clave = $val; }
00417 
00418 
00427         public function cambiarClave( $val ){
00428 
00429                 if( !$this->getId(  ) )
00430                         return false;
00431 
00432                 $db = Database::getInstance(  );
00433 
00434                 $consulta = $db->prepare( 'UPDATE usuarios SET usu_clave = :clave WHERE usu_id = :id'  );
00435 
00436                 $consulta->execute( array( ':id'=>$this->getId(  ), ':clave'=>$val ) );
00437 
00438                 if( $consulta->rowCount(  ) == 0 ){
00439                         unset($consulta);
00440                         return false;
00441                 }else
00442                         unset($consulta);
00443                         return true;
00444 
00445         }
00446 
00454         public function setStatus( $val ){ $this->usu_status = $val; }
00455 
00463         public function getStatus(  ){ return $this->usu_status; }
00464 
00472         public function getTipoDeCuenta(  ){ return 'U'; }
00473 
00474 }