|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace modules\users\widgets; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\Widget; |
|
7
|
|
|
use yii\helpers\Html; |
|
8
|
|
|
use modules\users\models\User; |
|
9
|
|
|
use modules\users\models\UploadForm; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class AvatarWidget |
|
13
|
|
|
* @package modules\users\widgets |
|
14
|
|
|
* |
|
15
|
|
|
* @property array $imageOptions |
|
16
|
|
|
* @property string $email |
|
17
|
|
|
* @property int|string $user_id |
|
18
|
|
|
* @property-read string|int $userId |
|
19
|
|
|
* @property-read string $gravatarEmail |
|
20
|
|
|
* @property string|int $size |
|
21
|
|
|
*/ |
|
22
|
|
|
class AvatarWidget extends Widget |
|
23
|
|
|
{ |
|
24
|
|
|
public $imageOptions = [ |
|
25
|
|
|
'class' => 'img-circle', |
|
26
|
|
|
]; |
|
27
|
|
|
public $email = ''; |
|
28
|
|
|
public $user_id; |
|
29
|
|
|
public $size = '150'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @inheritdoc |
|
33
|
|
|
*/ |
|
34
|
|
|
public function init() |
|
35
|
|
|
{ |
|
36
|
|
|
parent::init(); |
|
37
|
|
|
$this->email = !empty($this->email) ? $this->email : $this->getGravatarEmail(); |
|
38
|
|
|
$this->user_id = !empty($this->user_id) ? $this->user_id : $this->getUserId(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritdoc |
|
43
|
|
|
*/ |
|
44
|
|
|
public function run() |
|
45
|
|
|
{ |
|
46
|
|
|
$model = new UploadForm(); |
|
47
|
|
|
$fileName = $model->getFileName(); |
|
48
|
|
|
$avatar = $model->getPath($this->user_id) . DIRECTORY_SEPARATOR . $fileName; |
|
49
|
|
|
if (file_exists($avatar)) { |
|
50
|
|
|
echo Html::img( |
|
51
|
|
|
[ |
|
52
|
|
|
'/users/profile/avatar', |
|
53
|
|
|
'filename' => 'avatar.jpg', |
|
54
|
|
|
'id' => $this->user_id |
|
55
|
|
|
], |
|
56
|
|
|
$this->imageOptions |
|
57
|
|
|
); |
|
58
|
|
|
} else { |
|
59
|
|
|
echo $this->getGravatar($this->email, $this->size, 'mm', 'g', true, $this->imageOptions); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get either a Gravatar URL or complete image tag for a specified email address. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $email The email address |
|
67
|
|
|
* @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ] |
|
68
|
|
|
* @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] |
|
69
|
|
|
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ] |
|
70
|
|
|
* @param bool $img True to return a complete IMG tag False for just the URL |
|
71
|
|
|
* @param array $attr Optional, additional key/value attributes to include in the IMG tag |
|
72
|
|
|
* @return string containing either just a URL or a complete image tag |
|
73
|
|
|
* @source https://gravatar.com/site/implement/images/php/ |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getGravatar($email = '', $s = '80', $d = 'mm', $r = 'g', $img = false, $attr = []) |
|
76
|
|
|
{ |
|
77
|
|
|
$data = ['email' => $email, 's' => $s, 'd' => $d, 'r' => $r]; |
|
78
|
|
|
$key = 'gravatar_' . md5($email); |
|
79
|
|
|
$duration = 60 * 60; // 3600 сек или 1 час |
|
80
|
|
|
$cache = Yii::$app->cache; |
|
81
|
|
|
$url = $cache->getOrSet($key, function () use ($data) { |
|
82
|
|
|
return $this->calculateSomething($data); |
|
83
|
|
|
}, $duration); |
|
84
|
|
|
return $img ? Html::img($url, $attr) : $url; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param array $data |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function calculateSomething($data = []) |
|
92
|
|
|
{ |
|
93
|
|
|
$url = 'https://www.gravatar.com/avatar/'; |
|
94
|
|
|
$url .= md5(strtolower(trim($data['email']))) . '?'; |
|
95
|
|
|
$url .= http_build_query([ |
|
96
|
|
|
's' => $data['s'], |
|
97
|
|
|
'd' => $data['d'], |
|
98
|
|
|
'r' => $data['r'], |
|
99
|
|
|
]); |
|
100
|
|
|
return $url; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getGravatarEmail() |
|
107
|
|
|
{ |
|
108
|
|
|
/** @var User $user */ |
|
109
|
|
|
$user = Yii::$app->user->identity; |
|
110
|
|
|
return (!Yii::$app->user->isGuest) ? $user->profile->email_gravatar : $this->email; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return int|string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getUserId() |
|
117
|
|
|
{ |
|
118
|
|
|
/** @var User $user */ |
|
119
|
|
|
$user = Yii::$app->user->identity; |
|
120
|
|
|
return (!Yii::$app->user->isGuest) ? $user->id : $this->user_id; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|