Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,490 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\SmsBundle\Entity;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Delete;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use ApiPlatform\Metadata\Patch;
|
||||
use ApiPlatform\Metadata\Post;
|
||||
use ApiPlatform\Metadata\Put;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver;
|
||||
use Mautic\CategoryBundle\Entity\Category;
|
||||
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
|
||||
use Mautic\CoreBundle\Entity\FormEntity;
|
||||
use Mautic\CoreBundle\Entity\TranslationEntityInterface;
|
||||
use Mautic\CoreBundle\Entity\TranslationEntityTrait;
|
||||
use Mautic\CoreBundle\Entity\UuidInterface;
|
||||
use Mautic\CoreBundle\Entity\UuidTrait;
|
||||
use Mautic\CoreBundle\Entity\VariantEntityInterface;
|
||||
use Mautic\CoreBundle\Entity\VariantEntityTrait;
|
||||
use Mautic\CoreBundle\Validator\EntityEvent;
|
||||
use Mautic\LeadBundle\Entity\LeadList;
|
||||
use Mautic\LeadBundle\Form\Validator\Constraints\LeadListAccess;
|
||||
use Mautic\ProjectBundle\Entity\ProjectTrait;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
use Symfony\Component\Validator\Constraints\Callback;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new GetCollection(security: "is_granted('sms:smses:viewown')"),
|
||||
new Post(security: "is_granted('sms:smses:create')"),
|
||||
new Get(security: "is_granted('sms:smses:viewown')"),
|
||||
new Put(security: "is_granted('sms:smses:editown')"),
|
||||
new Patch(security: "is_granted('sms:smses:editother')"),
|
||||
new Delete(security: "is_granted('sms:smses:deleteown')"),
|
||||
],
|
||||
normalizationContext: [
|
||||
'groups' => ['sms:read'],
|
||||
'swagger_definition_name' => 'Read',
|
||||
'api_included' => ['category'],
|
||||
],
|
||||
denormalizationContext: [
|
||||
'groups' => ['sms:write'],
|
||||
'swagger_definition_name' => 'Write',
|
||||
]
|
||||
)]
|
||||
/**
|
||||
* @use TranslationEntityTrait<Sms>
|
||||
* @use VariantEntityTrait<Sms>
|
||||
*/
|
||||
class Sms extends FormEntity implements UuidInterface, TranslationEntityInterface, VariantEntityInterface
|
||||
{
|
||||
use UuidTrait;
|
||||
use ProjectTrait;
|
||||
use TranslationEntityTrait;
|
||||
use VariantEntityTrait;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
#[Groups(['sms:read'])]
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
#[Groups(['sms:read', 'sms:write'])]
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
#[Groups(['sms:read', 'sms:write'])]
|
||||
private $description;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
#[Groups(['sms:read', 'sms:write'])]
|
||||
private $message;
|
||||
|
||||
/**
|
||||
* @var \DateTimeInterface
|
||||
*/
|
||||
#[Groups(['sms:read', 'sms:write'])]
|
||||
private $publishUp;
|
||||
|
||||
/**
|
||||
* @var \DateTimeInterface
|
||||
*/
|
||||
#[Groups(['sms:read', 'sms:write'])]
|
||||
private $publishDown;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
#[Groups(['sms:read'])]
|
||||
private $sentCount = 0;
|
||||
|
||||
/**
|
||||
* @var Category|null
|
||||
**/
|
||||
#[Groups(['sms:read', 'sms:write'])]
|
||||
private $category;
|
||||
|
||||
/**
|
||||
* @var ArrayCollection<int, LeadList>
|
||||
*/
|
||||
#[Groups(['sms:read', 'sms:write'])]
|
||||
private $lists;
|
||||
|
||||
/**
|
||||
* @var ArrayCollection<int, Stat>
|
||||
*/
|
||||
private $stats;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
#[Groups(['sms:read', 'sms:write'])]
|
||||
private $smsType = 'template';
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
#[Groups(['sms:read'])]
|
||||
private $pendingCount = 0;
|
||||
|
||||
public function __clone()
|
||||
{
|
||||
$this->id = null;
|
||||
$this->stats = new ArrayCollection();
|
||||
$this->sentCount = 0;
|
||||
|
||||
$this->clearTranslations();
|
||||
|
||||
parent::__clone();
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->lists = new ArrayCollection();
|
||||
$this->stats = new ArrayCollection();
|
||||
$this->initializeProjects();
|
||||
$this->translationChildren = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function clearStats(): void
|
||||
{
|
||||
$this->stats = new ArrayCollection();
|
||||
}
|
||||
|
||||
public static function loadMetadata(ORM\ClassMetadata $metadata): void
|
||||
{
|
||||
$builder = new ClassMetadataBuilder($metadata);
|
||||
|
||||
$builder->setTable('sms_messages')
|
||||
->setCustomRepositoryClass(SmsRepository::class);
|
||||
|
||||
$builder->addIdColumns();
|
||||
|
||||
$builder->createField('message', 'text')
|
||||
->build();
|
||||
|
||||
$builder->createField('smsType', 'text')
|
||||
->columnName('sms_type')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->addPublishDates();
|
||||
|
||||
$builder->createField('sentCount', 'integer')
|
||||
->columnName('sent_count')
|
||||
->build();
|
||||
|
||||
$builder->addCategory();
|
||||
|
||||
$builder->createManyToMany('lists', LeadList::class)
|
||||
->setJoinTable('sms_message_list_xref')
|
||||
->setIndexBy('id')
|
||||
->addInverseJoinColumn('leadlist_id', 'id', false, false, 'CASCADE')
|
||||
->addJoinColumn('sms_id', 'id', false, false, 'CASCADE')
|
||||
->fetchExtraLazy()
|
||||
->build();
|
||||
|
||||
$builder->createOneToMany('stats', 'Stat')
|
||||
->setIndexBy('id')
|
||||
->mappedBy('sms')
|
||||
->cascadePersist()
|
||||
->fetchExtraLazy()
|
||||
->build();
|
||||
|
||||
self::addTranslationMetadata($builder, self::class);
|
||||
|
||||
static::addUuidField($builder);
|
||||
self::addProjectsField($builder, 'sms_projects_xref', 'sms_id');
|
||||
}
|
||||
|
||||
public static function loadValidatorMetadata(ClassMetadata $metadata): void
|
||||
{
|
||||
$metadata->addPropertyConstraint(
|
||||
'name',
|
||||
new NotBlank(
|
||||
[
|
||||
'message' => 'mautic.core.name.required',
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
$metadata->addConstraint(new Callback(
|
||||
function (Sms $sms, ExecutionContextInterface $context): void {
|
||||
$type = $sms->getSmsType();
|
||||
if ('list' == $type) {
|
||||
$validator = $context->getValidator();
|
||||
$violations = $validator->validate(
|
||||
$sms->getLists(),
|
||||
[
|
||||
new NotBlank(
|
||||
[
|
||||
'message' => 'mautic.lead.lists.required',
|
||||
]
|
||||
),
|
||||
new LeadListAccess(),
|
||||
]
|
||||
);
|
||||
|
||||
foreach ($violations as $violation) {
|
||||
$context->buildViolation($violation->getMessage())
|
||||
->atPath('lists')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
},
|
||||
));
|
||||
|
||||
$metadata->addConstraint(new EntityEvent());
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the metadata for API usage.
|
||||
*/
|
||||
public static function loadApiMetadata(ApiMetadataDriver $metadata): void
|
||||
{
|
||||
$metadata->setGroupPrefix('sms')
|
||||
->addListProperties(
|
||||
[
|
||||
'id',
|
||||
'name',
|
||||
'message',
|
||||
'language',
|
||||
'category',
|
||||
]
|
||||
)
|
||||
->addProperties(
|
||||
[
|
||||
'publishUp',
|
||||
'publishDown',
|
||||
'sentCount',
|
||||
]
|
||||
)
|
||||
->build();
|
||||
|
||||
self::addProjectsInLoadApiMetadata($metadata, 'sms');
|
||||
}
|
||||
|
||||
protected function isChanged($prop, $val)
|
||||
{
|
||||
$getter = 'get'.ucfirst($prop);
|
||||
$current = $this->$getter();
|
||||
|
||||
if ('category' == $prop || 'list' == $prop) {
|
||||
$currentId = ($current) ? $current->getId() : '';
|
||||
$newId = ($val) ? $val->getId() : null;
|
||||
if ($currentId != $newId) {
|
||||
$this->changes[$prop] = [$currentId, $newId];
|
||||
}
|
||||
} else {
|
||||
parent::isChanged($prop, $val);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->isChanged('name', $name);
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription()
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $description
|
||||
*/
|
||||
public function setDescription($description): void
|
||||
{
|
||||
$this->isChanged('description', $description);
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCategory()
|
||||
{
|
||||
return $this->category;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setCategory($category)
|
||||
{
|
||||
$this->isChanged('category', $category);
|
||||
$this->category = $category;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMessage()
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*/
|
||||
public function setMessage($message): void
|
||||
{
|
||||
$this->isChanged('message', $message);
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPublishDown()
|
||||
{
|
||||
return $this->publishDown;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setPublishDown($publishDown)
|
||||
{
|
||||
$this->isChanged('publishDown', $publishDown);
|
||||
$this->publishDown = $publishDown;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPublishUp()
|
||||
{
|
||||
return $this->publishUp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setPublishUp($publishUp)
|
||||
{
|
||||
$this->isChanged('publishUp', $publishUp);
|
||||
$this->publishUp = $publishUp;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSentCount(bool $includeVariants = false): mixed
|
||||
{
|
||||
return ($includeVariants) ? $this->getAccumulativeTranslationCount('getSentCount') : $this->sentCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setSentCount($sentCount)
|
||||
{
|
||||
$this->sentCount = $sentCount;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLists()
|
||||
{
|
||||
return $this->lists;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Sms
|
||||
*/
|
||||
public function addList(LeadList $list)
|
||||
{
|
||||
$this->lists[] = $list;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeList(LeadList $list): void
|
||||
{
|
||||
$this->lists->removeElement($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getStats()
|
||||
{
|
||||
return $this->stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSmsType()
|
||||
{
|
||||
return $this->smsType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $smsType
|
||||
*/
|
||||
public function setSmsType($smsType): void
|
||||
{
|
||||
$this->isChanged('smsType', $smsType);
|
||||
$this->smsType = $smsType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $pendingCount
|
||||
*
|
||||
* @return Sms
|
||||
*/
|
||||
public function setPendingCount($pendingCount)
|
||||
{
|
||||
$this->pendingCount = $pendingCount;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getPendingCount()
|
||||
{
|
||||
return $this->pendingCount;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\SmsBundle\Entity;
|
||||
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\Tools\Pagination\Paginator;
|
||||
use Mautic\CoreBundle\Entity\CommonRepository;
|
||||
use Mautic\ProjectBundle\Entity\ProjectRepositoryTrait;
|
||||
|
||||
/**
|
||||
* @extends CommonRepository<Sms>
|
||||
*/
|
||||
class SmsRepository extends CommonRepository
|
||||
{
|
||||
use ProjectRepositoryTrait;
|
||||
|
||||
/**
|
||||
* Get a list of entities.
|
||||
*
|
||||
* @return Paginator
|
||||
*/
|
||||
public function getEntities(array $args = [])
|
||||
{
|
||||
$q = $this->_em
|
||||
->createQueryBuilder()
|
||||
->select($this->getTableAlias())
|
||||
->from(Sms::class, $this->getTableAlias(), $this->getTableAlias().'.id');
|
||||
|
||||
if (empty($args['iterable_mode'])) {
|
||||
$q->leftJoin($this->getTableAlias().'.category', 'c');
|
||||
}
|
||||
|
||||
$args['qb'] = $q;
|
||||
|
||||
return parent::getEntities($args);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<Sms>
|
||||
*/
|
||||
public function getPublishedBroadcastsIterable(?int $id = null): iterable
|
||||
{
|
||||
return $this->getPublishedBroadcastsQuery($id)->toIterable();
|
||||
}
|
||||
|
||||
private function getPublishedBroadcastsQuery(?int $id = null): Query
|
||||
{
|
||||
$qb = $this->createQueryBuilder($this->getTableAlias());
|
||||
$expr = $this->getPublishedByDateExpression($qb, null, true, true, false);
|
||||
|
||||
$expr->add(
|
||||
$qb->expr()->eq($this->getTableAlias().'.smsType', $qb->expr()->literal('list'))
|
||||
);
|
||||
|
||||
if (null !== $id && 0 !== $id) {
|
||||
$expr->add(
|
||||
$qb->expr()->eq($this->getTableAlias().'.id', (int) $id)
|
||||
);
|
||||
}
|
||||
$qb->where($expr);
|
||||
|
||||
return $qb->getQuery();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Doctrine\DBAL\Query\QueryBuilder
|
||||
*/
|
||||
public function getSegmentsContactsQuery(int $smsId)
|
||||
{
|
||||
// Main query
|
||||
$q = $this->getEntityManager()->getConnection()->createQueryBuilder();
|
||||
$q->from(MAUTIC_TABLE_PREFIX.'sms_message_list_xref', 'sml')
|
||||
->join('sml', MAUTIC_TABLE_PREFIX.'lead_lists', 'll', 'll.id = sml.leadlist_id and ll.is_published = 1')
|
||||
->join('ll', MAUTIC_TABLE_PREFIX.'lead_lists_leads', 'lll', 'lll.leadlist_id = sml.leadlist_id and lll.manually_removed = 0')
|
||||
->join('lll', MAUTIC_TABLE_PREFIX.'leads', 'l', 'lll.lead_id = l.id')
|
||||
->where(
|
||||
$q->expr()->and(
|
||||
$q->expr()->eq('sml.sms_id', ':smsId')
|
||||
)
|
||||
)
|
||||
->setParameter('smsId', $smsId)
|
||||
// Order by ID so we can query by greater than X contact ID when batching
|
||||
->orderBy('lll.lead_id');
|
||||
|
||||
return $q;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get amounts of sent and read emails.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getSentCount()
|
||||
{
|
||||
$q = $this->_em->createQueryBuilder();
|
||||
$q->select('SUM(e.sentCount) as sent_count')
|
||||
->from(Sms::class, 'e');
|
||||
$results = $q->getQuery()->getSingleResult(Query::HYDRATE_ARRAY);
|
||||
|
||||
if (!isset($results['sent_count'])) {
|
||||
$results['sent_count'] = 0;
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Doctrine\ORM\QueryBuilder|\Doctrine\DBAL\Query\QueryBuilder $q
|
||||
*/
|
||||
protected function addSearchCommandWhereClause($q, $filter): array
|
||||
{
|
||||
[$expr, $parameters] = $this->addStandardSearchCommandWhereClause($q, $filter);
|
||||
if ($expr) {
|
||||
return [$expr, $parameters];
|
||||
}
|
||||
|
||||
$command = $filter->command;
|
||||
$unique = $this->generateRandomParameterName();
|
||||
$returnParameter = false; // returning a parameter that is not used will lead to a Doctrine error
|
||||
|
||||
switch ($command) {
|
||||
case $this->translator->trans('mautic.core.searchcommand.lang'):
|
||||
$langUnique = $this->generateRandomParameterName();
|
||||
$langValue = $filter->string.'_%';
|
||||
$forceParameters = [
|
||||
$langUnique => $langValue,
|
||||
$unique => $filter->string,
|
||||
];
|
||||
$expr = $q->expr()->or(
|
||||
$q->expr()->eq('e.language', ":$unique"),
|
||||
$q->expr()->like('e.language', ":$langUnique")
|
||||
);
|
||||
$returnParameter = true;
|
||||
break;
|
||||
case $this->translator->trans('mautic.project.searchcommand.name'):
|
||||
case $this->translator->trans('mautic.project.searchcommand.name', [], null, 'en_US'):
|
||||
return $this->handleProjectFilter(
|
||||
$this->_em->getConnection()->createQueryBuilder(),
|
||||
'sms_id',
|
||||
'sms_projects_xref',
|
||||
$this->getTableAlias(),
|
||||
$filter->string,
|
||||
$filter->not
|
||||
);
|
||||
}
|
||||
|
||||
if ($expr && $filter->not) {
|
||||
$expr = $q->expr()->not($expr);
|
||||
}
|
||||
|
||||
if (!empty($forceParameters)) {
|
||||
$parameters = $forceParameters;
|
||||
} elseif ($returnParameter) {
|
||||
$string = ($filter->strict) ? $filter->string : "%{$filter->string}%";
|
||||
$parameters = ["$unique" => $string];
|
||||
}
|
||||
|
||||
return [$expr, $parameters];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getSearchCommands(): array
|
||||
{
|
||||
$commands = [
|
||||
'mautic.core.searchcommand.ispublished',
|
||||
'mautic.core.searchcommand.isunpublished',
|
||||
'mautic.core.searchcommand.isuncategorized',
|
||||
'mautic.core.searchcommand.ismine',
|
||||
'mautic.core.searchcommand.category',
|
||||
'mautic.core.searchcommand.lang',
|
||||
'mautic.project.searchcommand.name',
|
||||
];
|
||||
|
||||
return array_merge($commands, parent::getSearchCommands());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<array<string>>
|
||||
*/
|
||||
protected function getDefaultOrder(): array
|
||||
{
|
||||
return [
|
||||
['e.name', 'ASC'],
|
||||
];
|
||||
}
|
||||
|
||||
public function getTableAlias(): string
|
||||
{
|
||||
return 'e';
|
||||
}
|
||||
|
||||
/**
|
||||
* Up the click/sent counts.
|
||||
*
|
||||
* @param string $type
|
||||
* @param int $increaseBy
|
||||
*/
|
||||
public function upCount($id, $type = 'sent', $increaseBy = 1): void
|
||||
{
|
||||
try {
|
||||
$q = $this->_em->getConnection()->createQueryBuilder();
|
||||
|
||||
$q->update(MAUTIC_TABLE_PREFIX.'sms_messages')
|
||||
->set($type.'_count', $type.'_count + '.(int) $increaseBy)
|
||||
->where('id = '.(int) $id);
|
||||
|
||||
$q->executeStatement();
|
||||
} catch (\Exception) {
|
||||
// not important
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int> $ignoreIds
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getSmsList(
|
||||
mixed $search = '',
|
||||
int $limit = 10,
|
||||
int $start = 0,
|
||||
bool $viewOther = false,
|
||||
?string $smsType = null,
|
||||
?string $topLevel = null,
|
||||
array $ignoreIds = [],
|
||||
) {
|
||||
$q = $this->createQueryBuilder('e');
|
||||
$q->select('partial e.{id, name, language}');
|
||||
|
||||
if (!empty($search)) {
|
||||
if (is_array($search)) {
|
||||
$search = array_map('intval', $search);
|
||||
$q->andWhere($q->expr()->in('e.id', ':search'))
|
||||
->setParameter('search', $search);
|
||||
} else {
|
||||
$q->andWhere($q->expr()->like('e.name', ':search'))
|
||||
->setParameter('search', "%{$search}%");
|
||||
}
|
||||
}
|
||||
|
||||
if (!$viewOther) {
|
||||
$q->andWhere($q->expr()->eq('e.createdBy', ':id'))
|
||||
->setParameter('id', $this->currentUser->getId());
|
||||
}
|
||||
|
||||
if (!empty($smsType)) {
|
||||
$q->andWhere(
|
||||
$q->expr()->eq('e.smsType', $q->expr()->literal($smsType))
|
||||
);
|
||||
}
|
||||
|
||||
if ('translation' === $topLevel) {
|
||||
$q->andWhere($q->expr()->isNull('e.translationParent'));
|
||||
}
|
||||
|
||||
if (!empty($ignoreIds)) {
|
||||
$q->andWhere($q->expr()->notIn('e.id', ':smsIds'))
|
||||
->setParameter('smsIds', $ignoreIds);
|
||||
}
|
||||
|
||||
$q->orderBy('e.name');
|
||||
|
||||
if (!empty($limit)) {
|
||||
$q->setFirstResult($start)
|
||||
->setMaxResults($limit);
|
||||
}
|
||||
|
||||
return $q->getQuery()->getArrayResult();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,383 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\SmsBundle\Entity;
|
||||
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver;
|
||||
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
|
||||
use Mautic\CoreBundle\Entity\IpAddress;
|
||||
use Mautic\LeadBundle\Entity\Lead;
|
||||
use Mautic\LeadBundle\Entity\LeadList;
|
||||
|
||||
class Stat
|
||||
{
|
||||
public const TABLE_NAME = 'sms_message_stats';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @var Sms|null
|
||||
*/
|
||||
private $sms;
|
||||
|
||||
/**
|
||||
* @var Lead|null
|
||||
*/
|
||||
private $lead;
|
||||
|
||||
/**
|
||||
* @var LeadList|null
|
||||
*/
|
||||
private $list;
|
||||
|
||||
/**
|
||||
* @var IpAddress|null
|
||||
*/
|
||||
private $ipAddress;
|
||||
|
||||
/**
|
||||
* @var \DateTimeInterface
|
||||
*/
|
||||
private $dateSent;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $trackingHash;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $source;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
private $sourceId;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $tokens = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $details = [];
|
||||
|
||||
/**
|
||||
* @var bool|null
|
||||
*/
|
||||
private $isFailed = false;
|
||||
|
||||
public static function loadMetadata(ORM\ClassMetadata $metadata): void
|
||||
{
|
||||
$builder = new ClassMetadataBuilder($metadata);
|
||||
|
||||
$builder->setTable(self::TABLE_NAME)
|
||||
->setCustomRepositoryClass(StatRepository::class)
|
||||
->addIndex(['sms_id', 'lead_id'], 'stat_sms_search')
|
||||
->addIndex(['tracking_hash'], 'stat_sms_hash_search')
|
||||
->addIndex(['source', 'source_id'], 'stat_sms_source_search')
|
||||
->addIndex(['is_failed'], 'stat_sms_failed_search');
|
||||
|
||||
$builder->addBigIntIdField();
|
||||
|
||||
$builder->createManyToOne('sms', 'Sms')
|
||||
->inversedBy('stats')
|
||||
->addJoinColumn('sms_id', 'id', true, false, 'SET NULL')
|
||||
->build();
|
||||
|
||||
$builder->addLead(true, 'SET NULL');
|
||||
|
||||
$builder->createManyToOne('list', LeadList::class)
|
||||
->addJoinColumn('list_id', 'id', true, false, 'SET NULL')
|
||||
->build();
|
||||
|
||||
$builder->addIpAddress(true);
|
||||
|
||||
$builder->createField('dateSent', 'datetime')
|
||||
->columnName('date_sent')
|
||||
->build();
|
||||
|
||||
$builder->createField('isFailed', 'boolean')
|
||||
->columnName('is_failed')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createField('trackingHash', 'string')
|
||||
->columnName('tracking_hash')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createField('source', 'string')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createField('sourceId', 'integer')
|
||||
->columnName('source_id')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createField('tokens', 'array')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->addField('details', Types::JSON);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the metadata for API usage.
|
||||
*/
|
||||
public static function loadApiMetadata(ApiMetadataDriver $metadata): void
|
||||
{
|
||||
$metadata->setGroupPrefix('stat')
|
||||
->addProperties(
|
||||
[
|
||||
'id',
|
||||
'ipAddress',
|
||||
'dateSent',
|
||||
'isFailed',
|
||||
'source',
|
||||
'sourceId',
|
||||
'trackingHash',
|
||||
'lead',
|
||||
'sms',
|
||||
'details',
|
||||
]
|
||||
)
|
||||
->build();
|
||||
}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
return (int) $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Sms
|
||||
*/
|
||||
public function getSms()
|
||||
{
|
||||
return $this->sms;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Stat
|
||||
*/
|
||||
public function setSms(Sms $sms)
|
||||
{
|
||||
$this->sms = $sms;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Lead
|
||||
*/
|
||||
public function getLead()
|
||||
{
|
||||
return $this->lead;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Stat
|
||||
*/
|
||||
public function setLead(Lead $lead)
|
||||
{
|
||||
$this->lead = $lead;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return LeadList
|
||||
*/
|
||||
public function getList()
|
||||
{
|
||||
return $this->list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Stat
|
||||
*/
|
||||
public function setList(LeadList $list)
|
||||
{
|
||||
$this->list = $list;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return IpAddress
|
||||
*/
|
||||
public function getIpAddress()
|
||||
{
|
||||
return $this->ipAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Stat
|
||||
*/
|
||||
public function setIpAddress(IpAddress $ipAddress)
|
||||
{
|
||||
$this->ipAddress = $ipAddress;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \DateTimeInterface
|
||||
*/
|
||||
public function getDateSent()
|
||||
{
|
||||
return $this->dateSent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTime $dateSent
|
||||
*
|
||||
* @return Stat
|
||||
*/
|
||||
public function setDateSent($dateSent)
|
||||
{
|
||||
$this->dateSent = $dateSent;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTrackingHash()
|
||||
{
|
||||
return $this->trackingHash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $trackingHash
|
||||
*
|
||||
* @return Stat
|
||||
*/
|
||||
public function setTrackingHash($trackingHash)
|
||||
{
|
||||
$this->trackingHash = $trackingHash;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSource()
|
||||
{
|
||||
return $this->source;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $source
|
||||
*
|
||||
* @return Stat
|
||||
*/
|
||||
public function setSource($source)
|
||||
{
|
||||
$this->source = $source;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getSourceId()
|
||||
{
|
||||
return $this->sourceId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $sourceId
|
||||
*
|
||||
* @return Stat
|
||||
*/
|
||||
public function setSourceId($sourceId)
|
||||
{
|
||||
$this->sourceId = $sourceId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getTokens()
|
||||
{
|
||||
return $this->tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Stat
|
||||
*/
|
||||
public function setTokens(array $tokens)
|
||||
{
|
||||
$this->tokens = $tokens;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $isFailed
|
||||
*
|
||||
* @return Stat
|
||||
*/
|
||||
public function setIsFailed($isFailed)
|
||||
{
|
||||
$this->isFailed = $isFailed;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isFailed()
|
||||
{
|
||||
return $this->isFailed;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getDetails()
|
||||
{
|
||||
return $this->details;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $details
|
||||
*
|
||||
* @return Stat
|
||||
*/
|
||||
public function setDetails($details)
|
||||
{
|
||||
$this->details = $details;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string $detail
|
||||
*
|
||||
* @return Stat
|
||||
*/
|
||||
public function addDetail($type, $detail)
|
||||
{
|
||||
$this->details[$type][] = $detail;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\SmsBundle\Entity;
|
||||
|
||||
use Mautic\CoreBundle\Entity\CommonRepository;
|
||||
use Mautic\CoreBundle\Helper\DateTimeHelper;
|
||||
use Mautic\LeadBundle\Entity\TimelineTrait;
|
||||
|
||||
/**
|
||||
* @extends CommonRepository<Stat>
|
||||
*/
|
||||
class StatRepository extends CommonRepository
|
||||
{
|
||||
use TimelineTrait;
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \Doctrine\ORM\NoResultException
|
||||
* @throws \Doctrine\ORM\NonUniqueResultException
|
||||
*/
|
||||
public function getSmsStatus($trackingHash)
|
||||
{
|
||||
$q = $this->createQueryBuilder('s');
|
||||
$q->select('s')
|
||||
->leftJoin('s.lead', 'l')
|
||||
->leftJoin('s.sms', 'e')
|
||||
->where(
|
||||
$q->expr()->eq('s.trackingHash', ':hash')
|
||||
)
|
||||
->setParameter('hash', $trackingHash);
|
||||
|
||||
$result = $q->getQuery()->getResult();
|
||||
|
||||
return (!empty($result)) ? $result[0] : null;
|
||||
}
|
||||
|
||||
public function getSentStats($smsId, $listId = null): array
|
||||
{
|
||||
$q = $this->_em->getConnection()->createQueryBuilder();
|
||||
$q->select('s.lead_id')
|
||||
->from(MAUTIC_TABLE_PREFIX.'sms_messages_stats', 's')
|
||||
->where('s.sms_id = :sms')
|
||||
->setParameter('sms', $smsId);
|
||||
|
||||
if ($listId) {
|
||||
$q->andWhere('s.list_id = :list')
|
||||
->setParameter('list', $listId);
|
||||
}
|
||||
|
||||
$result = $q->executeQuery()->fetchAllAssociative();
|
||||
|
||||
// index by lead
|
||||
$stats = [];
|
||||
foreach ($result as $r) {
|
||||
$stats[$r['lead_id']] = $r['lead_id'];
|
||||
}
|
||||
|
||||
unset($result);
|
||||
|
||||
return $stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|array $smsIds
|
||||
* @param int $listId
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getSentCount($smsIds = null, $listId = null)
|
||||
{
|
||||
$q = $this->_em->getConnection()->createQueryBuilder();
|
||||
|
||||
$q->select('count(s.id) as sent_count')
|
||||
->from(MAUTIC_TABLE_PREFIX.'sms_message_stats', 's');
|
||||
|
||||
if ($smsIds) {
|
||||
if (!is_array($smsIds)) {
|
||||
$smsIds = [(int) $smsIds];
|
||||
}
|
||||
$q->where(
|
||||
$q->expr()->in('s.sms_id', $smsIds)
|
||||
);
|
||||
}
|
||||
|
||||
if ($listId) {
|
||||
$q->andWhere('s.list_id = '.(int) $listId);
|
||||
}
|
||||
|
||||
$q->andWhere('s.is_failed = :false')
|
||||
->setParameter('false', false, 'boolean');
|
||||
|
||||
$results = $q->executeQuery()->fetchAllAssociative();
|
||||
|
||||
return (isset($results[0])) ? $results[0]['sent_count'] : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a lead's email stat.
|
||||
*
|
||||
* @param int $leadId
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Doctrine\ORM\NoResultException
|
||||
* @throws \Doctrine\ORM\NonUniqueResultException
|
||||
*/
|
||||
public function getLeadStats($leadId, array $options = [])
|
||||
{
|
||||
$query = $this->getEntityManager()->getConnection()->createQueryBuilder();
|
||||
$query->from(MAUTIC_TABLE_PREFIX.'sms_message_stats', 's')
|
||||
->leftJoin('s', MAUTIC_TABLE_PREFIX.'sms_messages', 'e', 's.sms_id = e.id');
|
||||
|
||||
if ($leadId) {
|
||||
$query->andWhere(
|
||||
$query->expr()->eq('s.lead_id', ':leadId')
|
||||
)->setParameter('leadId', $leadId);
|
||||
}
|
||||
|
||||
if (!empty($options['basic_select'])) {
|
||||
$query->select(
|
||||
's.sms_id, s.id, s.date_sent as dateSent, e.name, e.name as sms_name, s.is_failed as isFailed'
|
||||
);
|
||||
} else {
|
||||
$query->select(
|
||||
's.sms_id, s.id, s.date_sent as dateSent, e.name, e.name as sms_name, e.message, e.sms_type as type, s.is_failed as isFailed, s.list_id, l.name as list_name, s.tracking_hash as idHash, s.lead_id, s.details'
|
||||
)
|
||||
->leftJoin('s', MAUTIC_TABLE_PREFIX.'lead_lists', 'l', 's.list_id = l.id');
|
||||
}
|
||||
|
||||
if (isset($options['state'])) {
|
||||
$state = $options['state'];
|
||||
if ('failed' == $state) {
|
||||
$query->andWhere(
|
||||
$query->expr()->eq('s.is_failed', 1)
|
||||
);
|
||||
}
|
||||
}
|
||||
$state = 'sent';
|
||||
|
||||
if (isset($options['search']) && $options['search']) {
|
||||
$query->andWhere(
|
||||
$query->expr()->or(
|
||||
$query->expr()->like('e.name', $query->expr()->literal('%'.$options['search'].'%'))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($options['fromDate']) && $options['fromDate']) {
|
||||
$dt = new DateTimeHelper($options['fromDate']);
|
||||
$query->andWhere(
|
||||
$query->expr()->gte('s.date_sent', $query->expr()->literal($dt->toUtcString()))
|
||||
);
|
||||
}
|
||||
|
||||
return $this->getTimelineResults(
|
||||
$query,
|
||||
$options,
|
||||
'e.name',
|
||||
's.date_'.$state
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates lead ID (e.g. after a lead merge).
|
||||
*/
|
||||
public function updateLead($fromLeadId, $toLeadId): void
|
||||
{
|
||||
$q = $this->_em->getConnection()->createQueryBuilder();
|
||||
$q->update(MAUTIC_TABLE_PREFIX.'sms_message_stats')
|
||||
->set('sms_id', (int) $toLeadId)
|
||||
->where('sms_id = '.(int) $fromLeadId)
|
||||
->executeStatement();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a stat.
|
||||
*/
|
||||
public function deleteStat($id): void
|
||||
{
|
||||
$this->_em->getConnection()->delete(MAUTIC_TABLE_PREFIX.'sms_message_stats', ['id' => (int) $id]);
|
||||
}
|
||||
|
||||
public function getTableAlias(): string
|
||||
{
|
||||
return 's';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user