Skip to content

Commit

Permalink
Now storing timing data in a new Timing Entity and table. This allows…
Browse files Browse the repository at this point in the history
… us to easily merge in with the existing Mautic db schema.
  • Loading branch information
collinkrawll committed Oct 18, 2016
1 parent 9d4f1ea commit 67caae9
Show file tree
Hide file tree
Showing 12 changed files with 394 additions and 263 deletions.
18 changes: 11 additions & 7 deletions Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
'version' => '1.0',
'author' => 'Third Set Productions',
'services' => array(
//MODELS
'models' => array(
'plugin.thirdset.timing.event_timing_model' => array(
'class' => 'MauticPlugin\ThirdSetMauticTimingBundle\Model\TimingModel',
),
),
//OTHER
'other' => array(
//MANAGERS
'plugin.thirdset.timing.campaign_event_manager' => array(
'class' => 'MauticPlugin\ThirdSetMauticTimingBundle\Model\CampaignEventManager',
'arguments' => 'doctrine.orm.entity_manager',
),
//SUBSCRIBERS
'plugin.thirdset.timing.doctrine_subscriber' => array(
'class' => 'MauticPlugin\ThirdSetMauticTimingBundle\EventListener\DoctrineSubscriber',
Expand All @@ -38,12 +39,15 @@
'helpers' => array(
'plugin.thirdset.timing.timing_helper' => array(
'class' => 'MauticPlugin\ThirdSetMauticTimingBundle\Helper\TimingHelper',
'arguments' => 'plugin.thirdset.timing.campaign_event_manager',
'arguments' => [
'mautic.campaign.model.event',
'plugin.thirdset.timing.event_timing_model',
]
),
),
//EVENT SUBSCRIBERS/LISTENERS (Note: there are more in the "other" section)
'events' => array(
'plugin.thirdset.timing.campaign_pre_execution_event_listener' => array(
'plugin.thirdset.timing.campaign_event_subscriber' => array(
'class' => 'MauticPlugin\ThirdSetMauticTimingBundle\EventListener\CampaignEventSubscriber',
'arguments' => 'plugin.thirdset.timing.timing_helper',
),
Expand Down
202 changes: 202 additions & 0 deletions Entity/Timing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php
/**
* @package ThirdSetMauticTimingBundle
* @copyright 2016 Third Set Productions. All rights reserved.
* @author Third Set Productions
* @link http://www.thirdset.com
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace MauticPlugin\ThirdSetMauticTimingBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
use Mautic\CampaignBundle\Entity\Event;

/**
* The Timing class is an Entity class that holds timing data for an event.
* It holds a one-to-one relationship with the Entity class.
*/
class Timing
{
/**
* The event that the Timing is for.
* @var Event
*/
private $event;

/**
* The cron expression for the Timing.
* @var string
*/
private $expression;

/**
* Whether or not to use the contact's timezone.
* @var boolean
*/
private $useContactTimezone;

/**
* The timezone for the Timing (ex: "America/Los_Angeles")
* @var string
*/
private $timezone;

/**
* Constructor.
* @param Event $event 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,
$expression = null,
$useContactTimezone = null,
$timezone = null
)
{
$this->event = $event;
$this->expression = $expression;
$this->useContactTimezone = $useContactTimezone;
$this->timezone = $timezone;
}

/**
* @param ORM\ClassMetadata $metadata
*/
public static function loadMetadata(ORM\ClassMetadata $metadata)
{
$builder = new ClassMetadataBuilder($metadata);

$builder->setTable('campaign_events_timing')
->setCustomRepositoryClass('MauticPlugin\ThirdSetMauticTimingBundle\Entity\TimingRepository');

$builder->createOneToOne('event', 'Mautic\CampaignBundle\Entity\Event')
->addJoinColumn('event_id', 'id')
->isPrimaryKey()
->build();

$builder->createField('expression', 'string')
->columnName('expression')
->nullable()
->build();

$builder->createField('useContactTimezone', 'integer')
->columnName('use_contact_timezone')
->nullable()
->build();

$builder->createField('timezone', 'string')
->columnName('timezone')
->nullable()
->build();
}

/**
* Method to post data to the Timing entity.
* @param Event $event The eventId of the Campaign Event that the Timing
* belongs to.
* @param array $dataArray The post data to be added to the Timing entity.
*/
public function addPostData($dataArray)
{
$this->expression = $dataArray['expression'];

$useContactTimezone = ( ! empty($dataArray['use_contact_timezone'])) ? $dataArray['use_contact_timezone'] : 0;
$this->useContactTimezone = $useContactTimezone;

$this->timezone = $dataArray['timezone'];
}

/**
* Set event.
*
* @param \Mautic\CampaignBundle\Entity\Event $event
*
* @return Timing
*/
public function setEvent(\Mautic\CampaignBundle\Entity\Campaign $event)
{
$this->event = $event;

return $this;
}

/**
* Get event.
*
* @return \Mautic\CampaignBundle\Entity\Event
*/
public function getEvent()
{
return $this->event;
}

/**
* @return array
*/
public function convertToArray()
{
return get_object_vars($this);
}

/**
* Sets the cron expression.
* @param string The cron expression.
*/
public function setExpression($expression)
{
$this->expression = $expression;
}

/**
* Gets the cron expression.
* @return string Returns the cron expression.
*/
public function getExpression()
{
return $this->expression;
}

/**
* Sets whether or not to use the contact's timezone.
* @param boolean Whether or not to use the contact's timezone.
*/
public function setUseContactTimezone($useContactTimezone)
{
$this->useContactTimezone = $useContactTimezone;
}

/**
* Gets whether or not to use the contact's timezone.
* @return boolean Returns whether or not to use the contact's timezone.
*/
public function getUseContactTimezone()
{
return $this->useContactTimezone;
}

/**
* Sets the timezone.
* @param string The timezone.
*/
public function setTimezone($timezone)
{
$this->timezone = $timezone;
}

/**
* Gets the timezone.
* @return string Returns the timezone.
*/
public function getTimezone()
{
return $this->timezone;
}

}
17 changes: 17 additions & 0 deletions Entity/TimingRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* @package ThirdSetMauticTimingBundle
* @copyright 2016 Third Set Productions. All rights reserved.
* @author Third Set Productions
* @link http://www.thirdset.com
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace MauticPlugin\ThirdSetMauticTimingBundle\Entity;

use Mautic\CoreBundle\Entity\CommonRepository;

class TimingRepository extends CommonRepository
{

}
42 changes: 21 additions & 21 deletions EventListener/DoctrineSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Symfony\Component\HttpFoundation\Session\Session;

use MauticPlugin\ThirdSetMauticTimingBundle\Form\Model\Timing;
use MauticPlugin\ThirdSetMauticTimingBundle\Entity\Timing;

/**
* Class DoctrineSubscriber.
Expand Down Expand Up @@ -73,9 +73,8 @@ public function postUpdate(LifecycleEventArgs $args)
/**
* Private helper method for adding timing data to an Event.
*
* Note: we access the db from within this function (instead of a manager,
* model class) because the EM included in the args is set to avoid circular
* references.
* Note: we access the db from within this function (instead of a model
* class) to avoid circular references.
*
* @param LifecycleEventArgs $args
*/
Expand All @@ -87,31 +86,32 @@ private function saveTimingData(LifecycleEventArgs $args)
/** @var \Mautic\CampaignBundle\Entity\Event $event */
$event = $entity;

$campaignId = $event->getCampaign()->getId();;
$campaignId = $event->getCampaign()->getId();

//get the timing data out of the session
$modifiedEvents = $this->session->get('mautic.campaign.'.$campaignId.'.events.modified', []);
$eventData = $modifiedEvents[$event->getId()];
$timingArr = $eventData['timing'];
$timing = Timing::createFromDataArray($timingArr);

//----- update the event with the timing data ------
//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());

/* @var $qb \Doctrine\DBAL\Query\QueryBuilder */
$qb = $em->getConnection()->createQueryBuilder();

$qb->update(MAUTIC_TABLE_PREFIX . 'campaign_events')
->set('timing_expression', ':expression')
->set('timing_use_contact_timezone', ':useContactTimezone')
->set('timing_timezone', ':timezone')
->where('id = :id')
->setParameter('expression', $timing->getExpression())
->setParameter('useContactTimezone', $timing->getUseContactTimezone())
->setParameter('timezone', $timing->getTimezone())
->setParameter('id', $event->getId())
->execute();
//--------------------------------------------------
//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($timingArr);

//persist the Timing
$em->persist($timing);
$em->flush();
}
}
}
Loading

0 comments on commit 67caae9

Please sign in to comment.