Skip to content

Commit

Permalink
Add SMS Properties event
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzmany committed Feb 9, 2024
1 parent 01007a0 commit 4fa8c0d
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 0 deletions.
28 changes: 28 additions & 0 deletions app/bundles/SmsBundle/Entity/Sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Mautic\SmsBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver;
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
Expand Down Expand Up @@ -81,6 +82,11 @@ class Sms extends FormEntity
*/
private $pendingCount = 0;

/**
* @var array
*/
private $properties = [];

public function __clone()
{
$this->id = null;
Expand Down Expand Up @@ -131,6 +137,8 @@ public static function loadMetadata(ORM\ClassMetadata $metadata): void
->columnName('sent_count')
->build();

$builder->addField('properties', Types::JSON);

$builder->addCategory();

$builder->createManyToMany('lists', \Mautic\LeadBundle\Entity\LeadList::class)
Expand Down Expand Up @@ -207,6 +215,7 @@ public static function loadApiMetadata(ApiMetadataDriver $metadata): void
'publishUp',
'publishDown',
'sentCount',
'properties',
]
)
->build();
Expand Down Expand Up @@ -459,4 +468,23 @@ public function getPendingCount()
{
return $this->pendingCount;
}

/**
* @return array<int|string|array<int|string>>
*/
public function getProperties(): array
{
return $this->properties;
}

/**
* @param array<int|string|array<int|string>> $properties
*/
public function setProperties(array $properties): static
{
$this->isChanged('properties', $properties);
$this->properties = $properties;

return $this;
}
}
40 changes: 40 additions & 0 deletions app/bundles/SmsBundle/Event/SmsPropertiesEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Mautic\SmsBundle\Event;

use Symfony\Component\Form\FormBuilder;

class SmsPropertiesEvent extends \Symfony\Contracts\EventDispatcher\Event
{
private array $fields = [];

public function __construct(private FormBuilder $formBuilder, private array $data)
{
}

public function getFormBuilder(): FormBuilder
{
return $this->formBuilder;
}

public function getData(): array
{
return $this->data;
}

public function addField(string $child, string $type = null, array $options = []): void
{
$this->fields[] = [
'child' => $child,
'type' => $type,
'options' => $options,
];
}

public function getFields(): array
{
return $this->fields;
}
}
31 changes: 31 additions & 0 deletions app/bundles/SmsBundle/Form/Type/SmsPropertiesType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Mautic\SmsBundle\Form\Type;

use Mautic\SmsBundle\Event\SmsPropertiesEvent;
use Mautic\SmsBundle\SmsEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class SmsPropertiesType extends AbstractType
{
private EventDispatcherInterface $dispatcher;

public function __construct(\Symfony\Contracts\EventDispatcher\EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$event = new SmsPropertiesEvent($builder, $options['data'] ?? []);
$this->dispatcher->dispatch($event, SmsEvents::SMS_PROPERTIES);

foreach ($event->getFields() as $formField) {
$builder->add($formField['child'], $formField['type'], $formField['options']);
}
}
}
9 changes: 9 additions & 0 deletions app/bundles/SmsBundle/Form/Type/SmsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
]
);

$builder->add(
'properties',
SmsPropertiesType::class,
[
'label'=> false,
'data' => $options['data']->getProperties(),
]
);

$builder->add(
'language',
LocaleType::class,
Expand Down
9 changes: 9 additions & 0 deletions app/bundles/SmsBundle/SmsEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,13 @@ final class SmsEvents
* Mautic\SmsBundle\Event\TokensBuildEvent
*/
public const ON_SMS_TOKENS_BUILD = 'mautic.sms.on_tokens_build';

/**
* The mautic.sms_properties event is dispatched during the configuration in send text message dialog.
*
* The event listener receives a Mautic\SmsBundle\Event\SmsPropertiesEvent instance.
*
* @var string
*/
public const SMS_PROPERTIES = 'mautic.sms_properties';
}
31 changes: 31 additions & 0 deletions app/migrations/Version20240209104703.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Mautic\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\Exception\SkipMigration;
use Mautic\CoreBundle\Doctrine\AbstractMauticMigration;

final class Version20240209104703 extends AbstractMauticMigration
{
public function preUp(Schema $schema): void
{
$smsTable = $schema->getTable(MAUTIC_TABLE_PREFIX.'sms_messages');
if ($smsTable->hasColumn('properties')) {
throw new SkipMigration('Schema includes this migration');
}
}

/**
* @throws \Doctrine\DBAL\Schema\SchemaException
*/
public function up(Schema $schema): void
{
$smsTable = $schema->getTable(MAUTIC_TABLE_PREFIX.'sms_messages');
if (!$smsTable->hasColumn('properties')) {
$this->addSql("ALTER TABLE {$this->prefix}sms_messages ADD properties LONGTEXT NOT NULL COMMENT '(DC2Type:json)'");
}
}
}

0 comments on commit 4fa8c0d

Please sign in to comment.