blob: 01c90da1b478ca2329a985a54730af61df1333cf [file] [log] [blame]
BacLucab3ad352016-04-07 20:13:05 +02001<?php
BacLuc4b5de062020-04-05 16:03:46 +02002
BacLucab3ad352016-04-07 20:13:05 +02003namespace Concrete\Package\BaclucEventPackage\Block\BaclucEventBlock;
4
BacLuc4b5de062020-04-05 16:03:46 +02005use BaclucC5Crud\Adapters\Concrete5\Concrete5Renderer;
BacLuc0f233592020-02-22 19:34:31 +01006use BaclucC5Crud\Adapters\Concrete5\DIContainerFactory;
7use BaclucC5Crud\Controller\ActionProcessor;
BacLuc4b5de062020-04-05 16:03:46 +02008use BaclucC5Crud\Controller\ActionRegistry;
BacLuc0f233592020-02-22 19:34:31 +01009use BaclucC5Crud\Controller\ActionRegistryFactory;
10use BaclucC5Crud\Controller\CrudController;
BacLuc4b5de062020-04-05 16:03:46 +020011use BaclucC5Crud\Controller\Renderer;
12use BaclucC5Crud\Controller\RowActionConfiguration;
BacLuc0f233592020-02-22 19:34:31 +010013use BaclucC5Crud\FieldConfigurationOverride\EntityFieldOverrideBuilder;
14use BaclucC5Crud\View\FormType;
BacLuc4b5de062020-04-05 16:03:46 +020015use BaclucC5Crud\View\TableView\DontShowTableField;
16use BaclucC5Crud\View\TableView\Field as TableField;
17use BaclucC5Crud\View\ViewActionRegistry;
BacLuc0f233592020-02-22 19:34:31 +010018use BaclucEventPackage\Event;
BacLuc4b5de062020-04-05 16:03:46 +020019use BaclucEventPackage\EventActionRegistryFactory;
20use BaclucEventPackage\EventCancellation;
21use BaclucEventPackage\EventRowActionConfiguration;
22use BaclucEventPackage\NextEvent\NextEventConfiguration;
23use BaclucEventPackage\NoEditIdFallbackActionProcessor;
24use BaclucEventPackage\ShowErrorActionProcessor;
25use BaclucEventPackage\ViewActionRegistryFactory;
BacLucab3ad352016-04-07 20:13:05 +020026use Concrete\Core\Block\BlockController;
BacLuc3e987ec2020-03-15 17:43:19 +010027use Concrete\Core\Package\PackageService;
BacLuc9865d8a2020-03-14 17:15:50 +010028use Concrete\Core\Page\Page;
BacLuc0f233592020-02-22 19:34:31 +010029use Concrete\Core\Routing\Redirect;
BacLuc3e987ec2020-03-15 17:43:19 +010030use Concrete\Core\Support\Facade\Application;
BacLuc0f233592020-02-22 19:34:31 +010031use Concrete\Package\BaclucC5Crud\Controller as PackageController;
BacLuc3e987ec2020-03-15 17:43:19 +010032use Concrete\Package\BaclucEventPackage\Controller as EventPackageController;
BacLuc4b5de062020-04-05 16:03:46 +020033use DI\ContainerBuilder;
BacLuc0f233592020-02-22 19:34:31 +010034use DI\DependencyException;
35use DI\NotFoundException;
36use Exception;
BacLuc4b5de062020-04-05 16:03:46 +020037use Psr\Container\ContainerInterface;
BacLuc0f233592020-02-22 19:34:31 +010038use ReflectionException;
BacLuc4b5de062020-04-05 16:03:46 +020039use function DI\autowire;
40use function DI\create;
41use function DI\factory;
42use function DI\value;
BacLucab3ad352016-04-07 20:13:05 +020043
BacLuc0f233592020-02-22 19:34:31 +010044class Controller extends BlockController
BacLucab3ad352016-04-07 20:13:05 +020045{
BacLucab3ad352016-04-07 20:13:05 +020046
BacLuc5b1c5bd2016-04-08 14:50:38 +020047
48 /**
BacLuc0f233592020-02-22 19:34:31 +010049 * @throws DependencyException
50 * @throws NotFoundException
51 * @throws ReflectionException
BacLuc5b1c5bd2016-04-08 14:50:38 +020052 */
BacLuc0f233592020-02-22 19:34:31 +010053 public function view()
BacLucab3ad352016-04-07 20:13:05 +020054 {
BacLuc9865d8a2020-03-14 17:15:50 +010055 $this->processAction($this->createCrudController()
BacLuc4a71e1b2020-03-15 12:10:33 +010056 ->getActionFor(ActionRegistryFactory::SHOW_TABLE, $this->bID));
BacLucab3ad352016-04-07 20:13:05 +020057 }
58
BacLuc0f233592020-02-22 19:34:31 +010059 /**
60 * @throws DependencyException
61 * @throws NotFoundException
62 * @throws ReflectionException
63 */
BacLucf6d09d12020-04-05 16:24:03 +020064 public function action_view()
65 {
66 $this->view();
67 }
68
69 /**
70 * @throws DependencyException
71 * @throws NotFoundException
72 * @throws ReflectionException
73 */
BacLuc9865d8a2020-03-14 17:15:50 +010074 public function action_add_new_row_form($blockId)
BacLuc0f233592020-02-22 19:34:31 +010075 {
76 $this->processAction($this->createCrudController()
BacLuc4a71e1b2020-03-15 12:10:33 +010077 ->getActionFor(ActionRegistryFactory::ADD_NEW_ROW_FORM, $blockId));
BacLuc0f233592020-02-22 19:34:31 +010078 }
BacLucab3ad352016-04-07 20:13:05 +020079
BacLuc0f233592020-02-22 19:34:31 +010080 /**
81 * @throws DependencyException
82 * @throws NotFoundException
83 * @throws ReflectionException
84 */
BacLuc9865d8a2020-03-14 17:15:50 +010085 public function action_edit_row_form($blockId, $editId)
BacLuc0f233592020-02-22 19:34:31 +010086 {
87 $this->processAction($this->createCrudController()
BacLuc4a71e1b2020-03-15 12:10:33 +010088 ->getActionFor(ActionRegistryFactory::EDIT_ROW_FORM, $blockId),
BacLuc0f233592020-02-22 19:34:31 +010089 $editId);
90 }
91
92 /**
93 * Attention: all action method are called twice.
94 * Because this is a form submission, we stop after the function is executed
95 * @throws DependencyException
96 * @throws NotFoundException
97 * @throws ReflectionException
98 */
BacLuc9865d8a2020-03-14 17:15:50 +010099 public function action_post_form($blockId, $editId = null)
BacLuc0f233592020-02-22 19:34:31 +0100100 {
BacLuc9865d8a2020-03-14 17:15:50 +0100101 $this->processAction($this->createCrudController()
BacLuc4a71e1b2020-03-15 12:10:33 +0100102 ->getActionFor(ActionRegistryFactory::POST_FORM, $blockId),
BacLuc0f233592020-02-22 19:34:31 +0100103 $editId);
104 if ($this->blockViewRenderOverride == null) {
105 Redirect::page(Page::getCurrentPage())->send();
106 exit();
107 }
108 }
109
110 /**
111 * @param $ignored
112 * @param $toDeleteId
113 * @throws DependencyException
114 * @throws NotFoundException
115 * @throws ReflectionException
116 */
BacLuc9865d8a2020-03-14 17:15:50 +0100117 public function action_delete_entry($blockId, $toDeleteId)
BacLuc0f233592020-02-22 19:34:31 +0100118 {
BacLuc9865d8a2020-03-14 17:15:50 +0100119 $this->processAction($this->createCrudController()
BacLuc4a71e1b2020-03-15 12:10:33 +0100120 ->getActionFor(ActionRegistryFactory::DELETE_ENTRY, $blockId),
BacLuc0f233592020-02-22 19:34:31 +0100121 $toDeleteId);
BacLuc8cb48d62020-03-14 17:16:43 +0100122 if ($this->blockViewRenderOverride == null) {
123 Redirect::page(Page::getCurrentPage())->send();
124 exit();
125 }
BacLuc0f233592020-02-22 19:34:31 +0100126 }
127
128 /**
129 * @throws DependencyException
130 * @throws NotFoundException
131 */
BacLuc9865d8a2020-03-14 17:15:50 +0100132 public function action_cancel_form($blockId)
BacLuc0f233592020-02-22 19:34:31 +0100133 {
BacLuc9865d8a2020-03-14 17:15:50 +0100134 $this->processAction($this->createCrudController()
BacLuc4a71e1b2020-03-15 12:10:33 +0100135 ->getActionFor(ActionRegistryFactory::SHOW_TABLE, $blockId));
BacLuc0f233592020-02-22 19:34:31 +0100136 }
137
138 /**
139 * @param $ignored
140 * @param $toShowId
141 * @throws DependencyException
142 * @throws NotFoundException
143 */
BacLuc9865d8a2020-03-14 17:15:50 +0100144 public function action_show_details($blockId, $toShowId)
BacLuc0f233592020-02-22 19:34:31 +0100145 {
146 $this->processAction($this->createCrudController()
BacLuc4a71e1b2020-03-15 12:10:33 +0100147 ->getActionFor(ActionRegistryFactory::SHOW_ENTRY_DETAILS, $blockId),
BacLuc0f233592020-02-22 19:34:31 +0100148 $toShowId);
149 }
150
151 private function processAction(ActionProcessor $actionProcessor, ...$additionalParams)
152 {
BacLuc9a3bc692020-04-29 23:06:05 +0200153 return $actionProcessor->process($this->request->query->all() ?: [],
BacLuc0f233592020-02-22 19:34:31 +0100154 $this->request->post(null) ?: [],
155 array_key_exists(0, $additionalParams) ? $additionalParams[0] : null);
156 }
157
158 /**
159 * @return CrudController
160 * @throws DependencyException
161 * @throws NotFoundException
162 * @throws Exception
163 * @throws ReflectionException
164 */
165 private function createCrudController(): CrudController
166 {
167 $entityManager = PackageController::getEntityManagerStatic();
168 $entityClass = Event::class;
169 $entityFieldOverrides = new EntityFieldOverrideBuilder($entityClass);
170
BacLuc3e987ec2020-03-15 17:43:19 +0100171 $app = Application::getFacadeApplication();
172 /** @var PackageController $packageController */
173 $packageController = $app->make(PackageService::class)->getByHandle(EventPackageController::PACKAGE_HANDLE);
174
BacLuc4b5de062020-04-05 16:03:46 +0200175 $containerBuilder = new ContainerBuilder();
176 $definitions =
177 DIContainerFactory::createDefinition($entityManager,
178 $entityClass,
179 "",
180 $entityFieldOverrides->build(),
181 $this->bID,
182 FormType::$BLOCK_VIEW);
183 $definitions[BlockController::class] = value($this);
184 $definitions[Renderer::class] =
185 create(Concrete5Renderer::class)->constructor($this, $packageController->getPackagePath());
186 $definitions[ViewActionRegistry::class] = factory([ViewActionRegistryFactory::class, "createActionRegistry"]);
187 $definitions[RowActionConfiguration::class] = autowire(EventRowActionConfiguration::class);
188 $containerBuilder->addDefinitions($definitions);
189 $container = $containerBuilder->build();
190 return $container->get(CrudController::class);
191 }
192
193 /**
194 * @param $blockId
195 * @param $editId
196 * @throws DependencyException
197 * @throws NotFoundException
198 * @throws ReflectionException
199 */
200 public function action_show_cancellations($blockId, $editId) {
201 $this->processAction($this->createEventCancellationController()
202 ->getActionFor(EventActionRegistryFactory::SHOW_CANCELLATIONS, $blockId),
203 $editId);
204 }
205
206 /**
207 * @return CrudController
208 * @throws DependencyException
209 * @throws NotFoundException
210 * @throws Exception
211 * @throws ReflectionException
212 */
213 private function createEventCancellationController(): CrudController
214 {
215 $entityManager = PackageController::getEntityManagerStatic();
216 $entityClass = EventCancellation::class;
217 $entityFieldOverrides = new EntityFieldOverrideBuilder($entityClass);
218
219 $entityFieldOverrides->forField("event")
220 ->forType(TableField::class)
221 ->useFactory(DontShowTableField::create())
222 ->buildField();
223
224 $definitions = DIContainerFactory::createDefinition(
BacLuc0f233592020-02-22 19:34:31 +0100225 $entityManager,
226 $entityClass,
BacLuc4b5de062020-04-05 16:03:46 +0200227 NextEventConfiguration::class,
BacLuc0f233592020-02-22 19:34:31 +0100228 $entityFieldOverrides->build(),
BacLuc4a71e1b2020-03-15 12:10:33 +0100229 $this->bID,
BacLuc0f233592020-02-22 19:34:31 +0100230 FormType::$BLOCK_VIEW);
BacLuc4b5de062020-04-05 16:03:46 +0200231
232 $app = Application::getFacadeApplication();
233 /** @var PackageController $packageController */
234 $packageController = $app->make(PackageService::class)->getByHandle(EventPackageController::PACKAGE_HANDLE);
235 $containerBuilder = new ContainerBuilder();
236 $definitions[BlockController::class] = value($this);
237 $definitions[Renderer::class] =
238 create(Concrete5Renderer::class)->constructor($this, $packageController->getPackagePath());
239 $definitions[ActionRegistry::class] = factory(function (ContainerInterface $container) {
240 return $container->get(EventActionRegistryFactory::class)->createActionRegistry();
241 });
242 $definitions[NoEditIdFallbackActionProcessor::class] = autowire(ShowErrorActionProcessor::class);
243 $containerBuilder->addDefinitions($definitions);
244 $container = $containerBuilder->build();
BacLuc0f233592020-02-22 19:34:31 +0100245 return $container->get(CrudController::class);
246 }
BacLucab3ad352016-04-07 20:13:05 +0200247
248 /**
249 * @return string
250 */
251 public function getBlockTypeDescription()
252 {
253 return t("Create, Edit or Delete Events");
254 }
BacLucab3ad352016-04-07 20:13:05 +0200255 /**
256 * @return string
257 */
258 public function getBlockTypeName()
259 {
260 return t("BaclucEventBlock");
261 }
262
BacLuc0f233592020-02-22 19:34:31 +0100263
BacLucab3ad352016-04-07 20:13:05 +0200264}