/app/bundles/SmsBundle/Entity/StatRepository.php

https://gitlab.com/mautic-master/mautic · PHP · 205 lines · 118 code · 31 blank · 56 comment · 12 complexity · b96ee831396b108354385c6e74c0f55f MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright 2016 Mautic Contributors. All rights reserved.
  4. * @author Mautic
  5. *
  6. * @link http://mautic.org
  7. *
  8. * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
  9. */
  10. namespace Mautic\SmsBundle\Entity;
  11. use Mautic\CoreBundle\Entity\CommonRepository;
  12. use Mautic\CoreBundle\Helper\DateTimeHelper;
  13. /**
  14. * Class StatRepository.
  15. */
  16. class StatRepository extends CommonRepository
  17. {
  18. /**
  19. * @param $trackingHash
  20. *
  21. * @return mixed
  22. *
  23. * @throws \Doctrine\ORM\NoResultException
  24. * @throws \Doctrine\ORM\NonUniqueResultException
  25. */
  26. public function getSmsStatus($trackingHash)
  27. {
  28. $q = $this->createQueryBuilder('s');
  29. $q->select('s')
  30. ->leftJoin('s.lead', 'l')
  31. ->leftJoin('s.sms', 'e')
  32. ->where(
  33. $q->expr()->eq('s.trackingHash', ':hash')
  34. )
  35. ->setParameter('hash', $trackingHash);
  36. $result = $q->getQuery()->getResult();
  37. return (!empty($result)) ? $result[0] : null;
  38. }
  39. /**
  40. * @param $smsId
  41. * @param null $listId
  42. *
  43. * @return array
  44. */
  45. public function getSentStats($smsId, $listId = null)
  46. {
  47. $q = $this->_em->getConnection()->createQueryBuilder();
  48. $q->select('s.lead_id')
  49. ->from(MAUTIC_TABLE_PREFIX . 'sms_messages_stats', 's')
  50. ->where('s.sms_id = :sms')
  51. ->setParameter('sms', $smsId);
  52. if ($listId) {
  53. $q->andWhere('s.list_id = :list')
  54. ->setParameter('list', $listId);
  55. }
  56. $result = $q->execute()->fetchAll();
  57. //index by lead
  58. $stats = array();
  59. foreach ($result as $r) {
  60. $stats[$r['lead_id']] = $r['lead_id'];
  61. }
  62. unset($result);
  63. return $stats;
  64. }
  65. /**
  66. * @param int|array $smsIds
  67. * @param int $listId
  68. *
  69. * @return int
  70. */
  71. public function getSentCount($smsIds = null, $listId = null)
  72. {
  73. $q = $this->_em->getConnection()->createQueryBuilder();
  74. $q->select('count(s.id) as sent_count')
  75. ->from(MAUTIC_TABLE_PREFIX . 'sms_message_stats', 's');
  76. if ($smsIds) {
  77. if (!is_array($smsIds)) {
  78. $smsIds = array((int) $smsIds);
  79. }
  80. $q->where(
  81. $q->expr()->in('s.sms_id', $smsIds)
  82. );
  83. }
  84. if ($listId) {
  85. $q->andWhere('s.list_id = ' . (int) $listId);
  86. }
  87. $q->andWhere('s.is_failed = :false')
  88. ->setParameter('false', false, 'boolean');
  89. $results = $q->execute()->fetchAll();
  90. return (isset($results[0])) ? $results[0]['sent_count'] : 0;
  91. }
  92. /**
  93. * Get a lead's email stat
  94. *
  95. * @param integer $leadId
  96. * @param array $options
  97. *
  98. * @return array
  99. * @throws \Doctrine\ORM\NoResultException
  100. * @throws \Doctrine\ORM\NonUniqueResultException
  101. */
  102. public function getLeadStats($leadId, array $options = array())
  103. {
  104. $query = $this->createQueryBuilder('s');
  105. $query->select('IDENTITY(s.sms) AS sms_id, s.id, s.dateSent, e.title, IDENTITY(s.list) AS list_id, l.name as list_name, s.trackingHash as idHash')
  106. ->leftJoin('MauticSmsBundle:Sms', 'e', 'WITH', 'e.id = s.sms')
  107. ->leftJoin('MauticLeadBundle:LeadList', 'l', 'WITH', 'l.id = s.list')
  108. ->where(
  109. $query->expr()->eq('IDENTITY(s.lead)', $leadId)
  110. );
  111. if (isset($options['search']) && $options['search']) {
  112. $query->andWhere(
  113. $query->expr()->like('e.title', $query->expr()->literal('%' . $options['search'] . '%'))
  114. );
  115. }
  116. if (isset($options['order'])) {
  117. list ($orderBy, $orderByDir) = $options['order'];
  118. switch ($orderBy) {
  119. case 'eventLabel':
  120. $orderBy = 'e.title';
  121. break;
  122. case 'timestamp':
  123. default:
  124. $orderBy = 's.dateSent';
  125. break;
  126. }
  127. $query->orderBy($orderBy, $orderByDir);
  128. }
  129. if (!empty($options['limit'])) {
  130. $query->setMaxResults($options['limit']);
  131. if (!empty($options['start'])) {
  132. $query->setFirstResult($options['start']);
  133. }
  134. }
  135. if (isset($options['fromDate']) && $options['fromDate']) {
  136. $dt = new DateTimeHelper($options['fromDate']);
  137. $query->andWhere(
  138. $query->expr()->gte('s.dateSent', $query->expr()->literal($dt->toUtcString()))
  139. );
  140. }
  141. $stats = $query->getQuery()->getArrayResult();
  142. return $stats;
  143. }
  144. /**
  145. * Updates lead ID (e.g. after a lead merge)
  146. *
  147. * @param $fromLeadId
  148. * @param $toLeadId
  149. */
  150. public function updateLead($fromLeadId, $toLeadId)
  151. {
  152. $q = $this->_em->getConnection()->createQueryBuilder();
  153. $q->update(MAUTIC_TABLE_PREFIX.'sms_message_stats')
  154. ->set('sms_id', (int) $toLeadId)
  155. ->where('sms_id = '.(int) $fromLeadId)
  156. ->execute();
  157. }
  158. /**
  159. * Delete a stat.
  160. *
  161. * @param $id
  162. */
  163. public function deleteStat($id)
  164. {
  165. $this->_em->getConnection()->delete(MAUTIC_TABLE_PREFIX.'sms_message_stats', ['id' => (int) $id]);
  166. }
  167. /**
  168. * @return string
  169. */
  170. public function getTableAlias()
  171. {
  172. return 's';
  173. }
  174. }