Spaces:
No application file
No application file
File size: 5,416 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
<?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', (int) $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';
}
}
|