Commit 6ffb6450 by Van

Remove event for update

parent ed0001e6
...@@ -30,55 +30,57 @@ class BoltSendEmailForNewContentExtension extends SimpleExtension ...@@ -30,55 +30,57 @@ class BoltSendEmailForNewContentExtension extends SimpleExtension
*/ */
public function hookPreSave(StorageEvent $event) public function hookPreSave(StorageEvent $event)
{ {
$app = $this->getContainer(); if(!$event->getId()) {
$app = $this->getContainer();
// Get contenttype // Get contenttype
$contenttype = $event->getContentType(); $contenttype = $event->getContentType();
$aNotificationsContentTypes = $this->getNotifContentTypes(); $aNotificationsContentTypes = $this->getNotifContentTypes();
if ( empty($contenttype) if ( empty($contenttype)
|| empty($aNotificationsContentTypes) || empty($aNotificationsContentTypes)
|| !in_array($contenttype, $aNotificationsContentTypes) ) { || !in_array($contenttype, $aNotificationsContentTypes) ) {
return; return;
} }
// Get the record : Bolt\Storage\Entity\Content // Get the record : Bolt\Storage\Entity\Content
$record = $event->getContent(); $record = $event->getContent();
// Test if newly published // Test if newly published
$contentNewlyPublished = false; $contentNewlyPublished = false;
if ( $event->isCreate() if ( $event->isCreate()
&& $record->getStatus() == 'published') { && $record->getStatus() == 'published') {
$contentNewlyPublished = true;
}
else if ($record->getStatus() == 'published') {
// @todo : check if notification already sent
// use a temporary file or a db table ?
$repo = $app['storage']->getRepository($contenttype);
$oldRecord = $repo->find($record->getId() );
if ( !empty($oldRecord)
&& $oldRecord->getStatus() != 'published') {
$contentNewlyPublished = true; $contentNewlyPublished = true;
} }
} else if ($record->getStatus() == 'published') {
// @todo : check if notification already sent
// use a temporary file or a db table ?
$repo = $app['storage']->getRepository($contenttype);
$oldRecord = $repo->find($record->getId() );
if ( !empty($oldRecord)
&& $oldRecord->getStatus() != 'published') {
$contentNewlyPublished = true;
}
}
if ($contentNewlyPublished) { if ($contentNewlyPublished) {
// Launch the notification // Launch the notification
$notify = new Notifications($app, $this->getConfig(), $record, $contenttype); $notify = new Notifications($app, $this->getConfig(), $record, $contenttype);
// Search subscribers // Search subscribers
try { try {
$aSubscribers = $this->getSubscribers($contenttype); $aSubscribers = $this->getSubscribers($contenttype);
// Send email foreach subscriber // Send email foreach subscriber
$notify->doNotification($aSubscribers); $notify->doNotification($aSubscribers);
} catch (\Exception $e) { } catch (\Exception $e) {
$app['logger.system']->error(sprintf("BoltSendEmailForNewContentExtension notifications can't be sent - %s", $e->getMessage() ), ['event' => 'extensions']); $app['logger.system']->error(sprintf("BoltSendEmailForNewContentExtension notifications can't be sent - %s", $e->getMessage() ), ['event' => 'extensions']);
return; return;
}
} }
}
return; return;
}
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment