|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* class for commenting |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Loom\Comment; |
|
6
|
|
|
|
|
7
|
|
|
class CommentsInSession extends \Phpmvc\Comment\CommentsInSession |
|
|
|
|
|
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
View Code Duplication |
private function humanTiming($time) |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
$time = time() - $time; // to get the time since that moment |
|
14
|
|
|
$time = ($time<1)? 1 : $time; |
|
15
|
|
|
$tokens = array ( |
|
16
|
|
|
31536000 => 'år', |
|
17
|
|
|
2592000 => 'månader', |
|
18
|
|
|
604800 => 'veckor', |
|
19
|
|
|
86400 => 'dagar', |
|
20
|
|
|
3600 => 'timmar', |
|
21
|
|
|
60 => 'minuter', |
|
22
|
|
|
1 => 'sekunder' |
|
23
|
|
|
); |
|
24
|
|
|
|
|
25
|
|
|
foreach ($tokens as $unit => $text) { |
|
26
|
|
|
if ($time < $unit) { |
|
27
|
|
|
continue; |
|
28
|
|
|
} |
|
29
|
|
|
$numberOfUnits = floor($time / $unit); |
|
30
|
|
|
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?' ':''); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get either a Gravatar URL or complete image tag for a specified email address. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $email The email address |
|
38
|
|
|
* @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ] |
|
39
|
|
|
* @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] |
|
40
|
|
|
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ] |
|
41
|
|
|
* @param boole $img True to return a complete IMG tag False for just the URL |
|
42
|
|
|
* @param array $atts Optional, additional key/value attributes to include in the IMG tag |
|
43
|
|
|
* @return String containing either just a URL or a complete image tag |
|
44
|
|
|
* @source http://gravatar.com/site/implement/images/php/ |
|
45
|
|
|
*/ |
|
46
|
|
View Code Duplication |
private function getGravatar($email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array()) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$url = 'http://www.gravatar.com/avatar/'; |
|
49
|
|
|
$url .= md5(strtolower(trim($email))); |
|
50
|
|
|
$url .= "?s=$s&d=$d&r=$r"; |
|
51
|
|
|
if ($img) { |
|
52
|
|
|
$url = '<img src="' . $url . '"'; |
|
53
|
|
|
foreach ($atts as $key => $val) { |
|
54
|
|
|
$url .= ' ' . $key . '="' . $val . '"'; |
|
55
|
|
|
} |
|
56
|
|
|
$url .= ' />'; |
|
57
|
|
|
} |
|
58
|
|
|
return $url; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Find and return all comments within the page comment flow. |
|
63
|
|
|
* |
|
64
|
|
|
* @return array with all comments. |
|
65
|
|
|
*/ |
|
66
|
|
|
public function findAll() |
|
67
|
|
|
{ |
|
68
|
|
|
$result = array(); |
|
69
|
|
|
// TODO: add flow tag through object instantiation instead? |
|
70
|
|
|
$flow = $this->request->getRoute(); |
|
|
|
|
|
|
71
|
|
|
$comments = $this->session->get('comments', []); |
|
|
|
|
|
|
72
|
|
|
foreach ($comments as $id => $comment) { |
|
73
|
|
|
if ($comment['comment-flow']==$flow) { |
|
74
|
|
|
$comment['since-time'] = $this->humanTiming($comment['timestamp']); |
|
75
|
|
|
$comment['id'] = $id; |
|
76
|
|
|
$comment['gravatar'] = $this->getGravatar($comment['mail']); |
|
77
|
|
|
$result[] = $comment; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
return $result; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Update a comment. |
|
85
|
|
|
* |
|
86
|
|
|
* @param array $comment with all details. |
|
87
|
|
|
* @param integer $id of comment to be updated |
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
|
|
public function update($comment, $id) |
|
92
|
|
|
{ |
|
93
|
|
|
$comments = $this->session->get('comments', []); |
|
94
|
|
|
$comments[$id] = $comment; |
|
95
|
|
|
$this->session->set('comments', $comments); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Delete a comment. |
|
100
|
|
|
* |
|
101
|
|
|
* @param $id of post to delete. |
|
102
|
|
|
* |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
public function delete($id) |
|
106
|
|
|
{ |
|
107
|
|
|
$comments = $this->session->get('comments', []); |
|
108
|
|
|
unset($comments[$id]); |
|
109
|
|
|
$this->session->set('comments', $comments); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|