<?php
// 文章模块控制器
class ArticleAction extends Action {
	protected function _initialize() {
		$this -> obj = M('article');
		$this -> assign('category', Tag :: getObj('category') -> categoryModule('article')); //设置标签类的栏目数组为当前模块栏目数组
	}
	public function defaults($id = 0) {
		$id or $id = intval(R('id'));
		$page = max(1, intval(R('page')));
		$isCreateHtml = defined('IN_CREATEHTML');
		if (C('urlmodel', 0, 'article') == 1) {
			$url = Url :: getContentUrl('article', $id, $page);
			$path = Url :: getContentPath('article', $id, $page);
			if (is_file($path)) {
				if (!$isCreateHtml) {
					header('Location:' . $url);
					exit;
				}
			} else $isCreateHtml = true;
		}
		$data = $this -> obj -> where(array('recycle' => 0, 'id' => $id)) -> find();
		if (empty($data) || (!$data['passed'] && $data['userid'] != get_cookie('userid'))) {
			if (defined('IN_CREATEHTML')) {
				return false;
			}
			$this -> showMsg(L('ACCESS_IS_NOT_EXIST'));
		}
		$category = Cache :: read('category', $data['catid']);
		if (empty($data['seotitle'])) {
			// 根据后台设置生成内容页seotitle
			$seotitle = C('contentseotitle', 0, 'article');
			$seotitle = str_replace('{sitename}', C('name'), $seotitle);
			$seotitle = str_replace('{catname}', $category['name'], $seotitle);
			$this -> assign('seotitle', $seotitle);
		}
		$data['content'] = Func :: fillDir($data['content']);
		$data['content'] = M('relatedlink') -> show($data['content'], 1, $data['keywords']);
		$this -> assign('extract', $data);
		$this -> assign('category', $category);
		// 确定风格
		define('STYLE', Tag :: getObj('category') -> config($data['catid'], 'style'));
		// 确定模板文件
		$tpl = $category['conttpl']?$category['conttpl']:get_module($category['moduleid'], 'name');
		$contents = explode('[nextpage]', $data['content']);
		$totle = count($contents);
		if ($isCreateHtml) {
			$htmlCnt = 0;
			foreach($contents as $k => $v) {
				$this -> assign('content', $v);
				$tmpPage = $k + 1;
				$this -> assign('pages', Func :: pages($tmpPage, $totle, Url :: getContentUrl('article', $id, '{page}'), Url :: getContentUrl('article', $id)));
				$html = $this -> fetch($tpl, true);
				if ($page == $tmpPage) $show = $html;
				$path = Url :: getContentPath('article', $id, $tmpPage);
				if (FileUtil :: write($path , $html)) $htmlCnt++;
			}
			if (!defined('IN_CREATEHTML')) {
				echo $show;
			} else {
				return $htmlCnt;
			}
		} else {
			if ($totle > 1) {
				$this -> assign('content', $contents[$page - 1]);
			}
			$this -> assign('pages', Func :: pages($page, $totle, Url :: getContentUrl('article', $id, '{page}'), Url :: getContentUrl('article', $id)));
			$this -> display($tpl);
		}
	}
	public function search() {
		$catid = intval(R('catid'));
		$q = R('q');
		$q = trim(urldecode($q));
		$q = Func :: FilterSearch($q);
		$this -> assign('title', L('SEARCH'));
		$this -> assign('catid', $catid);
		$this -> assign('data', $this -> obj -> search($q));
		$this -> assign('pages', $this -> obj -> pages());
		$this -> assign('totle', $this -> obj -> totle);
		$this -> assign('q', $q);
		$this -> assign('search_url', Url :: getSearchUrl('article'));
		// 确定风格
		define('STYLE', C('style', 0, 'article'));
		$this -> assign('category', Tag :: getObj('category') -> categoryModule('article'));
		$this -> display('article_search');
	}
	public function hits() {
		$id = intval(R('id'));
		$this -> obj -> setFields(array('hits' => 'hits+1'), 'id', $id);
		$data = $this -> obj -> field('hits') -> find($id);
		echo $data['hits'];
	}
}
