<?php
namespace App\EventListener;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\DataObject\MediaUser;
use Pimcore\Model\DataObject\Region;
use Pimcore\Model\Document\Email;
use Pimcore\Model\WebsiteSetting;
use Pimcore\Tool;
class MediaUserListener
{
protected $websiteSettingDefaultRegistrationCompleded = null;
public function postUpdateAction(ElementEventInterface $e) {
$object = $e->getElement();
$onlyVersionSave = $e->hasArgument('saveVersionOnly');
if(!$onlyVersionSave && $object instanceof MediaUser) {
if($object->getSendRegistrationCompletedMail() && $object->isPublished() && $object->getConfirmed()) {
$region = null;
$websiteSettingRegion = WebsiteSetting::getByName('mainRegion');
if ($websiteSettingRegion && $websiteSettingRegion->getData()) {
$region = $websiteSettingRegion->getData();
}
if($mailDoc = $this->getMailDoc(true, $region)) {
if($mailDoc instanceof Email) {
$notification = new \Pimcore\Mail();
$params = [
'title' => $object->getTitle(),
'firstname' => $object->getFirstname(),
'lastname' => $object->getLastname(),
];
$params['items'] = array_keys($params);
$notification->setDocument($mailDoc);
$notification->setParams($params);
$notification->addTo($object->getEmail());
$notification->send();
$object->setSendRegistrationCompletedMail(false);
if($region) {
$this->addRegionToUser($object, $region);
}
}
}
}
if(!empty($object->getFurtherRegionsBlock())) {
foreach ($object->getFurtherRegionsBlock() as $furtherRegionBlockElement) {
$registrationConfirmed = $furtherRegionBlockElement['confirmedFurtherRegion']->getData();
$sendRegistrationCompletedMail = $furtherRegionBlockElement['sendRegistrationCompletedMailFurtherRegion']->getData();
if($registrationConfirmed && $sendRegistrationCompletedMail && $object->isPublished() ) {
$region = $furtherRegionBlockElement['region']->getData();
if($mailDoc = $this->getMailDoc(false, $region)) {
$notification = new \Pimcore\Mail();
$params = [
'title' => $object->getTitle(),
'firstname' => $object->getFirstname(),
'lastname' => $object->getLastname(),
];
$params['items'] = array_keys($params);
$notification->setDocument($mailDoc);
$notification->setParams($params);
$notification->addTo($object->getEmail());
$notification->send();
$furtherRegionBlockElement['sendRegistrationCompletedMailFurtherRegion']->setData(false);
$this->addRegionToUser($object, $region);
}
}
}
}
}
}
private function getMailDoc(bool $isMainRegion, ?Region $region = null) {
$config = $region?->getSiteConfig();
if (!$config) { //set region to null, if main region should be used
$websiteSettingConfig = WebsiteSetting::getByName('DefaultSiteConfig');
if ($websiteSettingConfig && $websiteSettingConfig->getData()) {
$config = $websiteSettingConfig->getData();
}
}
if($config && $config->getPressRegistrationConfirmMail('de')) {
return $config->getPressRegistrationConfirmMail('de');
}
// if no config or no mail in config use fallback
if(empty($this->websiteSettingDefaultRegistrationCompleded)) {
$websiteSetting = WebsiteSetting::getByName('pressUserRegistrationCompletedMail');
if($websiteSetting && $websiteSetting->getData()) {
$this->websiteSettingDefaultRegistrationCompleded = $websiteSetting->getData();
}
}
if($this->websiteSettingDefaultRegistrationCompleded) {
return $this->websiteSettingDefaultRegistrationCompleded;
}
}
private function addRegionToUser(MediaUser $user, Region $region)
{
$regions = $user->getRegisteredRegions();
foreach ($regions as $registeredRegion) {
if ($registeredRegion->getId() == $region->getId()) {
return; // already registered
}
}
$regions[] = $region;
$user->setRegisteredRegions($regions);
}
}