📄 tag.php
字号:
<?php
/**
* Tag management for XOOPS
*
* @copyright The XOOPS project http://www.xoops.org/
* @license http://www.fsf.org/copyleft/gpl.html GNU public license
* @author Taiwen Jiang (phppp or D.J.) <php_pp@hotmail.com>
* @since 1.00
* @version $Id$
* @package module::tag
*/
if (!defined("XOOPS_ROOT_PATH")) {
exit();
}
defined("FRAMEWORKS_ART_FUNCTIONS_INI") || include_once XOOPS_ROOT_PATH.'/Frameworks/art/functions.ini.php';
load_object();
/**
* TagTag
*
* @author D.J. (phppp)
* @copyright copyright © Xoops Project
* @package module::tag
*
* {@link ArtObject}
*
*/
class TagTag extends ArtObject
{
/**
* Constructor
*
* @param int $id ID of the tag, deprecated
*/
function TagTag($id = null)
{
$this->ArtObject("tag_tag");
$this->initVar("tag_id", XOBJ_DTYPE_INT, null, false);
$this->initVar("tag_term", XOBJ_DTYPE_TXTBOX, "", true);
$this->initVar("tag_status", XOBJ_DTYPE_INT, 0);
$this->initVar("tag_count", XOBJ_DTYPE_INT, 0);
}
}
/**
* Tag object handler class.
* @package module::tag
*
* @author D.J. (phppp)
* @copyright copyright © 2006 The XOOPS Project
*
* {@link ArtObjectHandler}
*
*/
class TagTagHandler extends ArtObjectHandler
{
var $table_link;
var $table_stats;
/**
* Constructor
*
* @param object $db reference to the {@link XoopsDatabase} object
**/
function TagTagHandler(&$db) {
$this->ArtObjectHandler($db, "tag_tag", "TagTag", "tag_id", "tag_term");
$this->table_link = $this->db->prefix("tag_link");
$this->table_stats = $this->db->prefix("tag_stats");
}
/**
* Get tags linked to an item
*
* @param integer $itemid item ID
* @param integer $modid module ID, optional
* @param integer $catid id of corresponding category, optional
* @return array associative array of tags (id, term)
*/
function getByItem($itemid, $modid = 0, $catid = 0)
{
$ret = array();
$itemid = intval($itemid);
$modid = (empty($modid) && is_object($GLOBALS["xoopsModule"]) && "tag" != $GLOBALS["xoopsModule"]->getVar("dirname") ) ? $GLOBALS["xoopsModule"]->getVar("mid") : intval($modid);
if(empty($itemid) || empty($modid)) return $ret;
$sql = "SELECT o.tag_id, o.tag_term".
" FROM {$this->table_link} AS l ".
" LEFT JOIN {$this->table} AS o ON o.{$this->keyName} = l.{$this->keyName} ".
" WHERE l.tag_itemid = {$itemid} AND l.tag_modid = {$modid}".
(empty($catid) ? "": ( " AND l.tag_catid=".intval($catid))).
" ORDER BY o.tag_count DESC"
;
if( ($result = $this->db->query($sql)) == false){
xoops_error($this->db->error());
return $ret;
}
while ($myrow = $this->db->fetchArray($result)) {
$ret[$myrow[$this->keyName]] = $myrow["tag_term"];
}
return $ret;
}
/**
* Update tags linked to an item
*
* @param array $tags
* @param integer $itemid item ID
* @param integer $modid module ID or module dirname, optional
* @param integer $catid id of corresponding category, optional
* @return boolean
*/
function updateByItem($tags, $itemid, $modid = "", $catid = 0)
{
$catid = intval($catid);
$itemid = intval($itemid);
if(!empty($modid) && !is_numeric($modid)) {
if(is_object($GLOBALS["xoopsModule"]) && $modid == $GLOBALS["xoopsModule"]->getVar("dirname") ){
$modid = $GLOBALS["xoopsModule"]->getVar("mid");
}else{
$module_handler =& xoops_gethandler("module");
if($module_obj =& $module_handler->getByDirname($modid)){
$modid = $module_obj->getVar("mid");
}else{
$modid = 0;
}
}
}
if(empty($itemid) || empty($modid)) return false;
if(empty($tags)) $tags = array();
elseif(!is_array($tags)){
include_once XOOPS_ROOT_PATH."/modules/tag/include/functions.php";
$tags = tag_parse_tag(addslashes(stripslashes($tags)));
}
$tags_existing = $this->getByItem($itemid, $modid, $catid);
$tags_delete = array_diff(array_values($tags_existing), $tags);
$tags_add = array_diff($tags, array_values($tags_existing));
$tags_update = array();
if(!empty($tags_delete)){
$tags_delete = array_map(array($this->db, "quoteString"), $tags_delete);
if($tags_id = $this->getIds(new Criteria("tag_term", "(".implode(", ", $tags_delete).")", "IN"))){
$sql = "DELETE FROM {$this->table_link}".
" WHERE ".
" {$this->keyName} IN (".implode(", ", $tags_id).")".
" AND tag_modid = {$modid} AND tag_catid = {$catid} AND tag_itemid = {$itemid}";
if( ($result = $this->db->queryF($sql)) == false){
xoops_error($this->db->error());
}
$sql = "DELETE FROM " . $this->table.
" WHERE ".
" tag_count < 2 AND ".
" {$this->keyName} IN (".implode(", ", $tags_id).")";
if( ($result = $this->db->queryF($sql)) == false){
xoops_error($this->db->error());
}
$sql = "UPDATE " . $this->table.
" SET tag_count = tag_count -1".
" WHERE ".
" {$this->keyName} IN (".implode(", ", $tags_id).")";
if( ($result = $this->db->queryF($sql)) == false){
xoops_error($this->db->error());
}
$tags_update = $tags_id;
}
}
if(!empty($tags_add)){
$tag_link = array();
$tag_count = array();
foreach($tags_add as $tag){
if($tags_id = $this->getIds(new Criteria("tag_term", $tag))){
$tag_id = $tags_id[0];
$tag_count[] = $tag_id;
}else{
$tag_obj =& $this->create();
$tag_obj->setVar("tag_term", $tag);
$tag_obj->setVar("tag_count", 1);
$this->insert($tag_obj);
$tag_id = $tag_obj->getVar("tag_id");
unset($tag_obj);
}
$tag_link[] = "({$tag_id}, {$itemid}, {$catid}, {$modid}, ". time() .")";
$tags_update[] = $tag_id;
}
$sql = "INSERT INTO {$this->table_link}".
" (tag_id, tag_itemid, tag_catid, tag_modid, tag_time) ".
" VALUES ". implode(", ", $tag_link);
if( ($result = $this->db->queryF($sql)) == false){
xoops_error($this->db->error());
}
if(!empty($tag_count)) {
$sql = "UPDATE " . $this->table.
" SET tag_count = tag_count+1".
" WHERE ".
" {$this->keyName} IN (".implode(", ", $tag_count).")";
if( ($result = $this->db->queryF($sql)) == false){
xoops_error($this->db->error());
}
}
}
foreach($tags_update as $tag_id){
$this->update_stats($tag_id, $modid, $catid);
}
return true;
}
function update_stats($tag_id, $modid = 0, $catid = 0)
{
$tag_id = intval($tag_id);
if(empty($tag_id)) return true;
$modid = intval($modid);
$catid = empty($modid) ? -1 : intval($catid);
$count = 0;
$sql = " SELECT COUNT(*) " .
" FROM {$this->table_link}".
" WHERE tag_id = {$tag_id}".
(empty($modid) ? "" : " AND tag_modid = {$modid}").
(($catid < 0) ? "" : " AND tag_catid = {$catid}");
if( ($result = $this->db->query($sql)) == false){
xoops_error($this->db->error());
}else{
list($count) = $this->db->fetchRow($result);
}
if(empty($modid)){
$tag_obj =& $this->get($tag_id);
if(empty($count)){
$this->delete($tag_obj);
}else{
$tag_obj->setVar("tag_count", $count);
$this->insert($tag_obj, true);
}
}else{
if(empty($count)){
$sql = "DELETE FROM {$this->table_stats}".
" WHERE ".
" {$this->keyName} = {$tag_id}".
" AND tag_modid = {$modid}".
" AND tag_catid = {$catid}"
;
if( ($result = $this->db->queryF($sql)) == false){
xoops_error($this->db->error());
}
}else{
$ts_id = null;
$sql = " SELECT ts_id, tag_count ".
" FROM {$this->table_stats}".
" WHERE ".
" {$this->keyName} = {$tag_id}".
" AND tag_modid = {$modid}".
" AND tag_catid = {$catid}"
;
if($result = $this->db->query($sql)){
list($ts_id, $tag_count) = $this->db->fetchRow($result);
}
$sql = "";
if($ts_id && $tag_count != $count){
$sql = " UPDATE {$this->table_stats}".
" SET tag_count = {$count}".
" WHERE ".
" ts_id = {$ts_id}";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -