📄 正在查看:twcms/model/cms_content_model.class.php
大小:6,754 字节 · 修改:2014-04-07 07:15:32 · 行数:193
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 extends model {
10 function __construct() {
11 $this->table = ''; // 表名 (可以是 cms_article、cms_product、cms_photo 等)
12 $this->pri = array('id'); // 主键
13 $this->maxid = 'id'; // 自增字段
14 }
15
16 // 暂时用些方法解决获取 cfg 值
17 function __get($var) {
18 if($var == 'cfg') {
19 return $this->cfg = $this->runtime->xget();
20 }else{
21 return parent::__get($var);
22 }
23 }
24
25 // 格式化内容数组
26 public function format(&$v, $mid, $dateformat = 'Y-m-d H:i:s', $titlenum = 0, $intronum = 0) {
27 // hook cms_content_model_format_before.php
28
29 if(empty($v)) return FALSE;
30
31 $v['date'] = date($dateformat, $v['dateline']);
32 $v['subject'] = $titlenum ? utf8::cutstr_cn($v['title'], $titlenum) : $v['title'];
33 $v['url'] = $this->content_url($v['cid'], $v['id'], $v['alias'], $v['dateline']);
34 $v['tags'] = _json_decode($v['tags']);
35 if($v['tags']) {
36 $v['tag_arr'] = array();
37 foreach($v['tags'] as $name) {
38 $v['tag_arr'][] = array('name'=>$name, 'url'=> $this->tag_url($mid, $name));
39 }
40 }
41
42 $intronum && $v['intro'] = utf8::cutstr_cn($v['intro'], $intronum);
43 $v['pic'] = $this->cfg['webdir'].(empty($v['pic']) ? 'static/img/nopic.gif' : $v['pic']);
44
45 // hook cms_content_model_format_after.php
46 }
47
48 // 获取内容列表
49 public function list_arr($where, $orderby, $orderway, $start, $limit, $total) {
50 // 优化大数据量翻页
51 if($start > 1000 && $total > 2000 && $start > $total/2) {
52 $orderway = -$orderway;
53 $newstart = $total-$start-$limit;
54 if($newstart < 0) {
55 $limit += $newstart;
56 $newstart = 0;
57 }
58 $list_arr = $this->find_fetch($where, array($orderby => $orderway), $newstart, $limit);
59 return array_reverse($list_arr, TRUE);
60 }else{
61 return $this->find_fetch($where, array($orderby => $orderway), $start, $limit);
62 }
63 }
64
65 // 内容关联删除
66 public function xdelete($table, $id, $cid) {
67 // hook cms_content_model_xdelete_before.php
68
69 $this->table = 'cms_'.$table;
70 $this->cms_content_data->table = 'cms_'.$table.'_data';
71 $this->cms_content_comment->table = 'cms_'.$table.'_comment';
72 $this->cms_content_comment_sort->table = 'cms_'.$table.'_comment_sort';
73 $this->cms_content_attach->table = 'cms_'.$table.'_attach';
74 $this->cms_content_flag->table = 'cms_'.$table.'_flag';
75 $this->cms_content_tag->table = 'cms_'.$table.'_tag';
76 $this->cms_content_tag_data->table = 'cms_'.$table.'_tag_data';
77 $this->cms_content_views->table = 'cms_'.$table.'_views';
78
79 // 内容读取
80 $data = $this->read($id);
81 if(empty($data)) return '内容不存在!';
82
83 // 删除评论
84 $this->cms_content_comment->find_delete(array('id'=>$id));
85 $this->cms_content_comment_sort->delete($id);
86
87 // 删除附件
88 $attach_arr = $this->cms_content_attach->find_fetch(array('id'=>$id));
89 $updir = TWCMS_PATH.'upload/'.$table.'/';
90 foreach($attach_arr as $v) {
91 $file = $updir.$v['filepath'];
92 $thumb = image::thumb_name($file);
93 try{
94 is_file($file) && unlink($file);
95 is_file($thumb) && unlink($thumb);
96 }catch(Exception $e) {}
97 $this->cms_content_attach->delete($v['aid']);
98 }
99
100 // 更新标签表
101 if(!empty($data['tags'])) {
102 $tags_arr = _json_decode($data['tags']);
103 foreach($tags_arr as $tagid => $name) {
104 $this->cms_content_tag_data->delete($tagid, $id);
105 $tagdata = $this->cms_content_tag->read($tagid);
106 $tagdata['count']--;
107 if($tagdata['count'] > 0) $this->cms_content_tag->update($tagdata);
108 }
109 }
110
111 // 更新分类表
112 $catedata = $this->category->read($cid);
113 if(empty($catedata)) return '读取分类表出错!';
114 if($catedata['count'] > 0) {
115 $catedata['count']--;
116 if(!$this->category->update($catedata)) return '写入内容表出错!';
117 $this->category->update_cache($cid);
118 }
119
120 // 删除内容
121 $this->cms_content_data->delete($id);
122 $this->cms_content_views->delete($id);
123 $this->cms_content_flag->find_delete(array('id'=>$id));
124 $this->only_alias->delete($data['alias']);
125 $ret = $this->delete($id);
126 return $ret ? '' : '删除失败!';
127 }
128
129 // 标签链接格式化
130 public function tag_url(&$mid, &$name, $page = FALSE) {
131 // hook cms_content_model_tag_url_before.php
132
133 if(empty($_ENV['_config']['twcms_parseurl'])) {
134 $s = $page ? '-page-{page}' : '';
135 return $this->cfg['webdir'].'index.php?tag--mid-'.$mid.'-name-'.urlencode($name).$s.$_ENV['_config']['url_suffix'];
136 }else{
137 return $this->cfg['webdir'].$this->cfg['link_tag_pre'].$mid.'_'.urlencode($name).($page ? '_{page}' : '').$this->cfg['link_tag_end'];
138 }
139 }
140
141 // 评论链接格式化
142 public function comment_url(&$cid, &$id, $page = FALSE) {
143 // hook cms_content_model_comment_url_before.php
144
145 if(empty($_ENV['_config']['twcms_parseurl'])) {
146 $s = $page ? '-page-{page}' : '';
147 return $this->cfg['webdir'].'index.php?comment--cid-'.$cid.'-id-'.$id.$s.$_ENV['_config']['url_suffix'];
148 }else{
149 return $this->cfg['webdir'].$this->cfg['link_comment_pre'].$cid.'_'.$id.($page ? '_{page}' : '').$this->cfg['link_comment_end'];
150 }
151 }
152
153 // 首页分页链接格式化
154 public function index_url(&$mid) {
155 // hook cms_content_model_index_url_before.php
156
157 if(empty($_ENV['_config']['twcms_parseurl'])) {
158 return $this->cfg['webdir'].'index.php?index-index-mid-'.$mid.'-page-{page}'.$_ENV['_config']['url_suffix'];
159 }else{
160 return $this->cfg['webdir'].'index_'.$mid.'_{page}'.$this->cfg['link_index_end'];
161 }
162 }
163
164 // 内容链接格式化
165 public function content_url(&$cid, &$id, &$alias, &$dateline) {
166 // hook cms_content_model_content_url_before.php
167
168 if(empty($_ENV['_config']['twcms_parseurl'])) {
169 return $this->cfg['webdir'].'index.php?show--cid-'.$cid.'-id-'.$id.$_ENV['_config']['url_suffix'];
170 }else{
171 // 性能方面,最后一个最差
172 switch($this->cfg['link_show_type']) {
173 case 1:
174 return $this->cfg['webdir'].$cid.'/'.$id.$this->cfg['link_show_end'];
175 case 2:
176 return $this->cfg['webdir'].$this->cfg['cate_arr'][$cid].'/'.$id.$this->cfg['link_show_end'];
177 case 3:
178 return $this->cfg['webdir'].($alias ? $alias : $cid.'_'.$id).$this->cfg['link_show_end'];
179 default:
180 return $this->cfg['webdir'].strtr($this->cfg['link_show'], array(
181 '{cid}' => $cid,
182 '{id}' => $id,
183 '{alias}' => $alias ? $alias : $cid.'_'.$id,
184 '{cate_alias}' => $this->cfg['cate_arr'][$cid],
185 '{y}' => date('Y', $dateline),
186 '{m}' => date('m', $dateline),
187 '{d}' => date('d', $dateline)
188 ));
189 }
190 }
191 }
192}
193