Hola x_atrix adapté un poco las cosas en Rol al final me quedó así:
Rol.php
Código PHP:
Ver original<?php
namespace Clases;
use \Model\RolModel;
class Rol
{
private $id;
private $nombre;
private $modelo;
function getId() {
return $this->id;
}
function getNombre() {
return $this->nombre;
}
function setNombre($nombre) {
}
function __construct() {
switch($nargs){
case 1:
self::__construct0();
break;
case 2:
self::__construct1($args[0], $args[1]);
break;
}
}
function __construct0() { }
function __construct1($xid, $xnombre) {
$this->id = $xid;
}
public function equals(Rol $obj) {
return $this->nombre == $obj->nombre;
}
public function save(){
$this->modelo = new RolModel();
return ($this->id == 0) ? $this->modelo->guardame($this) : $this->modelo->modificame($this);
}
public function del(){
$this->modelo = new RolModel();
return $this->modelo->eliminame($this);
}
public function obtenerTodos(){
$this->modelo = new RolModel();
return $this->modelo->obtenerTodos();
}
public function obtenerPorId($id){
$this->modelo = new RolModel();
return $this->modelo->obtenerPorId($id);
}
}
RolesController.php
Código PHP:
Ver original<?php
namespace Controller;
use \App\Controller;
use \App\Session;
use \Clases\Rol;
class RolesController extends Controller
{
function __construct() {
parent::__construct();
}
public function index(){
if($this->checkUser()){
"roles" => (new Rol())->obtenerTodos()
));
}
}
public function add(){
if($this->checkUser()){
if (isset($_POST['btnaceptar'])) { if($this->checkDates()) {
$rol = new Rol(0, $_POST['txtnom']);
$id = $rol->save();
Session
::set("msg",(isset($id)) ?
"Rol Creado" : Session
::get('msg')); header("Location:index.php?c=roles&a=index"); }
}
$this->redirect(array('add.php')); }
}
public function edit(){
if($this->checkUser()){
Session::set("id",$_GET['p']);
if (Session
::get('id')!=null && isset($_POST['btnaceptar'])){ if($this->checkDates()) {
$rol= new Rol($_POST['hid'],$_POST['txtnom']);
$id = $rol->save();
Session
::set("msg",(isset($id)) ?
"Rol Editado" : Session
::get('msg')); header("Location:index.php?c=roles&a=index"); }
}
"rol" => (new Rol())->obtenerPorId(Session::get('id'))
));
}
}
public function delete(){
if($this->checkUser()){
$rol = (new Rol())->obtenerPorId($_GET['p']);
$id= $rol->del();
Session
::set("msg", (isset($id)) ?
"Rol Borrado" : "No se pudo borrar el rol"); header("Location:index.php?c=roles&a=index"); }
}
}
private function checkDates(){
if(empty($_POST['txtnom'])){ Session::set("msg","Ingrese los datos obligatorios (*) para continuar.");
return false;
}
else{
return true;
}
}
private function checkUser(){
if(Session::get("log_in")!= null and Session::get("log_in")->getRol()->getNombre() == "ADMIN"){
return true;
}
else {
Session::set("msg","Debe ser administrador para acceder.");
$this->redirect(array('Main','index.php')); }
}
}