📄 正在查看:twcms/model/cms_content_tag_model.class.php
大小:2,154 字节 · 修改:2014-01-23 01:42:52 · 行数:67
1<?php
2/**
3 * (C)2012-2014 twcms.com TongWang Inc.
4 * Author: wuzhaohuan <kongphp@gmail.com>
5 */
6
7defined('TWCMS_PATH') or exit;
8
9class cms_content_tag extends model {
10 function __construct() {
11 $this->table = ''; // 表名 (可以是 cms_article_tag cms_product_tag cms_photo_tag 等)
12 $this->pri = array('tagid'); // 主键
13 $this->maxid = 'tagid'; // 自增字段
14 }
15
16 // 获取标签列表
17 public function list_arr($orderway, $start, $limit, $total) {
18 // 优化大数据量翻页
19 if($start > 1000 && $total > 2000 && $start > $total/2) {
20 $orderway = -$orderway;
21 $newstart = $total-$start-$limit;
22 if($newstart < 0) {
23 $limit += $newstart;
24 $newstart = 0;
25 }
26 $list_arr = $this->find_fetch(array(), array('count' => $orderway), $newstart, $limit);
27 return array_reverse($list_arr, TRUE);
28 }else{
29 return $this->find_fetch(array(), array('count' => $orderway), $start, $limit);
30 }
31 }
32
33 // 标签关联删除 (需要删除三个表: cms_content_tag cms_content_tag_data cms_content)
34 public function xdelete($table, $tagid) {
35 $this->table = 'cms_'.$table.'_tag';
36 $this->cms_content->table = 'cms_'.$table;
37 $this->cms_content_tag_data->table = 'cms_'.$table.'_tag_data';
38
39 // 删除 cms_content 表的内容
40 try{
41 // 如果内容数太大,会删除失败。(这时程序需要改进做分批删除设计)
42 $list_arr = $this->cms_content_tag_data->find_fetch(array('tagid'=>$tagid));
43 foreach($list_arr as $v) {
44 $data = $this->cms_content->read($v['id']);
45 if(empty($data)) return '读取内容表出错!';
46
47 $row = _json_decode($data['tags']);
48 unset($row[$tagid]);
49 $data['tags'] = _json_encode($row);
50
51 if(!$this->cms_content->update($data)) return '写入内容表出错!';
52 }
53 }catch(Exception $e) {
54 return '修改内容表出错!';
55 }
56
57 // 删除 cms_content_tag_data 表的内容
58 try{
59 $this->cms_content_tag_data->find_delete(array('tagid'=>$tagid));
60 }catch(Exception $e) {
61 return '删除标签数据表出错!';
62 }
63
64 return $this->delete($tagid) ? '' : '删除失败!';
65 }
66}
67