<?php
namespace HelloWorld;

class Worlds {
    protected $db; // Database connection object
    
    public function __construct() {
        global $Ossn;
        $this->db = $Ossn->db; // Assigning global database object to local variable
    }

    /**
     * Add a new page
     * 
     * @param array $params Page details
     * 
     * @return boolean
     */
    public function addPage(array $params) {
        $title = isset($params['title']) ? $params['title'] : '';
        $description = isset($params['description']) ? $params['description'] : '';
        $owner_guid = ossn_loggedin_user()->guid;
        $subtype = 'worlds:page';
        
        // Example: Saving cover photo URL to data object
        $coverphoto = isset($params['coverphoto']) ? $params['coverphoto'] : '';
        $data = array('coverphoto' => $coverphoto);
        
        return $this->addObject($title, $description, $owner_guid, $subtype, $data, $coverphoto);
    }

    /**
     * Generic method to add object to database
     * 
     * @param string $title Page title
     * @param string $description Page description
     * @param int $owner_guid Owner GUID
     * @param string $subtype Subtype
     * @param array $data Additional data
     * 
     * @return boolean
     */
 protected function addObject($title, $description, $owner_guid, $subtype, $data = array()) {
    if (empty($title) || empty($owner_guid)) {
        return false; // Make sure critical data isn't missing
    }

    $params = array(
        'title' => $title,
        'description' => $description,
        'owner_guid' => $owner_guid,
        'subtype' => $subtype,
        'data' => json_encode($data) // Ensure data is encoded
    );

    error_log(print_r($params, true)); // Log the params for debugging

    $insert = $this->db->insert('entities', $params);

    return $insert ? true : false;
}


    /**
     * Get cover URL
     * 
     * @return string
     */			
   public function coverURL($guid) {
    // Assuming $guid is the unique ID of the object
    $query = "SELECT data FROM entities WHERE guid = {$guid} AND subtype = 'worlds:page'";
    $result = $this->db->fetch($query);

    if ($result && isset($result->data)) {
        $data = json_decode($result->data, true);
        return isset($data['coverphoto']) ? $data['coverphoto'] : '';
    }

    return '';
}


    /**
     * Retrieve pages based on search criteria
     * 
     * @param array $params Search parameters
     * 
     * @return array
     */
 public function getAll($params = array()) {
    $vars = array(
        'type' => 'admin', // Default type
        'subtype' => 'worlds:page', // Default subtype
    );
    $args = array_merge($vars, $params); // Merge defaults with passed params
    return $this->searchObject($args); // Call searchObject to retrieve results
}
		
		
		
		
		  // Define the searchObject method here
   protected function searchObject($params) {
    // Ensure $this->db is initialized
    if (!$this->db) {
        return null;
    }

    // Build the query from parameters
    $type = isset($params['type']) ? $params['type'] : 'user';
    $subtype = isset($params['subtype']) ? $params['subtype'] : 'worlds:page';
    
    // Example: Fetch records from the 'entities' table where type and subtype match
    $query = "SELECT * FROM entities WHERE type = '{$type}' AND subtype = '{$subtype}'";

    // Perform the query
    $results = $this->db->execute($query);
    
    return $results ? $results : array(); // Return results or an empty array
}
		
		
		
}
?>
