<?php

class page
{

	private $query;
	public $exists;
	public $data;

	public $slug;

	public function __construct($slug = false)
	{

		global $_sql;

		if ($slug)
		{
			$slug = sqlprot($slug);
			$this->slug = $slug;
			$this->query = $_sql->query("SELECT * FROM pages WHERE pageSlug = '$slug'");
			$this->exists = ($this->query->num_rows ? true : false);

			if ($this->exists)
				$this->data = $this->query->fetch_assoc();
		} else
		{
			$this->query = $_sql->query("SELECT * FROM pages".($trash ? ' WHERE pageDeleted = 1' : ''));
			$this->exists = ($this->query->num_rows ? true : false);
		}

	}


	public function status()
	{
		if ($this->exists)
			return true;
		return false;
	}

	public function readable()
	{
		if ($this->exists && !$this->data['pageDeleted'])
			return true;
		return false;
	}

	public function get_list()
	{
		$pages = array();
		while ($data = $this->query->fetch_assoc())
			array_push($pages, $data);
	}

	public function update($title, $content)
	{
		global $_sql;

		$title = sqlprot($title);
		$content = sqlprot($content);

		if (Check::title($title) && $this->data)
			if ($_sql->query("UPDATE pages SET pageTitle = '$title', pageContent = '$content' WHERE pageSlug = '".$this->data['pageSlug']."'"))
				return true;
		return false;
	}

	public function create($title, $content)
	{
		global $_sql;

		$slug = sqlprot($this->slug);
		$title = sqlprot($title);
		$content = sqlprot($content);

		if (Check::title($title) && Check::slug($slug) && !$this->data)
			if ($_sql->query("INSERT INTO pages (pageSlug, pageTitle, pageContent) VALUES ('$slug', '$title', '$content')"))
				return true;
		return false;
	}

	public function delete()
	{
		global $_sql;

		if ($this->data)
			if ($_sql->query("UPDATE pages SET pageDeleted = 1 WHERE pageSlug = '".$this->data['pageSlug']."'"))
				return true;
		return false;
	}
}