diff --git a/assets/php/common/manager.php b/assets/php/common/manager.php new file mode 100644 index 0000000..4a80b49 --- /dev/null +++ b/assets/php/common/manager.php @@ -0,0 +1,36 @@ +_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;} + + } +?> \ No newline at end of file diff --git a/assets/php/common/manager_dao.php b/assets/php/common/manager_dao.php new file mode 100644 index 0000000..67d4f71 --- /dev/null +++ b/assets/php/common/manager_dao.php @@ -0,0 +1,91 @@ +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); + } + + } + +?> diff --git a/panel_admin/includes/formManager.php b/panel_admin/includes/formManager.php new file mode 100644 index 0000000..106fcdd --- /dev/null +++ b/panel_admin/includes/formManager.php @@ -0,0 +1,148 @@ +reply = array(); + } + + public function getReply() { + if($this->correct){ + if($this->option == "new"){ + $this->reply = "
+
+
+
+

Operacion realizada con exito


+

Se ha añadido la promoción correctamente en la base de datos.

+ +
+
+
+ "; + }else if($this->option == "edit"){ + $this->reply = "
+
+
+
+

Operacion realizada con exito


+

Se ha editado la promoción correctamente en la base de datos.

+ +
+
+
+ "; + }else if($this->option == "del"){ + $this->reply = "
+
+
+
+

Operacion realizada con exito


+

Se ha eliminado la promoción correctamente en la base de datos.

+ +
+
+
+ "; + } + + } else { + $this->reply = "
+
+
+
+

ERROR


+

Ha habido un error en la operacion. Revisa los datos introducidos

+ +
+
+
+ "; + + } + 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))); + } +} + + +?> \ No newline at end of file diff --git a/panel_admin/manage_managers.php b/panel_admin/manage_managers.php new file mode 100644 index 0000000..16db494 --- /dev/null +++ b/panel_admin/manage_managers.php @@ -0,0 +1,186 @@ +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 "
+
+
+ + + + + + + + + + + + "; + for($i = 0; $i < count($promos); $i++){ + echo ' + + + + + + + + + '; + } + echo' +
IdNombreEmailpasswordRol
'. $ids[$i] .''. $usernames[$i] .''. $email[$i] .''. $pass[$i] .''. $rol[$i] .' +
+ + + + + + +
+
+
+ + + + + + +
+
+
+
+ '; + + } + + function addManager(){ + echo'
+
+

Añadir promoción

+
+
+ Datos dela Promoción +
+ +
+
+ +
+
+ +
+
+
+ + +
+
+
+
+
+ '; + } + function deleteManager() { + echo'
+
+

Editar Promoción

+
+
+
+ ¿Estás seguro de que quieres eliminar este gerente? + +

Id: '.$_POST['id'].'

+

Nombre: '.$_POST['username'].'

+

Email: '.$_POST['email'].'

+

Password: '.$_POST['pass'].'

+

Rol: '.$_POST['rol'].'

+
+
+ + +
+
+
+
+
+ '; + } + function editManager() { + echo'
+
+

Editar promoción

+
+
+
+ Datos de la promoción + +
+ +
+
+ +
+
+ +
+
+
+ + +
+
+
+
+
+ '; + } + + // 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'); + } + + +?> \ No newline at end of file