<?php
use Intervention\Image\ImageManagerStatic as Image;

$noticiasDao = new \Dao\Noticias();
$noticiasDao->getNoticiaPorId($conecta, filter_input(INPUT_POST, 'id'));
$noticiasDao->data = implode('-', array_reverse(explode('/', filter_input(INPUT_POST, 'data'))));
$noticiasDao->titulo = $conecta->real_escape_string(filter_input(INPUT_POST, 'titulo'));
$noticiasDao->texto = addslashes(filter_input(INPUT_POST, 'texto'));
$noticiasDao->slug = \Dao\Funcoes::removeCharacters($noticiasDao->titulo, '-');
$noticiasDao->data_cadastro = date('Y-m-d H:i:s');
$noticiasDao->usuario_cadastrou = $_SESSION['id_usuario'];
$noticiasDao->data_alteracao = date('Y-m-d H:i:s');
$noticiasDao->usuario_alterou = $_SESSION['id_usuario'];
if (!empty($_FILES['image']['name'])) {
    if ($noticiasDao->image != '' &&file_exists($pasta . $noticiasDao->image))
        unlink($pasta . $noticiasDao->image);
    
    $noticiasDao->image = \Dao\Funcoes::randFileName($_FILES['image']['name']);
    $image = Image::make($_FILES['image']['tmp_name'])->resize(800, null, function ($constraint) {
        $constraint->aspectRatio();
    });

    $image->save($pasta . $noticiasDao->image, 85);
}
$res = json_decode($noticiasDao->salvar($conecta));
if ($res->data == 'Success') 
    \Dao\Funcoes::displayMessage('Confirmação', $res->message, 'noticias.php');
else 
    \Dao\Funcoes::displayMessage('Erro', $res->message, 'noticias.php', 'red');