<?php
$title = input('title');
$description = input('description');
$guid = input('guid');

if (empty($title) || empty($description)) {
    ossn_trigger_message(ossn_print("wpages:fillfields"), 'error');
    redirect(REF);
}

// Prepare $params array to pass to addPage() method
$params = array(
    'title'       => $title,
    'description' => $description,
    'coverphoto'  => isset($_FILES['coverphoto']) ? $_FILES['coverphoto'] : '',
);

// Create an instance of the Worlds class
$worlds = new \HelloWorld\Worlds;

// Use the addPage() method and pass the $params array
if ($worlds->addPage($params)) {
    // Handle cover photo upload if present
    if ($_FILES['coverphoto']['error'] === UPLOAD_ERR_OK) {
        $cover = new \HelloWorld\Cover;
        if ($cover->addCover($worlds->guid)) {
            ossn_trigger_message(ossn_print("wpages:page:saved"));
            redirect(REF);
        } else {
            ossn_trigger_message(ossn_print("wpages:cover:upload:failed"), 'error');
            redirect(REF);
        }
    } else {
        ossn_trigger_message(ossn_print("wpages:page:saved"));
        redirect(REF);
    }
} else {
    ossn_trigger_message(ossn_print("wpages:page:save:failed"), 'error');
    redirect(REF);
}
?>
