Issues (3627)

app/bundles/StageBundle/Entity/StageRepository.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\StageBundle\Entity;
13
14
use Mautic\CoreBundle\Entity\CommonRepository;
15
16
/**
17
 * Class StageRepository.
18
 */
19
class StageRepository extends CommonRepository
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getEntities(array $args = [])
25
    {
26
        $q = $this
27
            ->createQueryBuilder($this->getTableAlias())
28
            ->leftJoin($this->getTableAlias().'.category', 'c');
29
30
        $args['qb'] = $q;
31
32
        return parent::getEntities($args);
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getTableAlias()
39
    {
40
        return 's';
41
    }
42
43
    /**
44
     * Get array of published actions based on type.
45
     *
46
     * @param string $type
47
     *
48
     * @return array
49
     */
50
    public function getPublishedByType($type)
51
    {
52
        $q = $this->createQueryBuilder('s')
53
            ->select('partial s.{id, name}')
54
            ->setParameter('type', $type);
55
56
        //make sure the published up and down dates are good
57
        $expr = $this->getPublishedByDateExpression($q);
58
59
        $q->where($expr);
60
61
        return $q->getQuery()->getResult();
62
    }
63
64
    /**
65
     * @param string $type
66
     * @param int    $leadId
67
     *
68
     * @return array
69
     */
70
    public function getCompletedLeadActions($type, $leadId)
71
    {
72
        $q = $this->_em->getConnection()->createQueryBuilder()
73
            ->select('s.*')
74
            ->from(MAUTIC_TABLE_PREFIX.'stage_lead_action_log', 'x')
75
            ->innerJoin('x', MAUTIC_TABLE_PREFIX.'stages', 's', 'x.stage_id = s.id');
76
77
        //make sure the published up and down dates are good
78
        $q->where(
79
            $q->expr()->andX(
80
                $q->expr()->eq('x.lead_id', (int) $leadId)
81
            )
82
        );
83
84
        $results = $q->execute()->fetchAll();
85
86
        $return = [];
87
88
        foreach ($results as $r) {
89
            $return[$r['id']] = $r;
90
        }
91
92
        return $return;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    protected function addCatchAllWhereClause($q, $filter)
99
    {
100
        return $this->addStandardCatchAllWhereClause($q, $filter, [
101
            's.name',
102
            's.description',
103
        ]);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    protected function addSearchCommandWhereClause($q, $filter)
110
    {
111
        return $this->addStandardSearchCommandWhereClause($q, $filter);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function getSearchCommands()
118
    {
119
        return $this->getStandardSearchCommands();
120
    }
121
122
    /**
123
     * Get a list of lists.
124
     *
125
     * @param bool   $user
126
     * @param string $alias
127
     * @param string $id
128
     *
129
     * @return array
130
     */
131
    public function getStages($user = false, $id = '')
132
    {
133
        static $stages = [];
134
135
        if (is_object($user)) {
136
            $user = $user->getId();
137
        }
138
139
        $key = (int) $user.$id;
140
        if (isset($stages[$key])) {
141
            return $stages[$key];
142
        }
143
144
        $q = $this->_em->createQueryBuilder()
145
            ->from('MauticStageBundle:Stage', 's', 's.id');
146
147
        $q->select('partial s.{id, name}')
148
            ->andWhere($q->expr()->eq('s.isPublished', ':true'))
149
            ->setParameter('true', true, 'boolean');
150
151
        if (!empty($user)) {
152
            $q->orWhere('s.createdBy = :user');
153
            $q->setParameter('user', $user);
154
        }
155
156
        if (!empty($id)) {
157
            $q->andWhere(
158
                $q->expr()->neq('s.id', $id)
159
            );
160
        }
161
162
        $q->orderBy('s.name');
163
164
        $results = $q->getQuery()->getArrayResult();
165
166
        $stages[$key] = $results;
167
168
        return $results;
169
    }
170
171
    /**
172
     * Get a list of stages.
173
     *
174
     * @param string $name
175
     *
176
     * @return array
177
     */
178
    public function getStageByName($stageName)
179
    {
180
        if (!$stageName) {
181
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
182
        }
183
184
        $q = $this->_em->createQueryBuilder()
185
            ->from('MauticStageBundle:Stage', 's', 's.id');
186
187
        $q->select('partial s.{id, name}')
188
            ->andWhere($q->expr()->eq('s.isPublished', ':true'))
189
            ->setParameter('true', true, 'boolean');
190
        $q->andWhere('s.name = :stage')
191
            ->setParameter('stage', $stageName);
192
193
        $result = $q->getQuery()->getResult();
194
195
        if ($result) {
196
            $key = array_keys($result);
197
198
            return $result[$key[0]];
199
        }
200
201
        return null;
202
    }
203
204
    /**
205
     * @param string|int $value
206
     *
207
     * @return array
208
     */
209
    public function findByIdOrName($value)
210
    {
211
        $qb = $this->_em->createQueryBuilder()
212
            ->select('s')
213
            ->from(Stage::class, 's');
214
215
        if (is_numeric($value)) {
216
            // This is numeric value so check id and name
217
            $qb->where('s.id = :value');
218
        } else {
219
            // This is string, no need to check IDs
220
            $qb->where('s.name = :value');
221
        }
222
223
        return $qb
224
            ->setParameter('value', $value)
225
            ->getQuery()
226
            ->getOneOrNullResult();
227
    }
228
}
229