/
controller.php
208 lines (178 loc) · 6.56 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
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
<?php
namespace Concrete\Block\ExpressEntryDetail;
use Concrete\Core\Attribute\Key\CollectionKey;
use Concrete\Core\Block\BlockController;
use Concrete\Core\Express\Form\Context\FrontendViewContext;
use Concrete\Core\Express\Form\Renderer;
use Concrete\Core\Feature\Features;
use Concrete\Core\Feature\UsesFeatureInterface;
use Concrete\Core\Form\Context\ContextFactory;
use Concrete\Core\Html\Service\Seo;
use Concrete\Core\Support\Facade\Express;
use Concrete\Core\Support\Facade\Facade;
use Concrete\Core\Url\SeoCanonical;
use Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
class Controller extends BlockController implements UsesFeatureInterface
{
protected $btInterfaceWidth = '640';
protected $btInterfaceHeight = '400';
protected $btTable = 'btExpressEntryDetail';
public $exEntityID;
public $exSpecificEntryID;
public $exEntryAttributeKeyHandle;
public $exFormID;
public $entryMode;
/**
* @var \Doctrine\ORM\EntityManager
*/
protected $entityManager;
public function on_start()
{
parent::on_start();
$this->app = Facade::getFacadeApplication();
$this->entityManager = $this->app->make('database/orm')->entityManager();
}
public function getBlockTypeDescription()
{
return t('Add an Express entry detail display to a page.');
}
public function getBlockTypeName()
{
return t('Express Entry Detail');
}
public function getBlockTypeInSetName()
{
return t('Details');
}
public function getRequiredFeatures(): array
{
return [
Features::EXPRESS,
];
}
public function add()
{
$this->loadData();
$this->set('entryMode', 'L');
$this->set('exEntityID', null);
$this->set('entity', null);
$this->set('exEntryAttributeKeyHandle', null);
$this->set('exFormID', null);
}
public function edit()
{
$this->loadData();
if ($this->exEntityID) {
$entity = $this->entityManager->find('Concrete\Core\Entity\Express\Entity', $this->exEntityID);
if (is_object($entity)) {
$this->set('entity', $entity);
}
if ($this->exSpecificEntryID) {
$this->set('entry', Express::getEntry($this->exSpecificEntryID));
}
}
$this->set('exEntryAttributeKeyHandle', $this->exEntryAttributeKeyHandle ?? null);
}
public function view()
{
$c = \Page::getCurrentPage();
$entity = null;
if ($this->entryMode == 'A') {
$ak = CollectionKey::getByHandle($this->exEntryAttributeKeyHandle);
if (is_object($ak)) {
$settings = $ak->getAttributeKeySettings();
$value = $c->getAttribute($ak);
if (is_object($settings)) {
$entity = $settings->getEntity();
$this->set('entity', $settings->getEntity());
}
if (is_object($value)) {
$this->set('entry', $value->getSelectedEntries()[0]);
}
}
} else {
$entity = $this->entityManager->find('Concrete\Core\Entity\Express\Entity', $this->exEntityID);
if (is_object($entity)) {
$this->set('entity', $entity);
if ($this->entryMode == 'S' && $this->exSpecificEntryID) {
$this->set('entry', Express::getEntry($this->exSpecificEntryID));
}
}
}
$form = null;
try {
$form = $this->entityManager->find('Concrete\Core\Entity\Express\Form', $this->exFormID);
} catch (Exception $e) {
$logger = $this->app->make('log/exceptions');
$logger->addEmergency($e->getMessage());
}
if ($form && $entity) {
$express = \Core::make('express');
$controller = $express->getEntityController($entity);
$factory = new ContextFactory($controller);
$context = $factory->getContext(new FrontendViewContext());
$renderer = new Renderer(
$context,
$form
);
$this->set('renderer', $renderer);
}
}
public function action_view_express_entity($exEntryID = null)
{
$entry = $this->entityManager->find('Concrete\Core\Entity\Express\Entry', $exEntryID);
if (is_object($entry)) {
$entity = $this->entityManager->find('Concrete\Core\Entity\Express\Entity', $this->exEntityID);
if ($entry->getEntity()->getID() == $entity->getID()) {
/** @var Seo $seo */
$seo = $this->app->make('helper/seo');
$seo->addTitleSegmentBefore($entry->getLabel());
/** @var SeoCanonical $canonical */
$canonical = $this->app->make(SeoCanonical::class);
$canonical->setPathArguments(['view_express_entity', $exEntryID]);
$this->set('entry', $entry);
$this->view();
}
}
}
public function action_load_entity_data()
{
$exEntityID = $this->request->request->get('exEntityID');
if ($exEntityID) {
$entity = $this->entityManager->find('Concrete\Core\Entity\Express\Entity', $exEntityID);
if (is_object($entity)) {
$r = new \stdClass();
$r->forms = [];
foreach ($entity->getForms() as $form) {
$r->forms[] = $form;
}
ob_start();
$form_selector = $this->app->make('form/express/entry_selector');
echo $form_selector->selectEntry($entity, 'exSpecificEntryID');
$r->selector = ob_get_contents();
ob_end_clean();
return new JsonResponse($r);
}
}
\Core::make('app')->shutdown();
}
public function loadData()
{
$r = $this->entityManager->getRepository('Concrete\Core\Entity\Express\Entity');
$entityObjects = $r->findAll();
$entities = ['' => t('** Choose Entity')];
foreach ($entityObjects as $entity) {
$entities[$entity->getID()] = $entity->getEntityDisplayName();
}
$this->set('entities', $entities);
$attributeKeys = [];
$keys = CollectionKey::getList();
foreach ($keys as $ak) {
if ($ak->getAttributeTypeHandle() == 'express') {
$attributeKeys[] = $ak;
}
}
$this->set('expressAttributes', $attributeKeys);
}
}