-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontroller.php
141 lines (140 loc) · 5.54 KB
/
controller.php
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
<?php
/* extends ActiveRecord.php Base class*/
class BaseController extends Base{
public function render($view, $params=array(), $layout='layout.html', $templatePath='web/', $return=false){
if ($return) ob_start();
MicroTpl::render($templatePath. $view, array_merge($params, array('controller'=>$this), $this->data), $layout?$templatePath. $layout:false);
if ($return) return ob_get_clean();
}
}
/* widgets */
class Widget extends BaseController{
public function __toString(){
if ($content = mcache(get_class($this))) return $content;
$this->run();
$content = $this->render($this->template, array(), '', 'web/', true);
return mcache(get_class($this), $content);
}
}
class RecentPost extends Widget{
public function run(){
$this->template = 'recent_post.html';
$this->posts = (new Post)->orderby('time desc')->limit(0, 5)->findAll();
}
}
class Tags extends Widget{
public function run(){
$this->template = 'tags.html';
$this->tags = (new Tag)->orderby('count desc')->findAll();
}
}
class Categories extends Widget{
public function run(){
$this->template = 'categories.html';
$this->categories = (new Category)->orderby('count desc')->findAll();
}
}
class RecentComment extends Widget{
public function run(){
$this->template = 'recent_comment.html';
$this->comments = (new Comment)->orderby('time desc')->limit(0, 5)->findAll();
}
}
class Archives extends Widget{
public function run(){
$this->template = 'archives.html';
$archives = array();
foreach((new Post())->orderby('time desc')->findAll() as $post){
if (($year = date('Y', $post->time)) && !isset($archives[$year])) $archives[$year] = array();
if (($month = date('M', $post->time)) && !isset($archives[$year][$month])) $archives[$year][$month] = 0;
$archives[$year][$month] += 1;
}
$this->archives = $archives;
}
}
class Controller extends BaseController {
public function initSilder(){
$this->tags = new Tags();
$this->archives = new Archives();
$this->categories = new Categories();
$this->recentPost = new RecentPost();
$this->recentComment = new RecentComment();
}
}
class PostController extends Controller{
public function listall($tagid=null, $categoryid=null, $userid){
if ($categoryid){
$category = (new Category())->eq('id', intval($categoryid))->find();
$this->title = $category->name;
$this->posts = $category->posts;
}elseif ($userid){
$this->user = $user = (new user())->eq('id', intval($userid))->find();
$this->title = $user->name;
$this->posts = $user->posts;
}elseif ($tagid){
$tag = (new Tag())->eq('id', intval($tagid))->find();
$this->title = $tag->tag->name;
$tags = (new Post2Tag())->eq('tag_id', intval($tagid))->findAll();
$this->posts = array_map(function($t){ return $t->post; }, $tags);
}else{
$this->title = 'Blog Name';
$this->posts = (new Post())->orderby('time desc')->findAll();
}
$this->initSilder();
$this->render('list.html');
}
public function view($id){
$this->post = (new Post())->find(intval($id));
$this->initSilder();
$this->render('post.html');
}
public function delete($id, $router){
(new Post())->find(intval($id))->delete();
$router->error(302, '/posts');
}
public function create($router, $user_id, $category_id, $category, $title, $content, $tag){
if ($user_id){
if ($category){
$cate = (new Category)->eq('name', $category)->find();
if (!$cate->id) {
$cate->name = $category;
$cate->count = 0;
$cate->insert();
}
$category_id = $cate->id;
}
$post = new Post(array('user_id'=>(int)($user_id), 'category_id'=>intval($category_id), 'title'=>$title, 'content'=>$content, 'time'=>time()));
$post->insert()->updateTag($tag)->updateCategory();
$router->error(302, '/post/'. $post->id. '/view', true);
}
$this->initSilder();
$this->cates = (new Category)->findAll();
$this->user = (new User)->find(1);
$this->render('post.html');
}
public function edit($id, $router, $user_id, $category_id, $category, $title, $content, $tag){
$this->post = $post = (new Post())->find(intval($id));
if ($user_id){
if ($category){
$cate = (new Category)->eq('name', $category)->find();
if (!$cate->id) {
$cate->name = $category;
$cate->count = 0;
$cate->insert();
}
if ($category_id != $cate->id){
(new Category)->set('count=count-1')->update();
$category_id = $cate->id;
$post->updateCategory();
}
}
$post->dirty(array('user_id'=>(int)($user_id), 'category_id'=>intval($category_id), 'title'=>$title, 'content'=>$content, 'time'=>time()));
$post->update()->updateTag($tag);
$router->error(302, '/post/'. $post->id. '/view', true);
}
$this->initSilder();
$this->cates = (new Category)->findAll();
$this->user = $this->post->author;
$this->render('post.html');
}
}