Ver Mensaje Individual
  #14 (permalink)  
Antiguo 11/01/2016, 20:50
Avatar de detective_jd
detective_jd
 
Fecha de Ingreso: abril-2011
Ubicación: Salto
Mensajes: 437
Antigüedad: 13 años
Puntos: 6
Respuesta: Evitar eliminar registros con php

Hola x_atrix adapté un poco las cosas en Rol al final me quedó así:

Rol.php

Código PHP:
Ver original
  1. <?php
  2. namespace Clases;
  3. use \Model\RolModel;
  4. class Rol
  5. {
  6.     private $id;
  7.     private $nombre;
  8.     private $modelo;
  9.     function getId() {
  10.         return $this->id;
  11.     }
  12.     function getNombre() {
  13.         return $this->nombre;
  14.     }    
  15.     function setNombre($nombre) {
  16.         $this->nombre = strtoupper($nombre);
  17.     }
  18.     function __construct() {
  19.         $args = func_get_args();
  20.         $nargs = func_num_args();
  21.         switch($nargs){
  22.             case 1:
  23.                 self::__construct0();
  24.                 break;
  25.             case 2:
  26.                 self::__construct1($args[0], $args[1]);
  27.                 break;
  28.         }
  29.     }
  30.     function __construct0() { }
  31.     function __construct1($xid, $xnombre) {
  32.         $this->id = $xid;
  33.         $this->nombre = strtoupper($xnombre);
  34.     }
  35.     public function equals(Rol $obj) {
  36.         return $this->nombre == $obj->nombre;                
  37.     }
  38.     public function save(){
  39.         $this->modelo = new RolModel();
  40.         return ($this->id == 0) ? $this->modelo->guardame($this) : $this->modelo->modificame($this);
  41.     }
  42.     public function del(){
  43.         $this->modelo = new RolModel();
  44.         return $this->modelo->eliminame($this);
  45.     }
  46.     public function obtenerTodos(){
  47.         $this->modelo = new RolModel();
  48.         return $this->modelo->obtenerTodos();
  49.     }
  50.     public function obtenerPorId($id){
  51.         $this->modelo = new RolModel();
  52.         return $this->modelo->obtenerPorId($id);
  53.     }
  54. }

RolesController.php

Código PHP:
Ver original
  1. <?php
  2. namespace Controller;
  3. use \App\Controller;
  4. use \App\Session;
  5. use \Clases\Rol;
  6. class RolesController extends Controller
  7. {
  8.     function __construct() {
  9.         parent::__construct();
  10.     }
  11.     public function index(){
  12.         if($this->checkUser()){
  13.             $this->redirect(array("index.php"),array(
  14.                 "roles" => (new Rol())->obtenerTodos()
  15.             ));
  16.         }  
  17.     }
  18.     public function add(){
  19.         if($this->checkUser()){
  20.             if (isset($_POST['btnaceptar'])) {
  21.                 if($this->checkDates()) {                
  22.                     $rol = new Rol(0, $_POST['txtnom']);
  23.                     $id = $rol->save();
  24.                     Session::set("msg",(isset($id)) ? "Rol Creado" : Session::get('msg'));
  25.                     header("Location:index.php?c=roles&a=index");
  26.                     exit();
  27.                 }
  28.             }
  29.             $this->redirect(array('add.php'));
  30.         }
  31.     }  
  32.     public function edit(){        
  33.         if($this->checkUser()){
  34.             Session::set("id",$_GET['p']);
  35.             if (Session::get('id')!=null && isset($_POST['btnaceptar'])){                            
  36.                 if($this->checkDates()) {                
  37.                     $rol= new Rol($_POST['hid'],$_POST['txtnom']);
  38.                     $id = $rol->save();
  39.                     Session::set("msg",(isset($id)) ? "Rol Editado" : Session::get('msg'));
  40.                     header("Location:index.php?c=roles&a=index");
  41.                     exit();
  42.                 }
  43.             }
  44.             $this->redirect(array('edit.php'),array(
  45.                 "rol" => (new Rol())->obtenerPorId(Session::get('id'))
  46.             ));
  47.         }        
  48.     }
  49.     public function delete(){
  50.         if($this->checkUser()){
  51.             if (isset($_GET['p'])){
  52.                 $rol = (new Rol())->obtenerPorId($_GET['p']);
  53.                 $id= $rol->del();
  54.                 Session::set("msg", (isset($id)) ? "Rol Borrado" : "No se pudo borrar el rol");
  55.                 header("Location:index.php?c=roles&a=index");
  56.             }                          
  57.         }
  58.     }
  59.     private function checkDates(){
  60.         if(empty($_POST['txtnom'])){
  61.             Session::set("msg","Ingrese los datos obligatorios (*) para continuar.");
  62.             return false;
  63.         }
  64.         else{
  65.             return true;
  66.         }
  67.     }
  68.     private function checkUser(){
  69.         if(Session::get("log_in")!= null and Session::get("log_in")->getRol()->getNombre() == "ADMIN"){
  70.             return true;
  71.         }
  72.         else {
  73.             Session::set("msg","Debe ser administrador para acceder.");
  74.             $this->redirect(array('Main','index.php'));
  75.         }
  76.     }
  77. }