<?php
// Include necessary files and initialize the OSSN framework
include_once(dirname(dirname(dirname(__FILE__))) . "/system/start.php");

// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $title = input('title');
    $description = input('description');
    $guid = input('guid');
    
    // Validate inputs
    if (empty($title) || empty($description)) {
        ossn_trigger_message(ossn_print("wpages:fillfields"), 'error');
        redirect(REF);
    }
    
    // Handle file upload
    if (isset($_FILES['coverphoto']) && $_FILES['coverphoto']['error'] === UPLOAD_ERR_OK) {
        $uploadDir = ossn_get_userdata('page/cover/');
        $uploadFile = $uploadDir . basename($_FILES['coverphoto']['name']);
        
        // Move uploaded file to destination
        if (move_uploaded_file($_FILES['coverphoto']['tmp_name'], $uploadFile)) {
            $coverPhotoPath = $uploadFile;
        } else {
            echo "Error uploading file.";
            exit;
        }
    } else {
        echo "File upload error.";
        exit;
    }
    
    // Save page information
    $page = new \Ossn\Component\HelloWorld\Worlds;
    $page->title = $title;
    $page->description = $description;
    $page->data->coverphoto = $coverPhotoPath;
    
    if ($page->save()) {
        ossn_trigger_message(ossn_print("wpages:page:saved"));
        redirect("administrator/settings/wpages?mpage=list");
    } else {
        ossn_trigger_message(ossn_print("wpages:page:save:failed"), 'error');
        redirect(REF);
    }
}
?>


// process_form.php
<?php
// Ensure OSSN framework is initialized
require_once('/path/to/ossn/ossn_initialize.php');

// Check if form submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
    // Handle file upload
    if (isset($_FILES['coverphoto']) && $_FILES['coverphoto']['error'] === UPLOAD_ERR_OK) {
        $uploadDir = ossn_get_userdata('page/cover/');
        if (!is_dir($uploadDir)) {
            mkdir($uploadDir, 0755, true);
        }

        $uploadFile = $uploadDir . basename($_FILES['coverphoto']['name']);
        
        // Move uploaded file to destination
        if (move_uploaded_file($_FILES['coverphoto']['tmp_name'], $uploadFile)) {
            $coverPhotoPath = $uploadFile;
        } else {
            ossn_trigger_message('Error uploading file.', 'error');
            redirect(REF);
        }
    }

    // Process other form fields (title, description, etc.)
    $title = input('title');
    $description = input('description');

    if (empty($title) || empty($description)) {
        ossn_trigger_message('Please fill in all fields.', 'error');
        redirect(REF);
    }

    // Create a new Worlds object and save it
    $page = new Worlds;
    $page->title = $title;
    $page->description = $description;
    $page->owner_guid = ossn_loggedin_user()->guid;
    $page->subtype = 'worlds:page';

    if ($page->addObject()) {
        if (isset($coverPhotoPath)) {
            // Save the cover photo path in the object's data
            $page->data->coverphoto = $coverPhotoPath;
            $page->save();
        }
        ossn_trigger_message('Page created successfully.');
        redirect("administrator/settings/wpages?mpage=list");
    } else {
        ossn_trigger_message('Error creating page.', 'error');
        redirect(REF);
    }
}

