Example #1
0
 public function add()
 {
     if (!isset($this->currentUser)) {
         throw new Exception("Not in session. Adding posts requires login");
     }
     $pincho = new Pincho();
     if (isset($_POST["submit"])) {
         // reaching via HTTP Post...
         // populate the Post object with data form the form
         $pincho->setNombrePincho($_POST["nombrePincho"]);
         $pincho->setDescripcionPincho($_POST["descripcion"]);
         $pincho->setPrecio($_POST["precio"]);
         $pincho->setCeliaco($_POST["celiaco"]);
         // The user of the Post is the currentUser (user in session)
         try {
             // validate Post object
             $pincho->checkIsValidForCreate();
             // if it fails, ValidationException
             // save the Post object into the database
             $this->pinchoMapper->save($pincho);
             // POST-REDIRECT-GET
             // Everything OK, we will redirect the user to the list of posts
             // We want to see a message after redirection, so we establish
             // a "flash" message (which is simply a Session variable) to be
             // get in the view after redirection.
             $this->view->setFlash("Pincho \"" . $pincho->getNombrePincho() . "\" successfully added.");
             // perform the redirection. More or less:
             // header("Location: index.php?controller=posts&action=index")
             // die();
             $this->view->redirect("Pincho", "view");
         } catch (ValidationException $ex) {
             // Get the errors array inside the exepction...
             $errors = $ex->getErrors();
             // And put it to the view as "errors" variable
             $this->view->setVariable("errors", $errors);
         }
     }
     // Put the Post object visible to the view
     $this->view->setVariable("pincho", $pincho);
     // render the view (/view/posts/add.php)
     $this->view->render("pinchos", "add");
 }
 public function register()
 {
     $currentuser = $this->view->getVariable("currentusername");
     $establecimiento = $this->establecimientoMapper->findById($currentuser);
     $concursos = $this->concursoMapper->findConcurso("pinchosOurense");
     $this->view->setVariable("concursos", $concursos);
     $pincho = $this->pincho->findByEstablecimiento($establecimiento);
     if ($pincho != NULL) {
         $this->view->setFlash(sprintf("Ya hay un pincho registrado."));
         $this->view->render("concursos", "index");
     } else {
         if (isset($_POST["nombre"])) {
             $pinc = new Pincho();
             $pinc->setNombre($_POST["nombre"]);
             if (isset($_POST["celiaco"])) {
                 $pinc->setCeliaco(1);
             } else {
                 $pinc->setCeliaco(0);
             }
             if (isset($_FILES['img'])) {
                 $fichero = $_FILES['img']['name'];
                 $ext = strrchr($fichero, '.');
                 $ruta = 'imagenes/pincho_' . $pinc->getNombre() . $ext;
                 if (move_uploaded_file($_FILES['img']['tmp_name'], $ruta)) {
                     $pinc->setImagen($pinc->getNombre() . $ext);
                 }
             }
             $pinc->setDescripcion($_POST["descripcion"]);
             $pinc->setValidado(0);
             $pinc->setEstablecimiento($currentuser);
             $pinc->setConcurso("pinchosOurense");
             try {
                 $this->establecimientoMapper->savePincho($pinc);
                 $pincho = $this->pincho->findByEstablecimiento($establecimiento);
                 if (isset($_POST["ingredientesSelected"])) {
                     $ingredientesPOST = $_POST["ingredientesSelected"];
                     $ingredientesNew = array_unique($ingredientesPOST, SORT_STRING);
                     foreach ($ingredientesNew as $ingrediente) {
                         if (!empty($ingrediente)) {
                             $this->ingredienteMapper->insert($pincho, $ingrediente);
                         }
                     }
                 }
                 $this->view->setFlash("Pincho " . $pinc->getNombre() . " registrado.");
             } catch (ValidationException $ex) {
                 $errors = $ex->getErrors();
                 $this->view->setVariable("errors", $errors);
             }
         }
         $this->view->setVariable("pincho", $pinc);
         $this->view->render("concursos", "index");
     }
 }