forked from cynial/STBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhome.php
More file actions
515 lines (432 loc) · 13 KB
/
Copy pathhome.php
File metadata and controls
515 lines (432 loc) · 13 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<?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 内容控制器
*
* 主要用于控制日志相关内容的功能表现
*
* @package STBLOG
* @subpackage Controllers
* @category Front-controllers
* @author Saturn <huyanggang@gmail.com>
* @link http://code.google.com/p/stblog/
*/
class Home extends ST_Controller
{
/**
* 当前uri
*
* @access private
* @var string
*/
private $_uri = '';
/**
* 当前页码
*
* @access private
* @var string
*/
private $_current_page = 1;
/**
* 每页条目数
*
* @access private
* @var int
*/
private $_limit = 5;
/**
* 偏移
*
* @access private
* @var int
*/
private $_offset = 0;
/**
* 条目总数
*
* @access private
* @var int
*/
private $_total_count = 0;
/**
* 条目总数
*
* @access private
* @var array
*/
private $_posts = array();
/**
* 分页字符串 wrapper
*
* @access private
* @var string
*/
private $_pagination = '';
/**
* 构造函数
*
* @access public
* @return void
*/
public function __construct()
{
parent::__construct();
$this->_uri = $this->uri->segment(1) . '/';
}
/**
* 初始化分页参数
*
* @access public
* @param int $current_page
* @return void
*/
private function _init_pagination($current_page)
{
/** 当前页 */
$this->_current_page = ($current_page && is_numeric($current_page)) ? intval($current_page) : 1;
/** 每页多少项 */
$page_size = setting_item('posts_page_size');
$this->_limit = ($page_size && is_numeric($page_size)) ? intval($page_size) : 5;
/** 偏移量 */
$this->_offset = ($this->_current_page - 1) * $this->_limit;
if($this->_offset < 0)
{
redirect(site_url());
}
}
/**
* 加工和处理posts数据
*
* @access private
* @param array $posts 内容stdClass对象数组
* @return void
*/
private function _prepare_posts()
{
foreach($this->_posts as &$post)
{
/** 日志固定链接 */
$post->permalink = site_url('posts/'. $post->slug);
/** 日志发表日期 */
$post->published = setting_item('post_date_format')
? date(setting_item('post_date_format'), $post->created)
: date('Y-m-d', $post->created);
$this->metas_mdl->get_metas($post->pid);
/** 日志分类 */
$post->categories = $this->metas_mdl->metas['category'];
/** 日志标签 */
$post->tags = $this->metas_mdl->metas['tag'];
/** 日志摘要 */
$post->excerpt = Common::get_excerpt($post->text);
/** 是否存在摘要 */
$post->more = (Common::has_break($post->text)) ? TRUE : FALSE;
unset($post->slug);
unset($post->text);
}
}
/**
* 应用分页规则
*
* @access private
* @param string $target_uri 目标uri
* @param bool $url_friendly 开启友好url
* @param string $parament_name 页码参数 e.g ?p=1
* @param string $page 页码
* @return void
*/
private function _apply_pagination($target_uri, $url_friendly = TRUE, $parament_name = 'p')
{
if($this->_total_count > $this->_limit)
{
$this->dpagination->currentPage($this->_current_page);
$this->dpagination->items($this->_total_count);
$this->dpagination->limit($this->_limit);
$this->dpagination->adjacents(2);
$this->dpagination->target($target_uri);
$this->dpagination->nextLabel('');
$this->dpagination->PrevLabel('');
if($url_friendly)
{
$this->dpagination->urlFriendly();
}
else
{
$this->dpagination->parameterName($parament_name);
}
$this->_pagination = $this->dpagination->getOutput();
}
}
/**
* 提取归档提示语
*
* @access private
* @param int $year 归档年(必需)
* @param int $month 归档月(可选)
* @param int $day 归档日(可选)
* @return string
*/
private function _archive_hints($year, $month, $day)
{
if($year > 0)
{
if($month > 0)
{
if($day > 0)
{
$month = sprintf("%02d", $month);
$day = sprintf("%02d", $day);
$this->_uri .= "$year/$month/$day";
return date('Y年m月d日', mktime(0, 0, 0, $month, $day, $year));
}
$month = sprintf("%02d", $month);
$this->_uri .= "$year/$month";
return date('Y年m月', mktime(0, 0, 0, $month, 1, $year));
}
$this->_uri .= $year;
return date('Y年', mktime(0, 0, 0, 1, 1, $year));
}
return;
}
/**
* 默认日志分页显示
*
* @access public
* @return void
*/
public function index($page = 1)
{
/** 分页参数 */
$this->_init_pagination($page);
$this->_posts = $this->posts_mdl->get_posts('post', 'publish', NULL, $this->_limit, $this->_offset)->result();
$this->_total_count = $this->posts_mdl->get_posts('post', 'publish', NULL, 10000, 0)->num_rows();
if(!empty($this->_posts))
{
$this->_prepare_posts();
$this->_apply_pagination(site_url('page').'/%');
}
/** 页面初始化 */
$data['page_title'] = '首页';
$data['page_description'] = setting_item('blog_description');
$data['page_keywords'] = setting_item('blog_keywords');
$data['posts'] = $this->_posts;
$data['parsed_feed'] = Common::render_feed_meta();
$data['pagination'] = $this->_pagination;
/** 加载主题下的页面 */
$this->load_theme_view('index', $data);
}
/**
* 分类浏览
*
* @access public
* @param string $slug
* @param int $page
* @return void
*/
public function category($slug, $page = 1)
{
if(empty($slug) || !is_numeric($page))
{
redirect(site_url());
}
$category = $this->metas_mdl->get_meta_by_slug(trim($slug));
if(!$category)
{
show_error('分类不存在或已被管理员删除');
exit();
}
/** 分页参数 */
$this->_init_pagination($page);
$this->_posts = $this->posts_mdl->get_posts_by_meta($slug, 'category', 'post', 'publish', 'posts.*', $this->_limit, $this->_offset)->result();
$this->_total_count = $this->posts_mdl->get_posts_by_meta($slug, 'category', 'post', 'publish', 'posts.*', 10000, 0)->num_rows();
if(!empty($this->_posts))
{
$this->_prepare_posts();
$this->_apply_pagination(site_url('category/' . $slug) . '/%');
}
/** 页面初始化 */
$data['page_title'] = $category->name;
$data['page_description'] = $category->description;
$data['page_keywords'] = setting_item('blog_keywords');
$data['posts'] = $this->_posts;
$data['parsed_feed'] = Common::render_feed_meta('category', $category->slug, $category->name);
$data['current_view_hints'] = sprintf('%s 分类下的文章(第 %d 页 / 共 %d 篇)', $category->name, $this->_current_page, $this->_total_count);
$data['pagination'] = $this->_pagination;
$this->load_theme_view('index', $data);
}
/**
* 分类浏览
*
* @access public
* @param string $slug
* @param int $page
* @return void
*/
public function tag($slug, $page = 1)
{
if(empty($slug) || !is_numeric($page))
{
redirect(site_url());
}
$tag = $this->metas_mdl->get_meta_by_slug(trim($slug));
if(!$tag)
{
show_error('标签不存在或已被主人删除');
exit();
}
/** 分页参数 */
$this->_init_pagination($page);
$this->_posts = $this->posts_mdl->get_posts_by_meta($slug, 'tag', 'post', 'publish', 'posts.*', $this->_limit, $this->_offset)->result();
$this->_total_count = $this->posts_mdl->get_posts_by_meta($slug, 'tag', 'post', 'publish', 'posts.*', 10000, 0)->num_rows();
if(!empty($this->_posts))
{
$this->_prepare_posts();
$this->_apply_pagination(site_url('tag/' . $slug) . '/%');
}
/** 页面初始化 */
$data['page_title'] = $tag->name;
$data['page_description'] = $tag->description;
$data['page_keywords'] = setting_item('blog_keywords');
$data['posts'] = $this->_posts;
$data['parsed_feed'] = Common::render_feed_meta('tag', $tag->slug, $tag->name);
$data['current_view_hints'] = sprintf('标记有标签 %s 的文章(第 %d 页 / 共 %d 篇)', $tag->name, $this->_current_page, $this->_total_count);
$data['pagination'] = $this->_pagination;
$this->load_theme_view('index', $data);
}
/**
* 按作者显示日志
*
* @access public
* @param int $uid
* @param string $page 页码
* @return void
*/
public function authors($uid, $page = 1)
{
if(empty($uid) || !is_numeric($uid) || !is_numeric($page))
{
redirect(site_url());
}
/** 分页参数 */
$this->_init_pagination($page);
$uid = intval($uid);
$author = NULL;
$this->_posts = $this->posts_mdl
->get_posts_by_author($uid, 'post', 'publish', $this->_limit, $this->_offset)
->result();
$this->_total_count = $this->posts_mdl
->get_posts_by_author($uid, 'post', 'publish', 10000, 0)
->num_rows();
if(!empty($this->_posts))
{
$this->_prepare_posts();
list($temp) = $this->_posts;
$author = $temp->screenName;
unset($temp);
$this->_apply_pagination(site_url('authors/' . $uid) . '/%');
}
/** 页面初始化 */
$data['page_title'] = $author;
$data['page_description'] = setting_item('blog_description');
$data['page_keywords'] = setting_item('blog_keywords');
$data['posts'] = $this->_posts;
$data['parsed_feed'] = Common::render_feed_meta();
$data['current_view_hints'] = sprintf('%s 所写的文章(第 %d 页 / 共 %d 篇)', $author, $this->_current_page, $this->_total_count);
$data['pagination'] = $this->_pagination;
$this->load_theme_view('index', $data);
}
/**
* 归档
*
* @access public
* @param int $year 归档年(必需)
* @param int $month 归档月(可选)
* @param int $day 归档日(可选)
* @param string $page 页码
* @return void
*/
public function archives($year, $month = NULL, $day = NULL, $page = 'p1')
{
if(empty($year))
{
redirect(site_url());
}
/** 基本参数 */
$year = intval($year);
$month = intval($month);
$day = intval($day);
$date = $this->_archive_hints($year, $month, $day);
/** 分页参数 */
$page = str_replace('p','', $page);
$this->_init_pagination($page);
$this->_posts = $this->posts_mdl
->get_posts_by_date($year, $month, $day, $this->_limit, $this->_offset)
->result();
$this->_total_count = $this->posts_mdl
->get_posts_by_date($year, $month, $day, 10000, 0)
->num_rows();
if(!empty($this->_posts))
{
$this->_prepare_posts();
$this->_apply_pagination(site_url($this->_uri).'/p%/');
}
/** 页面初始化 */
$data['page_title'] = $date;
$data['page_description'] = sprintf('日志归档:%s', $date);
$data['page_keywords'] = setting_item('blog_keywords');
$data['posts'] = $this->_posts;
$data['parsed_feed'] = Common::render_feed_meta();
$data['current_view_hints'] = sprintf('%s日志归档(第 %d 页 / 共 %d 篇)', $date, $this->_current_page, $this->_total_count);
$data['pagination'] = $this->_pagination;
$this->load_theme_view('index', $data);
}
/**
* 搜索
*
* @access public
* @return void
*/
public function search()
{
$keywords = strip_tags($this->input->get('s', TRUE));
$page = strip_tags($this->input->get('p', TRUE));
/** 分页参数 */
$this->_init_pagination($page);
$this->_posts = $this->posts_mdl
->get_posts('post', 'publish', NULL, $this->_limit, $this->_offset, 0, $keywords, TRUE)
->result();
$this->_total_count = $this->posts_mdl
->get_posts('post', 'publish', NULL, 10000, 0, 0, $keywords, TRUE)
->num_rows();
if(!empty($this->_posts))
{
$this->_prepare_posts();
$this->_apply_pagination(site_url('search?s='. urlencode($keywords)), FALSE, 'p');
}
/** 页面初始化 */
$data['page_title'] = sprintf('搜索:%s', $keywords);
$data['page_description'] = setting_item('blog_description');
$data['page_keywords'] = setting_item('blog_keywords');
$data['posts'] = $this->_posts;
$data['parsed_feed'] = Common::render_feed_meta();
$data['current_view_hints'] = sprintf('关键字 %s 的搜索结果(第 %d 页 / 共 %d 篇)', $keywords, $this->_current_page, $this->_total_count);
$data['pagination'] = $this->_pagination;
$this->load_theme_view('index', $data);
}
}
/* End of file home.php */
/* Location: ./application/controllers/home.php */