1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* class for commenting. Model for comments. |
4
|
|
|
*/ |
5
|
|
|
namespace Anax\CommentDb; |
6
|
|
|
|
7
|
|
|
class CommentsInDb extends \Anax\MVC\CDatabaseModel |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Initialize model. |
12
|
|
|
* |
13
|
|
|
* @return array |
14
|
|
|
*/ |
15
|
|
|
public function init() |
16
|
|
|
{ |
17
|
|
|
// $this->db->setVerbose(); |
18
|
|
|
$this->db->dropTableIfExists('commentsindb')->execute(); |
|
|
|
|
19
|
|
|
|
20
|
|
|
$this->db->createTable( |
|
|
|
|
21
|
|
|
'commentsindb', |
22
|
|
|
[ |
23
|
|
|
'id' => ['integer', 'primary key', 'not null', 'auto_increment'], |
24
|
|
|
'flow' => ['varchar(80)'], |
25
|
|
|
'name' => ['varchar(80)'], |
26
|
|
|
'mail' => ['varchar(80)'], |
27
|
|
|
'web' => ['varchar(80)'], |
28
|
|
|
'content' => ['varchar(1024)'], |
29
|
|
|
'created' => ['datetime'], |
30
|
|
|
] |
31
|
|
|
)->execute(); |
32
|
|
|
$this->db->insert( |
|
|
|
|
33
|
|
|
'commentsindb', |
34
|
|
|
['flow', 'name', 'mail', 'web', 'content', 'created'] |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
$now = time(); |
38
|
|
|
|
39
|
|
|
$this->db->execute([ |
|
|
|
|
40
|
|
|
'commentadmin/lorem', |
41
|
|
|
'Fredrik Nilsson', |
42
|
|
|
'[email protected]', |
43
|
|
|
'www.dbwebb.se', |
44
|
|
|
'Lorem ipsum dolor sit amet, ad nam graeci dissentias, te verear utroque per. Doming intellegat mea id, mel ei dicta iudico. Dicunt fabulas usu ad. Per nemore possim commune ut, eu probo dicta has. ', |
45
|
|
|
$now |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
$this->db->execute([ |
|
|
|
|
49
|
|
|
'commentadmin/lorem', |
50
|
|
|
'Joe Doe', |
51
|
|
|
'[email protected]', |
52
|
|
|
'test.dbwebb.se', |
53
|
|
|
'Accusam eleifend qui ex. Has duis iuvaret salutatus id, dico illud porro ea mei, id oblique tibique eos. Ne eam meis equidem admodum, eos nisl maluisset id. Ancillae lucilius persecuti no sed.', |
54
|
|
|
$now |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
$this->db->execute([ |
|
|
|
|
58
|
|
|
'calendar', |
59
|
|
|
'Jane Doe', |
60
|
|
|
'[email protected]', |
61
|
|
|
'jane.dbwebb.se', |
62
|
|
|
'Sea te vocibus dolores pertinax, quodsi insolens appellantur sit an, dicam definitionem sed ne.', |
63
|
|
|
$now |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
} |
67
|
|
View Code Duplication |
private function humanTiming($time) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
|
70
|
|
|
$time = time() - $time; // to get the time since that moment |
71
|
|
|
$time = ($time<1)? 1 : $time; |
72
|
|
|
$tokens = array ( |
73
|
|
|
31536000 => 'år', |
74
|
|
|
2592000 => 'månader', |
75
|
|
|
604800 => 'veckor', |
76
|
|
|
86400 => 'dagar', |
77
|
|
|
3600 => 'timmar', |
78
|
|
|
60 => 'minuter', |
79
|
|
|
1 => 'sekunder' |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
foreach ($tokens as $unit => $text) { |
83
|
|
|
if ($time < $unit) { |
84
|
|
|
continue; |
85
|
|
|
} |
86
|
|
|
$numberOfUnits = floor($time / $unit); |
87
|
|
|
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?' ':''); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get either a Gravatar URL or complete image tag for a specified email address. |
93
|
|
|
* |
94
|
|
|
* @param string $email The email address |
95
|
|
|
* @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ] |
96
|
|
|
* @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] |
97
|
|
|
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ] |
98
|
|
|
* @param boole $img True to return a complete IMG tag False for just the URL |
99
|
|
|
* @param array $atts Optional, additional key/value attributes to include in the IMG tag |
100
|
|
|
* @return String containing either just a URL or a complete image tag |
101
|
|
|
* @source http://gravatar.com/site/implement/images/php/ |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
private function getGravatar($email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array()) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$url = 'http://www.gravatar.com/avatar/'; |
106
|
|
|
$url .= md5(strtolower(trim($email))); |
107
|
|
|
$url .= "?s=$s&d=$d&r=$r"; |
108
|
|
|
if ($img) { |
109
|
|
|
$url = '<img src="' . $url . '"'; |
110
|
|
|
foreach ($atts as $key => $val) { |
111
|
|
|
$url .= ' ' . $key . '="' . $val . '"'; |
112
|
|
|
} |
113
|
|
|
$url .= ' />'; |
114
|
|
|
} |
115
|
|
|
return $url; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Find and return all comments in complete database. |
120
|
|
|
* |
121
|
|
|
* @return array with all comments. |
122
|
|
|
*/ |
123
|
|
|
public function findAll() |
124
|
|
|
{ |
125
|
|
|
$comments = parent::findAll(); |
126
|
|
|
|
127
|
|
View Code Duplication |
foreach ($comments as $id => $comment) { |
|
|
|
|
128
|
|
|
$comment->since_time = $this->humanTiming($comment->created); |
129
|
|
|
$comment->gravatar = $this->getGravatar($comment->mail); |
130
|
|
|
} |
131
|
|
|
// echo __FILE__ . " : " . __LINE__ . "<br>";dump($comments); |
132
|
|
|
return $comments; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Find and return all comments within the page comment flow. |
137
|
|
|
* |
138
|
|
|
* @return array with all comments. |
139
|
|
|
*/ |
140
|
|
|
public function findFlow($flow = null) |
141
|
|
|
{ |
142
|
|
|
$comments = $this->query() |
143
|
|
|
->where('flow = ' . "'$flow'") |
144
|
|
|
->execute(); |
145
|
|
|
// Add human timing and gravatars to each comment post. |
146
|
|
View Code Duplication |
foreach ($comments as $id => $comment) { |
|
|
|
|
147
|
|
|
$comment->since_time = $this->humanTiming($comment->created); |
148
|
|
|
$comment->gravatar = $this->getGravatar($comment->mail); |
149
|
|
|
} |
150
|
|
|
return $comments; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.