<?php

/*
 * Copyright (C) xgcms.com
 */

class tags extends base_model {
	
	function __construct(&$conf) {
		parent::__construct($conf);
		$this->table = 'tags';
		$this->primarykey = array('tag_md5');
	}
	public function _read($tag){
		$tag=md5($tag);
		return $this->read($tag);
	}
	public function _create($tag,$type='search'){
		if(empty($tag)) return 0;
		$tag_md5=md5($tag);
		if($rs=$this->read($tag_md5)){
			$rs[$type]++;
			$this->update($rs);
			return $rs['id'];
		}else{
			$arr=array('tag'=>$tag,'tag_md5'=>$tag_md5,$type=>1);
			return $this->create($arr);
		}
	}
	public function dec_topics($tag_id){
		$rs=$this->index_fetch(array('id'=>$tag_id),array(),0,1);
		if($rs){
		  $rs=array_pop($rs);
		  $rs['topics']--;
		  $this->update($rs);	
		}
	}
	public function get_hots($num=10){
		$list=$this->index_fetch(array(),array('search'=>-1),0,$num);
		$tags=array();
		foreach($list as $v){
			$tags[]=$v['tag'];
		}
		return $tags;
		
	}
	public function get_list_by_fid($fid,$start=0,$pagesize=20){
		$list=$this->index_fetch(array('fid'=>$fid),array('tid'=>-1),$start,$pagesize);
		$tids=array();
		foreach($list as $v){
			$tids[]=array($v['fid'],$v['tid']);
		}
		//$tids=array_unique($tids);
		return $list=$this->topic->mget($tids);
		
	}
	
	
}