Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,673 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\NotificationBundle\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\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\LeadBundle\Entity\LeadList;
|
||||
use Mautic\LeadBundle\Form\Validator\Constraints\LeadListAccess;
|
||||
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('notification:notifications:viewown')"),
|
||||
new Post(security: "is_granted('notification:notifications:create')"),
|
||||
new Get(security: "is_granted('notification:notifications:viewown')"),
|
||||
new Put(security: "is_granted('notification:notifications:editown')"),
|
||||
new Patch(security: "is_granted('notification:notifications:editother')"),
|
||||
new Delete(security: "is_granted('notification:notifications:deleteown')"),
|
||||
],
|
||||
normalizationContext: [
|
||||
'groups' => ['notification:read'],
|
||||
'swagger_definition_name' => 'Read',
|
||||
'api_included' => ['category'],
|
||||
],
|
||||
denormalizationContext: [
|
||||
'groups' => ['notification:write'],
|
||||
'swagger_definition_name' => 'Write',
|
||||
]
|
||||
)]
|
||||
class Notification extends FormEntity implements UuidInterface, TranslationEntityInterface
|
||||
{
|
||||
use UuidTrait;
|
||||
use TranslationEntityTrait;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
#[Groups(['notification:read'])]
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
#[Groups(['notification:read', 'notification:write'])]
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
#[Groups(['notification:read', 'notification:write'])]
|
||||
private $description;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
#[Groups(['notification:read', 'notification:write'])]
|
||||
private $url;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
#[Groups(['notification:read', 'notification:write'])]
|
||||
private $heading;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
#[Groups(['notification:read', 'notification:write'])]
|
||||
private $message;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
#[Groups(['notification:read', 'notification:write'])]
|
||||
private $button;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
#[Groups(['notification:read', 'notification:write'])]
|
||||
private $utmTags = [];
|
||||
|
||||
/**
|
||||
* @var \DateTimeInterface
|
||||
*/
|
||||
#[Groups(['notification:read', 'notification:write'])]
|
||||
private $publishUp;
|
||||
|
||||
/**
|
||||
* @var \DateTimeInterface
|
||||
*/
|
||||
#[Groups(['notification:read', 'notification:write'])]
|
||||
private $publishDown;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
#[Groups(['notification:read'])]
|
||||
private $readCount = 0;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
#[Groups(['notification:read'])]
|
||||
private $sentCount = 0;
|
||||
|
||||
/**
|
||||
* @var \Mautic\CategoryBundle\Entity\Category|null
|
||||
**/
|
||||
#[Groups(['notification:read', 'notification:write'])]
|
||||
private $category;
|
||||
|
||||
/**
|
||||
* @var ArrayCollection<int, LeadList>
|
||||
*/
|
||||
#[Groups(['notification:read', 'notification:write'])]
|
||||
private $lists;
|
||||
|
||||
/**
|
||||
* @var ArrayCollection<int, Stat>
|
||||
*/
|
||||
private $stats;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
#[Groups(['notification:read', 'notification:write'])]
|
||||
private $notificationType = 'template';
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
#[Groups(['notification:read', 'notification:write'])]
|
||||
private $mobile = false;
|
||||
|
||||
/**
|
||||
* @var ?array
|
||||
*/
|
||||
#[Groups(['notification:read', 'notification:write'])]
|
||||
private $mobileSettings;
|
||||
|
||||
public function __clone()
|
||||
{
|
||||
$this->id = null;
|
||||
$this->stats = new ArrayCollection();
|
||||
$this->sentCount = 0;
|
||||
$this->readCount = 0;
|
||||
|
||||
parent::__clone();
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->lists = new ArrayCollection();
|
||||
$this->stats = new ArrayCollection();
|
||||
$this->translationChildren = new ArrayCollection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear stats.
|
||||
*/
|
||||
public function clearStats(): void
|
||||
{
|
||||
$this->stats = new ArrayCollection();
|
||||
}
|
||||
|
||||
public static function loadMetadata(ORM\ClassMetadata $metadata): void
|
||||
{
|
||||
$builder = new ClassMetadataBuilder($metadata);
|
||||
|
||||
$builder->setTable('push_notifications')
|
||||
->setCustomRepositoryClass(NotificationRepository::class);
|
||||
|
||||
$builder->addIdColumns();
|
||||
|
||||
$builder->createField('url', 'text')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createField('heading', 'text')
|
||||
->build();
|
||||
|
||||
$builder->createField('message', 'text')
|
||||
->build();
|
||||
|
||||
$builder->createField('button', 'text')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createField('utmTags', 'array')
|
||||
->columnName('utm_tags')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createField('notificationType', 'text')
|
||||
->columnName('notification_type')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->addPublishDates();
|
||||
|
||||
$builder->createField('readCount', 'integer')
|
||||
->columnName('read_count')
|
||||
->build();
|
||||
|
||||
$builder->createField('sentCount', 'integer')
|
||||
->columnName('sent_count')
|
||||
->build();
|
||||
|
||||
$builder->addCategory();
|
||||
|
||||
$builder->createManyToMany('lists', LeadList::class)
|
||||
->setJoinTable('push_notification_list_xref')
|
||||
->setIndexBy('id')
|
||||
->addInverseJoinColumn('leadlist_id', 'id', false, false, 'CASCADE')
|
||||
->addJoinColumn('notification_id', 'id', false, false, 'CASCADE')
|
||||
->fetchExtraLazy()
|
||||
->build();
|
||||
|
||||
$builder->createOneToMany('stats', 'Stat')
|
||||
->setIndexBy('id')
|
||||
->mappedBy('notification')
|
||||
->cascadePersist()
|
||||
->fetchExtraLazy()
|
||||
->build();
|
||||
|
||||
$builder->createField('mobile', 'boolean')->build();
|
||||
|
||||
$builder->createField('mobileSettings', 'array')->build();
|
||||
|
||||
static::addUuidField($builder);
|
||||
|
||||
self::addTranslationMetadata($builder, self::class);
|
||||
}
|
||||
|
||||
public static function loadValidatorMetadata(ClassMetadata $metadata): void
|
||||
{
|
||||
$metadata->addPropertyConstraint(
|
||||
'name',
|
||||
new NotBlank(
|
||||
[
|
||||
'message' => 'mautic.core.name.required',
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
$metadata->addPropertyConstraint(
|
||||
'heading',
|
||||
new NotBlank(
|
||||
[
|
||||
'message' => 'mautic.core.heading.required',
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
$metadata->addPropertyConstraint(
|
||||
'message',
|
||||
new NotBlank(
|
||||
[
|
||||
'message' => 'mautic.core.message.required',
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
$metadata->addConstraint(new Callback(
|
||||
function (Notification $notification, ExecutionContextInterface $context): void {
|
||||
$type = $notification->getNotificationType();
|
||||
if ('list' == $type) {
|
||||
$validator = $context->getValidator();
|
||||
$violations = $validator->validate(
|
||||
$notification->getLists(),
|
||||
[
|
||||
new LeadListAccess(
|
||||
[
|
||||
'message' => 'mautic.lead.lists.required',
|
||||
]
|
||||
),
|
||||
new NotBlank(
|
||||
[
|
||||
'message' => 'mautic.lead.lists.required',
|
||||
]
|
||||
),
|
||||
]
|
||||
);
|
||||
|
||||
if (count($violations) > 0) {
|
||||
$string = (string) $violations;
|
||||
$context->buildViolation($string)
|
||||
->atPath('lists')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the metadata for API usage.
|
||||
*/
|
||||
public static function loadApiMetadata(ApiMetadataDriver $metadata): void
|
||||
{
|
||||
$metadata->setGroupPrefix('notification')
|
||||
->addListProperties(
|
||||
[
|
||||
'id',
|
||||
'name',
|
||||
'heading',
|
||||
'message',
|
||||
'url',
|
||||
'language',
|
||||
'category',
|
||||
'button',
|
||||
]
|
||||
)
|
||||
->addProperties(
|
||||
[
|
||||
'utmTags',
|
||||
'publishUp',
|
||||
'publishDown',
|
||||
'readCount',
|
||||
'sentCount',
|
||||
]
|
||||
)
|
||||
->build();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get id.
|
||||
*
|
||||
* @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 getHeading()
|
||||
{
|
||||
return $this->heading;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $heading
|
||||
*/
|
||||
public function setHeading($heading): void
|
||||
{
|
||||
$this->isChanged('heading', $heading);
|
||||
$this->heading = $heading;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getButton()
|
||||
{
|
||||
return $this->button;
|
||||
}
|
||||
|
||||
public function setButton($button): void
|
||||
{
|
||||
$this->isChanged('button', $button);
|
||||
$this->button = $button;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMessage()
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*/
|
||||
public function setMessage($message): void
|
||||
{
|
||||
$this->isChanged('message', $message);
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getUtmTags()
|
||||
{
|
||||
return $this->utmTags;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $utmTags
|
||||
*/
|
||||
public function setUtmTags($utmTags)
|
||||
{
|
||||
$this->isChanged('utmTags', $utmTags);
|
||||
$this->utmTags = $utmTags;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl()
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
*/
|
||||
public function setUrl($url): void
|
||||
{
|
||||
$this->isChanged('url', $url);
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getReadCount()
|
||||
{
|
||||
return $this->readCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setReadCount($readCount)
|
||||
{
|
||||
$this->readCount = $readCount;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add list.
|
||||
*
|
||||
* @return Notification
|
||||
*/
|
||||
public function addList(LeadList $list)
|
||||
{
|
||||
$this->lists[] = $list;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove list.
|
||||
*/
|
||||
public function removeList(LeadList $list): void
|
||||
{
|
||||
$this->lists->removeElement($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getStats()
|
||||
{
|
||||
return $this->stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getNotificationType()
|
||||
{
|
||||
return $this->notificationType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $notificationType
|
||||
*/
|
||||
public function setNotificationType($notificationType): void
|
||||
{
|
||||
$this->isChanged('notificationType', $notificationType);
|
||||
$this->notificationType = $notificationType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isMobile()
|
||||
{
|
||||
return $this->mobile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $mobile
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setMobile($mobile)
|
||||
{
|
||||
$this->mobile = $mobile;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getMobileSettings()
|
||||
{
|
||||
return $this->mobileSettings ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setMobileSettings(array $mobileSettings)
|
||||
{
|
||||
$this->mobileSettings = $mobileSettings;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\NotificationBundle\Entity;
|
||||
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\Tools\Pagination\Paginator;
|
||||
use Mautic\CoreBundle\Entity\CommonRepository;
|
||||
|
||||
/**
|
||||
* @extends CommonRepository<Notification>
|
||||
*/
|
||||
class NotificationRepository extends CommonRepository
|
||||
{
|
||||
/**
|
||||
* Get a list of entities.
|
||||
*
|
||||
* @return Paginator
|
||||
*/
|
||||
public function getEntities(array $args = [])
|
||||
{
|
||||
$q = $this->_em
|
||||
->createQueryBuilder()
|
||||
->select('e')
|
||||
->from(Notification::class, 'e', 'e.id');
|
||||
if (empty($args['iterable_mode'])) {
|
||||
$q->leftJoin('e.category', 'c');
|
||||
}
|
||||
|
||||
$args['qb'] = $q;
|
||||
|
||||
return parent::getEntities($args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get amounts of sent and read notifications.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getSentReadCount()
|
||||
{
|
||||
$q = $this->_em->createQueryBuilder();
|
||||
$q->select('SUM(e.sentCount) as sent_count, SUM(e.readCount) as read_count')
|
||||
->from(Notification::class, 'e');
|
||||
$results = $q->getQuery()->getSingleResult(Query::HYDRATE_ARRAY);
|
||||
|
||||
if (!isset($results['sent_count'])) {
|
||||
$results['sent_count'] = 0;
|
||||
}
|
||||
if (!isset($results['read_count'])) {
|
||||
$results['read_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'):
|
||||
case $this->translator->trans('mautic.core.searchcommand.lang', [], null, 'en_US'):
|
||||
$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;
|
||||
}
|
||||
|
||||
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',
|
||||
];
|
||||
|
||||
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.'push_notifications')
|
||||
->set($type.'_count', $type.'_count + '.(int) $increaseBy)
|
||||
->where('id = '.(int) $id);
|
||||
|
||||
$q->executeStatement();
|
||||
} catch (\Exception) {
|
||||
// not important
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $search
|
||||
* @param int $limit
|
||||
* @param int $start
|
||||
* @param bool $viewOther
|
||||
* @param string $notificationType
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getNotificationList($search = '', $limit = 10, $start = 0, $viewOther = false, $notificationType = null)
|
||||
{
|
||||
$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($notificationType)) {
|
||||
$q->andWhere(
|
||||
$q->expr()->eq('e.notificationType', $q->expr()->literal($notificationType))
|
||||
);
|
||||
}
|
||||
|
||||
$q->andWhere('e.mobile != 1');
|
||||
|
||||
$q->orderBy('e.name');
|
||||
|
||||
if (!empty($limit)) {
|
||||
$q->setFirstResult($start)
|
||||
->setMaxResults($limit);
|
||||
}
|
||||
|
||||
return $q->getQuery()->getArrayResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array<mixed> $search
|
||||
* @param array<mixed> $options
|
||||
*
|
||||
* @return array<int, array<string, int|string>>
|
||||
*/
|
||||
public function getMobileNotificationList(string|array $search = '', int $limit = 10, int $start = 0, bool $viewOther = false, array $options = []): array
|
||||
{
|
||||
$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($options['notification_type'])) {
|
||||
$q->andWhere(
|
||||
$q->expr()->eq('e.notificationType', $q->expr()->literal($options['notification_type']))
|
||||
);
|
||||
}
|
||||
|
||||
if (!empty($options['top_level']) && 'translation' === $options['top_level']) {
|
||||
$q->andWhere($q->expr()->isNull('e.translationParent'));
|
||||
}
|
||||
|
||||
if (!empty($options['ignore_ids'])) {
|
||||
$q->andWhere($q->expr()->notIn('e.id', $options['ignore_ids']));
|
||||
}
|
||||
|
||||
$q->andWhere('e.mobile = 1');
|
||||
|
||||
$q->orderBy('e.name');
|
||||
|
||||
if (!empty($limit)) {
|
||||
$q->setFirstResult($start)
|
||||
->setMaxResults($limit);
|
||||
}
|
||||
|
||||
return $q->getQuery()->getArrayResult();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\NotificationBundle\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
|
||||
use Mautic\LeadBundle\Entity\Lead;
|
||||
|
||||
class PushID
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @var Lead|null
|
||||
*/
|
||||
private $lead;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $pushID;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $enabled;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $mobile;
|
||||
|
||||
public static function loadMetadata(ORM\ClassMetadata $metadata): void
|
||||
{
|
||||
$builder = new ClassMetadataBuilder($metadata);
|
||||
|
||||
$builder->setTable('push_ids')
|
||||
->setCustomRepositoryClass(PushIDRepository::class);
|
||||
|
||||
$builder->createField('id', 'integer')
|
||||
->makePrimaryKey()
|
||||
->generatedValue()
|
||||
->build();
|
||||
|
||||
$builder->createField('pushID', 'string')
|
||||
->columnName('push_id')
|
||||
->nullable(false)
|
||||
->build();
|
||||
|
||||
$builder->createManyToOne('lead', Lead::class)
|
||||
->addJoinColumn('lead_id', 'id', true, false, 'SET NULL')
|
||||
->inversedBy('pushIds')
|
||||
->build();
|
||||
|
||||
$builder->createField('enabled', 'boolean')->build();
|
||||
$builder->createField('mobile', 'boolean')->build();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setId($id)
|
||||
{
|
||||
$this->id = $id;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Lead
|
||||
*/
|
||||
public function getLead()
|
||||
{
|
||||
return $this->lead;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setLead(Lead $lead)
|
||||
{
|
||||
$this->lead = $lead;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPushID()
|
||||
{
|
||||
return $this->pushID;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $pushID
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setPushID($pushID)
|
||||
{
|
||||
$this->pushID = $pushID;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEnabled()
|
||||
{
|
||||
return $this->enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setEnabled($enabled)
|
||||
{
|
||||
$this->enabled = $enabled;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isMobile()
|
||||
{
|
||||
return $this->mobile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $mobile
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setMobile($mobile)
|
||||
{
|
||||
$this->mobile = $mobile;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\NotificationBundle\Entity;
|
||||
|
||||
use Mautic\CoreBundle\Entity\CommonRepository;
|
||||
|
||||
/**
|
||||
* @extends CommonRepository<PushID>
|
||||
*/
|
||||
class PushIDRepository extends CommonRepository
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,495 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\NotificationBundle\Entity;
|
||||
|
||||
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;
|
||||
|
||||
class Stat
|
||||
{
|
||||
public const TABLE_NAME = 'push_notification_stats';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @var Notification|null
|
||||
*/
|
||||
private $notification;
|
||||
|
||||
/**
|
||||
* @var Lead|null
|
||||
*/
|
||||
private $lead;
|
||||
|
||||
/**
|
||||
* @var \Mautic\LeadBundle\Entity\LeadList|null
|
||||
*/
|
||||
private $list;
|
||||
|
||||
/**
|
||||
* @var IpAddress|null
|
||||
*/
|
||||
private $ipAddress;
|
||||
|
||||
/**
|
||||
* @var \DateTimeInterface
|
||||
*/
|
||||
private $dateSent;
|
||||
|
||||
/**
|
||||
* @var \DateTimeInterface
|
||||
*/
|
||||
private $dateRead;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $isClicked = false;
|
||||
|
||||
/**
|
||||
* @var \DateTimeInterface
|
||||
*/
|
||||
private $dateClicked;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $trackingHash;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
private $retryCount = 0;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $source;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
private $sourceId;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $tokens = [];
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
private $clickCount;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $clickDetails = [];
|
||||
|
||||
/**
|
||||
* @var \DateTimeInterface
|
||||
*/
|
||||
private $lastClicked;
|
||||
|
||||
public static function loadMetadata(ORM\ClassMetadata $metadata): void
|
||||
{
|
||||
$builder = new ClassMetadataBuilder($metadata);
|
||||
|
||||
$builder->setTable(self::TABLE_NAME)
|
||||
->setCustomRepositoryClass(StatRepository::class)
|
||||
->addIndex(['notification_id', 'lead_id'], 'stat_notification_search')
|
||||
->addIndex(['is_clicked'], 'stat_notification_clicked_search')
|
||||
->addIndex(['tracking_hash'], 'stat_notification_hash_search')
|
||||
->addIndex(['source', 'source_id'], 'stat_notification_source_search');
|
||||
|
||||
$builder->addBigIntIdField();
|
||||
|
||||
$builder->createManyToOne('notification', 'Notification')
|
||||
->inversedBy('stats')
|
||||
->addJoinColumn('notification_id', 'id', true, false, 'SET NULL')
|
||||
->build();
|
||||
|
||||
$builder->addLead(true, 'SET NULL');
|
||||
|
||||
$builder->createManyToOne('list', \Mautic\LeadBundle\Entity\LeadList::class)
|
||||
->addJoinColumn('list_id', 'id', true, false, 'SET NULL')
|
||||
->build();
|
||||
|
||||
$builder->addIpAddress(true);
|
||||
|
||||
$builder->createField('dateSent', 'datetime')
|
||||
->columnName('date_sent')
|
||||
->build();
|
||||
|
||||
$builder->createField('dateRead', 'datetime')
|
||||
->columnName('date_read')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createField('isClicked', 'boolean')
|
||||
->columnName('is_clicked')
|
||||
->build();
|
||||
|
||||
$builder->createField('dateClicked', 'datetime')
|
||||
->columnName('date_clicked')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createField('trackingHash', 'string')
|
||||
->columnName('tracking_hash')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createField('retryCount', 'integer')
|
||||
->columnName('retry_count')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createField('source', 'string')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createField('sourceId', 'integer')
|
||||
->columnName('source_id')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createField('tokens', 'array')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->addNullableField('clickCount', 'integer', 'click_count');
|
||||
|
||||
$builder->addNullableField('lastClicked', 'datetime', 'last_clicked');
|
||||
|
||||
$builder->addNullableField('clickDetails', 'array', 'click_details');
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the metadata for API usage.
|
||||
*/
|
||||
public static function loadApiMetadata(ApiMetadataDriver $metadata): void
|
||||
{
|
||||
$metadata->setGroupPrefix('stat')
|
||||
->addProperties(
|
||||
[
|
||||
'id',
|
||||
'ipAddress',
|
||||
'dateSent',
|
||||
'isClicked',
|
||||
'dateClicked',
|
||||
'retryCount',
|
||||
'source',
|
||||
'clickCount',
|
||||
'lastClicked',
|
||||
'sourceId',
|
||||
'trackingHash',
|
||||
'lead',
|
||||
'notification',
|
||||
]
|
||||
)
|
||||
->build();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDateClicked()
|
||||
{
|
||||
return $this->dateClicked;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $dateClicked
|
||||
*/
|
||||
public function setDateClicked($dateClicked): void
|
||||
{
|
||||
$this->dateClicked = $dateClicked;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDateSent()
|
||||
{
|
||||
return $this->dateSent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $dateSent
|
||||
*/
|
||||
public function setDateSent($dateSent): void
|
||||
{
|
||||
$this->dateSent = $dateSent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Notification
|
||||
*/
|
||||
public function getNotification()
|
||||
{
|
||||
return $this->notification;
|
||||
}
|
||||
|
||||
public function setNotification(?Notification $notification = null): void
|
||||
{
|
||||
$this->notification = $notification;
|
||||
}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
return (int) $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return IpAddress|null
|
||||
*/
|
||||
public function getIpAddress()
|
||||
{
|
||||
return $this->ipAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $ip
|
||||
*/
|
||||
public function setIpAddress(IpAddress $ip): void
|
||||
{
|
||||
$this->ipAddress = $ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getIsClicked()
|
||||
{
|
||||
return $this->isClicked;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $isClicked
|
||||
*/
|
||||
public function setIsClicked($isClicked): void
|
||||
{
|
||||
$this->isClicked = $isClicked;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Lead
|
||||
*/
|
||||
public function getLead()
|
||||
{
|
||||
return $this->lead;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $lead
|
||||
*/
|
||||
public function setLead(?Lead $lead = null): void
|
||||
{
|
||||
$this->lead = $lead;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTrackingHash()
|
||||
{
|
||||
return $this->trackingHash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $trackingHash
|
||||
*/
|
||||
public function setTrackingHash($trackingHash): void
|
||||
{
|
||||
$this->trackingHash = $trackingHash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Mautic\LeadBundle\Entity\LeadList
|
||||
*/
|
||||
public function getList()
|
||||
{
|
||||
return $this->list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $list
|
||||
*/
|
||||
public function setList($list): void
|
||||
{
|
||||
$this->list = $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getRetryCount()
|
||||
{
|
||||
return $this->retryCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $retryCount
|
||||
*/
|
||||
public function setRetryCount($retryCount): void
|
||||
{
|
||||
$this->retryCount = $retryCount;
|
||||
}
|
||||
|
||||
public function upRetryCount(): void
|
||||
{
|
||||
++$this->retryCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSource()
|
||||
{
|
||||
return $this->source;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $source
|
||||
*/
|
||||
public function setSource($source): void
|
||||
{
|
||||
$this->source = $source;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSourceId()
|
||||
{
|
||||
return $this->sourceId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $sourceId
|
||||
*/
|
||||
public function setSourceId($sourceId): void
|
||||
{
|
||||
$this->sourceId = (int) $sourceId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTokens()
|
||||
{
|
||||
return $this->tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $tokens
|
||||
*/
|
||||
public function setTokens($tokens): void
|
||||
{
|
||||
$this->tokens = $tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getClickCount()
|
||||
{
|
||||
return $this->clickCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $clickCount
|
||||
*
|
||||
* @return Stat
|
||||
*/
|
||||
public function setClickCount($clickCount)
|
||||
{
|
||||
$this->clickCount = $clickCount;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addClickDetails($details): void
|
||||
{
|
||||
$this->clickDetails[] = $details;
|
||||
|
||||
++$this->clickCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Up the sent count.
|
||||
*
|
||||
* @return Stat
|
||||
*/
|
||||
public function upClickCount()
|
||||
{
|
||||
$count = (int) $this->clickCount + 1;
|
||||
$this->clickCount = $count;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLastClicked()
|
||||
{
|
||||
return $this->lastClicked;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Stat
|
||||
*/
|
||||
public function setLastClicked(\DateTime $lastClicked)
|
||||
{
|
||||
$this->lastClicked = $lastClicked;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getClickDetails()
|
||||
{
|
||||
return $this->clickDetails;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $clickDetails
|
||||
*
|
||||
* @return Stat
|
||||
*/
|
||||
public function setClickDetails($clickDetails)
|
||||
{
|
||||
$this->clickDetails = $clickDetails;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \DateTimeInterface
|
||||
*/
|
||||
public function getDateRead()
|
||||
{
|
||||
return $this->dateRead;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTime $dateRead
|
||||
*
|
||||
* @return Stat
|
||||
*/
|
||||
public function setDateRead($dateRead)
|
||||
{
|
||||
$this->dateRead = $dateRead;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\NotificationBundle\Entity;
|
||||
|
||||
use Doctrine\DBAL\Query\QueryBuilder;
|
||||
use Mautic\CoreBundle\Entity\CommonRepository;
|
||||
use Mautic\CoreBundle\Helper\DateTimeHelper;
|
||||
|
||||
/**
|
||||
* @extends CommonRepository<Stat>
|
||||
*/
|
||||
class StatRepository extends CommonRepository
|
||||
{
|
||||
/**
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \Doctrine\ORM\NoResultException
|
||||
* @throws \Doctrine\ORM\NonUniqueResultException
|
||||
*/
|
||||
public function getNotificationStatus($trackingHash)
|
||||
{
|
||||
$q = $this->createQueryBuilder('s');
|
||||
$q->select('s')
|
||||
->leftJoin('s.lead', 'l')
|
||||
->leftJoin('s.notification', 'e')
|
||||
->where(
|
||||
$q->expr()->eq('s.trackingHash', ':hash')
|
||||
)
|
||||
->setParameter('hash', $trackingHash);
|
||||
|
||||
$result = $q->getQuery()->getResult();
|
||||
|
||||
return (!empty($result)) ? $result[0] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates lead ID (e.g. after a lead merge).
|
||||
*/
|
||||
public function getSentStats($notificationId, $listId = null): array
|
||||
{
|
||||
$q = $this->_em->getConnection()->createQueryBuilder();
|
||||
$q->select('s.lead_id')
|
||||
->from(MAUTIC_TABLE_PREFIX.'push_notification_stats', 's')
|
||||
->where('s.notification_id = :notification')
|
||||
->setParameter('notification', $notificationId);
|
||||
|
||||
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 $notificationIds
|
||||
* @param int $listId
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getSentCount($notificationIds = null, $listId = null)
|
||||
{
|
||||
$q = $this->_em->getConnection()->createQueryBuilder();
|
||||
|
||||
$q->select('count(s.id) as sent_count')
|
||||
->from(MAUTIC_TABLE_PREFIX.'push_notification_stats', 's');
|
||||
|
||||
if ($notificationIds) {
|
||||
if (!is_array($notificationIds)) {
|
||||
$notificationIds = [(int) $notificationIds];
|
||||
}
|
||||
$q->where(
|
||||
$q->expr()->in('s.notification_id', $notificationIds)
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|int $notificationIds
|
||||
* @param int $listId
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getReadCount($notificationIds = null, $listId = null)
|
||||
{
|
||||
$q = $this->_em->getConnection()->createQueryBuilder();
|
||||
|
||||
$q->select('count(s.id) as read_count')
|
||||
->from(MAUTIC_TABLE_PREFIX.'push_notification_stats', 's');
|
||||
|
||||
if ($notificationIds) {
|
||||
if (!is_array($notificationIds)) {
|
||||
$notificationIds = [(int) $notificationIds];
|
||||
}
|
||||
$q->where(
|
||||
$q->expr()->in('s.notification_id', $notificationIds)
|
||||
);
|
||||
}
|
||||
|
||||
if ($listId) {
|
||||
$q->andWhere('s.list_id = '.(int) $listId);
|
||||
}
|
||||
|
||||
$q->andWhere('is_read = :true')
|
||||
->setParameter('true', true, 'boolean');
|
||||
$results = $q->executeQuery()->fetchAllAssociative();
|
||||
|
||||
return (isset($results[0])) ? $results[0]['read_count'] : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get pie graph data for Sent, Read and Failed notifications count.
|
||||
*
|
||||
* @param QueryBuilder $query
|
||||
*
|
||||
* @throws \Doctrine\ORM\NoResultException
|
||||
* @throws \Doctrine\ORM\NonUniqueResultException
|
||||
*/
|
||||
public function getMostNotifications($query, $limit = 10, $offset = 0): array
|
||||
{
|
||||
$query
|
||||
->setMaxResults($limit)
|
||||
->setFirstResult($offset);
|
||||
|
||||
return $query->executeQuery()->fetchAllAssociative();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sent counts based grouped by notification Id.
|
||||
*
|
||||
* @param array $notificationIds
|
||||
*/
|
||||
public function getSentCounts($notificationIds = [], ?\DateTime $fromDate = null): array
|
||||
{
|
||||
$q = $this->_em->getConnection()->createQueryBuilder();
|
||||
$q->select('s.notification_id, count(n.id) as sentcount')
|
||||
->from(MAUTIC_TABLE_PREFIX.'push_notification_stats', 's')
|
||||
->where(
|
||||
$q->expr()->in('s.notification_id', $notificationIds)
|
||||
);
|
||||
|
||||
if (null !== $fromDate) {
|
||||
// make sure the date is UTC
|
||||
$dt = new DateTimeHelper($fromDate);
|
||||
$q->andWhere(
|
||||
$q->expr()->gte('s.date_read', $q->expr()->literal($dt->toUtcString()))
|
||||
);
|
||||
}
|
||||
$q->groupBy('s.notification_id');
|
||||
|
||||
// get a total number of sent notifications first
|
||||
$results = $q->executeQuery()->fetchAllAssociative();
|
||||
|
||||
$counts = [];
|
||||
|
||||
foreach ($results as $r) {
|
||||
$counts[$r['notification_id']] = $r['sentcount'];
|
||||
}
|
||||
|
||||
return $counts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.'push_notification_stats')
|
||||
->set('notification_id', (int) $toLeadId)
|
||||
->where('notification_id = '.(int) $fromLeadId)
|
||||
->executeStatement();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a stat.
|
||||
*/
|
||||
public function deleteStat($id): void
|
||||
{
|
||||
$this->_em->getConnection()->delete(MAUTIC_TABLE_PREFIX.'push_notification_stats', ['id' => (int) $id]);
|
||||
}
|
||||
|
||||
public function getTableAlias(): string
|
||||
{
|
||||
return 's';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user