Forms restructuring

This commit is contained in:
Daniel Muñoz Garcia 2021-05-13 10:00:47 +02:00 committed by GitHub
parent 4c7c93d691
commit 657d1cc0fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 402 additions and 53 deletions

View File

@ -0,0 +1,111 @@
<?php
//General Config File:
include_once('../assets/php/config.php');
include_once('../assets/php/common/film_dao.php');
include_once('../assets/php/common/film.php');
include_once('../assets/php/form.php');
class formAddFilm extends Form{
//Constants:
const HTML5_EMAIL_REGEXP = '^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$';
public function __construct() {
$options = array("action" => "./?state=mf");
parent::__construct('formAddFilm', $options);
}
protected function generaCamposFormulario($datos, $errores = array()){
// Se generan los mensajes de error si existen.
$htmlErroresGlobales = self::generaListaErroresGlobales($errores);
$errorTittle = self::createMensajeError($errores, 'tittle', 'span', array('class' => 'error'));
$errorDuration = self::createMensajeError($errores, 'duration', 'span', array('class' => 'error'));
$errorLanguage = self::createMensajeError($errores, 'language', 'span', array('class' => 'error'));
$errorDescription = self::createMensajeError($errores, 'description', 'span', array('class' => 'error'));
//$errorImage = self::createMensajeError($errores, 'image', 'span', array('class' => 'error'));
$html = '<div class="row">
<fieldset id="film_form"><pre>'.$htmlErroresGlobales.'</pre>
<legend>AÑADIR PELÍCULA</legend>
<input type="text" name="tittle" id="tittle" placeholder="Título" required/><pre>'.$errorTittle.'</pre>
<input type="number" name="duration" id="duration" placeholder="Duración" required/><pre>'.$errorDuration.'</pre>
<input type="text" name="language" id="language" placeholder="Idioma" required/><pre>'.$errorLanguage.'</pre>
<input type="text" name="description" id="description" placeholder="Descripción" required/><pre>'.$errorDescription.'</pre>
<div class="file">Imagen promocional:<input type="file" name="file" id="file" placeholder="Imagen promocional" /></div>
</fieldset>
<div class="actions">
<input type="submit" id="submit" value="Añadir pelicula" name="add_film" class="primary" />
<input type="reset" id="reset" value="Borrar" />
</div>
</div>
</div>';
return $html;
}
protected function procesaFormulario($datos){
$result = array();
$tittle = $this->test_input($datos['tittle']) ?? null;
//|| !mb_ereg_match(self::HTML5_EMAIL_REGEXP, $tittle)
if ( empty($tittle) ) {
$result['tittle'] = "El título no es válido";
}
$duration = $this->test_input($datos['duration']) ?? null;
//||!mb_ereg_match(self::HTML5_EMAIL_REGEXP, $duration)
if ( empty($duration) || $duration <0) {
$result['duration'] = "La duración no es válida";
}
$language = $this->test_input($datos['language']) ?? null;
//|| !mb_ereg_match(self::HTML5_EMAIL_REGEXP, $language)
if ( empty($language) ) {
$result['language'] = "El idioma no es válido";
}
$description = $this->test_input($datos['description']) ?? null;
//|| !mb_ereg_match(self::HTML5_EMAIL_REGEXP, $description)
if ( empty($language)) {
$result['language'] = "La descripcion no es válida";
}
if (count($result) === 0) {
$bd = new Film_DAO("complucine");
//FALTARIA SUBIR LA IMAGEN
$exist = $bd-> GetFilm($tittle,$language);
if(mysqli_num_rows($exist) != 0){
$result[] = "Ya existe una nueva pelicula con el mismo titulo e idioma.";
}
else{
$bd->createFilm(null, $tittle,$duration,$language,$description);
$_SESSION['message'] = "<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 pelicula correctamente en la base de datos.</p>
<a href='../panel_admin/index.php?state=mf'><button>Cerrar Mensaje</button></a>
</div>
</div>
<div class='column side'></div>
</div>
";
$result = './?state=mf';
}
$exist->free();
}
return $result;
}
protected function test_input($input){
return htmlspecialchars(trim(strip_tags($input)));
}
}
?>

View File

@ -0,0 +1,89 @@
<?php
//General Config File:
include_once('../assets/php/config.php');
include_once('../assets/php/common/film_dao.php');
include_once('../assets/php/common/film.php');
include_once('../assets/php/form.php');
class formDeleteFilm extends Form{
//Constants:
const HTML5_EMAIL_REGEXP = '^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$';
public function __construct() {
$options = array("action" => "./?state=mf");
parent::__construct('formDeleteFilm', $options);
}
protected function generaCamposFormulario($datos, $errores = array()){
// Se generan los mensajes de error si existen.
$htmlErroresGlobales = self::generaListaErroresGlobales($errores);
//$errorTittle = self::createMensajeError($errores, 'tittle', 'span', array('class' => 'error'));
//$errorDuration = self::createMensajeError($errores, 'duration', 'span', array('class' => 'error'));
//$errorLanguage = self::createMensajeError($errores, 'language', 'span', array('class' => 'error'));
//$errorDescription = self::createMensajeError($errores, 'description', 'span', array('class' => 'error'));
//$errorImage = self::createMensajeError($errores, 'image', 'span', array('class' => 'error'));
$html = '<div class="row">
<fieldset id="film_form">
<legend>¿Estás seguro de que quieres eliminar esta pelicula?</legend>
<input type="hidden" name="id" value='.$_POST['id'].'/>
<p>Id: '.$_POST['id'].' </p>
<p>Título: '.$_POST['tittle'].' </p>
<p>Duración: '.$_POST['duration'].' </p>
<p>Idioma: '.$_POST['language'].' </p>
<p>Descripción: '.$_POST['description'].' </p>
</fieldset>
<div class="actions">
<input type="submit" id="submit" value="Eliminar" name="delete_film" class="primary" />
<input type="submit" id="submit" value="Cancelar" class="primary" />
</div>
</div>';
return $html;
}
protected function procesaFormulario($datos){
$result = array();
$id = $this->test_input($_POST['id']) ?? null;
if ( empty($id)) {
$result[] = "La pelicula seleccionada no existe.";
}
if (count($result) === 0) {
$bd = new Film_DAO("complucine");
$exist = $bd-> FilmData($id);
if( mysqli_num_rows($exist) == 1){
$bd->deleteFilm($id);
$_SESSION['message'] = "<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 pelicula correctamente en la base de datos.</p>
<a href='../panel_admin/index.php?state=mf'><button>Cerrar Mensaje</button></a>
</div>
</div>
<div class='column side'></div>
</div>
";
$result = './?state=mf';
}
else{
$result[] = "La pelicula seleccionada no existe.";
}
$exist->free();
}
return $result;
}
protected function test_input($input){
return htmlspecialchars(trim(strip_tags($input)));
}
}
?>

View File

@ -0,0 +1,123 @@
<?php
//General Config File:
include_once('../assets/php/config.php');
include_once('../assets/php/common/film_dao.php');
include_once('../assets/php/common/film.php');
include_once('../assets/php/form.php');
class formEditFilm extends Form{
//Constants:
const HTML5_EMAIL_REGEXP = '^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$';
public function __construct() {
$options = array("action" => "./?state=mf");
parent::__construct('formEditFilm', $options);
}
protected function generaCamposFormulario($datos, $errores = array()){
// Se generan los mensajes de error si existen.
$htmlErroresGlobales = self::generaListaErroresGlobales($errores);
$errorTittle = self::createMensajeError($errores, 'tittle', 'span', array('class' => 'error'));
$errorDuration = self::createMensajeError($errores, 'duration', 'span', array('class' => 'error'));
$errorLanguage = self::createMensajeError($errores, 'language', 'span', array('class' => 'error'));
$errorDescription = self::createMensajeError($errores, 'description', 'span', array('class' => 'error'));
$errorImage = self::createMensajeError($errores, 'image', 'span', array('class' => 'error'));
$html = '<div class="column side"></div>
<div class="column middle">
<h2>Editar pelicula</h2>
<form method="post" enctype="multipart/form-data" action="index.php?state=mf">
<div class="row">
<fieldset id="film_form">
<legend>Datos de pelicula</legend>
<input type="hidden" name="id" value='.$_POST['id'].'/>
<input type="text" name="tittle" value='.$_POST['tittle'].' required/><pre>'.$errorTittle.'</pre>
<input type="number" name="duration" id="duration" value='.$_POST['duration'].' required/><pre>'.$errorDuration.'</pre>
<input type="text" name="language" id="language" value="'.$_POST['language'].'" required/><pre>'.$errorLanguage.'</pre>
<input type="text" name="description" id="description" value="'.$_POST['description'].'"required/><pre>'.$errorDescription.'</pre>
<div class="file">Imagen promocional:<input type="file" name="file" id="file" placeholder="Imagen promocional" /></div>
</fieldset>
<div class="actions">
<input type="submit" id="submit" value="Editar" name="edit_film" class="primary" />
<input type="reset" id="reset" value="Borrar" />
</div>
</div>
</form>
</div>
<div class="column side"></div>
';
return $html;
}
protected function procesaFormulario($datos){
$result = array();
$id = $this->test_input($_POST['id']) ?? null;
if ( empty($id)) {
$result[] = "La pelicula seleccionada no existe.";
}
$tittle = $this->test_input($datos['tittle']) ?? null;
//|| !mb_ereg_match(self::HTML5_EMAIL_REGEXP, $tittle)
if ( empty($tittle) ) {
$result['tittle'] = "El título no es válido";
}
$duration = $this->test_input($datos['duration']) ?? null;
//||!mb_ereg_match(self::HTML5_EMAIL_REGEXP, $duration)
if ( empty($duration) || $duration <0) {
$result['duration'] = "La duración no es válida";
}
$language = $this->test_input($datos['language']) ?? null;
//|| !mb_ereg_match(self::HTML5_EMAIL_REGEXP, $language)
if ( empty($language) ) {
$result['language'] = "El idioma no es válido";
}
$description = $this->test_input($datos['description']) ?? null;
//|| !mb_ereg_match(self::HTML5_EMAIL_REGEXP, $description)
if ( empty($language)) {
$result['language'] = "La descripcion no es válida";
}
if (count($result) === 0) {
$bd = new Film_DAO("complucine");
$exist = $bd-> FilmData($id);
if( mysqli_num_rows($exist) == 1){
$bd->editFilm($id,$tittle,$duration,$language,$description);
$_SESSION['message'] = "<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 pelicula correctamente en la base de datos.</p>
<a href='../panel_admin/index.php?state=mf'><button>Cerrar Mensaje</button></a>
</div>
</div>
<div class='column side'></div>
</div>
";
$result = './?state=mf';
}
else{
$result[] = "La pelicula seleccionada no existe.";
}
$exist->free();
}
return $result;
}
protected function test_input($input){
return htmlspecialchars(trim(strip_tags($input)));
}
}
?>

View File

@ -90,7 +90,7 @@ class FormManager extends Form {
if($bd){
if($this->option == "new"){
//Check if any var is empty
if(!is_null($id)&&!is_null($idcinema)){
if(!is_null($id)&&!empty($idcinema)){
// check if already exist a manager with same name
$exist = $bd->GetManagerCinema($id, $idcinema);
if( mysqli_num_rows($exist) != 0){

View File

@ -21,7 +21,7 @@
$usernames = array();
$email = array();
$rol = array();
if(is_array($managers)){
foreach($managers as $key => $value){
$ids[$key] = $value->getId();
$idscinemas[$key] = $value->getIdcinema();
@ -29,7 +29,7 @@
$email[$key] = $value->getEmail();
$rol[$key] = $value->getRoll();
}
}
echo "<div class='row'>
<div class='column side'></div>
@ -47,7 +47,6 @@
</thead>
<tbody>
";
if(is_array($managers)){
for($i = 0; $i < count($managers); $i++){
echo '<tr>
<td>'. $ids[$i] .'</td>
@ -78,7 +77,6 @@
</tr>
';
}
}
echo'</tbody>
</table>
</div>

View File

@ -33,24 +33,18 @@
};
break;
case 'mf': require_once('manage_films.php');
if(isset($_POST['edit_film'])) {
editFilm();
case 'mf': if(isset($_POST['edit_film'])) {
$this->editFilm();
}
else if(isset($_POST['delete_film'])) {
deleteFilm();
$this->deleteFilm();
}
else if(isset($_POST['add_film'])) {
confirmAdd();
}
else if(isset($_POST['confirm_delete_film'])) {
confirmDelete();
}
else if(isset($_POST['confirm_edit_film'])) {
confirmEdit();
$this->addFilm();
$template->print_fimls();
}
else {
addFilm();
$this->addFilm();
$template->print_fimls();
};
break;
@ -119,6 +113,40 @@
function getTemplate(){
return $this->template;
}
//Functions FILMS
function addFilm(){
include_once('./includes/formAddFilm.php');
$formAF = new formAddFilm();
$htmlAForm = $formAF->gestiona();
echo '<!-- Add film -->
<div class="column side"></div>
<div class="column middle">
'.$htmlAForm.'
</div>'."\n";
}
function deleteFilm() {
include_once('./includes/formDeleteFilm.php');
$formDF = new formDeleteFilm();
$htmlDForm = $formDF->gestiona();
echo '<!-- Add film -->
<div class="column side"></div>
<div class="column middle">
'.$htmlDForm.'
</div>'."\n";
}
function editFilm() {
include_once('./includes/formEditFilm.php');
$formEF = new formEditFilm();
$htmlDForm = $formEF->gestiona();
echo '<!-- Add film -->
<div class="column side"></div>
<div class="column middle">
'.$htmlDForm.'
</div>'."\n";
}
}
?>