-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.itop-gravatar.php
111 lines (93 loc) · 2.78 KB
/
main.itop-gravatar.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
106
107
108
109
110
111
<?php
@include_once('../../approot.inc.php');
require_once(MODULESROOT.'/itop-gravatar/lib/gravatarlib/Gravatar.php');
class ormGravatarImage extends ormDocument
{
private $sEmailValue;
private $sDefaultImageUrl;
private $iMaxSize;
/**
* We can have different parameters than in parent class... but this breaks substitution principle !
* Ok for our use case, but kids, do not do this at home ;)
*
* @see http://www.php.net/oop5.decon
*
* @param \ormDocument $value
* @param string $sEmailValue
* @param string $sDefaultImageUrl
* @param int $iMaxSize
*/
public function __construct(ormDocument $value, $sEmailValue, $sDefaultImageUrl, $iMaxSize)
{
$data = $value->GetData();
$sMimeType = 'image/png'; // used for form modifications, see #GetData
$sFileName = $value->GetFileName();
parent::__construct($data, $sMimeType, $sFileName);
$this->sEmailValue = $sEmailValue;
$this->sDefaultImageUrl = $sDefaultImageUrl;
$this->iMaxSize = $iMaxSize;
}
/**
* @return bool false, as we will always return an URL
* when modifying form we will use #GetData method
*/
public function IsEmpty()
{
return false;
}
public function IsEmptyValue()
{
return parent::IsEmpty();
}
public function GetDownloadURL($sClass, $Id, $sAttCode)
{
return $this->GetImageURL($sClass, $Id, $sAttCode);
}
public function GetDisplayURL($sClass, $Id, $sAttCode)
{
return $this->GetImageURL($sClass, $Id, $sAttCode);
}
protected function GetImageURL($sClass, $Id, $sAttCode)
{
$bIsEmptyValue = $this->IsEmptyValue();
$bEmptyEmail = empty($this->sEmailValue);
if (!$bIsEmptyValue || $bEmptyEmail)
{
return parent::GetDownloadURL($sClass, $Id, $sAttCode);
}
$sGravatarUrl = $this->GetGravatarUrl($this->sEmailValue, $this->sDefaultImageUrl, $this->iMaxSize);
return $sGravatarUrl;
}
/**
* @param string $sEmail
* @param string $sDefaultImageUrl won't work for localhost but ok with a valid hostname (eg
* https://demo.combodo.com/simple/env-production/itop-config-mgmt/images/silhouette.png)
* @param int $iMaxSize
*
* @return string
*/
private function GetGravatarUrl($sEmail, $sDefaultImageUrl, $iMaxSize)
{
if (empty($sEmail))
{
return null;
}
$oGravatar = new \emberlabs\GravatarLib\Gravatar();
$oGravatar->enableSecureImages()
->setDefaultImage($sDefaultImageUrl)
->setAvatarSize($iMaxSize);
return $oGravatar->buildGravatarURL($sEmail);
}
/**
* Used in the object modification form as preview
*
* @return mixed binary content of the gravatar icon
* @see \cmdbAbstractObject::GetFormElementForField
*/
public function GetData()
{
$fGravatarIcon = MODULESROOT.'/itop-gravatar/lib/gravatar-black-256-icon.png';
$GravatarIconBinary = file_get_contents($fGravatarIcon);
return $GravatarIconBinary;
}
}