<?php
// 产品模块控制器
class ProductAction extends Action {
	public function defaults($id = 0) {
		$id or $id = intval(R('id'));
		$page = max(1, intval(R('page')));
		$isCreateHtml = defined('IN_CREATEHTML');
		if (C('urlmodel') == 1) {
			$url = Url :: getContentUrl('product', $id, $page);
			$path = Url :: getContentPath('product', $id, $page);
			if (is_file($path)) {
				if (!$isCreateHtml) {
					header('Location:' . $url);
					exit;
				}
			} else $isCreateHtml = true;
		}
		$data = M('product') -> 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, 'product');
			$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('product', $id, '{page}'), Url :: getContentUrl('product', $id)));
				$html = $this -> fetch($tpl);
				if ($page == $tmpPage) $show = $html;
				$path = Url :: getContentPath('product', $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('product', $id, '{page}'), Url :: getContentUrl('product', $id)));
			$this -> display($tpl);
		}
	}
	public function search() {
		$obj = M('product');
		$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', $obj -> search($q));
		$this -> assign('pages', $obj -> pages());
		$this -> assign('totle', $obj -> totle);
		$this -> assign('q', $q);
		$this -> assign('search_url', Url :: getSearchUrl('product'));
		// 确定风格
		define('STYLE', C('style', 0, 'product'));
		$this -> assign('category', Tag :: getObj('category') -> categoryModule('product'));
		$this -> display('product_search');
	}
	public function hits() {
		$id = intval(R('id'));
		$obj = M('product');
		$obj -> setFields(array('hits' => 'hits+1'), 'id', $id);
		$data = $obj -> field('hits') -> find($id);
		echo $data['hits'];
	}
}
