<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

class admin_pages_api extends ModuleAPI
{
	public $module='admin_pages';
	protected $implemented=array('sitemap', 'search', 'checkCommentAccess');

	function sitemap()
	{
		$result=array();
		
		$pages=Db::instance()->selectCol(
			"SELECT path AS ARRAY_KEY, updated
			FROM ?_tree t
			JOIN ?_pages p ON t.tree_id=p.tree_id
			WHERE t.sitemap=1 AND `show`=1"
		);

		if ($pages) {
			foreach ($pages as $link=>$date) {
				$changefreq=getFreq($date);
				$loc=_url(false, false, false, $link);
				$result[$loc]=array('changefreq'=>$changefreq,'lastmod'=>$date);
			}
		}
		return $result;
	}
	
	function search($string)
	{
		$search=Db::i()->select(
			"SELECT title, path, `text`,
			updated as `date`, MATCH(`text`) AGAINST(? IN BOOLEAN MODE) as rel
			FROM ?_pages p
			JOIN ?_tree t ON p.tree_id=t.tree_id
			WHERE MATCH(`text`) AGAINST(? IN BOOLEAN MODE)
			ORDER BY rel", $string, $string
		);
		return $search;
	}
	
	public function checkCommentAccess($key)
	{
		//проверяем что комментарии в настройках включены
		if (!config::get('module.'.$this->module.'.use_comments')) return false;
		//проверяем что страница существует и доступна
		$page=Db::i()->selectRow(
			"SELECT path, use_comments, `show` FROM ?_pages p JOIN ?_tree t ON p.tree_id=t.tree_id WHERE page_id=?", $key
		);
		if ($page['path'] && $page['use_comments'] && $page['show']) return true;
		return false;
	}
}