<?php defined('BLUDIT') or die('Bludit CMS.');

/*
Database structure
- To re index the list of posts and pages need to be sorted

{
	"videos": {
		"name": "Videos",
		"posts": [ "first-post", "second-post" ],
		"pages": [ "my-page", "second-page" ]
	},
	"pets": {
		"name": "Pets",
		"posts": [ "second-post", "bull-terrier" ],
		"pages": [ ]
	}
}

*/

class dbCategories extends dbJSON
{
	public $dbFields = array();

	function __construct()
	{
		parent::__construct(PATH_DATABASES.'categories.php');
	}

	private function getByCategory($type='posts', $categoryKey, $amountPerPage, $pageNumber)
	{
		// Check if the category exists
		if( !isset($this->db[$categoryKey]) ) {
			Log::set(__METHOD__.LOG_SEP.'Error getting '.$type.' by the category: '.$categoryKey);
			return array();
		}

		$list = $this->db[$categoryKey][$type];

		$init = (int) $amountPerPage * $pageNumber;
		$end  = (int) min( ($init + $amountPerPage - 1), count($list) - 1 );
		$outrange = $init<0 ? true : $init > $end;

		if($outrange) {
			Log::set(__METHOD__.LOG_SEP.'Error getting '.$type.' by the category, out of range, pageNumber: '.$pageNumber);
			return array();
		}

		$tmp = array_flip($list);
		return array_slice($tmp, $init, $amountPerPage, true);
	}

	public function getPagesByCategory($categoryKey, $amountPerPage, $pageNumber)
	{
		return $this->getByCategory('pages', $categoryKey, $amountPerPage, $pageNumber);
	}

	public function getPostsByCategory($categoryKey, $amountPerPage, $pageNumber)
	{
		return $this->getByCategory('posts', $categoryKey, $amountPerPage, $pageNumber);
	}

	private function countByCategory($type='posts', $categoryKey)
	{
		if( isset($this->db[$categoryKey][$type]) ) {
			return count($this->db[$categoryKey][$type]);
		}

		return 0;
	}

	public function countPostsByCategory($categoryKey)
	{
		return $this->countByCategory('posts', $categoryKey);
	}

	public function countPagesByCategory($categoryKey)
	{
		return $this->countByCategory('pages', $categoryKey);
	}

	public function getAll()
	{
		$tmp = array();
		foreach($this->db as $key=>$data) {
			$tmp[$key] = $data['name'];
		}

		// Sort low to high, by value.
		natcasesort($tmp);

		return $tmp;
	}

	// Re-generate posts index
	// (array) $db, the $db must be sorted by date and the posts published only.
	public function reIndexPosts($db)
	{
		$index = array();

		// Foreach post in the database
		foreach($db as $postKey=>$postData) {
			if(!empty($postData['category'])) {
				$categoryKey = $postData['category'];
				array_push($index, $postKey);
			}
		}

		$this->db[$categoryKey]['posts'] = $index;

		if( $this->save() === false ) {
			Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
			return false;
		}

		return true;
	}

	// Re-generate pages index
	// (array) $db, the $db must be sorted by date and the posts published only.
	public function reIndexPages($db)
	{
		$index = array();

		// Foreach post in the database
		foreach($db as $pageKey=>$pageData) {
			$categoryKey = $pageData['category'];
			array_push($index, $pageKey);
		}

		$this->db[$categoryKey]['pages'] = $index;

		if( $this->save() === false ) {
			Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
			return false;
		}

		return true;
	}


}