Issues (3627)

LeadBundle/Entity/CompanyLeadRepository.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\LeadBundle\Entity;
13
14
use Mautic\CoreBundle\Entity\CommonRepository;
15
16
class CompanyLeadRepository extends CommonRepository
17
{
18
    /**
19
     * @param CompanyLead[] $entities
20
     */
21
    public function saveEntities($entities, $new = true)
22
    {
23
        // Get a list of contacts and set primary to 0
24
        if ($new) {
25
            $contacts = [];
26
            foreach ($entities as $entity) {
27
                $contactId            = $entity->getLead()->getId();
28
                $contacts[$contactId] = $contactId;
29
                $entity->setPrimary(true);
30
            }
31
            if ($contactId) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $contactId seems to be defined by a foreach iteration on line 26. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
32
                $qb = $this->getEntityManager()->getConnection()->createQueryBuilder()
33
                    ->update(MAUTIC_TABLE_PREFIX.'companies_leads')
34
                    ->set('is_primary', 0);
35
36
                $qb->where(
37
                    $qb->expr()->in('lead_id', $contacts)
38
                )->execute();
39
            }
40
        }
41
42
        return parent::saveEntities($entities);
43
    }
44
45
    /**
46
     * Get companies by leadId.
47
     *
48
     * @param $leadId
49
     * @param $companyId
50
     *
51
     * @return array
52
     */
53
    public function getCompaniesByLeadId($leadId, $companyId = null)
54
    {
55
        $q = $this->_em->getConnection()->createQueryBuilder();
56
57
        $q->select('cl.company_id, cl.date_added as date_associated, cl.is_primary, comp.*')
58
            ->from(MAUTIC_TABLE_PREFIX.'companies_leads', 'cl')
59
            ->join('cl', MAUTIC_TABLE_PREFIX.'companies', 'comp', 'comp.id = cl.company_id')
60
        ->where('cl.lead_id = :leadId')
61
        ->setParameter('leadId', $leadId);
62
63
        if ($companyId) {
64
            $q->andWhere(
65
                $q->expr()->eq('cl.company_id', ':companyId')
66
            )->setParameter('companyId', $companyId);
67
        }
68
69
        return $q->execute()->fetchAll();
70
    }
71
72
    /**
73
     * @param $companyId
74
     *
75
     * @return array
76
     */
77
    public function getCompanyLeads($companyId)
78
    {
79
        $q = $this->_em->getConnection()->createQueryBuilder();
80
        $q->select('cl.lead_id')
81
            ->from(MAUTIC_TABLE_PREFIX.'companies_leads', 'cl');
82
83
        $q->where($q->expr()->eq('cl.company_id', ':company'))
84
            ->setParameter(':company', $companyId);
85
86
        return $q->execute()->fetchAll();
87
    }
88
89
    /**
90
     * @param $leadId
91
     *
92
     * @return array
93
     */
94
    public function getLatestCompanyForLead($leadId)
95
    {
96
        $q = $this->_em->getConnection()->createQueryBuilder();
97
98
        $q->select('cl.company_id, comp.companyname, comp.companycity, comp.companycountry')
99
            ->from(MAUTIC_TABLE_PREFIX.'companies_leads', 'cl')
100
            ->join('cl', MAUTIC_TABLE_PREFIX.'companies', 'comp', 'comp.id = cl.company_id')
101
            ->where('cl.lead_id = :leadId')
102
            ->setParameter('leadId', $leadId);
103
        $q->orderBy('cl.date_added', 'DESC');
104
        $result = $q->execute()->fetchAll();
105
106
        return !empty($result) ? $result[0] : [];
107
    }
108
109
    /**
110
     * @param $leadId
111
     * @param $companyId
112
     */
113
    public function getCompanyLeadEntity($leadId, $companyId)
114
    {
115
        $qb = $this->getEntityManager()->getConnection()->createQueryBuilder();
116
        $qb->select('cl.is_primary, cl.lead_id, cl.company_id')
117
            ->from(MAUTIC_TABLE_PREFIX.'companies_leads', 'cl')
118
            ->where(
119
                    $qb->expr()->eq('cl.lead_id', ':leadId'),
120
                    $qb->expr()->eq('cl.company_id', ':companyId')
121
            )->setParameter('leadId', $leadId)
122
            ->setParameter('companyId', $companyId);
123
124
        return $qb->execute()->fetchAll();
125
    }
126
127
    /**
128
     * @return mixed
129
     */
130
    public function getEntitiesByLead(Lead $lead)
131
    {
132
        $qb = $this->getEntityManager()->createQueryBuilder();
133
        $qb->select('cl')
134
            ->from(CompanyLead::class, 'cl')
135
            ->where(
136
                $qb->expr()->eq('cl.lead', ':lead')
137
            )->setParameter('lead', $lead);
138
139
        return $qb->getQuery()->execute();
140
    }
141
142
    /**
143
     * Updates leads company name If company name changed and company is primary.
144
     */
145
    public function updateLeadsPrimaryCompanyName(Company $company)
146
    {
147
        if ($company->isNew() || empty($company->getChanges()['fields']['companyname'])) {
148
            return;
149
        }
150
        $q = $this->getEntityManager()->getConnection()->createQueryBuilder();
151
        $q->select('cl.lead_id')
152
            ->from(MAUTIC_TABLE_PREFIX.'companies_leads', 'cl');
153
        $q->where($q->expr()->eq('cl.company_id', ':companyId'))
154
            ->setParameter(':companyId', $company->getId())
155
            ->andWhere('cl.is_primary = 1');
156
        $leadIds = $q->execute()->fetchColumn();
157
        if (!empty($leadIds)) {
158
            $this->getEntityManager()->getConnection()->createQueryBuilder()
159
            ->update(MAUTIC_TABLE_PREFIX.'leads')
160
            ->set('company', ':company')
161
            ->setParameter(':company', $company->getName())
162
            ->where(
163
                $q->expr()->in('id', $leadIds)
164
            )->execute();
165
        }
166
    }
167
}
168