<?php
/**
 * @package iCMS
 * @copyright 2007-2010, iDreamSoft
 * @license http://www.idreamsoft.cn iDreamSoft
 * @author coolmoo <idreamsoft@qq.com>
 */
!defined('iPATH') && exit('What are you doing?');
switch ($operation) {
	case 'add':
		include_once(iPATH."include/fckeditor.php");
		$editor = new FCKeditor('replace') ;
		$id=(int)$_GET['id'];
		$rs	= $iCMS->db->getRow("SELECT * FROM `#iCMS@__keywords` where `id`='$id'");
		$editor->Value= $rs->replace;
		include iCMS_admincp_tpl('keywords.add');
	break;
	case 'del':
		$id=(int)$_GET['id'];
		$id && $iCMS->db->query("DELETE FROM `#iCMS@__keywords` WHERE `id` ='$id'");
		keywords_cache();
		_Header();
	break;
	case 'disabled':
		$id=(int)$_GET['id'];
		$id&&$iCMS->db->query("UPDATE `#iCMS@__keywords` SET `status` = '0'  WHERE `id` ='$id'");
		keywords_cache();
		_Header();
	break;
	case 'open':
		$id=(int)$_GET['id'];
		$id&&$iCMS->db->query("UPDATE `#iCMS@__keywords` SET `status` = '1'  WHERE `id` ='$id'");
		keywords_cache();
		_Header();
	break;
	case 'post':
		if($action=='edit'){
			if(isset($_POST['delete'])){
				foreach($_POST['delete'] as $k=>$id){
					$id && $iCMS->db->query("DELETE FROM `#iCMS@__keywords` WHERE `id` ='$id'");
				}
				keywords_cache();
				_Header();
			}
			foreach($_POST['name'] as $id=>$value){
				$iCMS->db->query("update `#iCMS@__keywords` set `keyword`='$value',`replace`='".$_POST['replace'][$id]."' where `id`='$id'");
			}
			keywords_cache();
			_Header();
			
		}
		if($action=='save'){
			$id	= (int)$_POST['id'];
			$keyword=dhtmlspecialchars($_POST['keyword']);
			$replace = preg_replace ("'<p>(.*?)<\/p>'si",'\\1', $_POST['replace']);
			if(empty($id)){
				$iCMS->db->query("insert into `#iCMS@__keywords`(`keyword`,`replace`,`addtime`,`status`) values ('$keyword','$replace','".time()."','1')");
			}else{
				$iCMS->db->query("update `#iCMS@__keywords` set `keyword`='$keyword',`replace`='$replace' where id='$id'");
			}
			keywords_cache();
			_Header(__SELF__.'?do=keywords');
		}
	break;
	default:
		$Admin->MP("menu_keywords");
		$maxperpage =20;
		$total=$iCMS->db->getValue("SELECT count(*) FROM `#iCMS@__keywords` order by id DESC");
		page($total,$maxperpage,"个关键字");
		$rs=$iCMS->db->getArray("SELECT * FROM `#iCMS@__keywords` order by id DESC LIMIT {$firstcount},{$maxperpage}");
		$_count=count($rs);
		include iCMS_admincp_tpl('keywords');
}
?>
