<?php
include('action.abs.php');
class keyword extends action {
		var $db = null;
		function keyword() {
				parent :: action();
				$this->db = APP :: ADP('db');
				$this->db->setTable(T_KEYWORD);
		}

		function add() {
				if ($this->_isPost()) {
					$keyword = V('p:keyword');
					if (!$keyword) {
						$this->_error('请填写要要过滤的关键字', array('add'));
					}
					$type = (int)V('p:type', '1');
					$keyword = trim($keyword, ', ');
					$keyword = explode(',', $keyword);
					if (sizeof($keyword) <=0 ) {
						$this->_error('没有可以插入的数据', array('add'));
					}
					$data = array();
					foreach ($keyword as $v) {
						$data[] = '("' . mysql_escape_string($v). '","' . $type. '","' . time() . '","' .$this->_getUid(). '")';
					}
				
					$data = implode(',', $data);
					
					$sql = 'INSERT IGNORE INTO ' . $this->db->getTable() . '(`keyword`,`type`,`add_time`,`operator`) VALUES' . $data;

					$this->db->execute($sql);
					if ($err = $this->db->getError()) {
						$this->_error('添加关键字时出错', array('add'));
					}
					if ($this->db->getAffectedRows() > 0) {
						$key = $type == 1? CACHE_DISABLED_NICK_KEYWORD : CACHE_DISABLED_CONTENT_KEYWORD;
						$cache = CACHE::get($key);
						if (!is_array($cache) || empty($cache)) {
							$this->_synchronousMemcache($type);
						} else {
							$size = sizeof($cache);
							foreach ($keyword as $k) {
								$cache[$k] = '1';
							}
							if (sizeof($cache) != $size) {
								CACHE :: set($key, $cache);
							}
						}
					}
					$this->_succ('已成功保存', URL('admin/keyword.keywordList','type=' . $type));
				}
				TPL :: display('admin/keyword/keyword_add', '', 0, false);
		}

		function keywordList() {
				$page = V('g:page', 1);
				$each = V('g:each', 20);
				$keyword = V('g:keyword', '');
				$type = (int)V('g:type','0');
				$offset = ($page -1) * $each;
				$where = array();
				if ($type != 0) {
					$where[] = '`type`="' . $type . '"';
				}
				if ($keyword) {
					$where[] = '`keyword` LIKE "%' . $keyword . '%"';
				}
				if (!empty($where)) {
					$where = ' WHERE ' . implode(' AND ', $where);
				} else {
					$where = '';
				}

				$select_count = 'SELECT COUNT(*) FROM ' . $this->db->getTable() . $where;
				$count = $this->db->getOne($select_count);

				// 检查memcache同否与mysql同步
				$key = $type == 1? CACHE_DISABLED_NICK_KEYWORD : CACHE_DISABLED_CONTENT_KEYWORD;
				$cache = CACHE :: get($key);
				if (!is_array($cache) || sizeof($cache) != $count) {
					$this->_synchronousMemcache($type);
				}

				$pager = APP :: N('pager');
				$page_param = array('currentPage'=> $page, 'pageSize' => $each, 'recordCount' => $count, 'linkNumber' => 10);
				$pager->setParam($page_param);
				TPL :: assign('pager', $pager->makePage());

				$sql = 'SELECT a.*,b.name FROM ' . $this->db->getTable() . ' AS a LEFT JOIN ' . $this->db->getTable(T_ADMIN_USER) . ' AS b ON a.operator=b.id ' . $where . ' ORDER BY a.id DESC LIMIT ' . $offset . ',' . $each;
				$rs = $this->db->query($sql);
				TPL :: assign('list', $rs);
				TPL :: assign('type', $type);
				TPL :: assign('key_type', array('1'=>'昵称和个人简介', '2'=>'微博和评论'));
				TPL :: display('admin/keyword/keyword_list', '', 0, false);
		}

		function delete() {
				$id = V('r:id');
				$type = (int)V('r:type');

				if (!is_array($id)) {
					$id = trim((string)$id, ' ,');
					$id = explode(',', $id);
				}
				
				$sql = 'SELECT `keyword` FROM '. $this->db->getTable() . ' WHERE id IN(' .implode(',', $id). ')';
				$data = $this->db->query($sql);

				$rs = $this->db->delete($id);
				if ($rs !== false && $rs > 0) {
					$key = $type == 1? CACHE_DISABLED_NICK_KEYWORD : CACHE_DISABLED_CONTENT_KEYWORD;
					$cache = CACHE::get($key);
					if (!is_array($cache) || empty($cache)) {
						$this->_synchronousMemcache($type);
					} else {
						$size = sizeof($cache);

						foreach ($data as $row) {
							unset($cache[$row['keyword']]) ;
						}
						if (sizeof($cache) != $size) {
							CACHE :: set($key, $cache);
						}
					}
				}
				//$this->_succ('已成功删除', URL('admin/keyword.keywordList','type=' . $type));
				APP::redirect(URL('admin/keyword.keywordList','type=' . $type), 3);
		}

		/**
		 * 同步Memcache
		 */
		function _synchronousMemcache($type) {
				$sql = 'SELECT `keyword` FROM ' . $this->db->getTable() . ' WHERE `type`=' . $type;
				$data = $this->db->query($sql);
				$cache = array();
				while($r = array_pop($data)) {
					$cache[$r['keyword']] = 1;
				}
				$key = $type == 1? CACHE_DISABLED_NICK_KEYWORD : CACHE_DISABLED_CONTENT_KEYWORD;
				CACHE :: set($key, $cache);
		}
}
