<?php

require_once("componentes/tinify-php-master/lib/Tinify/Exception.php");
require_once("componentes/tinify-php-master/lib/Tinify/ResultMeta.php");
require_once("componentes/tinify-php-master/lib/Tinify/Result.php");
require_once("componentes/tinify-php-master/lib/Tinify/Source.php");
require_once("componentes/tinify-php-master/lib/Tinify/Client.php");
require_once("componentes/tinify-php-master/lib/Tinify.php");



Tinify\setKey("fFzCmf5BZzTHKbld00lLNDFKB471jcjR");



class DaoBlog extends conexao {

    public function Atualizar(blog $blog) {

        $flag = 'NAO';
		if (( isset($_FILES['blo_foto']) && !empty($_FILES['blo_foto'])  && ($_FILES["blo_foto"]["name"]) != '')   ) {
			$quantidade	= count   ($_FILES["blo_foto"]['name']);
			$extensao   = pathinfo($_FILES['blo_foto']['name']);
			$extensao   = ".".$extensao['extension'];
			$blo_foto   = md5(uniqid()) . '-' . time(). $extensao;


            $source = \Tinify\fromFile($_FILES["blo_foto"]["tmp_name"]);
            $resized = $source->resize(array(
                "method" => "cover",
                "width" => 600,
                "height" => 268
            ));
            $resized->toFile('uploads/blog/'.'optimized'.$blo_foto);
		   
			if (move_uploaded_file($_FILES["blo_foto"]["tmp_name"], 'uploads/blog/'.$blo_foto )){
				$flag = 'SIM';
				echo '<script>alert("Foto atualizada com sucesso: '.$blo_foto. '")</script>';
			}			
		}


        try {
            if ($blog->getBlo_id() != '')  {

                if ($flag == 'SIM'){  
                    $sql = " UPDATE blog SET
                                blo_id          = ?,
                                blo_titulo      = ?,
                                blo_texto       = ?,
                                blo_autor       = ?,
                                blo_categoria   = ?,
                                blo_dt_inserido = ?,
                                blo_foto        = ?,
                                blo_situacao    = ?
                            WHERE blo_id = ? ";

                    $stmt = self::conectar()->prepare($sql);
                    $stmt->bindValue(1, $blog->getBlo_id());
                    $stmt->bindValue(2, $blog->getBlo_titulo());
                    $stmt->bindValue(3, $blog->getBlo_texto());
                    $stmt->bindValue(4, $blog->getBlo_autor());
                    $stmt->bindValue(5, $blog->getBlo_categoria());
                    $stmt->bindValue(6, $blog->getBlo_dt_inserido());
                    $stmt->bindValue(7, $blo_foto);
                    $stmt->bindValue(8, $blog->getBlo_situacao());
                    $stmt->bindValue(9, $blog->getBlo_id());
                    
                }else{
                    $sql = " UPDATE blog SET
                                blo_id          = ?,
                                blo_titulo      = ?,
                                blo_texto       = ?,
                                blo_autor       = ?,
                                blo_categoria   = ?,
                                blo_dt_inserido = ?,
                                blo_situacao    = ?
                            WHERE blo_id = ? ";

                    $stmt = self::conectar()->prepare($sql);
                    $stmt->bindValue(1, $blog->getBlo_id());
                    $stmt->bindValue(2, $blog->getBlo_titulo());
                    $stmt->bindValue(3, $blog->getBlo_texto());
                    $stmt->bindValue(4, $blog->getBlo_autor());
                    $stmt->bindValue(5, $blog->getBlo_categoria());
                    $stmt->bindValue(6, $blog->getBlo_dt_inserido());
                    $stmt->bindValue(7, $blog->getBlo_situacao());
                    $stmt->bindValue(8, $blog->getBlo_id());

                }

            }
            else {

                $sql = " INSERT INTO blog (blo_id,blo_titulo,blo_texto,blo_autor,blo_categoria,blo_dt_inserido,blo_foto,blo_situacao)
                            VALUES (?,?,?,?,?,?,?,?)";

                $stmt = self::conectar()->prepare($sql);
                $stmt->bindValue(1, $blog->getBlo_id());
                $stmt->bindValue(2, $blog->getBlo_titulo());
                $stmt->bindValue(3, $blog->getBlo_texto());
                $stmt->bindValue(4, $blog->getBlo_autor());
                $stmt->bindValue(5, $blog->getBlo_categoria());
                $stmt->bindValue(6, $blog->getBlo_dt_inserido());
                $stmt->bindValue(7, $blo_foto);
                $stmt->bindValue(8, $blog->getBlo_situacao());

            }
            if ($stmt->execute()) {
                return true;
            }
            else 
                return false;

        }
        catch(Exception $e)  {
            print "Erro ao tentar atualizar: " . $e;
        }

    }

    public function PesquisarBlog($codigo)  {
        $sql = " SELECT * FROM blog WHERE blo_id = ? ";
        $stmt = self::conectar()->prepare($sql);
        $stmt->bindValue(1, $codigo);
        $stmt->execute();
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }

    public function ListarBlogs() {
        $sql = " SELECT * FROM blog ORDER BY blo_id ";
        $stmt = self::conectar()->prepare($sql);
        $stmt->execute();
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }

    public function ListarCategoriasBlogs() {
        $sql = " SELECT * FROM blog_categoria ORDER BY blc_id ";
        $stmt = self::conectar()->prepare($sql);
        $stmt->execute();
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }

    public function ListarUltimasPostagensBlogs() {
        $sql = " SELECT * FROM blog ORDER BY blo_id limit 0,4";
        $stmt = self::conectar()->prepare($sql);
        $stmt->execute();
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }

}

?>
