<?php

class blog
{

	private $entry;
	private $query;

	public $perpage = 10;
	public $entries = 0;

	public function __construct($entry = false, $page = 1)
	{
		global $_set, $_sql;

		$this->perpage = $_set['entriesPerPage'];
		if ($entry)
		{
			$this->query = $_sql->query("SELECT entries.*, users.userName AS userName, users.userPublicName AS publicName FROM entries INNER JOIN users ON userId = entryBy WHERE entrySlug = '".sqlprot($entry)."' LIMIT 1");
			if ($this->query->num_rows)
			{
				$this->entries = 1;
			}
		} else
		{
			$this->query = $_sql->query("SELECT entries.*, users.userName AS userName, users.userPublicName AS publicName FROM entries INNER JOIN users ON userId = entryBy WHERE entryHidden IS NULL AND entryPublished <= ".time()." ORDER BY entryPinned DESC, entryPublished DESC, entryId DESC LIMIT ".$this->perpage." OFFSET ".(($page-1) * $this->perpage)."");
			$this->entries = $this->query->num_rows;
		}
	}

	public function entries()
	{
		if ($this->entries)
			return $this->query->fetch_assoc();
		return false;
	}

	public function entry()
	{
		if ($this->entries == 1)
			return $this->query->fetch_assoc();
		return false;
	}

	public static function update($id, $header, $title, $text, $pub, $hidden = false, $pin = false)
	{
		global $_sql;

		$header = sqlprot($header);
		$title = sqlprot($title);
		$text = sqlprot($text);
		$published = strtotime($pub); if (!$published) $published = time();

		if (is_numeric($id) && Check::url($header, true) && Check::title($title) && $_sql->query("UPDATE entries SET entryHeader = '$header', entryTitle = '$title', entryContent = '$text', entryPublished = $published, entryUpdated = ".time().", entryHidden = ".($hidden ? '1' : 'NULL').", entryPIN = ".($pin && is_numeric($pin) ? $pin : 'NULL')." WHERE entryId = $id"))
			return true;
		return false;
	}

	public static function add($header, $title, $slug, $text, $pub, $hidden = false, $pin = false)
	{
		global $_sql, $user;

		$header = sqlprot($header);
		$title = sqlprot($title);
		$text = sqlprot($text);
		$slug = sqlprot($slug);
		$published = strtotime($pub); if (!$published) $published = time();
		if (Check::url($header, true) && Check::title($title) && Check::slug($slug) && $_sql->query("INSERT INTO entries (entryHeader, entryTitle, entrySlug, entryContent, entryBy, entryCreated, entryPublished, entryHidden, entryPIN) VALUES ('$header', '$title', '$slug', '$text', $user[userId], ".time().", $published, ".($hidden ? '1' : 'NULL').", ".($pin && is_numeric($pin) ? $pin : 'NULL').")"))
			return true;
		//die($text);
		return false;
	}

	public static function delete($id)
	{
		global $_sql;

		if (is_numeric($id) && $_sql->query("DELETE FROM entries WHERE entryId = $id"))
			return true;
		return false;
	}

	public static function pin($id)
	{
		global $_sql;

		if (is_numeric($id) && $_sql->query("UPDATE entries SET entryPinned = 1 WHERE entryId = $id"))
			return true;
		return false;
	}

	public static function unpin($id)
	{
		global $_sql;

		if (is_numeric($id) && $_sql->query("UPDATE entries SET entryPinned = NULL WHERE entryId = $id"))
			return true;
		return false;
	}

	public static function tag($cid, $id)
	{
		if (!is_numeric($cid) || !is_numeric($id)) return false;

		global $_sql;

		if (!$_sql->query("SELECT * FROM tags WHERE tagId = $cid")->num_rows) return false;
		if ($_sql->query("SELECT * FROM tagged WHERE taggedTag = $cid AND taggedEntry = $id")->num_rows) return false;
		if ($_sql->query("INSERT INTO tagged (taggedTag, taggedEntry) VALUES ($cid, $id)")) return true;

		return false;
	}

	public static function tagRemove($cid, $id)
	{
		if (!is_numeric($cid) || !is_numeric($id)) return false;

		global $_sql;

		if ($_sql->query("DELETE FROM tagged WHERE taggedTag = $cid AND taggedEntry = $id")) return true;

		return false;
	}
}
