-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
SubmissionModel.php
1018 lines (871 loc) · 38.7 KB
/
SubmissionModel.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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\FormBundle\Model;
use Doctrine\ORM\ORMException;
use Mautic\CampaignBundle\Model\CampaignModel;
use Mautic\CoreBundle\Exception\FileUploadException;
use Mautic\CoreBundle\Helper\Chart\ChartQuery;
use Mautic\CoreBundle\Helper\Chart\LineChart;
use Mautic\CoreBundle\Helper\DateTimeHelper;
use Mautic\CoreBundle\Helper\InputHelper;
use Mautic\CoreBundle\Helper\IpLookupHelper;
use Mautic\CoreBundle\Helper\TemplatingHelper;
use Mautic\CoreBundle\Model\FormModel as CommonFormModel;
use Mautic\CoreBundle\Templating\Helper\DateHelper;
use Mautic\FormBundle\Crate\UploadFileCrate;
use Mautic\FormBundle\Entity\Action;
use Mautic\FormBundle\Entity\Field;
use Mautic\FormBundle\Entity\Form;
use Mautic\FormBundle\Entity\Submission;
use Mautic\FormBundle\Entity\SubmissionRepository;
use Mautic\FormBundle\Event\Service\FieldValueTransformer;
use Mautic\FormBundle\Event\SubmissionEvent;
use Mautic\FormBundle\Event\ValidationEvent;
use Mautic\FormBundle\Exception\FileValidationException;
use Mautic\FormBundle\Exception\NoFileGivenException;
use Mautic\FormBundle\Exception\ValidationException;
use Mautic\FormBundle\FormEvents;
use Mautic\FormBundle\Helper\FormFieldHelper;
use Mautic\FormBundle\Helper\FormUploader;
use Mautic\FormBundle\Validator\UploadFieldValidator;
use Mautic\LeadBundle\DataObject\LeadManipulator;
use Mautic\LeadBundle\Entity\Company;
use Mautic\LeadBundle\Entity\CompanyChangeLog;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Helper\IdentifyCompanyHelper;
use Mautic\LeadBundle\Model\CompanyModel;
use Mautic\LeadBundle\Model\FieldModel as LeadFieldModel;
use Mautic\LeadBundle\Model\LeadModel;
use Mautic\LeadBundle\Tracker\Service\DeviceTrackingService\DeviceTrackingServiceInterface;
use Mautic\PageBundle\Model\PageModel;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
* Class SubmissionModel.
*/
class SubmissionModel extends CommonFormModel
{
/**
* @var IpLookupHelper
*/
protected $ipLookupHelper;
/**
* @var TemplatingHelper
*/
protected $templatingHelper;
/**
* @var FormModel
*/
protected $formModel;
/**
* @var PageModel
*/
protected $pageModel;
/**
* @var LeadModel
*/
protected $leadModel;
/**
* @var CampaignModel
*/
protected $campaignModel;
/**
* @var LeadFieldModel
*/
protected $leadFieldModel;
/**
* @var CompanyModel
*/
protected $companyModel;
/**
* @var FormFieldHelper
*/
protected $fieldHelper;
/**
* @var UploadFieldValidator
*/
private $uploadFieldValidator;
/**
* @var FormUploader
*/
private $formUploader;
/**
* @var DeviceTrackingServiceInterface
*/
private $deviceTrackingService;
/**
* @var FieldValueTransformer
*/
private $fieldValueTransformer;
/**
* @var DateHelper
*/
private $dateHelper;
public function __construct(
IpLookupHelper $ipLookupHelper,
TemplatingHelper $templatingHelper,
FormModel $formModel,
PageModel $pageModel,
LeadModel $leadModel,
CampaignModel $campaignModel,
LeadFieldModel $leadFieldModel,
CompanyModel $companyModel,
FormFieldHelper $fieldHelper,
UploadFieldValidator $uploadFieldValidator,
FormUploader $formUploader,
DeviceTrackingServiceInterface $deviceTrackingService,
FieldValueTransformer $fieldValueTransformer,
DateHelper $dateHelper
) {
$this->ipLookupHelper = $ipLookupHelper;
$this->templatingHelper = $templatingHelper;
$this->formModel = $formModel;
$this->pageModel = $pageModel;
$this->leadModel = $leadModel;
$this->campaignModel = $campaignModel;
$this->leadFieldModel = $leadFieldModel;
$this->companyModel = $companyModel;
$this->fieldHelper = $fieldHelper;
$this->uploadFieldValidator = $uploadFieldValidator;
$this->formUploader = $formUploader;
$this->deviceTrackingService = $deviceTrackingService;
$this->fieldValueTransformer = $fieldValueTransformer;
$this->dateHelper = $dateHelper;
}
/**
* {@inheritdoc}
*
* @return SubmissionRepository
*/
public function getRepository()
{
return $this->em->getRepository('MauticFormBundle:Submission');
}
/**
* @param $post
* @param $server
* @param bool $returnEvent
*
* @return bool|array
*
* @throws ORMException
*/
public function saveSubmission($post, $server, Form $form, Request $request, $returnEvent = false)
{
$leadFields = $this->leadFieldModel->getFieldListWithProperties(false);
//everything matches up so let's save the results
$submission = new Submission();
$submission->setDateSubmitted(new \DateTime());
$submission->setForm($form);
//set the landing page the form was submitted from if applicable
if (!empty($post['mauticpage'])) {
$page = $this->pageModel->getEntity((int) $post['mauticpage']);
if (null != $page) {
$submission->setPage($page);
}
}
$ipAddress = $this->ipLookupHelper->getIpAddress();
$submission->setIpAddress($ipAddress);
if (!empty($post['return'])) {
$referer = $post['return'];
} elseif (!empty($server['HTTP_REFERER'])) {
$referer = $server['HTTP_REFERER'];
} else {
$referer = '';
}
//clean the referer by removing mauticError and mauticMessage
$referer = InputHelper::url($referer, null, null, ['mauticError', 'mauticMessage']);
$submission->setReferer($referer);
// Create an event to be dispatched through the processes
$submissionEvent = new SubmissionEvent($submission, $post, $server, $request);
// Get a list of components to build custom fields from
$components = $this->formModel->getCustomComponents();
$fields = $form->getFields();
$fieldArray = [];
$results = [];
$tokens = [];
$leadFieldMatches = [];
$validationErrors = [];
$filesToUpload = new UploadFileCrate();
/** @var Field $f */
foreach ($fields as $f) {
$id = $f->getId();
$type = $f->getType();
$alias = $f->getAlias();
$value = (isset($post[$alias])) ? $post[$alias] : '';
$fieldArray[$id] = [
'id' => $id,
'type' => $type,
'alias' => $alias,
];
if ($f->isCaptchaType()) {
$captcha = $this->fieldHelper->validateFieldValue($type, $value, $f);
if (!empty($captcha)) {
$props = $f->getProperties();
//check for a custom message
$validationErrors[$alias] = (!empty($props['errorMessage'])) ? $props['errorMessage'] : implode('<br />', $captcha);
}
continue;
} elseif ($f->isFileType()) {
try {
$file = $this->uploadFieldValidator->processFileValidation($f, $request);
$value = $file->getClientOriginalName();
$filesToUpload->addFile($file, $f);
} catch (NoFileGivenException $e) { //No error here, we just move to another validation, eg. if a field is required
} catch (FileValidationException $e) {
$validationErrors[$alias] = $e->getMessage();
}
}
if ('' === $value && $f->isRequired()) {
//field is required, but hidden from form because of 'ShowWhenValueExists'
if (false === $f->getShowWhenValueExists() && !isset($post[$alias])) {
continue;
}
//somehow the user got passed the JS validation
$msg = $f->getValidationMessage();
if (empty($msg)) {
$msg = $this->translator->trans(
'mautic.form.field.generic.validationfailed',
[
'%label%' => $f->getLabel(),
],
'validators'
);
}
$validationErrors[$alias] = $msg;
continue;
}
if (isset($components['viewOnlyFields']) && in_array($type, $components['viewOnlyFields'])) {
//don't save items that don't have a value associated with it
continue;
}
//clean and validate the input
if ($f->isCustom()) {
if (!isset($components['fields'][$f->getType()])) {
continue;
}
$params = $components['fields'][$f->getType()];
if (!empty($value)) {
if (isset($params['valueFilter'])) {
if (is_string($params['valueFilter']) && is_callable(['\Mautic\CoreBundle\Helper\InputHelper', $params['valueFilter']])) {
$value = InputHelper::_($value, $params['valueFilter']);
} elseif (is_callable($params['valueFilter'])) {
$value = call_user_func_array($params['valueFilter'], [$f, $value]);
} else {
$value = InputHelper::_($value, 'clean');
}
} else {
$value = InputHelper::_($value, 'clean');
}
}
} elseif (!empty($value)) {
$filter = $this->fieldHelper->getFieldFilter($type);
$value = InputHelper::_($value, $filter);
$isValid = $this->validateFieldValue($f, $value);
if (true !== $isValid) {
$validationErrors[$alias] = is_array($isValid) ? implode('<br />', $isValid) : $isValid;
}
}
// Check for custom validators
$isValid = $this->validateFieldValue($f, $value);
if (true !== $isValid) {
$validationErrors[$alias] = $isValid;
}
$leadField = $f->getLeadField();
if (!empty($leadField)) {
$leadValue = $value;
$leadFieldMatches[$leadField] = $leadValue;
}
//convert array from checkbox groups and multiple selects
if (is_array($value)) {
$value = implode(', ', $value);
}
$tokens["{formfield={$alias}}"] = $value;
//save the result
if (false !== $f->getSaveResult()) {
$results[$alias] = $value;
}
}
// Set the results
$submission->setResults($results);
// Update the event
$submissionEvent->setFields($fieldArray)
->setTokens($tokens)
->setResults($results)
->setContactFieldMatches($leadFieldMatches);
// Create/update lead
if (!empty($leadFieldMatches)) {
$this->createLeadFromSubmit($form, $leadFieldMatches, $leadFields);
}
$lead = $this->leadModel->getCurrentLead();
$trackedDevice = $this->deviceTrackingService->getTrackedDevice();
$trackingId = (null === $trackedDevice ? null : $trackedDevice->getTrackingId());
//set tracking ID for stats purposes to determine unique hits
$submission->setTrackingId($trackingId)
->setLead($lead);
// Remove validation errors if the field is not visible
if ($lead && $form->usesProgressiveProfiling()) {
$leadSubmissions = $this->formModel->getLeadSubmissions($form, $lead->getId());
foreach ($fields as $field) {
if (isset($validationErrors[$field->getAlias()]) && !$field->showForContact($leadSubmissions, $lead, $form)) {
unset($validationErrors[$field->getAlias()]);
}
}
}
//return errors if there any
if (!empty($validationErrors)) {
return ['errors' => $validationErrors];
}
/*
* Process File upload and save the result to the entity
* Upload is here to minimize a need for deleting file if there is a validation error
* The action can still be invalidated below - deleteEntity takes care for File deletion
*
* @todo Refactor form validation to execute this code only if Submission is valid
*/
try {
$this->formUploader->uploadFiles($filesToUpload, $submission);
} catch (FileUploadException $e) {
$msg = $this->translator->trans('mautic.form.submission.error.file.uploadFailed', [], 'validators');
$validationErrors[$e->getMessage()] = $msg;
return ['errors' => $validationErrors];
}
// Save the submission
$this->saveEntity($submission);
$this->fieldValueTransformer->transformValuesAfterSubmit($submissionEvent);
// Now handle post submission actions
try {
$this->executeFormActions($submissionEvent);
} catch (ValidationException $exception) {
// The action invalidated the form for whatever reason
$this->deleteEntity($submission);
if ($validationErrors = $exception->getViolations()) {
return ['errors' => $validationErrors];
}
return ['errors' => [$exception->getMessage()]];
}
// update contact fields with transform values
if (!empty($this->fieldValueTransformer->getContactFieldsToUpdate())) {
$this->leadModel->setFieldValues($lead, $this->fieldValueTransformer->getContactFieldsToUpdate());
$this->leadModel->saveEntity($lead, false);
}
if (!$form->isStandalone()) {
// Find and add the lead to the associated campaigns
$campaigns = $this->campaignModel->getCampaignsByForm($form);
if (!empty($campaigns)) {
foreach ($campaigns as $campaign) {
$this->campaignModel->addLead($campaign, $lead);
}
}
}
if ($this->dispatcher->hasListeners(FormEvents::FORM_ON_SUBMIT)) {
// Reset action config from executeFormActions()
$submissionEvent->setAction(null);
// Dispatch to on submit listeners
$this->dispatcher->dispatch(FormEvents::FORM_ON_SUBMIT, $submissionEvent);
}
//get callback commands from the submit action
if ($submissionEvent->hasPostSubmitCallbacks()) {
return ['callback' => $submissionEvent];
}
// made it to the end so return the submission event to give the calling method access to tokens, results, etc
// otherwise return false that no errors were encountered (to keep BC really)
return ($returnEvent) ? ['submission' => $submissionEvent] : false;
}
/**
* @param Submission $submission
*/
public function deleteEntity($submission)
{
$this->formUploader->deleteUploadedFiles($submission);
parent::deleteEntity($submission);
}
/**
* {@inheritdoc}
*/
public function getEntities(array $args = [])
{
return $this->getRepository()->getEntities($args);
}
/**
* @param $format
* @param $form
* @param $queryArgs
*
* @return StreamedResponse|Response
*
* @throws \Exception
*/
public function exportResults($format, $form, $queryArgs)
{
$viewOnlyFields = $this->formModel->getCustomComponents()['viewOnlyFields'];
$queryArgs['viewOnlyFields'] = $viewOnlyFields;
$queryArgs['simpleResults'] = true;
$results = $this->getEntities($queryArgs);
$translator = $this->translator;
$date = (new DateTimeHelper())->toLocalString();
$name = str_replace(' ', '_', $date).'_'.$form->getAlias();
switch ($format) {
case 'csv':
$response = new StreamedResponse(
function () use ($results, $form, $translator, $viewOnlyFields) {
$handle = fopen('php://output', 'r+');
//build the header row
$fields = $form->getFields();
$header = [
$translator->trans('mautic.core.id'),
$translator->trans('mautic.form.result.thead.date'),
$translator->trans('mautic.core.ipaddress'),
$translator->trans('mautic.form.result.thead.referrer'),
];
foreach ($fields as $f) {
if (in_array($f->getType(), $viewOnlyFields) || false === $f->getSaveResult()) {
continue;
}
$header[] = $f->getLabel();
}
//free memory
unset($fields);
//write the row
fputcsv($handle, $header);
//build the data rows
foreach ($results as $k => $s) {
$row = [
$s['id'],
$this->dateHelper->toFull($s['dateSubmitted'], 'UTC'),
$s['ipAddress'],
$s['referer'],
];
foreach ($s['results'] as $k2 => $r) {
if (in_array($r['type'], $viewOnlyFields)) {
continue;
}
$row[] = htmlspecialchars_decode($r['value'], ENT_QUOTES);
//free memory
unset($s['results'][$k2]);
}
fputcsv($handle, $row);
//free memory
unset($row, $results[$k]);
}
fclose($handle);
}
);
$response->headers->set('Content-Type', 'application/force-download');
$response->headers->set('Content-Type', 'application/octet-stream');
$response->headers->set('Content-Disposition', 'attachment; filename="'.$name.'.csv"');
$response->headers->set('Expires', 0);
$response->headers->set('Cache-Control', 'must-revalidate');
$response->headers->set('Pragma', 'public');
return $response;
case 'html':
$content = $this->templatingHelper->getTemplating()->renderResponse(
'MauticFormBundle:Result:export.html.php',
[
'form' => $form,
'results' => $results,
'pageTitle' => $name,
'viewOnlyFields' => $viewOnlyFields,
]
)->getContent();
return new Response($content);
case 'xlsx':
if (class_exists(Spreadsheet::class)) {
$response = new StreamedResponse(
function () use ($results, $form, $translator, $name, $viewOnlyFields) {
$objPHPExcel = new Spreadsheet();
$objPHPExcel->getProperties()->setTitle($name);
$objPHPExcel->createSheet();
//build the header row
$fields = $form->getFields();
$header = [
$translator->trans('mautic.core.id'),
$translator->trans('mautic.form.result.thead.date'),
$translator->trans('mautic.core.ipaddress'),
$translator->trans('mautic.form.result.thead.referrer'),
];
foreach ($fields as $f) {
if (in_array($f->getType(), $viewOnlyFields) || false === $f->getSaveResult()) {
continue;
}
$header[] = $f->getLabel();
}
//free memory
unset($fields);
//write the row
$objPHPExcel->getActiveSheet()->fromArray($header, null, 'A1');
//build the data rows
$count = 2;
foreach ($results as $k => $s) {
$row = [
$s['id'],
$this->dateHelper->toFull($s['dateSubmitted'], 'UTC'),
$s['ipAddress'],
$s['referer'],
];
foreach ($s['results'] as $k2 => $r) {
if (in_array($r['type'], $viewOnlyFields)) {
continue;
}
$row[] = htmlspecialchars_decode($r['value'], ENT_QUOTES);
//free memory
unset($s['results'][$k2]);
}
$objPHPExcel->getActiveSheet()->fromArray($row, null, "A{$count}");
//free memory
unset($row, $results[$k]);
//increment letter
++$count;
}
$objWriter = IOFactory::createWriter($objPHPExcel, 'Xlsx');
$objWriter->setPreCalculateFormulas(false);
$objWriter->save('php://output');
}
);
$response->headers->set('Content-Type', 'application/force-download');
$response->headers->set('Content-Type', 'application/octet-stream');
$response->headers->set('Content-Disposition', 'attachment; filename="'.$name.'.xlsx"');
$response->headers->set('Expires', 0);
$response->headers->set('Cache-Control', 'must-revalidate');
$response->headers->set('Pragma', 'public');
return $response;
}
throw new \Exception('PHPSpreadsheet is required to export to Excel spreadsheets');
default:
return new Response();
}
}
/**
* Get line chart data of submissions.
*
* @param string $unit {@link php.net/manual/en/function.date.php#refsect1-function.date-parameters}
* @param string $dateFormat
* @param array $filter
* @param bool $canViewOthers
*
* @return array
*/
public function getSubmissionsLineChartData(
$unit,
\DateTime $dateFrom,
\DateTime $dateTo,
$dateFormat = null,
$filter = [],
$canViewOthers = true
) {
$chart = new LineChart($unit, $dateFrom, $dateTo, $dateFormat);
$query = new ChartQuery($this->em->getConnection(), $dateFrom, $dateTo);
$q = $query->prepareTimeDataQuery('form_submissions', 'date_submitted', $filter);
if (!$canViewOthers) {
$q->join('t', MAUTIC_TABLE_PREFIX.'forms', 'f', 'f.id = t.form_id')
->andWhere('f.created_by = :userId')
->setParameter('userId', $this->userHelper->getUser()->getId());
}
$data = $query->loadAndBuildTimeData($q);
$chart->setDataset($this->translator->trans('mautic.form.submission.count'), $data);
return $chart->render();
}
/**
* Get a list of top submission referrers.
*
* @param int $limit
* @param string $dateFrom
* @param string $dateTo
* @param array $filters
* @param bool $canViewOthers
*
* @return array
*/
public function getTopSubmissionReferrers($limit = 10, $dateFrom = null, $dateTo = null, $filters = [], $canViewOthers = true)
{
$q = $this->em->getConnection()->createQueryBuilder();
$q->select('COUNT(DISTINCT t.id) AS submissions, t.referer')
->from(MAUTIC_TABLE_PREFIX.'form_submissions', 't')
->orderBy('submissions', 'DESC')
->groupBy('t.referer')
->setMaxResults($limit);
if (!$canViewOthers) {
$q->join('t', MAUTIC_TABLE_PREFIX.'forms', 'f', 'f.id = t.form_id')
->andWhere('f.created_by = :userId')
->setParameter('userId', $this->userHelper->getUser()->getId());
}
$chartQuery = new ChartQuery($this->em->getConnection(), $dateFrom, $dateTo);
$chartQuery->applyFilters($q, $filters);
$chartQuery->applyDateFilters($q, 'date_submitted');
$results = $q->execute()->fetchAll();
return $results;
}
/**
* Get a list of the most submisions per lead.
*
* @param int $limit
* @param string $dateFrom
* @param string $dateTo
* @param array $filters
* @param bool $canViewOthers
*
* @return array
*/
public function getTopSubmitters($limit = 10, $dateFrom = null, $dateTo = null, $filters = [], $canViewOthers = true)
{
$q = $this->em->getConnection()->createQueryBuilder();
$q->select('COUNT(DISTINCT t.id) AS submissions, t.lead_id, l.firstname, l.lastname, l.email')
->from(MAUTIC_TABLE_PREFIX.'form_submissions', 't')
->join('t', MAUTIC_TABLE_PREFIX.'leads', 'l', 'l.id = t.lead_id')
->orderBy('submissions', 'DESC')
->groupBy('t.lead_id, l.firstname, l.lastname, l.email')
->setMaxResults($limit);
if (!$canViewOthers) {
$q->join('t', MAUTIC_TABLE_PREFIX.'forms', 'f', 'f.id = t.form_id')
->andWhere('f.created_by = :userId')
->setParameter('userId', $this->userHelper->getUser()->getId());
}
$chartQuery = new ChartQuery($this->em->getConnection(), $dateFrom, $dateTo);
$chartQuery->applyFilters($q, $filters);
$chartQuery->applyDateFilters($q, 'date_submitted');
$results = $q->execute()->fetchAll();
return $results;
}
/**
* Execute a form submit action.
*
* @throws ValidationException
*/
protected function executeFormActions(SubmissionEvent $event): void
{
$actions = $event->getSubmission()->getForm()->getActions();
$availableActions = $this->formModel->getCustomComponents()['actions'];
$actions->filter(function (Action $action) use ($availableActions) {
return array_key_exists($action->getType(), $availableActions);
})->map(function (Action $action) use ($event, $availableActions) {
$event->setAction($action);
$this->dispatcher->dispatch($availableActions[$action->getType()]['eventName'], $event);
});
}
/**
* Create/update lead from form submit.
*
* @return Lead
*
* @throws ORMException
*/
protected function createLeadFromSubmit(Form $form, array $leadFieldMatches, $leadFields)
{
//set the mapped data
$inKioskMode = $form->isInKioskMode();
$leadId = null;
$lead = new Lead();
$currentFields = $leadFieldMatches;
$companyFields = $this->leadFieldModel->getFieldListWithProperties('company');
if (!$inKioskMode) {
// Default to currently tracked lead
if ($currentLead = $this->leadModel->getCurrentLead()) {
$lead = $currentLead;
$leadId = $lead->getId();
$currentFields = $lead->getProfileFields();
}
$this->logger->debug('FORM: Not in kiosk mode so using current contact ID #'.$leadId);
} else {
// Default to a new lead in kiosk mode
$lead->setNewlyCreated(true);
$this->logger->debug('FORM: In kiosk mode so assuming a new contact');
}
$uniqueLeadFields = $this->leadFieldModel->getUniqueIdentifierFields();
// Closure to get data and unique fields
$getData = function ($currentFields, $uniqueOnly = false) use ($leadFields, $uniqueLeadFields) {
$uniqueFieldsWithData = $data = [];
foreach ($leadFields as $alias => $properties) {
if (isset($currentFields[$alias])) {
$value = $currentFields[$alias];
$data[$alias] = $value;
// make sure the value is actually there and the field is one of our uniques
if (!empty($value) && array_key_exists($alias, $uniqueLeadFields)) {
$uniqueFieldsWithData[$alias] = $value;
}
}
}
return ($uniqueOnly) ? $uniqueFieldsWithData : [$data, $uniqueFieldsWithData];
};
// Closure to get data and unique fields
$getCompanyData = function ($currentFields) use ($companyFields) {
$companyData = [];
// force add company contact field to company fields check
$companyFields = array_merge($companyFields, ['company'=> 'company']);
foreach ($companyFields as $alias => $properties) {
if (isset($currentFields[$alias])) {
$value = $currentFields[$alias];
$companyData[$alias] = $value;
}
}
return $companyData;
};
// Closure to help search for a conflict
$checkForIdentifierConflict = function ($fieldSet1, $fieldSet2) {
// Find fields in both sets
$potentialConflicts = array_keys(
array_intersect_key($fieldSet1, $fieldSet2)
);
$this->logger->debug(
'FORM: Potential conflicts '.implode(', ', array_keys($potentialConflicts)).' = '.implode(', ', $potentialConflicts)
);
$conflicts = [];
foreach ($potentialConflicts as $field) {
if (!empty($fieldSet1[$field]) && !empty($fieldSet2[$field])) {
if (strtolower($fieldSet1[$field]) !== strtolower($fieldSet2[$field])) {
$conflicts[] = $field;
}
}
}
return [count($conflicts), $conflicts];
};
// Get data for the form submission
list($data, $uniqueFieldsWithData) = $getData($leadFieldMatches);
$this->logger->debug('FORM: Unique fields submitted include '.implode(', ', $uniqueFieldsWithData));
// Check for duplicate lead
/** @var \Mautic\LeadBundle\Entity\Lead[] $leads */
$leads = (!empty($uniqueFieldsWithData)) ? $this->em->getRepository('MauticLeadBundle:Lead')->getLeadsByUniqueFields(
$uniqueFieldsWithData,
$leadId
) : [];
$uniqueFieldsCurrent = $getData($currentFields, true);
if (count($leads)) {
$this->logger->debug(count($leads).' found based on unique identifiers');
/** @var \Mautic\LeadBundle\Entity\Lead $foundLead */
$foundLead = $leads[0];
$this->logger->debug('FORM: Testing contact ID# '.$foundLead->getId().' for conflicts');
// Check for a conflict with the currently tracked lead
$foundLeadFields = $this->leadModel->flattenFields($foundLead->getFields());
// Get unique identifier fields for the found lead then compare with the lead currently tracked
$uniqueFieldsFound = $getData($foundLeadFields, true);
list($hasConflict, $conflicts) = $checkForIdentifierConflict($uniqueFieldsFound, $uniqueFieldsCurrent);
if ($inKioskMode || $hasConflict || !$lead->getId()) {
// Use the found lead without merging because there is some sort of conflict with unique identifiers or in kiosk mode and thus should not merge
$lead = $foundLead;
if ($hasConflict) {
$this->logger->debug('FORM: Conflicts found in '.implode(', ', $conflicts).' so not merging');
} else {
$this->logger->debug('FORM: In kiosk mode so not merging');
}
} else {
$this->logger->debug('FORM: Merging contacts '.$lead->getId().' and '.$foundLead->getId());
// Merge the found lead with currently tracked lead
$lead = $this->leadModel->mergeLeads($lead, $foundLead);
}
// Update unique fields data for comparison with submitted data
$currentFields = $lead->getProfileFields();
$uniqueFieldsCurrent = $getData($currentFields, true);
}
if (!$inKioskMode) {
// Check for conflicts with the submitted data and the currently tracked lead
list($hasConflict, $conflicts) = $checkForIdentifierConflict($uniqueFieldsWithData, $uniqueFieldsCurrent);
$this->logger->debug(
'FORM: Current unique contact fields '.implode(', ', array_keys($uniqueFieldsCurrent)).' = '.implode(', ', $uniqueFieldsCurrent)
);
$this->logger->debug(
'FORM: Submitted unique contact fields '.implode(', ', array_keys($uniqueFieldsWithData)).' = '.implode(', ', $uniqueFieldsWithData)
);
if ($hasConflict) {
// There's a conflict so create a new lead
$lead = new Lead();
$lead->setNewlyCreated(true);
$this->logger->debug(
'FORM: Conflicts found in '.implode(', ', $conflicts)
.' between current tracked contact and submitted data so assuming a new contact'
);
}
}
//check for existing IP address
$ipAddress = $this->ipLookupHelper->getIpAddress();
//no lead was found by a mapped email field so create a new one
if ($lead->isNewlyCreated()) {
if (!$inKioskMode) {
$lead->addIpAddress($ipAddress);
$this->logger->debug('FORM: Associating '.$ipAddress->getIpAddress().' to contact');
}
} elseif (!$inKioskMode) {
$leadIpAddresses = $lead->getIpAddresses();
if (!$leadIpAddresses->contains($ipAddress)) {
$lead->addIpAddress($ipAddress);
$this->logger->debug('FORM: Associating '.$ipAddress->getIpAddress().' to contact');
}
}
//set the mapped fields
$this->leadModel->setFieldValues($lead, $data, false, true, true);
// last active time
$lead->setLastActive(new \DateTime());
//create a new lead
$lead->setManipulator(new LeadManipulator(
'form',
'submission',
$form->getId(),
$form->getName()
));
$this->leadModel->saveEntity($lead, false);
if (!$inKioskMode) {
// Set the current lead which will generate tracking cookies
$this->leadModel->setCurrentLead($lead);
} else {
// Set system current lead which will still allow execution of events without generating tracking cookies
$this->leadModel->setSystemCurrentLead($lead);
}
$companyFieldMatches = $getCompanyData($leadFieldMatches);
if (!empty($companyFieldMatches)) {
list($company, $leadAdded, $companyEntity) = IdentifyCompanyHelper::identifyLeadsCompany($companyFieldMatches, $lead, $this->companyModel);
if ($leadAdded) {
$lead->addCompanyChangeLogEntry('form', 'Identify Company', 'Lead added to the company, '.$company['companyname'], $company['id']);
} elseif ($companyEntity instanceof Company) {
$this->companyModel->setFieldValues($companyEntity, $companyFieldMatches);
$this->companyModel->saveEntity($companyEntity);
}
if (!empty($company) and $companyEntity instanceof Company) {
// Save after the lead in for new leads created through the API and maybe other places
$this->companyModel->addLeadToCompany($companyEntity, $lead);
$this->leadModel->setPrimaryCompany($companyEntity->getId(), $lead->getId());
}
$this->em->clear(CompanyChangeLog::class);
}
return $lead;
}
/**
* Validates a field value.
*
* @param $value
*
* @return bool|string True if valid; otherwise string with invalid reason
*/
protected function validateFieldValue(Field $field, $value)
{
$standardValidation = $this->fieldHelper->validateFieldValue($field->getType(), $value, $field);
if (!empty($standardValidation)) {
return $standardValidation;
}
$components = $this->formModel->getCustomComponents();
foreach ([$field->getType(), 'form'] as $type) {
if (isset($components['validators'][$type])) {