Issues (3627)

Entity/IntegrationEntityRepository.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\PluginBundle\Entity;
13
14
use Doctrine\DBAL\Connection;
15
use Mautic\CoreBundle\Entity\CommonRepository;
16
17
/**
18
 * IntegrationRepository.
19
 */
20
class IntegrationEntityRepository extends CommonRepository
21
{
22
    /**
23
     * @param      $integration
24
     * @param      $integrationEntity
25
     * @param      $internalEntity
26
     * @param null $startDate
27
     * @param null $endDate
28
     * @param bool $push
29
     * @param int  $start
30
     * @param int  $limit
31
     *
32
     * @return array
33
     */
34
    public function getIntegrationsEntityId(
35
        $integration,
36
        $integrationEntity,
37
        $internalEntity,
38
        $internalEntityIds = null,
39
        $startDate = null,
40
        $endDate = null,
41
        $push = false,
42
        $start = 0,
43
        $limit = 0,
44
        $integrationEntityIds = null
45
    ) {
46
        $q = $this->_em->getConnection()->createQueryBuilder()
47
            ->select('DISTINCT(i.integration_entity_id), i.id, i.internal_entity_id, i.integration_entity, i.internal_entity')
48
            ->from(MAUTIC_TABLE_PREFIX.'integration_entity', 'i');
49
50
        $q->where('i.integration = :integration')
51
            ->andWhere('i.internal_entity = :internalEntity')
52
            ->setParameter('integration', $integration)
53
            ->setParameter('internalEntity', $internalEntity);
54
55
        if ($integrationEntity) {
56
            $q->andWhere('i.integration_entity = :integrationEntity')
57
                ->setParameter('integrationEntity', $integrationEntity);
58
        }
59
60
        if ($push) {
61
            $q->join('i', MAUTIC_TABLE_PREFIX.'leads', 'l', 'l.id = i.internal_entity_id and l.last_active >= :startDate')
62
                ->setParameter('startDate', $startDate);
63
        }
64
65
        if ($internalEntityIds) {
66
            if (is_array($internalEntityIds)) {
67
                $q->andWhere('i.internal_entity_id in (:internalEntityIds)')
68
                    ->setParameter('internalEntityIds', $internalEntityIds, Connection::PARAM_STR_ARRAY);
69
            } else {
70
                $q->andWhere('i.internal_entity_id = :internalEntityId')
71
                    ->setParameter('internalEntityId', $internalEntityIds);
72
            }
73
        }
74
75
        if ($startDate and !$push) {
76
            $q->andWhere('i.last_sync_date >= :startDate')
77
                ->setParameter('startDate', $startDate);
78
        }
79
80
        if ($endDate and !$push) {
81
            $q->andWhere('i.last_sync_date <= :endDate')
82
                ->setParameter('endDate', $endDate);
83
        }
84
85
        if ($integrationEntityIds) {
86
            if (is_array($integrationEntityIds)) {
87
                $q->andWhere('i.integration_entity_id in (:integrationEntityIds)')
88
                    ->setParameter('integrationEntityIds', $integrationEntityIds, Connection::PARAM_STR_ARRAY);
89
            } else {
90
                $q->andWhere('i.integration_entity_id = :integrationEntityId')
91
                    ->setParameter('integrationEntityId', $integrationEntityIds);
92
            }
93
        }
94
95
        if ($start) {
96
            $q->setFirstResult((int) $start);
97
        }
98
99
        if ($limit) {
100
            $q->setMaxResults((int) $limit);
101
        }
102
103
        return $q->execute()->fetchAll();
104
    }
105
106
    /**
107
     * @param      $integration
108
     * @param      $integrationEntity
109
     * @param      $internalEntity
110
     * @param      $internalEntityId
111
     * @param null $leadFields
112
     *
113
     * @return array
114
     */
115
    public function getIntegrationEntity($integration, $integrationEntity, $internalEntity, $internalEntityId, $leadFields = null)
116
    {
117
        $q = $this->_em->getConnection()->createQueryBuilder()
118
            ->from(MAUTIC_TABLE_PREFIX.'integration_entity', 'i')
119
            ->join('i', MAUTIC_TABLE_PREFIX.'leads', 'l', 'l.id = i.internal_entity_id');
120
        $q->select('i.integration_entity_id, i.integration_entity, i.id, i.internal_entity_id');
121
        if ($leadFields) {
122
            $q->addSelect($leadFields);
123
        }
124
125
        $q->where(
126
            $q->expr()->andX(
127
                $q->expr()->eq('i.integration', ':integration'),
128
                $q->expr()->eq('i.internal_entity', ':internalEntity'),
129
                $q->expr()->eq('i.integration_entity', ':integrationEntity'),
130
                $q->expr()->eq('i.internal_entity_id', (int) $internalEntityId)
131
            )
132
        )
133
            ->setParameter('integration', $integration)
134
            ->setParameter('internalEntity', $internalEntity)
135
            ->setParameter('integrationEntity', $integrationEntity)
136
            ->setMaxResults(1);
137
138
        $results = $q->execute()->fetchAll();
139
140
        return ($results) ? $results[0] : null;
141
    }
142
143
    /**
144
     * @param $integration
145
     * @param $integrationEntity
146
     * @param $internalEntity
147
     *
148
     * @return IntegrationEntity[]
149
     */
150
    public function getIntegrationEntities($integration, $integrationEntity, $internalEntity, $internalEntityIds)
151
    {
152
        $q = $this->createQueryBuilder('i', 'i.internalEntityId');
153
154
        $q->where(
155
            $q->expr()->andX(
156
                $q->expr()->eq('i.integration', ':integration'),
157
                $q->expr()->eq('i.internalEntity', ':internalEntity'),
158
                $q->expr()->eq('i.integrationEntity', ':integrationEntity'),
159
                $q->expr()->in('i.internalEntityId', ':internalEntityIds')
160
            )
161
        )
162
            ->setParameter('integration', $integration)
163
            ->setParameter('internalEntity', $internalEntity)
164
            ->setParameter('integrationEntity', $integrationEntity)
165
            ->setParameter('internalEntityIds', $internalEntityIds);
166
167
        return $q->getQuery()->getResult();
168
    }
169
170
    /**
171
     * @param       $integration
172
     * @param       $internalEntity
173
     * @param       $leadFields
174
     * @param int   $limit
175
     * @param null  $fromDate
176
     * @param null  $toDate
177
     * @param array $integrationEntity
178
     * @param array $excludeIntegrationIds
179
     */
180
    public function findLeadsToUpdate(
181
        $integration,
182
        $internalEntity,
183
        $leadFields,
184
        $limit = 25,
185
        $fromDate = null,
186
        $toDate = null,
187
        $integrationEntity = ['Contact', 'Lead'],
188
        $excludeIntegrationIds = []
189
    ) {
190
        if ('company' == $internalEntity) {
191
            $joinTable = 'companies';
192
        } else {
193
            $joinTable = 'leads';
194
        }
195
        $q = $this->_em->getConnection()->createQueryBuilder()
196
            ->from(MAUTIC_TABLE_PREFIX.'integration_entity', 'i')
197
            ->join('i', MAUTIC_TABLE_PREFIX.$joinTable, 'l', 'l.id = i.internal_entity_id');
198
199
        if (false === $limit) {
200
            $q->select('count(i.integration_entity_id) as total');
201
202
            if ($integrationEntity) {
203
                $q->addSelect('i.integration_entity');
204
            }
205
        } else {
206
            $q->select('i.integration_entity_id, i.integration_entity, i.id, i.internal_entity_id,'.$leadFields);
207
        }
208
209
        $q->where('i.integration = :integration');
210
211
        if ($integrationEntity) {
212
            if (!is_array($integrationEntity)) {
213
                $integrationEntity = [$integrationEntity];
214
            }
215
            $sub = $q->expr()->orX();
216
            foreach ($integrationEntity as $key => $entity) {
217
                $sub->add($q->expr()->eq('i.integration_entity', ':entity'.$key));
218
                $q->setParameter(':entity'.$key, $entity);
219
            }
220
            $q->andWhere($sub);
221
        }
222
223
        $q->andWhere('i.internal_entity = :internalEntity')
224
            ->setParameter('integration', $integration)
225
            ->setParameter('internalEntity', $internalEntity);
226
227
        if (!empty($excludeIntegrationIds)) {
228
            $q->andWhere(
229
                $q->expr()->notIn(
230
                    'i.integration_entity_id',
231
                    array_map(
232
                        function ($x) {
233
                            return "'".$x."'";
234
                        },
235
                        $excludeIntegrationIds
236
                    )
237
                )
238
            );
239
        }
240
241
        $q->andWhere(
242
                $q->expr()->andX(
243
                    $q->expr()->isNotNull('i.integration_entity_id'),
244
                    $q->expr()->orX(
245
                        $q->expr()->andX(
246
                            $q->expr()->isNotNull('i.last_sync_date'),
247
                            $q->expr()->gt('l.date_modified', 'i.last_sync_date')
248
                        ),
249
                        $q->expr()->andX(
250
                            $q->expr()->isNull('i.last_sync_date'),
251
                            $q->expr()->isNotNull('l.date_modified'),
252
                            $q->expr()->gt('l.date_modified', 'l.date_added')
253
                        )
254
                    )
255
                )
256
            );
257
258
        if ('lead' == $internalEntity) {
259
            $q->andWhere(
260
                $q->expr()->andX($q->expr()->isNotNull('l.email')));
261
        } else {
262
            $q->andWhere(
263
                $q->expr()->andX($q->expr()->isNotNull('l.companyname')));
264
        }
265
266
        if ($fromDate) {
267
            if ($toDate) {
268
                $q->andWhere(
269
                    $q->expr()->comparison('l.date_modified', 'BETWEEN', ':dateFrom and :dateTo')
270
                )
271
                    ->setParameter('dateFrom', $fromDate)
272
                    ->setParameter('dateTo', $toDate);
273
            } else {
274
                $q->andWhere(
275
                    $q->expr()->gte('l.date_modified', ':dateFrom')
276
                )
277
                    ->setParameter('dateFrom', $fromDate);
278
            }
279
        } elseif ($toDate) {
280
            $q->andWhere(
281
                $q->expr()->lte('l.date_modified', ':dateTo')
282
            )
283
                ->setParameter('dateTo', $toDate);
284
        }
285
286
        // Group by email to prevent duplicates from affecting this
287
288
        if (false === $limit and $integrationEntity) {
289
            $q->groupBy('i.integration_entity')->having('total');
290
        }
291
        if ($limit) {
292
            $q->setMaxResults($limit);
293
        }
294
        $results = $q->execute()->fetchAll();
295
        $leads   = [];
296
297
        if ($integrationEntity) {
298
            foreach ($integrationEntity as $entity) {
299
                $leads[$entity] = (false === $limit) ? 0 : [];
300
            }
301
        }
302
303
        foreach ($results as $result) {
304
            if ($integrationEntity) {
305
                if (false === $limit) {
306
                    $leads[$result['integration_entity']] = (int) $result['total'];
307
                } else {
308
                    $leads[$result['integration_entity']][$result['internal_entity_id']] = $result;
309
                }
310
            } else {
311
                $leads[$result['internal_entity_id']] = $result['internal_entity_id'];
312
            }
313
        }
314
315
        return $leads;
316
    }
317
318
    /**
319
     * @param      $integration
320
     * @param      $leadFields
321
     * @param int  $limit
322
     * @param null $fromDate
323
     * @param null $toDate
324
     *
325
     * @return array|int
326
     */
327
    public function findLeadsToCreate($integration, $leadFields, $limit = 25, $fromDate = null, $toDate = null, $internalEntity = 'lead')
328
    {
329
        if ('company' == $internalEntity) {
330
            $joinTable = 'companies';
331
        } else {
332
            $joinTable = 'leads';
333
        }
334
        $q = $this->_em->getConnection()->createQueryBuilder()
335
            ->from(MAUTIC_TABLE_PREFIX.$joinTable, 'l');
336
337
        if (false === $limit) {
338
            $q->select('count(*) as total');
339
        } else {
340
            $q->select('l.id as internal_entity_id,'.$leadFields);
341
        }
342
        if ('company' == $internalEntity) {
343
            $q->where('not exists (select null from '.MAUTIC_TABLE_PREFIX
344
                .'integration_entity i where i.integration = :integration and i.internal_entity LIKE "'.$internalEntity.'%" and i.internal_entity_id = l.id)')
345
                ->setParameter('integration', $integration);
346
        } else {
347
            $q->where('l.date_identified is not null')
348
                ->andWhere(
349
                    'not exists (select null from '.MAUTIC_TABLE_PREFIX
350
                    .'integration_entity i where i.integration = :integration and i.internal_entity LIKE "'.$internalEntity.'%" and i.internal_entity_id = l.id)'
351
                )
352
                ->setParameter('integration', $integration);
353
        }
354
355
        if ('company' == $internalEntity) {
356
            $q->andWhere('l.companyname is not null');
357
        } else {
358
            $q->andWhere('l.email is not null');
359
        }
360
        if ($limit) {
361
            $q->setMaxResults($limit);
362
        }
363
364
        if ($fromDate) {
365
            if ($toDate) {
366
                $q->andWhere(
367
                    $q->expr()->orX(
368
                        $q->expr()->andX(
369
                            $q->expr()->isNotNull('l.date_modified'),
370
                            $q->expr()->comparison('l.date_modified', 'BETWEEN', ':dateFrom and :dateTo')
371
                        ),
372
                        $q->expr()->andX(
373
                            $q->expr()->isNull('l.date_modified'),
374
                            $q->expr()->comparison('l.date_added', 'BETWEEN', ':dateFrom and :dateTo')
375
                        )
376
                    )
377
                )
378
                    ->setParameter('dateFrom', $fromDate)
379
                    ->setParameter('dateTo', $toDate);
380
            } else {
381
                $q->andWhere(
382
                    $q->expr()->orX(
383
                        $q->expr()->andX(
384
                            $q->expr()->isNotNull('l.date_modified'),
385
                            $q->expr()->gte('l.date_modified', ':dateFrom')
386
                        ),
387
                        $q->expr()->andX(
388
                            $q->expr()->isNull('l.date_modified'),
389
                            $q->expr()->gte('l.date_added', ':dateFrom')
390
                        )
391
                    )
392
                )
393
                    ->setParameter('dateFrom', $fromDate);
394
            }
395
        } elseif ($toDate) {
396
            $q->andWhere(
397
                $q->expr()->orX(
398
                    $q->expr()->andX(
399
                        $q->expr()->isNotNull('l.date_modified'),
400
                        $q->expr()->lte('l.date_modified', ':dateTo')
401
                    ),
402
                    $q->expr()->andX(
403
                        $q->expr()->isNull('l.date_modified'),
404
                        $q->expr()->lte('l.date_added', ':dateTo')
405
                    )
406
                )
407
            )
408
                ->setParameter('dateTo', $toDate);
409
        }
410
411
        $results = $q->execute()->fetchAll();
412
        if (false === $limit) {
413
            return (int) $results[0]['total'];
414
        }
415
416
        $leads = [];
417
        foreach ($results as $result) {
418
            $leads[$result['internal_entity_id']] = $result;
419
        }
420
421
        return $leads;
422
    }
423
424
    /**
425
     * @param $leadId
426
     * @param $integration
427
     * @param $integrationEntity
428
     * @param $internalEntity
429
     *
430
     * @return int
431
     */
432
    public function getIntegrationEntityCount($leadId, $integration = null, $integrationEntity = null, $internalEntity = null)
433
    {
434
        return $this->getIntegrationEntityByLead($leadId, $integration, $integrationEntity, $internalEntity, false);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getIntegra...$internalEntity, false) also could return the type array|array<mixed,mixed> which is incompatible with the documented return type integer.
Loading history...
435
    }
436
437
    /**
438
     * @param $leadId
439
     * @param $integration
440
     * @param $integrationEntity
441
     * @param $internalEntity
442
     * @param int|bool $limit
443
     *
444
     * @return array|int
445
     */
446
    public function getIntegrationEntityByLead($leadId, $integration = null, $integrationEntity = null, $internalEntity = null, $limit = 100)
447
    {
448
        $q = $this->_em->getConnection()->createQueryBuilder()
449
            ->from(MAUTIC_TABLE_PREFIX.'integration_entity', 'i');
450
451
        if (false === $limit) {
452
            $q->select('count(*) as total');
453
        } else {
454
            $q->select('i.integration, i.integration_entity, i.integration_entity_id, i.date_added, i.last_sync_date, i.internal');
455
        }
456
457
        $q->where('i.internal not like \'%error%\' and i.integration_entity_id is not null');
458
        $q->orderBy('i.last_sync_date', 'DESC');
459
460
        if (empty($integration)) {
461
            // get list of published integrations
462
            $pq = $this->_em->getConnection()->createQueryBuilder()
463
                ->select('p.name')
464
                ->from(MAUTIC_TABLE_PREFIX.'plugin_integration_settings', 'p')
465
                ->where('p.is_published = 1');
466
            $rows    = $pq->execute()->fetchAll();
467
            $plugins = array_map(function ($i) {
468
                return "'${i['name']}'";
469
            }, $rows);
470
            if (count($plugins) > 0) {
471
                $q->andWhere($q->expr()->in('i.integration', $plugins));
472
            } else {
473
                return [];
474
            }
475
        } else {
476
            $q->andWhere($q->expr()->eq('i.integration', ':integration'));
477
            $q->setParameter('integration', $integration);
478
        }
479
480
        $q->andWhere(
481
            $q->expr()->andX(
482
                "i.internal_entity='lead'",
483
                $q->expr()->eq('i.internal_entity_id', ':internalEntityId')
484
            )
485
        );
486
487
        $q->setParameter('internalEntityId', $leadId);
488
489
        if (!empty($internalEntity)) {
490
            $q->andWhere($q->expr()->eq('i.internalEntity', ':internalEntity'));
491
            $q->setParameter('internalEntity', $internalEntity);
492
        }
493
494
        if (!empty($integrationEntity)) {
495
            $q->andWhere($q->expr()->eq('i.integrationEntity', ':integrationEntity'));
496
            $q->setParameter('integrationEntity', $integrationEntity);
497
        }
498
499
        $results = $q->execute()->fetchAll();
500
501
        if (false === $limit && count($results) > 0) {
502
            return (int) $results[0]['total'];
503
        }
504
505
        return $results;
506
    }
507
508
    /**
509
     * @param $integration
510
     * @param $internalEntityType
511
     */
512
    public function markAsDeleted(array $integrationIds, $integration, $internalEntityType)
513
    {
514
        $q = $this->_em->getConnection()->createQueryBuilder();
515
        $q->update(MAUTIC_TABLE_PREFIX.'integration_entity')
516
            ->set('internal_entity', ':entity')
517
            ->where(
518
                $q->expr()->andX(
519
                    $q->expr()->eq('integration', ':integration'),
520
                    $q->expr()->in('integration_entity_id', array_map([$q->expr(), 'literal'], $integrationIds))
521
                )
522
            )
523
            ->setParameter('integration', $integration)
524
            ->setParameter('entity', $internalEntityType.'-deleted')
525
            ->execute();
526
    }
527
528
    /**
529
     * @param $internalEntity
530
     * @param $leadId
531
     *
532
     * @return array
533
     */
534
    public function findLeadsToDelete($internalEntity, $leadId)
535
    {
536
        $q = $this->_em->getConnection()->createQueryBuilder()
537
            ->delete(MAUTIC_TABLE_PREFIX.'integration_entity')
538
            ->from(MAUTIC_TABLE_PREFIX.'integration_entity');
539
540
        $q->where('internal_entity_id = :leadId')
541
            ->andWhere($q->expr()->like('internal_entity', ':internalEntity'))
542
            ->setParameter('leadId', $leadId)
543
            ->setParameter('internalEntity', $internalEntity)
544
            ->execute();
545
    }
546
547
    /**
548
     * @param $internalEntity
549
     * @param $leadId
550
     */
551
    public function updateErrorLeads($internalEntity, $leadId)
552
    {
553
        $q = $this->_em->getConnection()->createQueryBuilder()
554
            ->update(MAUTIC_TABLE_PREFIX.'integration_entity')
555
            ->set('internal_entity', ':lead')->setParameter('lead', 'lead');
556
557
        $q->where('internal_entity_id = :leadId')
558
            ->andWhere($q->expr()->isNotNull('integration_entity_id'))
559
            ->andWhere($q->expr()->eq('internal_entity', ':internalEntity'))
560
            ->setParameter('leadId', $leadId)
561
            ->setParameter('internalEntity', $internalEntity)
562
            ->execute();
563
564
        $z = $this->_em->getConnection()->createQueryBuilder()
565
            ->delete(MAUTIC_TABLE_PREFIX.'integration_entity')
566
            ->from(MAUTIC_TABLE_PREFIX.'integration_entity');
567
568
        $z->where('internal_entity_id = :leadId')
569
            ->andWhere($q->expr()->isNull('integration_entity_id'))
570
            ->andWhere($q->expr()->like('internal_entity', ':internalEntity'))
571
            ->setParameter('leadId', $leadId)
572
            ->setParameter('internalEntity', $internalEntity)
573
            ->execute();
574
    }
575
}
576