Ir a la documentación de este archivo.00001 <?php
00002
00012 class Rol{
00013
00014
00015 const LISTAR_USUARIOS=1;
00016 const CREAR_USUARIOS=2;
00017 const EDITAR_USUARIOS=4;
00018 const ELIMINAR_USUARIOS=8;
00019
00020
00021 const LISTAR_ROLES=16;
00022 const CREAR_ROLES=32;
00023 const EDITAR_ROLES=64;
00024 const ELIMINAR_ROLES=128;
00025
00026
00027 const LISTAR_EVALUACIONES=256;
00028 const CREAR_EVALUACIONES=512;
00029 const EDITAR_EVALUACIONES=1024;
00030 const ELIMINAR_EVALUACIONES=2048;
00031
00032 const LISTAR_INVENTARIOS=4096;
00033 const CREAR_INVENTARIOS=8192;
00034 const EDITAR_INVENTARIOS=16384;
00035 const ELIMINAR_INVENTARIOS=32768;
00036
00037
00038 const ESTATUS_ACTIVO=1;
00039 const ESTATUS_INACTIVO=2;
00040
00041 protected $rol_id;
00042 protected $rol_nombre;
00043 protected $rol_descripcion;
00044 protected $rol_status = 1;
00045 protected $rol_permisos = 0;
00046
00057 function __construct( $id=NULL ){
00058
00059 if( $id != NULL )
00060 $this->getRol( array(
00061 'where' => 'rol_id = :id',
00062 'data'=> array(
00063 ':id'=>$id
00064 )
00065 ));
00066
00067 }
00068
00075 private function limpiar( ){
00076 foreach( get_object_vars($this) as $name => $value )
00077 switch( $name ){
00078 case 'rol_usu_status': $this->{$name} = 1; break;
00079 case 'rol_usu_permisos': $this->{$name} = 0; break;
00080 default: $this->{$name} = NULL; break;
00081 }
00082 }
00083
00095 public function getRol( $data ){
00096
00097 $this->limpiar();
00098
00099 try{
00100
00101 $db = Database::getInstance( );
00102
00103 $attr = get_object_vars($this);
00104
00105 $query = 'SELECT '.implode( array_keys( $attr ), ', ' ).' FROM roles WHERE '.$data['where'];
00106
00107 $consulta = $db->prepare( $query );
00108 $consulta->execute( $data['data'] );
00109
00110 $resultado = $consulta->fetch( PDO::FETCH_ASSOC );
00111 unset( $consulta );
00112
00113 if( !$resultado )
00114 return false;
00115 else
00116 foreach( $resultado as $name => $value )
00117 $this->{$name} = $value;
00118
00119 return true;
00120 }catch( PDOException $e ){
00121 unset($consulta);
00122 throw new Exception( $e->getMessage( ) , $e->getCode( ) );
00123 }
00124 }
00125
00138 public function getRolPorId( $id ){
00139 return $this->getRol( array(
00140 'where' => 'rol_id = :id',
00141 'data'=> array(
00142 ':id'=>$id
00143 )
00144 ));
00145 }
00146
00159 public function getRolPorNombre( $nombre ){
00160 return $this->getRol( array(
00161 'where' => 'rol_nombre = :nombre',
00162 'data'=> array(
00163 ':nombre'=>$nombre
00164 )
00165 ));
00166 }
00167
00177 public function guardar( ){
00178
00179 $data = array( );
00180
00181 $field = array( );
00182 $value = array( );
00183
00184 if( !$this->getId( ) ){
00185
00186 foreach( get_object_vars($this) as $name => $val )
00187 if( !is_null( $val ) ){
00188 array_push( $field, $name );
00189 array_push( $value, ':'.$name );
00190 $data[':'.$name]=$val;
00191 }
00192
00193 $query = 'INSERT INTO roles ( '.implode( $field, ', ' ).' )VALUES( '.implode( $value, ', ' ).' )';
00194
00195 }else{
00196
00197 foreach( get_object_vars($this) as $name => $val )
00198 if( !is_null( $val ) && $name != 'rol_id' ){
00199 array_push( $field,"{$name}=:{$name}" );
00200 $data[':'.$name]=$val;
00201 }
00202
00203 $data[':rol_id']=$this->rol_id;
00204
00205 $query = 'UPDATE roles SET '.implode( $field, ', ' ).' WHERE rol_id = :rol_id';
00206
00207 }
00208
00209 $db = Database::getInstance( );
00210 $consulta = $db->prepare( $query );
00211 $consulta->execute( $data );
00212
00213 if( !$this->getId( ) ) $this->getRolPorId( Database::lastInsertId( 'roles_seq' ) );
00214 }
00215
00223 public function eliminar( ){
00224 if( !$this->getId( ) )
00225 return 0;
00226
00227 $db = Database::getInstance();
00228 $rt = $db->exec( 'DELETE FROM roles WHERE rol_id = '.$this->getId( ) );
00229
00230 $this->limpiar( );
00231
00232 return $rt ? $rt : 0;
00233 }
00234
00257 public function permite( $val ){
00258 return ( $this->rol_permisos & $val ) == 0 ? false : true ;
00259 }
00260
00268 public function getId( ){ return $this->rol_id ? $this->rol_id : NULL; }
00269
00277 public function setNombre( $val ){ $this->rol_nombre = $val; }
00278
00286 public function getNombre( ){ return $this->rol_nombre; }
00287
00295 public function setDescripcion( $val ){ $this->rol_descripcion = $val; }
00296
00304 public function getDescripcion( ){ return $this->rol_descripcion; }
00305
00332 public function setPermisos( $val ){ $this->rol_permisos = $val; }
00333
00341 public function getPermisos( ){ return $this->rol_permisos; }
00342
00352 public function setStatus( $val ){ $this->rol_status = $val; }
00353
00361 public function getStatus( ){ return $this->rol_status; }
00362
00363 }