Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\LeadBundle\Twig\Helper;
|
||||
|
||||
use Mautic\CoreBundle\Exception\FileNotFoundException;
|
||||
use Mautic\CoreBundle\Helper\PathsHelper;
|
||||
use Mautic\CoreBundle\Twig\Helper\AssetsHelper;
|
||||
use Mautic\CoreBundle\Twig\Helper\GravatarHelper;
|
||||
use Mautic\LeadBundle\Entity\Lead;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
|
||||
final class AvatarHelper
|
||||
{
|
||||
/**
|
||||
* @var array<string>
|
||||
*/
|
||||
private array $imageTypes = ['jpg', 'jpeg', 'png', 'gif'];
|
||||
|
||||
public function __construct(
|
||||
private AssetsHelper $assetsHelper,
|
||||
private PathsHelper $pathsHelper,
|
||||
private GravatarHelper $gravatarHelper,
|
||||
private DefaultAvatarHelper $defaultAvatarHelper,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filePath
|
||||
*
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
public function createAvatarFromFile(Lead $lead, $filePath): void
|
||||
{
|
||||
if (!file_exists($filePath)) {
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
|
||||
$avatarDir = $this->getAvatarPath(true);
|
||||
|
||||
if (!file_exists($avatarDir)) {
|
||||
mkdir($avatarDir);
|
||||
}
|
||||
|
||||
$ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
|
||||
if (!in_array($ext, $this->imageTypes)) {
|
||||
throw new \Exception('File is not image');
|
||||
}
|
||||
|
||||
$fs = new Filesystem();
|
||||
$fs->copy($filePath, $avatarDir.DIRECTORY_SEPARATOR.'avatar'.$lead->getId(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAvatar(Lead $lead)
|
||||
{
|
||||
$preferred = $lead->getPreferredProfileImage();
|
||||
$socialData = $lead->getSocialCache();
|
||||
$leadEmail = $lead->getEmail();
|
||||
|
||||
if ('custom' == $preferred) {
|
||||
$avatarPath = $this->getAvatarPath(true).'/avatar'.$lead->getId();
|
||||
if (file_exists($avatarPath) && $fmtime = filemtime($avatarPath)) {
|
||||
// Append file modified time to ensure the latest is used by browser
|
||||
$img = $this->assetsHelper->getUrl(
|
||||
$this->getAvatarPath().'/avatar'.$lead->getId().'?'.$fmtime,
|
||||
null,
|
||||
null,
|
||||
false,
|
||||
true
|
||||
);
|
||||
}
|
||||
} elseif (isset($socialData[$preferred]) && !empty($socialData[$preferred]['profile']['profileImage'])) {
|
||||
$img = $socialData[$preferred]['profile']['profileImage'];
|
||||
}
|
||||
|
||||
if (empty($img)) {
|
||||
// Default to gravatar if others failed
|
||||
if (!empty($leadEmail)) {
|
||||
$img = $this->gravatarHelper->getImage($leadEmail);
|
||||
} else {
|
||||
$img = $this->defaultAvatarHelper->getDefaultAvatar();
|
||||
}
|
||||
}
|
||||
|
||||
return $img;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get avatar path.
|
||||
*
|
||||
* @param bool $absolute
|
||||
*/
|
||||
public function getAvatarPath($absolute = false): string
|
||||
{
|
||||
$imageDir = $this->pathsHelper->getSystemPath('images', $absolute);
|
||||
|
||||
return $imageDir.'/lead_avatars';
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'lead_avatar';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\LeadBundle\Twig\Helper;
|
||||
|
||||
use Mautic\CoreBundle\Twig\Helper\AssetsHelper;
|
||||
|
||||
final class DefaultAvatarHelper
|
||||
{
|
||||
public function __construct(
|
||||
private AssetsHelper $assetsHelper,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getDefaultAvatar(bool $absolute = false): string
|
||||
{
|
||||
return $this->assetsHelper->getOverridableUrl('images/avatar.png', $absolute);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\LeadBundle\Twig\Helper;
|
||||
|
||||
use Mautic\LeadBundle\Entity\DoNotContact;
|
||||
use Mautic\LeadBundle\Exception\UnknownDncReasonException;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* Convert DNC reason ID to text.
|
||||
*/
|
||||
final class DncReasonHelper
|
||||
{
|
||||
public function __construct(
|
||||
private TranslatorInterface $translator,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert DNC reason ID to text.
|
||||
*
|
||||
* @throws UnknownDncReasonException
|
||||
*/
|
||||
public function toText(int $reasonId): string
|
||||
{
|
||||
$reasonKey = match ($reasonId) {
|
||||
DoNotContact::IS_CONTACTABLE => 'mautic.lead.event.donotcontact_contactable',
|
||||
DoNotContact::UNSUBSCRIBED => 'mautic.lead.event.donotcontact_unsubscribed',
|
||||
DoNotContact::BOUNCED => 'mautic.lead.event.donotcontact_bounced',
|
||||
DoNotContact::MANUAL => 'mautic.lead.event.donotcontact_manual',
|
||||
default => throw new UnknownDncReasonException(sprintf("Unknown DNC reason ID '%c'", $reasonId)),
|
||||
};
|
||||
|
||||
return $this->translator->trans($reasonKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the canonical name of this helper.
|
||||
*
|
||||
* @return string The canonical name
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return 'lead_dnc_reason';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user