/
controller.php
97 lines (86 loc) · 3.31 KB
/
controller.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
<?php
namespace Concrete\Block\DesktopLatestForm;
use Concrete\Core\Block\BlockController;
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Feature\Features;
use Concrete\Core\Feature\UsesFeatureInterface;
use Concrete\Core\Url\Resolver\Manager\ResolverManagerInterface;
use Doctrine\ORM\EntityManagerInterface;
class Controller extends BlockController implements UsesFeatureInterface
{
/**
* @return string
*/
public function getBlockTypeDescription()
{
return t('Shows the latest form submission.');
}
/**
* @return string
*/
public function getBlockTypeName()
{
return t('Latest Form');
}
/**
* @return string[]
*/
public function getRequiredFeatures(): array
{
return [Features::DESKTOP];
}
/**
* @throws \Doctrine\DBAL\Driver\Exception
* @throws \Doctrine\DBAL\Exception
* @throws \Doctrine\ORM\NonUniqueResultException
*
* @return void
*/
public function view()
{
/** @var Connection $db */
$db = app(Connection::class);
$r = $db->executeQuery('select * from btFormAnswerSet order by created desc limit 1');
$row = $r->fetchAssociative();
$legacyDateCreated = $row === false ? null : strtotime($row['created']);
/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager = app(EntityManagerInterface::class);
$forms = $entityManager->getRepository('Concrete\Core\Entity\Express\Entity')
->findExpressForms()
;
$ids = [];
foreach ($forms as $form) {
$ids[] = $form->getID();
}
if (count($ids)) {
$q = $entityManager->createQuery(
'select e from Concrete\Core\Entity\Express\Entry e where e.entity in (:entities) order by e.exEntryDateCreated desc'
);
$q->setParameter('entities', $forms);
$q->setMaxResults(1);
$result = $q->getOneOrNullResult();
} else {
$result = null;
}
$formDateCreated = 0;
if (is_object($result)) {
$formDateCreated = app('date')->toDateTime($result->getDateCreated())->getTimestamp();
}
if ($legacyDateCreated !== null && $legacyDateCreated > $formDateCreated) {
if (is_array($row) && isset($row['questionSetId'])) {
$r = $db->executeQuery('select * from btForm where questionSetId = ?', [$row['questionSetId']]);
$row2 = $r->fetchAssociative();
if (is_array($row2) && isset($row2['bID'])) {
$this->set('formName', $row2['surveyName']);
$this->set('date', app('date')->formatDateTime(strtotime($row['created'])));
$this->set('link', app(ResolverManagerInterface::class)->resolve(['/dashboard/reports/forms/legacy']) . '?qsid=' . $row['questionSetId']);
}
}
} elseif (is_object($result)) {
$entity = $result->getEntity();
$this->set('formName', $entity->getEntityDisplayName());
$this->set('date', app('date')->formatDateTime($result->getDateCreated()));
$this->set('link', app(ResolverManagerInterface::class)->resolve(['/dashboard/reports/forms/view_entry', $result->getID()]));
}
}
}