forked from cynial/STBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts_mdl.php
More file actions
463 lines (401 loc) · 11.3 KB
/
posts_mdl.php
File metadata and controls
463 lines (401 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* STBlog Blogging System
*
* 基于Codeigniter的单用户多权限开源博客系统
*
* STBlog is an open source multi-privilege blogging System built on the
* well-known PHP framework Codeigniter.
*
* @package STBLOG
* @author Saturn <huyanggang@gmail.com>
* @copyright Copyright (c) 2009 - 2010, cnsaturn.com.
* @license GNU General Public License 2.0
* @link http://code.google.com/p/stblog/
* @version 0.1.0
*/
// ------------------------------------------------------------------------
/**
* STBLOG Posts Model Class
*
* 内容操作Model
*
* @package STBLOG
* @subpackage Models
* @category Models
* @author Saturn <huyanggang@gmail.com>
* @link http://code.google.com/p/stblog/
*/
class Posts_mdl extends CI_Model {
const TBL_POSTS = 'posts';
const TBL_METAS = 'metas';
const TBL_RELATIONSHIPS = 'relationships';
const TBL_COMMENTS = 'comments';
/**
* 内容类型 日志/附件/独立页面
*
* @access private
* @var array
*/
private $_post_type = array('post', 'attachment', 'page');
/**
* 内容状态:发布/草稿/未归档/等待审核
*
* @access private
* @var array
*/
private $_post_status = array('publish', 'draft', 'unattached', 'attached', 'waiting');
/**
* 内容的唯一栏:pid/slug
*
* @access private
* @var array
*/
private $_post_unique_field = array('pid','slug');
/**
* 构造函数
*
* @access public
* @return void
*/
public function __construct()
{
parent::__construct();
log_message('debug', "STBLOG: Posts Model Class Initialized");
}
/**
* 获取内容列表
*
* @access public
* @param string $type 内容类型
* @param string $status 内容状态
* @param int $author_id 作者ID (optional)
* @param int $limit 条数 (optional)
* @param int $offset 偏移量 (optional)
* @param int $category_filter 需要过滤的栏目ID (optional)
* @param int $title_filter 需要过滤的标题关键字 (optional)
* @param bool $feed_filter 是否显示在feed里面 (optional)
* @return array 内容列表信息
*/
public function get_posts($type = 'post',$status = 'publish',$author_id = NULL,$limit = NULL,$offset = NULL,$category_filter = 0, $title_filter = '', $feed_filter = FALSE)
{
$this->db->select('posts.*, users.screenName');
$this->db->join('users','users.uid = posts.authorId');
//type
if($type && in_array($type, $this->_post_type))
{
$this->db->where('posts.type', $type);
}
//status
if($status && in_array($status,$this->_post_status))
{
$this->db->where('posts.status', $status);
}
//author
if(!empty($author_id))
{
$this->db->where('posts.authorId', intval($author_id));
}
//category filter
if(!empty($category_filter))
{
$this->db->join('relationships','posts.pid = relationships.pid','left');
$this->db->where('relationships.mid', intval($category_filter));
}
//title filter
if(!empty($title_filter))
{
$this->db->like('posts.title', $title_filter);
}
//feed filter
if($feed_filter)
{
$this->db->where('allowFeed', 1);
}
$this->db->order_by('posts.created','DESC');
//limit
if($limit && is_numeric($limit))
{
$this->db->limit(intval($limit));
}
//offset
if($offset && is_numeric($offset))
{
$this->db->offset(intval($offset));
}
return $this->db->get(self::TBL_POSTS);
}
/**
* 获取内容条目最大ID
*
* @access public
* @return int
*/
public function get_post_max_id()
{
$this->db->select_max('pid', 'pid');
$query = $this->db->get(self::TBL_POSTS);
$max = ($query->num_rows() > 0) ? $query->row()->pid : 0;
$query->free_result();
return $max;
}
/**
* 根据唯一键获取单个内容信息
*
* @access public
* @param string $identity 内容标识栏位:{"pid"|"slug"}
* @param mixed $value 标识栏位对应的值
* @return array 内容信息
*/
public function get_post_by_id($identity, $value)
{
if(!in_array($identity, $this->_post_unique_field))
{
return FALSE;
}
$this->db->select('posts.*,users.screenName');
$this->db->join('users', 'users.uid = posts.authorId');
$this->db->where($identity, $value);
return $this->db->get(self::TBL_POSTS)->row();
}
/**
* 根据元数据获取内容
*
* @access public
* @param string $meta_slug 元数据缩略名
* @param string $meta_type 元数据类型:{"category"|"tag"}
* @param string $post_type 内容类型
* @param string $post_status 内容状态
* @param string $post_status 要筛选的栏位值 (optional)
* @param int $limit 条数 (optional)
* @param int $offset 偏移量 (optional)
* @param bool $feed_filter 是否显示在feed里面 (optional)
* @return array - 内容信息
*/
public function get_posts_by_meta($meta_slug, $meta_type = 'category', $post_type = 'post', $post_status = 'publish', $fields = 'posts.*', $limit = NULL, $offset = NULL, $feed_filter = FALSE)
{
$this->db->select($fields . ',users.screenName');
$this->db->from('posts,metas,relationships');
$this->db->join('users','users.uid = posts.authorId');
$this->db->where('posts.pid = relationships.pid');
$this->db->where('posts.type', $post_type);
$this->db->where('posts.status', $post_status);
$this->db->where('metas.mid = relationships.mid');
$this->db->where('metas.type',$meta_type);
$this->db->where('metas.slug',$meta_slug);
$this->db->order_by('posts.created','DESC');
if($feed_filter)
{
$this->db->where('allowFeed', 1);
}
if($limit && is_numeric($limit))
{
$this->db->limit(intval($limit));
}
if($offset && is_numeric($offset))
{
$this->db->offset(intval($limit));
}
return $this->db->get();
}
/**
* 根据order值获得内容信息
*
* @access public
* @param string $order
* @return array - 内容信息
*/
public function get_posts_by_order($order)
{
$this->db->where('order', intval($order));
return $this->db->get(self::TBL_POSTS);
}
/**
* 日志归档:按日/按月/按年归档
*
* @access public
* @param int optional $year 归档年
* @param int optional $month 归档月
* @param int optional $day 归档日
* @param int $limit 条数
* @param int $offset 偏移量
* @return array - 内容信息
*/
public function get_posts_by_date($year = NULL, $month = NULL, $day = NULL, $limit = NULL, $offset = NULL)
{
//neither of the args are given, so exit from the func.
if(empty($year) && empty($month) && empty($day)) exit();
//archive by day
if(!empty($year) && !empty($month) && !empty($day))
{
$from = mktime(0, 0, 0, $month, $day, $year);
$to = mktime(23, 59, 59, $month, $day, $year);
}
//archive by month
else if(!empty($year) && !empty($month))
{
$from = mktime(0, 0, 0, $month, 1, $year);
$to = mktime(23, 59, 59, $month, date('t', $from), $year);
}
//archive by year
else if(!empty($year))
{
$from = mktime(0, 0, 0, 1, 1, $year);
$to = mktime(23, 59, 59, 12, 31, $year);
}
$this->db->select('posts.*,users.screenName');
$this->db->join('users','users.uid = posts.authorId');
$this->db->where('posts.created >=', $from);
$this->db->where('posts.created <=', $to);
$this->db->where('posts.status','publish');
$this->db->where('posts.type','post');
if($limit && is_numeric($limit))
{
$this->db->limit(intval($limit));
}
if($offset && is_numeric($offset))
{
$this->db->offset(intval($limit));
}
return $this->db->get(self::TBL_POSTS);
}
/**
* 根据作者ID获取文章
*
* @access public
* @param int $uid
* @param string $type
* @param string $status
* @param int $limit
* @param int $offset
* @return array - 内容信息
*/
public function get_posts_by_author($uid, $type = 'post', $status = 'publish', $limit = NULL, $offset = NULL)
{
$this->db->select('posts.* ,users.screenName');
$this->db->join('users','users.uid = posts.authorId');
//uid
$this->db->where('posts.authorId', intval($uid));
//type
if($type && in_array($type, $this->_post_type))
{
$this->db->where('posts.type', $type);
}
//status
if($status && in_array($status,$this->_post_status))
{
$this->db->where('posts.status', $status);
}
//limit
if($limit && is_numeric($limit))
{
$this->db->limit($limit);
}
//offset
if($offset && is_numeric($offset))
{
$this->db->offset($offset);
}
return $this->db->get(self::TBL_POSTS);
}
/**
* 获取合法的slug名称
*
* @access public
* @param string $slug slug name
* @param int $pid 内容id
* @return string slug
*/
public function get_slug_name($slug, $pid)
{
$result = $slug;
$count = 1;
while($this->db->select('pid')->where('slug',$result)->where('pid <>',$pid)->get(self::TBL_POSTS)->num_rows() > 0)
{
$result = $slug . '_' . $count;
$count ++;
}
return $result;
}
//----------------------CRUD-------------------------------------------------
/**
* 添加一个内容
*
* @access public
* @param array $content_data 内容
* @return mixed {post_id | FALSE}
*/
public function add_post($content_data)
{
$this->db->insert(self::TBL_POSTS, $content_data);
return ($this->db->affected_rows() ==1) ? $this->db->insert_id() : FALSE;
}
/**
* 修改一个内容
*
* @access public
* @param int $pid 内容ID
* @param array $data 内容数组
* @return boolean 成功或失败
*/
public function update_post($pid,$data)
{
$this->db->where('pid', intval($pid));
$this->db->update(self::TBL_POSTS, $data);
return ($this->db->affected_rows() == 1)?TRUE:FALSE;
}
/**
* 评论个数自减一
*
* @access public
* @param int $pid 内容ID
* @return void
*/
public function cmts_num_minus($pid)
{
$this->db->query('UPDATE '.self::TBL_POSTS.' SET `commentsNum` = `commentsNum`-1 WHERE `pid`='. intval($pid) .'');
}
/**
* 评论个数自曾一
*
* @access public
* @param int $pid 内容ID
* @return void
*/
public function cmts_num_plus($pid)
{
$this->db->query('UPDATE '.self::TBL_POSTS.' SET `commentsNum` = `commentsNum`+1 WHERE `pid`='. intval($pid) .'');
}
/**
* 删除一个内容
*
* @access public
* @param int $pid 内容id
* @return boolean 成功或失败
*/
public function remove_post($pid)
{
$this->db->delete(self::TBL_POSTS, array('pid' => intval($pid)));
return ($this->db->affected_rows() ==1) ? TRUE : FALSE;
}
/**
* 更新评论数
*
* @access public
* @param int $pid
* @return void
*/
public function refresh_comments_count($pid)
{
//calculation
$num = $this->db->select(self::TBL_COMMENTS.'.cid')
->from(self::TBL_COMMENTS)
->where(self::TBL_COMMENTS.'.status', 'approved')
->where(self::TBL_COMMENTS.'.pid', $pid)
->count_all_results();
//update
$this->update_post($pid, array('commentsNum' => $num));
}
}
/* End of file posts_mdl.php */
/* Location: ./application/models/posts_mdl.php */