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