/
PointsChangeLogRepository.php
139 lines (120 loc) · 3.8 KB
/
PointsChangeLogRepository.php
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
<?php
/*
* @copyright 2014 Mautic Contributors. All rights reserved
* @author Mautic
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
namespace Mautic\LeadBundle\Entity;
use Doctrine\DBAL\Query\QueryBuilder;
use Mautic\CoreBundle\Entity\CommonRepository;
/**
* PointsChangeLogRepository.
*/
class PointsChangeLogRepository extends CommonRepository
{
use TimelineTrait;
/**
* Get a lead's point log.
*
* @param int|null $leadId
* @param array $options
*
* @return array
*/
public function getLeadTimelineEvents($leadId = null, array $options = [])
{
$query = $this->getEntityManager()->getConnection()->createQueryBuilder()
->from(MAUTIC_TABLE_PREFIX.'lead_points_change_log', 'lp')
->select('lp.event_name as eventName, lp.action_name as actionName, lp.date_added as dateAdded, lp.type, lp.delta, lp.id, lp.lead_id');
if ($leadId) {
$query->where('lp.lead_id = '.(int) $leadId);
}
if (isset($options['search']) && $options['search']) {
$query->andWhere($query->expr()->orX(
$query->expr()->like('lp.event_name', $query->expr()->literal('%'.$options['search'].'%')),
$query->expr()->like('lp.action_name', $query->expr()->literal('%'.$options['search'].'%'))
));
}
return $this->getTimelineResults($query, $options, 'lp.event_name', 'lp.date_added', [], ['dateAdded']);
}
/**
* Get table stat data from point log table.
*
* @param QueryBuilder $query
*
* @return array
*
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function getMostPoints(QueryBuilder $query, $limit = 10, $offset = 0)
{
$query->setMaxResults($limit)
->setFirstResult($offset);
$results = $query->execute()->fetchAll();
return $results;
}
/**
* Get table stat data from lead table.
*
* @param QueryBuilder $query
*
* @return array
*
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function getMostLeads(QueryBuilder $query, $limit = 10, $offset = 0)
{
$query->setMaxResults($limit)
->setFirstResult($offset);
$results = $query->execute()->fetchAll();
return $results;
}
/**
* Count a value in a column.
*
* @param QueryBuilder $query
*
* @return array
*
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*
* @deprecated 2.10 - to be removed in 3.0 - never used in the codebase
*/
public function countValue(QueryBuilder $query, $column, $value)
{
$query->select('count('.$column.') as quantity')
->from(MAUTIC_TABLE_PREFIX.'leads', 'l')
->leftJoin('l', MAUTIC_TABLE_PREFIX.'lead_points_change_log', 'lp', 'lp.lead_id = l.id')
->andwhere($query->expr()->eq($column, ':value'))
->setParameter('value', $value);
$result = $query->execute()->fetch();
return $result['quantity'];
}
/**
* Updates lead ID (e.g. after a lead merge).
*
* @param int $fromLeadId
* @param int $toLeadId
*/
public function updateLead($fromLeadId, $toLeadId)
{
$q = $this->_em->getConnection()->createQueryBuilder();
$q->update(MAUTIC_TABLE_PREFIX.'lead_points_change_log')
->set('lead_id', (int) $toLeadId)
->where('lead_id = '.(int) $fromLeadId)
->execute();
}
/**
* @return string
*/
public function getTableAlias()
{
return 'lp';
}
}