日志
加入自动站外链接与标签(tag)系统
发布者:星野天河 发布时间:08-06-27
查看次数:8 评论数:0
标签:标签(8) tag(1) 站外链(1)
要加自动站外链接主要是考虑到音乐模块大多引用了朋友的作品,不加个链接过意不去,但也不想每次都手工加链接,所以做了个简单的自动链接系统。管理链接采用全AJAX方式,可以看看我以前写的演示。
TAG系统在去年七月就做过,不过当时核心类都是老大写的,我只负责调用而己,不过还是对这玩意儿头痛不己,看起来简单出错率却很高。
这次在修改前台页面前先写好TAG类,把所有可能用到的功能都写出来,一个一个方法调试,未想投入使用时“健壮性”却令人欣喜,基本上没发生什么错误,标签的统计还是采用+1,-1的方式,还准备好了“刷新全部标签数据”,“修改无效标签”方法,不过没出什么问题所以没有用上。实在是爽啊,遥想当年(好像就一年)花十倍的时间也达不到这种稳定性。
既然逻辑代码简单,那便在前面表现层提高效果,于是把之前写的“仿thickbox插件"功能做点扩充,做成鼠标移动(或点击)到标签,自动在当前位置弹出使用此标签的相关内容窗口的形式,用起来该算舒服。
标签系统可以考虑的扩展方向
1. 自动站内链接:其实是自动标签链接,在保存正文时,扫描其中的TAG,自动添加标签链接。
2. 写个FCK插件——注释系统:对一些可能需要更深入解释的词句添加注释块,这些词句形如普通链接,但鼠标移动到会弹出注释框,这样即保持了主体内容简炼清晰,又可以对相关内容更进一步说明。当然这个系统需要新的表,但前面表现可借鉴标签部分。
最后老规矩,扔代码跑人
PHP代码
- <?php
- class tag extends model
- {
- protected $_table='xy_tagContent';
- private $tb = 'xy_tag';
- public function __construct()
- {
- parent::__construct();
- }
- //新建内容时加入标签
- public function saveTag($tagStr, $mid, $cid)
- {
- $mid = intval($mid); $cid = intval($cid);
- if(!$mid || !$cid) return false;
- if($this->getField("SELECT id FROM $this->_table WHERE mid = $mid AND cid = $cid")) return $this->updateTag($tagStr, $mid, $cid);
- $tags = $this->format($tagStr);
- return $this->addTags($tags, $mid, $cid);
- }
- //更新标签,分为两步。1。删除不再使用的标签($delArr) 2。添加新标签($addArr)
- public function updateTag($tagStr, $mid, $cid)
- {
- $mid = intval($mid); $cid = intval($cid);
- $newTags = $this->format($tagStr);
- $oldTags = $this->getAll("SELECT title FROM $this->_table JOIN $this->tb on xy_tag.id = tid WHERE mid = $mid AND cid = $cid");
- foreach ($oldTags as &$tag) {
- $tag = $tag['title'];
- }
- foreach ($oldTags as $item) {
- if(!in_array($item, $newTags)) $delArr[] = $item;
- }
- foreach ($newTags as $item) {
- if(!in_array($item, $oldTags)) $addArr[] = $item;
- }
- $this->delTags($delArr, $mid, $cid);
- $this->addTags($addArr, $mid, $cid);
- }
- //取标签字符串 $link = 是否添加链接
- public function getStr($mid=0, $cid=0, $link=true, $num=20)
- {
- $tags = $this->getTags($mid, $cid, $num);
- foreach ($tags as $tag) {
- if($link) {
- $str .= '<a class="tag" href="'.ROOT.'?m=tag&id='.$tag['tid'].'">'.$tag['title'].'<span class="error">('.$tag['num'].')</span></a> ';
- }else{
- $str .= $tag['title'].' ';
- }
- }
- return trim($str);
- }
- //取使用某标签的内容的id,title,mid,num
- public function getContent($tag, $mid=0)
- {
- if (is_numeric($tag)) $tid = $tag;
- else $tid = $this->getField("SELECT id FROM $this->tb WHERE title = '$tag'");
- if($mid) $and = "AND mid = $mid";
- $sql = "SELECT mid,cid FROM $this->_table WHERE tid = $tid $and";
- $rs = $this->getAll($sql);
- $table = array('',PREV.'blog', PREV.'photo', PREV.'music');
- foreach ($rs as &$item) {
- $item['title'] = $this->getField("SELECT title FROM {$table[$item['mid']]} WHERE id = {$item['cid']}");
- }
- return $rs;
- }
- //取标签数组
- private function getTags($mid=0, $cid=0, $num=20)
- {
- $mid = intval($mid); $cid = intval($cid);
- if($mid && $cid) $where = "WHERE mid = $mid AND cid = $cid";
- elseif ($mid) $where = "WHERE mid = $mid";
- $sql = "SELECT title, tid, num FROM $this->tb JOIN $this->_table ON $this->tb.id = tid $where GROUP BY tid ORDER BY num DESC LIMIT $num";
- return $this->getAll($sql);
- }
- //标签字符串切成数组,去掉空白和重复项
- private function format($tagStr)
- {
- $tagStr = str_replace(array(' ', ',', ',',' '), ' ', strip_tags($tagStr));
- $tags = explode(' ', $tagStr);
- $temp = array();
- foreach ($tags as $tag){
- $tag = trim($tag);
- if(!emptyempty($tag) && !in_array($tag, $temp)) $temp[] = $tag;
- }
- return $temp ? $temp : array();
- }
- //删除标签数组
- private function delTags($delArr, $mid, $cid)
- {
- if(!$delArr) return ;
- foreach ($delArr as $tag) {
- $rs = $this->getOne("SELECT id,num FROM $this->tb WHERE title = '$tag'");
- $this->exec("DELETE FROM $this->_table WHERE mid = $mid AND cid = $cid AND tid = ".intval($rs['id']));
- if($rs['num'] <= 1) $this->exec("DELETE FROM $this->tb WHERE title = '$tag'");
- else $this->exec("UPDATE $this->tb SET num = num - 1 WHERE title = '$tag'");
- }
- }
- //添加标签数组
- private function addTags($addArr, $mid, $cid)
- {
- if(!$addArr) return ;
- foreach ($addArr as $tag) {
- //添加标签表,顺便返回标签id
- if($tid = $this->getField("SELECT id FROM $this->tb WHERE title = '$tag'")) {
- $this->exec("UPDATE $this->tb SET num = num + 1 WHERE id = $tid");
- }else{
- $tid = $this->exec("INSERT INTO $this->tb SET title = '$tag', num = 1");
- }
- //添加关系表
- $this->exec("INSERT INTO $this->_table SET mid = $mid, cid = $cid, tid = $tid");
- }
- }
- /**
- * 重计tag使用数目.1.只刷新一个tag 2.只刷新一个内容的所有tag
- * @param $tag; tag名称或id
- */
- public function count($tag='', $mid=0, $cid=0)
- {
- $mid = intval($mid); $cid = intval($cid);
- //$idArr 是需要重计的tag id
- if($tag) {
- if (is_numeric($tag)) $tid = $tag;
- else $tid = $this->getField("SELECT id FROM $this->tb WHERE title = '$tag'");
- $idArr = array($tid);
- }else{
- if($cid && $mid) {
- $rs = $this->getAll("SELECT id FROM $this->tb WHERE mid = $mid AND cid = $cid");
- }else{
- $rs = $this->getAll("SELECT id FROM $this->tb");
- }
- $idArr = array();
- foreach ($rs as $item) {
- $idArr[] = $item['id'];
- }
- }
- foreach ($idArr as $tid) {
- $num = $this->getField("SELECT COUNT(id) FROM $this->_table WHERE tid = $tid");
- if($num) $this->exec("UPDATE $this->tb SET num = $num WHERE id = $tid");
- else $this->exec("DELETE FROM $this->tb WHERE id = $tid");
- }
- }
- }
评论列表
评论表单
妮称

