Permalink
Browse files

Fixed issue with setting timing data on new campaigns.

  • Loading branch information...
collinkrawll committed Dec 14, 2016
1 parent 1999fdf commit 8dafe046dfa648f90c2324971e429aafb41a317c
Showing with 70 additions and 45 deletions.
  1. +2 −2 Entity/Timing.php
  2. +56 −36 EventListener/DoctrineSubscriber.php
  3. +11 −6 Form/Extension/EventTypeExtension.php
  4. +1 −1 Model/TimingModel.php
View
@@ -46,15 +46,15 @@ class Timing
/**
* Constructor.
* @param Event $event The event that the Timing is for.
* @param Event $event|null The event that the Timing is for.
* @param string|null $expression The cron expression for the Timing.
* @param boolean|null $useContactTimezone Whether or not to use the
* contact's timezone.
* @param string|null $timezone The timezone for the Timing
* (ex: "America/Los_Angeles")
*/
public function __construct(
Event $event,
Event $event = null,
$expression = null,
$useContactTimezone = null,
$timezone = null
@@ -14,6 +14,7 @@
use Doctrine\ORM\Events;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\ParameterBag;
use MauticPlugin\ThirdSetMauticTimingBundle\Entity\Timing;
@@ -23,7 +24,7 @@
* @package ThirdSetMauticTimingBundle
*/
class DoctrineSubscriber implements EventSubscriber
{
{
/* @var $session \Symfony\Component\HttpFoundation\Session\Session */
private $session;
@@ -57,7 +58,21 @@ public function getSubscribedEvents()
*/
public function postPersist(LifecycleEventArgs $args)
{
$this->saveTimingData($args);
$entity = $args->getEntity();
if($entity instanceof \Mautic\CampaignBundle\Entity\Event) {
/** @var \Mautic\CampaignBundle\Entity\Event $event */
$event = $entity;
$postVars = new ParameterBag($_POST);
$campaignId = $postVars->get('campaign')['sessionId'];
$modifiedEvents = $this->session->get('mautic.campaign.'.$campaignId.'.events.modified', []);
$eventData = $modifiedEvents[$event->getTempId()];
$timingData = $eventData['timing'];
$this->saveTimingData($args, $event, $timingData);
}
}
/**
@@ -67,19 +82,6 @@ public function postPersist(LifecycleEventArgs $args)
*/
public function postUpdate(LifecycleEventArgs $args)
{
$this->saveTimingData($args);
}
/**
* Private helper method for adding timing data to an Event.
*
* Note: we access the db from within this function (instead of a model
* class) to avoid circular references.
*
* @param LifecycleEventArgs $args
*/
private function saveTimingData(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if($entity instanceof \Mautic\CampaignBundle\Entity\Event) {
@@ -88,30 +90,48 @@ private function saveTimingData(LifecycleEventArgs $args)
$campaignId = $event->getCampaign()->getId();
//get the timing data out of the session
//get the timing data out of the session (for edit requests)
$modifiedEvents = $this->session->get('mautic.campaign.'.$campaignId.'.events.modified', []);
$eventData = $modifiedEvents[$event->getId()];
$timingArr = $eventData['timing'];
//get the timing object (note: we have to go through the attached em to prevent a circular reference)
/* @var $em \Doctrine\ORM\EntityManager */
$em = $args->getEntityManager();
/* @var $timingRepository \MauticPlugin\ThirdSetMauticTimingBundle\Entity\TimingRepository */
$timingRepository = $em->getRepository('ThirdSetMauticTimingBundle:Timing');
/* @var $timing \MauticPlugin\ThirdSetMauticTimingBundle\Entity\Timing */
$timing = $timingRepository->getEntity($event->getId());
//if there isn't any timing data yet, create a new Timing Entity.
if($timing == null) {
$timing = new Timing($event);
}
$timingData = $eventData['timing'];
//add the new data
$timing->addPostData($timingArr);
//persist the Timing
$em->persist($timing);
$em->flush();
$this->saveTimingData($args, $event, $timingData);
}
}
/**
* Private helper method for adding timing data to an Event.
*
* Note: we access the db from within this function (instead of a model
* class) to avoid circular references.
*
* @param Event $event The campaign Event to attach the timing data to.
* @param array $timingData An array of timing post data.
*/
private function saveTimingData(
LifecycleEventArgs $args,
Event $event,
$timingData
)
{
//get the timing object (note: we have to go through the attached em to prevent a circular reference)
/* @var $em \Doctrine\ORM\EntityManager */
$em = $args->getEntityManager();
/* @var $timingRepository \MauticPlugin\ThirdSetMauticTimingBundle\Entity\TimingRepository */
$timingRepository = $em->getRepository('ThirdSetMauticTimingBundle:Timing');
/* @var $timing \MauticPlugin\ThirdSetMauticTimingBundle\Entity\Timing */
$timing = $timingRepository->getEntity($event->getId());
//if there isn't any timing data yet, create a new Timing Entity.
if($timing == null) {
$timing = new Timing($event);
}
//add the new data
$timing->addPostData($timingData);
//persist the Timing
$em->persist($timing);
$em->flush();
}
}
@@ -77,12 +77,18 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'required' => false,
)
);
//var_dump($options['data']); //For debugging (goes to AJAX response).
$eventId = $options['data']['id'];
/* @var $event \Mautic\CampaignBundle\Entity\Event */
$event = $this->eventModel->getEntity($eventId);
//Get the event (or use null) if this is a new Campaign
$event = null;
$update = (!empty($options['data']['id']) && strpos($options['data']['id'], 'new') === false) ? true : false;
if($update) {
$eventId = $options['data']['id'];
/* @var $event \Mautic\CampaignBundle\Entity\Event */
$event = $this->eventModel->getEntity($eventId);
}
//add timing form event subscriber
$builder->addEventSubscriber(
new TimingFormSubscriber(
@@ -91,7 +97,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$this->timingModel
)
);
}
}
View
@@ -46,7 +46,7 @@ public function getRepository()
*
* @return null|Timing
*/
public function getEntity(Event $event)
public function getEntity(Event $event = null)
{
if ($event === null) {
return new Timing($event);

0 comments on commit 8dafe04

Please sign in to comment.