-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.php
107 lines (101 loc) · 3.75 KB
/
model.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
<?php
require_once('vendor/autoload.php');
class User extends ActiveRecord{
public $table = 'user';
public $relations = array(
'posts' => array(self::HAS_MANY, 'Post', 'user_id'),
);
}
class Post extends ActiveRecord{
public $table = 'post';
public $relations = array(
'tags' => array(self::HAS_MANY, 'Post2Tag', 'post_id'),
'comments' => array(self::HAS_MANY, 'Comment', 'post_id'),
'author' => array(self::BELONGS_TO, 'User', 'user_id'),
'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
);
public function url(){ return '/post/'. $this->id . '/view'; }
public function editurl(){ return '/post/'. $this->id . '/edit'; }
public function img(){
if (preg_match( '/src="([^"]*)"/i', $this->comment, $match))
return $match[1];
return '/web/images/post1.jpg';
}
public function showTime(){ return date('M, d Y', $this->time); }
public function commentCount(){ return '1 comments';}
public function summary(){ return strlen($this->content) > 300?substr($this->content, 0, 300). '...': $this->content;}
public function getTags(){
return array_map(function($tag){
return $tag->tag->name;
}, $this->tags);
}
function updateCategory(){
$category = (new Category)->eq('id', $this->category_id)->find();
$category->count = $category->count + 1;
$category->update();
return $this;
//(new Category)->set('count', 'count+1')->eq('id', $this->id)->update();
}
function updateTag($tags){
$tags = array_map(function($t){ return trim($t); }, explode(',', $tags));
$tags = array_filter($tags, function($t){ return strlen($t)>0; });
foreach($this->tags as $i=>$tag){
$key = array_search($tag->tag->name, $tags);
if (false === $key){
$tag->tag->count = $tag->tag->count - 1;
if ($tag->tag->count > 0)
$tag->tag->update();
else
$tag->tag->delete();
$tag->delete();
} else unset($tags[$key]);//do not change tag
}
foreach($tags as $i=>$t){
$tag = new Tag();
$post2tag = new Post2Tag();
$tag->reset()->eq('name', $t)->find();
if (!$tag->id){
$tag->name = $t;
$tag->count = 1;
$tag->insert();
}else{
$tag->count = $tag->count + 1;
$tag->update();
}
$post2tag->tag_id = $tag->id;
$post2tag->post_id = $this->id;
$post2tag->insert();
}
return $this;
}
}
class Comment extends ActiveRecord{
public $table = 'comments';
public $relations = array(
'post' => array(self::BELONGS_TO, 'Post', 'post_id'),
);
public function url(){ return '/post/'. $this->post_id. '/view#comment-'. $this->id; }
public function posturl(){ return '/post/'. $this->post_id. '/view'; }
public function sumarry(){ return $this->content; }
}
class Category extends ActiveRecord{
public $table = 'category';
public $relations = array(
'posts' => array(self::HAS_MANY, 'Post', 'category_id'),
);
public function url(){ return '/category/'. $this->id. '/post'; }
}
class Tag extends ActiveRecord{
public $table = 'tag';
public $relations = array(
'tags' => array(self::HAS_MANY, 'Post2Tag', 'tag_id'),
);
public function url(){ return '/tag/'. $this->id. '/post'; }
}
class Post2Tag extends ActiveRecord{
public $table = 'post_tag';
public $relations = array(
'post' => array(self::BELONGS_TO, 'Post', 'post_id'),
'tag' => array(self::BELONGS_TO, 'Tag', 'tag_id'),
);
}