Пример #1
0
 public static function getById($id)
 {
     $sql = 'select * from alimento where id = :id';
     $query = self::nuevaDb()->prepare($sql);
     $query->execute(array(":id" => $id));
     $arreglo = $query->fetchAll(PDO::FETCH_ASSOC);
     $result = Alimento::inicializar($arreglo[0]);
     return $result;
 }
 public function eliminar($id = null)
 {
     Session::tienePermiso('eliminar');
     $this->check_csrf('get');
     $this->actualizar_csrf();
     $alimento = Alimento::getById($id);
     if ($alimento->getId() != null) {
         if ($alimento->borrar()) {
             $this->redireccionar('alimento/listado&m=b');
         } else {
             $this->view->setError("No se pudo eliminar el alimento");
         }
     } else {
         $this->redireccionar('alimento/listado');
     }
 }
 public function modificar($id)
 {
     Session::tienePermiso('modificar');
     if (isset($_POST['aux'])) {
         $this->check_csrf('post');
         $this->actualizar_csrf();
         $modif = $_POST['params']['alimento'];
         $alimento = Alimento::getById($id);
         if (count($alimento->getErrores()) == 0) {
             $repetido = Alimento::buscarPor('codigo', $modif['codigo']);
             if (sizeof($repetido) == 0 || $repetido[0]['id'] == $id) {
                 $alimento->setCodigo($modif['codigo']);
                 $alimento->setDescripcion($modif['descripcion']);
                 if (!$alimento->actualizar()) {
                     $this->view->setError("no puedo actualizarse el alimento.");
                 }
             } else {
                 $this->view->setError("el codigo ya existe");
             }
             if ($this->view->getError() != null) {
                 $this->view->renderizar("formulario", array("alimento" => $alimento, "accion" => "modificar", "encabezado" => "Modificar alimento", "token" => $this->token, "token_id" => $this->token_id));
             } else {
                 $this->redireccionar('alimento/listado');
             }
         }
     } else {
         $alimento = Alimento::getById($id);
         if ($alimento->getId() != null) {
             $this->view->renderizar("formulario", array("alimento" => $alimento, "accion" => "modificar", "encabezado" => "Modificar alimento", "token" => $this->token, "token_id" => $this->token_id));
         } else {
             $this->redireccionar('alimento/listado');
         }
     }
 }