Add/edit/del manager todavia incompleto

falta terminar y probar
This commit is contained in:
marian1010 2021-05-05 19:23:09 +02:00
parent 13e0a508ff
commit c3371e41f1
4 changed files with 461 additions and 0 deletions

View File

@ -0,0 +1,36 @@
<?php
class Manager{
//Attributes:
private $_id; //Manager ID.
private $_username; //Manager username.
private $_email; //Email.
private $_pass; //Pass.
private $_roll; //Roll
//Constructor:
function __construct($id, $username, $email, $pass, $roll){
$this->_id = $id;
$this->_username = $username;
$this->_email = $email;
$this->_pass = $pass;
$this->_roll = $roll;
}
//Methods:
//Getters && Setters:
public function setId($id){ $this->_id = $id; }
public function getId(){ return $this->_id; }
public function setUsername($username){$this->_username = $username; }
public function getUsername(){ return $this->_username = $username; }
public function setEmail($email){$this->_email = $email;}
public function getEmail(){return $this->_email = $email;}
public function setPass($pass){$this->_pass = $pass;}
public function getPass(){return $this->pass;}
public function setRoll($roll){$this->_roll = $roll;}
public function getRoll(){return $this->_roll = $roll;}
}
?>

View File

@ -0,0 +1,91 @@
<?php
include_once('manager.php');
$template = new Template();
$prefix = $template->get_prefix();
include_once($prefix.'assets/php/dao.php');
class Manager_DAO extends DAO {
//Constructor:
function __construct($bd_name){
parent::__construct($bd_name);
}
//Methods:
//Create a new user Manager.
public function createManager($id, $username, $email, $pass, $rol){
$sql = sprintf( "INSERT INTO `users`( `id`, `username`, `email`, `password`, `rol`)
VALUES ( '%d', '%s', '%s', '%s', '%s')",
$id, $username, $email, $pass, $rol);
$resul = mysqli_query($this->mysqli, $sql) or die ('Error into query database');
return $resul;
}
//Returns a query to get All the managers.
public function allManagersData(){
$sql = sprintf( "SELECT * FROM users WHERE users.rol=manager");
$resul = mysqli_query($this->mysqli, $sql) or die ('Error into query database');
while($fila=$resul->fetch_assoc()){
$managers[] = $this->loadManager($fila["id"], $fila["username"], $fila["email"], $fila["password"], $fila["rol"]);
}
$resul->free();
return $managers;
}
//Returns a manager data .
public function GetManager($id){
$sql = sprintf( "SELECT * FROM users WHERE users.id = '%d'", $id );
$resul = mysqli_query($this->mysqli, $sql) or die ('Error into query database');
return $resul;
}
public function selectManager($username){
$username = $this->mysqli->real_escape_string($username);
$sql = sprintf( "SELECT * FROM users WHERE username = '%s'", $username );
$resul = mysqli_query($this->mysqli, $sql) or die ('Error into query database');
$resul->data_seek(0);
while ($fila = $resul->fetch_assoc()) {
$user = $this->loadUser($fila['id'], $fila['username'], $fila['email'], $fila['passwd'], $fila['rol']);
}
//mysqli_free_result($selectUser);
$resul->free();
return $user;
}
//Deleted manager by "id".
public function deleteManager($id){
$sql = sprintf( "DELETE FROM users WHERE users.id = '%d' ;",$id);
$resul = mysqli_query($this->mysqli, $sql) or die ('Error into query database');
return $resul;
}
//Edit manager.
public function editManager($id, $username, $email, $pass, $rol){
$sql = sprintf( "UPDATE users SET email = '%s' , pass = '%s',
WHERE users.id = '%d';",
$email, $pass, $id);
$resul = mysqli_query($this->mysqli, $sql) or die ('Error into query database');
return $resul;
}
//Create a new Manager Data Transfer Object.
public function loadManager($id, $username, $email, $pass, $rol){
return new Manager($id, $username, $email, $pass, $rol);
}
}
?>

View File

@ -0,0 +1,148 @@
<?php
include_once('../assets/php/config.php');
include_once('../assets/php/common/manager_dao.php');
include_once('../assets/php/common/manager.php');
include_once('../assets/php/form.php');
class FormManager extends Form {
//Atributes:
private $correct; // Indicates if the session is correct.
private $reply; // Validation response
private $option;
//Constructor:
public function __construct() {
parent::__construct('formManager');
$this->reply = array();
}
public function getReply() {
if($this->correct){
if($this->option == "new"){
$this->reply = "<div class='row'>
<div class='column side'></div>
<div class='column middle'>
<div class='code info'>
<h1> Operacion realizada con exito </h1><hr />
<p> Se ha añadido la promoción correctamente en la base de datos.</p>
<a href='../panel_admin/index.php?state=mp'><button>Cerrar Mensaje</button></a>
</div>
<div class='column side'></div>
</div>
";
}else if($this->option == "edit"){
$this->reply = "<div class='row'>
<div class='column side'></div>
<div class='column middle'>
<div class='code info'>
<h1> Operacion realizada con exito </h1><hr />
<p> Se ha editado la promoción correctamente en la base de datos.</p>
<a href='../panel_admin/index.php?state=mp'><button>Cerrar Mensaje</button></a>
</div>
<div class='column side'></div>
</div>
";
}else if($this->option == "del"){
$this->reply = "<div class='row'>
<div class='column side'></div>
<div class='column middle'>
<div class='code info'>
<h1> Operacion realizada con exito </h1><hr />
<p> Se ha eliminado la promoción correctamente en la base de datos.</p>
<a href='../panel_admin/index.php?state=mp'><button>Cerrar Mensaje</button></a>
</div>
<div class='column side'></div>
</div>
";
}
} else {
$this->reply = "<div class='row'>
<div class='column side'></div>
<div class='column middle'>
<div class='code info'>
<h1> ERROR </h1><hr />
<p> Ha habido un error en la operacion. Revisa los datos introducidos</p>
<a href='../panel_admin/index.php?state=mp'><button>Panel Admin</button></a>
</div>
<div class='column side'></div>
</div>
";
}
return $this->reply;
}
//Process form:
public function processesForm($_id, $_username, $_email, $_pass, $_rol) {
$this->correct = true;
$this->option = $_option;
$id= $this->test_input($_id);
$tittle=$this->test_input($_username);
$description=$this->test_input($_email);
$code=$this->test_input($_pass);
$active=$this->test_input($_rol);
//Habria que validar todo para que encaje en la base de datos
$bd = new Manager_DAO('complucine');
if($bd){
if($this->option == "new"){
//Check if any var is empty
if(!empty($_username)&&!empty($_email)&&!empty($_pass)&&!empty($_rol)){
// check if already exist a manager with same name
$exist = $bd->selectManager($_username);
if( mysqli_num_rows($exist) != 0){
$this->correct =false;
}
else{
$bd->createManager(null, $_username, $_email, $_pass, $_rol);
}
$exist->free();
}
else{
$this->correct =false;
}
} else if ($this->option == "del"){
//Check if exist a manager with this id
$exist = $bd-> GetManager($id);
if( mysqli_num_rows($exist) == 1){
$bd->deleteManager($id);
}
else{
$this->correct =false;
}
} else if ($this->option == "edit"){
//Check if any var is empty
if(!empty($_username)&&!empty($_email)&&!empty($_pass)&&!empty($_rol)){
//Check if exist a manager with this id
$exist = $bd-> PromotionData($id);
if( mysqli_num_rows($exist) == 1){
$bd->editManager($id,$_username, $_email, $_pass, $_rol);
}
else{
$this->correct =false;
}
$exist->free();
}
else{
$this->correct =false;
}
}
else {$this->correct = false;}
}
}
protected function test_input($input){
return htmlspecialchars(trim(strip_tags($input)));
}
}
?>

View File

@ -0,0 +1,186 @@
<?php
//General Config File:
include_once('../assets/php/config.php');
include_once('../assets/php/common/promotion.php');
include_once(__DIR__.'/includes/formPromotion.php');
// View functions
function print_managers(){
$manager = new Manager_DAO("complucine");
$managers = $manager->allManagersData();
$ids = array();
$usernames = array();
$email = array();
$pass = array();
$rol = array();
foreach($managers as $key => $value){
$ids[$key] = $value->getId();
$usernames[$key] = $value->getUsername();
$email[$key] = $value->getEmail();
$pass[$key] = $value->getPass();
$rol[$key] = $value->getRoll();
}
echo "<div class='row'>
<div class='column side'></div>
<div class='column middle'>
<table class='alt'>
<thead>
<tr>
<th>Id</th>
<th>Nombre</th>
<th>Email</th>
<th>password</th>
<th>Rol</th>
</tr>
</thead>
<tbody>
";
for($i = 0; $i < count($promos); $i++){
echo '<tr>
<td>'. $ids[$i] .'</td>
<td>'. $usernames[$i] .'</td>
<td>'. $email[$i] .'</td>
<td>'. $pass[$i] .'</td>
<td>'. $rol[$i] .'</td>
<td>
<form method="post" action="index.php?state=mp">
<input name="id" type="hidden" value="'.$ids[$i].'">
<input name="username" type="hidden" value="'.$usernames[$i].'">
<input name="email" type="hidden" value="'.$email[$i].'">
<input name="pass" type="hidden" value="'.$pass[$i].'">
<input name="rol" type="hidden" value="'.$rol[$i].'">
<input type="submit" id="submit" value="Editar" name="edit_manager" class="primary" />
</form>
</td>
<td>
<form method="post" action="index.php?state=mp">
<input name="id" type="hidden" value="'.$ids[$i].'">
<input name="username" type="hidden" value="'.$usernames[$i].'">
<input name="email" type="hidden" value="'.$email[$i].'">
<input name="pass" type="hidden" value="'.$pass[$i].'">
<input name="rol" type="hidden" value="'.$rol[$i].'">
<input type="submit" id="submit" value="Eliminar" name="delete_manager" class="primary" />
</form>
</td>
</tr>
';
}
echo'</tbody>
</table>
</div>
<div class="column side"></div>
';
}
function addManager(){
echo' <div class="column side"></div>
<div class="column middle">
<h2>Añadir promoción</h2>
<form method="post" action="index.php?state=mp">
<fieldset id="promotion_form">
<legend>Datos dela Promoción</legend>
<div>
<input type="text" name="username" id="username" placeholder="Nombre" />
</div>
<div>
<input type="email" name="email" id="email" placeholder="email" />
</div>
<div>
<input type="text" name="password" id="pass" placeholder="pass" />
</div>
</fieldset>
<div class="actions">
<input type="submit" id="submit" value="Añadir gerente" name="add_manager" class="primary" />
<input type="reset" id="reset" value="Borrar" />
</div>
</form>
</div>
<div class="column side"></div>
</div>
';
}
function deleteManager() {
echo'<div class="column side"></div>
<div class="column middle">
<h2>Editar Promoción</h2>
<form method="post" action="index.php?state=mp">
<div class="row">
<fieldset id="promotion_form">
<legend>¿Estás seguro de que quieres eliminar este gerente?</legend>
<input type="hidden" name="id" value='.$_POST['id'].'/>
<p>Id: '.$_POST['id'].' </p>
<p>Nombre: '.$_POST['username'].' </p>
<p>Email: '.$_POST['email'].' </p>
<p>Password: '.$_POST['pass'].' </p>
<p>Rol: '.$_POST['rol'].' </p>
</fieldset>
<div class="actions">
<input type="submit" id="submit" value="Eliminar" name="confirm_delete_manager" class="primary" />
<input type="submit" id="submit" value="Cancelar" name="cancel_delete_manager" class="primary" />
</div>
</div>
</form>
</div>
<div class="column side"></div>
';
}
function editManager() {
echo'<div class="column side"></div>
<div class="column middle">
<h2>Editar promoción</h2>
<form method="post" action="index.php?state=mp">
<div class="row">
<fieldset id="promotion_form">
<legend>Datos de la promoción</legend>
<input type="hidden" name="id" value='.$_POST['id'].'/>
<div>
<input type="text" name="username" value="'.$_POST['username'].'" />
</div>
<div>
<input type="email" name="email" value='.$_POST['email'].' />
</div>
<div>
<input type="text" name="pass" value='.$_POST['pass'].' />
</div>
</fieldset>
<div class="actions">
<input type="submit" id="submit" value="Editar" name="confirm_edit_manager" class="primary" />
<input type="reset" id="reset" value="Borrar" />
</div>
</div>
</form>
</div>
<div class="column side"></div>
';
}
// Logic Functions
function confirmDelete() {
$cine = new FormPromotion();
$cine->processesForm($_POST['id'],null,null,null,null,"del");
$_SESSION['message'] = $cine->getReply();
header('Location: ../panel_admin/index.php?state=mp');
}
function confirmEdit() {
$cine = new FormPromotion();
$cine->processesForm($_POST['id'], $_POST['username'], $_POST['email'], $_POST['pass'],"manager","edit");
$_SESSION['message']= $cine->getReply();
header('Location: ../panel_admin/index.php?state=mp');
}
function confirmAdd() {
$cine = new FormPromotion();
$cine->processesForm(null,$_POST['username'], $_POST['email'], $_POST['pass'],"manager","new");
$_SESSION['message'] = $cine->getReply();
header('Location: ../panel_admin/index.php?state=mp');
}
?>