<?php defined('KOBLOG') or die('Koblog CMS.');
header('Content-Type: application/json');

/*
| Search for pages that have in the title the string $query and returns the array of pages
|
| @_GET['type']		the type to search in the database 
| @_GET['state']		the state to search in the database
| @_GET['sticky']	boolean False to not search sticky post
|
| @return		array
*/

// $_GET
// ----------------------------------------------------------------------------
$type = empty($GET_['type']) ? ['article'] : explode(",", $GET_['type']);
$state = empty($GET_['state']) ? ['published'] : explode(",", $GET_['state']);
$sticky = isset($_GET['sticky']) ? $_GET['sticky'] != false:true; // True by default
$query = isset($_GET['query']) ? Text::lowercase($_GET['query']) : false;
// ----------------------------------------------------------------------------

if ($query===false) {
	ajaxResponse(1, 'Invalid query.');
}

$pageNumber = 1;
$numberOfItems = -1;
$pagesKey = $pages->getList($pageNumber, $numberOfItems, $type, $state, $sticky);
$tmp = array();
foreach ($pagesKey as $pageKey) {
	try {
		$page = new Page($pageKey);
		$lowerTitle = Text::lowercase($page->title());
		if (Text::stringContains($lowerTitle, $query)) {
			$tmp[$page->key()] = $page->json(true);
		}
	} catch (Exception $e) {
		// continue
	}
}

exit (json_encode($tmp));

?>