<?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 news_api extends ModuleAPI
{
	public $module='news';
	protected $implemented=array('sitemap', 'search', 'checkCommentAccess');
	
	function sitemap()
	{
		$result=array();
		
		$link=getModulePath($this->module);
		
		$result[$link]=array('changefreq'=>'hourly','lastmod'=>time());
		
		$news=Db::instance()->selectCol(
			"SELECT new_id AS ARRAY_KEY, `date`
			FROM ?_news
			WHERE `date`<=?
			ORDER BY `date` DESC
			LIMIT 10000", time()
		);
		if ($news) {
			foreach ($news as $new_id=>$date) {
				$changefreq=getFreq($date);
				$loc=LabCMS::instance()->url($new_id, 'item', false, $link);
				$result[$loc]=array('changefreq'=>$changefreq,'lastmod'=>$date);
			}
		}
		return $result;
	}
	
	function search($string)
	{
		$search=Db::instance()->select(
			"SELECT new_id, `date`, title, `text`, MATCH(`text`) AGAINST(? IN BOOLEAN MODE) as rel
			FROM ?_news
			WHERE `date`<=?
			AND MATCH(`text`) AGAINST(? IN BOOLEAN MODE)", $string, time(), $string
		);
		if ($search) {
			$path=getModulePath($this->module);
			foreach ($search as $k=>$v) {
				if (!$v['title']) $search[$k]['title']=date('Y.m.d', $v['date']);
				$search[$k]['path']=_url($v['new_id'], 'item', false, $path);
			}
		}
		return $search;
	}
	
	public function checkCommentAccess($key)
	{
		return false;
		//проверяем что комментарии в настройках включены
		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;
	}
}