Complex classes like EventController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EventController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class EventController extends AbstractController |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var EventCacheService |
||
| 40 | */ |
||
| 41 | protected $eventCacheService = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param EventCacheService $cacheService |
||
| 45 | */ |
||
| 46 | public function injectEventCacheService(EventCacheService $cacheService) |
||
| 47 | { |
||
| 48 | $this->eventCacheService = $cacheService; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Assign contentObjectData and pageData to earch view |
||
| 53 | * |
||
| 54 | * @param \TYPO3\CMS\Extbase\Mvc\View\ViewInterface $view |
||
| 55 | */ |
||
| 56 | protected function initializeView(\TYPO3\CMS\Extbase\Mvc\View\ViewInterface $view) |
||
| 57 | { |
||
| 58 | $view->assign('contentObjectData', $this->configurationManager->getContentObject()->data); |
||
| 59 | if (is_object($GLOBALS['TSFE'])) { |
||
| 60 | $view->assign('pageData', $GLOBALS['TSFE']->page); |
||
| 61 | } |
||
| 62 | parent::initializeView($view); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Initializes the current action |
||
| 67 | */ |
||
| 68 | public function initializeAction() |
||
| 69 | { |
||
| 70 | $typoScriptFrontendController = $this->getTypoScriptFrontendController(); |
||
| 71 | if ($typoScriptFrontendController !== null) { |
||
| 72 | static $cacheTagsSet = false; |
||
| 73 | |||
| 74 | if (!$cacheTagsSet) { |
||
| 75 | $typoScriptFrontendController->addCacheTags(['tx_sfeventmgt']); |
||
| 76 | $cacheTagsSet = true; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Creates an event demand object with the given settings |
||
| 83 | * |
||
| 84 | * @param array $settings The settings |
||
| 85 | * |
||
| 86 | * @return \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand |
||
| 87 | */ |
||
| 88 | public function createEventDemandObjectFromSettings(array $settings) |
||
| 89 | { |
||
| 90 | /** @var \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $demand */ |
||
| 91 | $demand = $this->objectManager->get(EventDemand::class); |
||
| 92 | $demand->setDisplayMode($settings['displayMode']); |
||
| 93 | $demand->setStoragePage(Page::extendPidListByChildren($settings['storagePage'], $settings['recursive'])); |
||
| 94 | $demand->setCategoryConjunction($settings['categoryConjunction']); |
||
| 95 | $demand->setCategory($settings['category']); |
||
| 96 | $demand->setIncludeSubcategories($settings['includeSubcategories']); |
||
| 97 | $demand->setTopEventRestriction((int)$settings['topEventRestriction']); |
||
| 98 | $demand->setOrderField($settings['orderField']); |
||
| 99 | $demand->setOrderFieldAllowed($settings['orderFieldAllowed']); |
||
| 100 | $demand->setOrderDirection($settings['orderDirection']); |
||
| 101 | $demand->setQueryLimit($settings['queryLimit']); |
||
| 102 | $demand->setLocation($settings['location']); |
||
| 103 | $demand->setOrganisator($settings['organisator']); |
||
| 104 | $demand->setSpeaker($settings['speaker']); |
||
| 105 | |||
| 106 | return $demand; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Creates a foreign record demand object with the given settings |
||
| 111 | * |
||
| 112 | * @param array $settings The settings |
||
| 113 | * |
||
| 114 | * @return \DERHANSEN\SfEventMgt\Domain\Model\Dto\ForeignRecordDemand |
||
| 115 | */ |
||
| 116 | public function createForeignRecordDemandObjectFromSettings(array $settings) |
||
| 117 | { |
||
| 118 | /** @var \DERHANSEN\SfEventMgt\Domain\Model\Dto\ForeignRecordDemand $demand */ |
||
| 119 | $demand = $this->objectManager->get(ForeignRecordDemand::class); |
||
| 120 | $demand->setStoragePage(Page::extendPidListByChildren($settings['storagePage'], $settings['recursive'])); |
||
| 121 | $demand->setRestrictForeignRecordsToStoragePage((bool)$settings['restrictForeignRecordsToStoragePage']); |
||
| 122 | |||
| 123 | return $demand; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Creates a category demand object with the given settings |
||
| 128 | * |
||
| 129 | * @param array $settings The settings |
||
| 130 | * |
||
| 131 | * @return \DERHANSEN\SfEventMgt\Domain\Model\Dto\CategoryDemand |
||
| 132 | */ |
||
| 133 | public function createCategoryDemandObjectFromSettings(array $settings) |
||
| 134 | { |
||
| 135 | /** @var \DERHANSEN\SfEventMgt\Domain\Model\Dto\CategoryDemand $demand */ |
||
| 136 | $demand = $this->objectManager->get(CategoryDemand::class); |
||
| 137 | 2 | $demand->setStoragePage(Page::extendPidListByChildren($settings['storagePage'], $settings['recursive'])); |
|
| 138 | $demand->setRestrictToStoragePage((bool)$settings['restrictForeignRecordsToStoragePage']); |
||
| 139 | $demand->setCategories($settings['categoryMenu']['categories']); |
||
| 140 | 2 | $demand->setIncludeSubcategories($settings['categoryMenu']['includeSubcategories']); |
|
| 141 | 2 | ||
| 142 | 2 | return $demand; |
|
| 143 | 2 | } |
|
| 144 | 2 | ||
| 145 | 2 | /** |
|
| 146 | 2 | * Hook into request processing and catch exceptions |
|
| 147 | 2 | * |
|
| 148 | 2 | * @param RequestInterface $request |
|
| 149 | 2 | * @param ResponseInterface $response |
|
| 150 | 2 | * @throws \Exception |
|
| 151 | 2 | */ |
|
| 152 | public function processRequest(RequestInterface $request, ResponseInterface $response) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Handle known exceptions |
||
| 163 | * |
||
| 164 | * @param \Exception $exception |
||
| 165 | * @throws \Exception |
||
| 166 | */ |
||
| 167 | private function handleKnownExceptionsElseThrowAgain(\Exception $exception) |
||
| 168 | { |
||
| 169 | $previousException = $exception->getPrevious(); |
||
| 170 | $actions = ['detailAction', 'registrationAction', 'icalDownloadAction']; |
||
| 171 | if (in_array($this->actionMethodName, $actions, true) |
||
| 172 | && $previousException instanceof \TYPO3\CMS\Extbase\Property\Exception |
||
| 173 | ) { |
||
| 174 | $this->handleEventNotFoundError($this->settings); |
||
| 175 | } else { |
||
| 176 | throw $exception; |
||
| 177 | 2 | } |
|
| 178 | } |
||
| 179 | |||
| 180 | 2 | /** |
|
| 181 | 2 | * Initialize list action and set format |
|
| 182 | 2 | * |
|
| 183 | 2 | * @return void |
|
| 184 | 2 | */ |
|
| 185 | 2 | public function initializeListAction() |
|
| 186 | { |
||
| 187 | if (isset($this->settings['list']['format'])) { |
||
| 188 | $this->request->setFormat($this->settings['list']['format']); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * List view |
||
| 194 | * |
||
| 195 | * @param array $overwriteDemand OverwriteDemand |
||
| 196 | 4 | * |
|
| 197 | * @return void |
||
| 198 | 4 | */ |
|
| 199 | 4 | public function listAction(array $overwriteDemand = []) |
|
| 200 | 4 | { |
|
| 201 | $eventDemand = $this->createEventDemandObjectFromSettings($this->settings); |
||
| 202 | 4 | $foreignRecordDemand = $this->createForeignRecordDemandObjectFromSettings($this->settings); |
|
| 203 | 4 | $categoryDemand = $this->createCategoryDemandObjectFromSettings($this->settings); |
|
| 204 | 2 | if ($this->isOverwriteDemand($overwriteDemand)) { |
|
| 205 | $eventDemand = $this->overwriteEventDemandObject($eventDemand, $overwriteDemand); |
||
| 206 | 4 | } |
|
| 207 | 4 | $events = $this->eventRepository->findDemanded($eventDemand); |
|
| 208 | 4 | $categories = $this->categoryRepository->findDemanded($categoryDemand); |
|
| 209 | $locations = $this->locationRepository->findDemanded($foreignRecordDemand); |
||
| 210 | $organisators = $this->organisatorRepository->findDemanded($foreignRecordDemand); |
||
| 211 | $speakers = $this->speakerRepository->findDemanded($foreignRecordDemand); |
||
| 212 | |||
| 213 | $values = [ |
||
| 214 | 'events' => $events, |
||
| 215 | 'categories' => $categories, |
||
| 216 | 'locations' => $locations, |
||
| 217 | 'organisators' => $organisators, |
||
| 218 | 'speakers' => $speakers, |
||
| 219 | 'overwriteDemand' => $overwriteDemand, |
||
| 220 | 'eventDemand' => $eventDemand |
||
| 221 | ]; |
||
| 222 | |||
| 223 | $this->signalDispatch(__CLASS__, __FUNCTION__ . 'BeforeRenderView', [&$values, $this]); |
||
| 224 | $this->view->assignMultiple($values); |
||
| 225 | |||
| 226 | $this->eventCacheService->addPageCacheTagsByEventDemandObject($eventDemand); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | 6 | * Calendar view |
|
| 231 | * |
||
| 232 | 6 | * @param array $overwriteDemand OverwriteDemand |
|
| 233 | 6 | * |
|
| 234 | 6 | * @return void |
|
| 235 | 6 | */ |
|
| 236 | 2 | public function calendarAction(array $overwriteDemand = []) |
|
| 237 | 2 | { |
|
| 238 | 6 | $eventDemand = $this->createEventDemandObjectFromSettings($this->settings); |
|
| 239 | 6 | $foreignRecordDemand = $this->createForeignRecordDemandObjectFromSettings($this->settings); |
|
| 240 | 6 | $categoryDemand = $this->createCategoryDemandObjectFromSettings($this->settings); |
|
| 241 | 6 | if ($this->isOverwriteDemand($overwriteDemand)) { |
|
| 242 | 6 | $eventDemand = $this->overwriteEventDemandObject($eventDemand, $overwriteDemand); |
|
| 243 | 6 | } |
|
| 244 | 6 | ||
| 245 | 6 | // Set month/year to demand if not given |
|
| 246 | if (!$eventDemand->getMonth()) { |
||
| 247 | $currentMonth = date('n'); |
||
| 248 | $eventDemand->setMonth($currentMonth); |
||
| 249 | } else { |
||
| 250 | $currentMonth = $eventDemand->getMonth(); |
||
| 251 | } |
||
| 252 | if (!$eventDemand->getYear()) { |
||
| 253 | $currentYear = date('Y'); |
||
| 254 | 2 | $eventDemand->setYear($currentYear); |
|
| 255 | } else { |
||
| 256 | 2 | $currentYear = $eventDemand->getYear(); |
|
| 257 | 2 | } |
|
| 258 | |||
| 259 | // Set demand from calendar date range instead of month / year |
||
| 260 | if ((bool)$this->settings['calendar']['includeEventsForEveryDayOfAllCalendarWeeks']) { |
||
| 261 | $eventDemand = $this->changeEventDemandToFullMonthDateRange($eventDemand); |
||
| 262 | } |
||
| 263 | |||
| 264 | $events = $this->eventRepository->findDemanded($eventDemand); |
||
| 265 | $weeks = $this->calendarService->getCalendarArray( |
||
| 266 | 2 | $currentMonth, |
|
| 267 | $currentYear, |
||
| 268 | 2 | strtotime('today midnight'), |
|
| 269 | 2 | (int)$this->settings['calendar']['firstDayOfWeek'], |
|
| 270 | $events |
||
| 271 | ); |
||
| 272 | |||
| 273 | $values = [ |
||
| 274 | 'events' => $events, |
||
| 275 | 'weeks' => $weeks, |
||
| 276 | 'categories' => $this->categoryRepository->findDemanded($categoryDemand), |
||
| 277 | 'locations' => $this->locationRepository->findDemanded($foreignRecordDemand), |
||
| 278 | 'organisators' => $this->organisatorRepository->findDemanded($foreignRecordDemand), |
||
| 279 | 2 | 'eventDemand' => $eventDemand, |
|
| 280 | 'overwriteDemand' => $overwriteDemand, |
||
| 281 | 2 | 'currentPageId' => $GLOBALS['TSFE']->id, |
|
| 282 | 'firstDayOfMonth' => \DateTime::createFromFormat('d.m.Y', sprintf('1.%s.%s', $currentMonth, $currentYear)), |
||
| 283 | 'previousMonthConfig' => $this->calendarService->getDateConfig($currentMonth, $currentYear, '-1 month'), |
||
| 284 | 2 | 'nextMonthConfig' => $this->calendarService->getDateConfig($currentMonth, $currentYear, '+1 month') |
|
| 285 | ]; |
||
| 286 | 2 | ||
| 287 | 2 | $this->signalDispatch(__CLASS__, __FUNCTION__ . 'BeforeRenderView', [&$values, $this]); |
|
| 288 | 2 | $this->view->assignMultiple($values); |
|
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Changes the given event demand object to select a date range for a calendar month including days of the previous |
||
| 293 | * month for the first week and they days for the next month for the last week |
||
| 294 | * |
||
| 295 | 2 | * @param EventDemand $eventDemand |
|
| 296 | * @return EventDemand |
||
| 297 | 2 | */ |
|
| 298 | 2 | protected function changeEventDemandToFullMonthDateRange(EventDemand $eventDemand) |
|
| 299 | 2 | { |
|
| 300 | 2 | $calendarDateRange = $this->calendarService->getCalendarDateRange( |
|
| 301 | 2 | $eventDemand->getMonth(), |
|
| 302 | 2 | $eventDemand->getYear(), |
|
| 303 | 2 | $this->settings['calendar']['firstDayOfWeek'] |
|
| 304 | 2 | ); |
|
| 305 | |||
| 306 | $eventDemand->setMonth(0); |
||
| 307 | $eventDemand->setYear(0); |
||
| 308 | |||
| 309 | $startDate = new \DateTime(); |
||
| 310 | $startDate->setTimestamp($calendarDateRange['firstDayOfCalendar']); |
||
| 311 | $endDate = new \DateTime(); |
||
| 312 | $endDate->setTimestamp($calendarDateRange['lastDayOfCalendar']); |
||
| 313 | $endDate->setTime(23, 59, 59); |
||
| 314 | |||
| 315 | 22 | $searchDemand = new SearchDemand(); |
|
| 316 | $searchDemand->setStartDate($startDate); |
||
| 317 | 22 | $searchDemand->setEndDate($endDate); |
|
| 318 | 22 | $eventDemand->setSearchDemand($searchDemand); |
|
| 319 | 22 | ||
| 320 | return $eventDemand; |
||
| 321 | } |
||
| 322 | 22 | ||
| 323 | 8 | /** |
|
| 324 | 8 | * Detail view for an event |
|
| 325 | 8 | * |
|
| 326 | 8 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
| 327 | 8 | * @return mixed string|void |
|
| 328 | 8 | */ |
|
| 329 | public function detailAction(Event $event = null) |
||
| 330 | 8 | { |
|
| 331 | 8 | $event = $this->evaluateSingleEventSetting($event); |
|
| 332 | 8 | $event = $this->evaluateIsShortcutSetting($event); |
|
|
|
|||
| 333 | 8 | if (is_a($event, Event::class) && $this->settings['detail']['checkPidOfEventRecord']) { |
|
| 334 | $event = $this->checkPidOfEventRecord($event); |
||
| 335 | 8 | } |
|
| 336 | 8 | ||
| 337 | 8 | if (is_null($event) && isset($this->settings['event']['errorHandling'])) { |
|
| 338 | 8 | return $this->handleEventNotFoundError($this->settings); |
|
| 339 | 8 | } |
|
| 340 | 8 | $values = ['event' => $event]; |
|
| 341 | 8 | $this->signalDispatch(__CLASS__, __FUNCTION__ . 'BeforeRenderView', [&$values, $this]); |
|
| 342 | 8 | $this->view->assignMultiple($values); |
|
| 343 | if ($event !== null) { |
||
| 344 | $this->eventCacheService->addCacheTagsByEventRecords([$event]); |
||
| 345 | 8 | } |
|
| 346 | } |
||
| 347 | |||
| 348 | 8 | /** |
|
| 349 | 2 | * Error handling if event is not found |
|
| 350 | 2 | * |
|
| 351 | 2 | * @param array $settings |
|
| 352 | 6 | * @return string |
|
| 353 | 6 | */ |
|
| 354 | protected function handleEventNotFoundError($settings) |
||
| 355 | 8 | { |
|
| 356 | if (empty($settings['event']['errorHandling'])) { |
||
| 357 | return null; |
||
| 358 | 8 | } |
|
| 359 | 6 | ||
| 360 | 6 | $configuration = GeneralUtility::trimExplode(',', $settings['event']['errorHandling'], true); |
|
| 361 | 6 | ||
| 362 | 6 | switch ($configuration[0]) { |
|
| 363 | case 'redirectToListView': |
||
| 364 | 6 | $listPid = (int)$settings['listPid'] > 0 ? (int)$settings['listPid'] : 1; |
|
| 365 | 6 | $this->redirect('list', null, null, null, $listPid); |
|
| 366 | 6 | break; |
|
| 367 | 6 | case 'pageNotFoundHandler': |
|
| 368 | 6 | $GLOBALS['TSFE']->pageNotFoundAndExit('Event not found.'); |
|
| 369 | break; |
||
| 370 | 6 | case 'showStandaloneTemplate': |
|
| 371 | 6 | if (isset($configuration[2])) { |
|
| 372 | $statusCode = constant(HttpUtility::class . '::HTTP_STATUS_' . $configuration[2]); |
||
| 373 | HttpUtility::setResponseCode($statusCode); |
||
| 374 | 8 | } |
|
| 375 | 2 | $standaloneTemplate = $this->objectManager->get(StandaloneView::class); |
|
| 376 | 2 | $standaloneTemplate->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($configuration[1])); |
|
| 377 | |||
| 378 | return $standaloneTemplate->render(); |
||
| 379 | 8 | break; |
|
| 380 | 8 | default: |
|
| 381 | } |
||
| 382 | 22 | } |
|
| 383 | 2 | ||
| 384 | 2 | /** |
|
| 385 | 2 | * Initiates the iCalendar download for the given event |
|
| 386 | 2 | * |
|
| 387 | * @param Event $event The event |
||
| 388 | 2 | * |
|
| 389 | 2 | * @return string|false |
|
| 390 | 2 | */ |
|
| 391 | 2 | public function icalDownloadAction(Event $event = null) |
|
| 392 | 2 | { |
|
| 393 | 20 | if (is_a($event, Event::class) && $this->settings['detail']['checkPidOfEventRecord']) { |
|
| 394 | 20 | $event = $this->checkPidOfEventRecord($event); |
|
| 395 | 20 | } |
|
| 396 | 20 | if (is_null($event) && isset($this->settings['event']['errorHandling'])) { |
|
| 397 | 20 | return $this->handleEventNotFoundError($this->settings); |
|
| 398 | 20 | } |
|
| 399 | $this->icalendarService->downloadiCalendarFile($event); |
||
| 400 | 22 | exit(); |
|
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Registration view for an event |
||
| 405 | * |
||
| 406 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 407 | * |
||
| 408 | * @return mixed string|void |
||
| 409 | 18 | */ |
|
| 410 | public function registrationAction(Event $event = null) |
||
| 411 | { |
||
| 412 | 18 | $event = $this->evaluateSingleEventSetting($event); |
|
| 413 | 2 | if (is_a($event, Event::class) && $this->settings['registration']['checkPidOfEventRecord']) { |
|
| 414 | 2 | $event = $this->checkPidOfEventRecord($event); |
|
| 415 | 2 | } |
|
| 416 | 16 | if (is_null($event) && isset($this->settings['event']['errorHandling'])) { |
|
| 417 | return $this->handleEventNotFoundError($this->settings); |
||
| 418 | } |
||
| 419 | if ($event->getRestrictPaymentMethods()) { |
||
| 420 | 16 | $paymentMethods = $this->paymentService->getRestrictedPaymentMethods($event); |
|
| 421 | 2 | } else { |
|
| 422 | 2 | $paymentMethods = $this->paymentService->getPaymentMethods(); |
|
| 423 | 2 | } |
|
| 424 | 14 | ||
| 425 | 2 | $values = [ |
|
| 426 | 2 | 'event' => $event, |
|
| 427 | 2 | 'paymentMethods' => $paymentMethods, |
|
| 428 | 12 | ]; |
|
| 429 | 2 | ||
| 430 | 2 | $this->signalDispatch(__CLASS__, __FUNCTION__ . 'BeforeRenderView', [&$values, $this]); |
|
| 431 | 2 | $this->view->assignMultiple($values); |
|
| 432 | 10 | } |
|
| 433 | 2 | ||
| 434 | 2 | /** |
|
| 435 | 2 | * Processes incoming registrations fields and adds field values to arguments |
|
| 436 | 8 | * |
|
| 437 | 2 | * @return void |
|
| 438 | 2 | */ |
|
| 439 | 2 | protected function setRegistrationFieldValuesToArguments() |
|
| 440 | 6 | { |
|
| 441 | 2 | $arguments = $this->request->getArguments(); |
|
| 442 | 2 | if (!isset($arguments['event'])) { |
|
| 443 | 2 | return; |
|
| 444 | 4 | } |
|
| 445 | 2 | ||
| 446 | 2 | /** @var Event $event */ |
|
| 447 | 2 | $event = $this->eventRepository->findByUid((int)$this->request->getArgument('event')); |
|
| 448 | 2 | if (!$event || $event->getRegistrationFields()->count() === 0) { |
|
| 449 | 2 | return; |
|
| 450 | 2 | } |
|
| 451 | 2 | ||
| 452 | $registrationMvcArgument = $this->arguments->getArgument('registration'); |
||
| 453 | 18 | $propertyMapping = $registrationMvcArgument->getPropertyMappingConfiguration(); |
|
| 454 | 18 | $propertyMapping->allowProperties('fieldValues'); |
|
| 455 | 18 | $propertyMapping->allowCreationForSubProperty('fieldValues'); |
|
| 456 | $propertyMapping->allowModificationForSubProperty('fieldValues'); |
||
| 457 | |||
| 458 | // allow creation of new objects (for validation) |
||
| 459 | $propertyMapping->setTypeConverterOptions( |
||
| 460 | PersistentObjectConverter::class, |
||
| 461 | [ |
||
| 462 | PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true, |
||
| 463 | PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED => true |
||
| 464 | ] |
||
| 465 | 6 | ); |
|
| 466 | |||
| 467 | // Set event to registration (required for validation) |
||
| 468 | 6 | $propertyMapping->allowProperties('event'); |
|
| 469 | $propertyMapping->allowCreationForSubProperty('event'); |
||
| 470 | 6 | $propertyMapping->allowModificationForSubProperty('event'); |
|
| 471 | 4 | $arguments['registration']['event'] = (int)$this->request->getArgument('event'); |
|
| 472 | 4 | ||
| 473 | $index = 0; |
||
| 474 | 4 | foreach ((array)$arguments['registration']['fields'] as $fieldUid => $value) { |
|
| 475 | 4 | // Only accept registration fields of the current event |
|
| 476 | 2 | if (!in_array((int)$fieldUid, $event->getRegistrationFieldsUids(), true)) { |
|
| 477 | 2 | continue; |
|
| 478 | } |
||
| 479 | |||
| 480 | 4 | // allow subvalues in new property mapper |
|
| 481 | 4 | $propertyMapping->forProperty('fieldValues')->allowProperties($index); |
|
| 482 | 4 | $propertyMapping->forProperty('fieldValues.' . $index)->allowAllProperties(); |
|
| 483 | 4 | $propertyMapping->allowCreationForSubProperty('fieldValues.' . $index); |
|
| 484 | $propertyMapping->allowModificationForSubProperty('fieldValues.' . $index); |
||
| 485 | 4 | ||
| 486 | 4 | if (is_array($value)) { |
|
| 487 | 4 | if (empty($value)) { |
|
| 488 | 4 | $value = ''; |
|
| 489 | 4 | } else { |
|
| 490 | $value = json_encode($value); |
||
| 491 | 4 | } |
|
| 492 | } |
||
| 493 | |||
| 494 | 4 | /** @var Registration\Field $field */ |
|
| 495 | 4 | $field = $this->fieldRepository->findByUid((int)$fieldUid); |
|
| 496 | 4 | ||
| 497 | 4 | $arguments['registration']['fieldValues'][$index] = [ |
|
| 498 | 'pid' => $field->getPid(), |
||
| 499 | 'value' => $value, |
||
| 500 | 6 | 'field' => strval($fieldUid), |
|
| 501 | 6 | 'valueType' => $field->getValueType() |
|
| 502 | ]; |
||
| 503 | |||
| 504 | $index++; |
||
| 505 | } |
||
| 506 | |||
| 507 | // Remove temporary "fields" field |
||
| 508 | if (isset($arguments['registration']['fields'])) { |
||
| 509 | $arguments = ArrayUtility::removeByPath($arguments, 'registration/fields'); |
||
| 510 | } |
||
| 511 | $this->request->setArguments($arguments); |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Set date format for field dateOfBirth |
||
| 516 | * |
||
| 517 | * @return void |
||
| 518 | 6 | */ |
|
| 519 | 6 | public function initializeSaveRegistrationAction() |
|
| 530 | 4 | ||
| 531 | /** |
||
| 532 | * Saves the registration |
||
| 533 | 4 | * |
|
| 534 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 535 | 4 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
| 536 | * @validate $registration \DERHANSEN\SfEventMgt\Validation\Validator\RegistrationFieldValidator |
||
| 537 | 2 | * @validate $registration \DERHANSEN\SfEventMgt\Validation\Validator\RegistrationValidator |
|
| 538 | 2 | * |
|
| 539 | 2 | * @return mixed string|void |
|
| 540 | 2 | */ |
|
| 541 | public function saveRegistrationAction(Registration $registration, Event $event) |
||
| 542 | 2 | { |
|
| 543 | 2 | if (is_a($event, Event::class) && $this->settings['registration']['checkPidOfEventRecord']) { |
|
| 544 | 2 | $event = $this->checkPidOfEventRecord($event); |
|
| 545 | 2 | } |
|
| 546 | 2 | if (is_null($event) && isset($this->settings['event']['errorHandling'])) { |
|
| 547 | return $this->handleEventNotFoundError($this->settings); |
||
| 548 | 2 | } |
|
| 549 | $autoConfirmation = (bool)$this->settings['registration']['autoConfirmation'] || $event->getEnableAutoconfirm(); |
||
| 550 | $result = RegistrationResult::REGISTRATION_SUCCESSFUL; |
||
| 551 | 2 | list($success, $result) = $this->registrationService->checkRegistrationSuccess($event, $registration, $result); |
|
| 552 | 2 | ||
| 553 | 2 | // Save registration if no errors |
|
| 554 | if ($success) { |
||
| 555 | $isWaitlistRegistration = $this->registrationService->isWaitlistRegistration( |
||
| 556 | 2 | $event, |
|
| 557 | $registration->getAmountOfRegistrations() |
||
| 558 | ); |
||
| 559 | 2 | $linkValidity = (int)$this->settings['confirmation']['linkValidity']; |
|
| 560 | 2 | if ($linkValidity === 0) { |
|
| 561 | 4 | // Use 3600 seconds as default value if not set |
|
| 562 | 4 | $linkValidity = 3600; |
|
| 563 | 4 | } |
|
| 564 | $confirmationUntil = new \DateTime(); |
||
| 565 | $confirmationUntil->add(new \DateInterval('PT' . $linkValidity . 'S')); |
||
| 566 | |||
| 567 | $registration->setEvent($event); |
||
| 568 | $registration->setPid($event->getPid()); |
||
| 569 | $registration->setConfirmationUntil($confirmationUntil); |
||
| 570 | 2 | $registration->setLanguage($GLOBALS['TSFE']->config['config']['language']); |
|
| 571 | $registration->setFeUser($this->registrationService->getCurrentFeUserObject()); |
||
| 572 | 2 | $registration->setWaitlist($isWaitlistRegistration); |
|
| 573 | 2 | $registration->_setProperty('_languageUid', $this->getSysLanguageUid()); |
|
| 574 | 2 | $this->registrationRepository->add($registration); |
|
| 575 | 2 | ||
| 576 | 2 | // Persist registration, so we have an UID |
|
| 577 | 2 | $this->objectManager->get(PersistenceManager::class)->persistAll(); |
|
| 578 | 2 | ||
| 579 | 2 | if ($isWaitlistRegistration) { |
|
| 580 | 2 | $messageType = MessageType::REGISTRATION_WAITLIST_NEW; |
|
| 581 | 2 | } else { |
|
| 582 | 2 | $messageType = MessageType::REGISTRATION_NEW; |
|
| 583 | 2 | } |
|
| 584 | 2 | ||
| 585 | 2 | // Fix event in registration for language other than default language |
|
| 586 | 2 | $this->registrationService->fixRegistrationEvent($registration, $event); |
|
| 587 | 2 | ||
| 588 | 2 | $this->signalDispatch(__CLASS__, __FUNCTION__ . 'AfterRegistrationSaved', [$registration, $this]); |
|
| 589 | |||
| 590 | // Send notifications to user and admin if confirmation link should be sent |
||
| 591 | if (!$autoConfirmation) { |
||
| 592 | $this->notificationService->sendUserMessage( |
||
| 593 | $event, |
||
| 594 | $registration, |
||
| 595 | $this->settings, |
||
| 596 | $messageType |
||
| 597 | ); |
||
| 598 | 12 | $this->notificationService->sendAdminMessage( |
|
| 599 | $event, |
||
| 600 | 12 | $registration, |
|
| 601 | 12 | $this->settings, |
|
| 602 | 12 | $messageType |
|
| 603 | 12 | ); |
|
| 604 | } |
||
| 605 | 12 | ||
| 606 | 10 | // Create given amount of registrations if necessary |
|
| 607 | $createDependingRegistrations = $registration->getAmountOfRegistrations() > 1; |
||
| 608 | 10 | $this->signalDispatch( |
|
| 609 | 2 | __CLASS__, |
|
| 610 | 2 | __FUNCTION__ . 'BeforeCreateDependingRegistrations', |
|
| 611 | [$registration, &$createDependingRegistrations, $this] |
||
| 612 | 10 | ); |
|
| 613 | 2 | if ($createDependingRegistrations) { |
|
| 614 | 2 | $this->registrationService->createDependingRegistrations($registration); |
|
| 615 | 10 | } |
|
| 616 | |||
| 617 | 12 | // Flush page cache for event, since new registration has been added |
|
| 618 | 2 | $this->eventCacheService->flushEventCache($event->getUid(), $event->getPid()); |
|
| 619 | 2 | } |
|
| 620 | |||
| 621 | 12 | if ($autoConfirmation && $success) { |
|
| 622 | 12 | $this->redirect( |
|
| 623 | 'confirmRegistration', |
||
| 624 | 12 | null, |
|
| 625 | null, |
||
| 626 | 12 | [ |
|
| 627 | 12 | 'reguid' => $registration->getUid(), |
|
| 628 | 12 | 'hmac' => $this->hashService->generateHmac('reg-' . $registration->getUid()) |
|
| 629 | 12 | ] |
|
| 630 | 12 | ); |
|
| 631 | 12 | } else { |
|
| 632 | $this->redirect( |
||
| 633 | 'saveRegistrationResult', |
||
| 634 | null, |
||
| 635 | null, |
||
| 636 | [ |
||
| 637 | 'result' => $result, |
||
| 638 | 'eventuid' => $event->getUid(), |
||
| 639 | 18 | 'hmac' => $this->hashService->generateHmac('event-' . $event->getUid()) |
|
| 640 | ] |
||
| 641 | 18 | ); |
|
| 642 | } |
||
| 643 | } |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Shows the result of the saveRegistrationAction |
||
| 647 | * |
||
| 648 | * @param int $result Result |
||
| 649 | * @param int $eventuid |
||
| 650 | * @param string $hmac |
||
| 651 | * |
||
| 652 | * @return void |
||
| 653 | */ |
||
| 654 | public function saveRegistrationResultAction($result, $eventuid, $hmac) |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Confirms the registration if possible and sends e-mails to admin and user |
||
| 716 | * |
||
| 717 | * @param int $reguid UID of registration |
||
| 718 | * @param string $hmac HMAC for parameters |
||
| 719 | * |
||
| 720 | * @return void |
||
| 721 | */ |
||
| 722 | public function confirmRegistrationAction($reguid, $hmac) |
||
| 723 | { |
||
| 724 | $event = null; |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Cancels the registration if possible and sends e-mails to admin and user |
||
| 796 | * |
||
| 797 | * @param int $reguid UID of registration |
||
| 798 | * @param string $hmac HMAC for parameters |
||
| 799 | * |
||
| 800 | * @return void |
||
| 801 | */ |
||
| 802 | public function cancelRegistrationAction($reguid, $hmac) |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Set date format for field startDate and endDate |
||
| 853 | * |
||
| 854 | * @return void |
||
| 855 | */ |
||
| 856 | public function initializeSearchAction() |
||
| 885 | |||
| 886 | /** |
||
| 887 | * Search view |
||
| 888 | * |
||
| 889 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\SearchDemand $searchDemand SearchDemand |
||
| 890 | * @param array $overwriteDemand OverwriteDemand |
||
| 891 | * |
||
| 892 | * @return void |
||
| 893 | */ |
||
| 894 | public function searchAction(SearchDemand $searchDemand = null, array $overwriteDemand = []) |
||
| 936 | |||
| 937 | /** |
||
| 938 | * Returns if a demand object can be overwritten with the given overwriteDemand array |
||
| 939 | * |
||
| 940 | * @param array $overwriteDemand |
||
| 941 | * @return bool |
||
| 942 | */ |
||
| 943 | protected function isOverwriteDemand($overwriteDemand) |
||
| 947 | |||
| 948 | /** |
||
| 949 | * If no event is given and the singleEvent setting is set, the configured single event is returned |
||
| 950 | * |
||
| 951 | * @param Event|null $event |
||
| 952 | * @return Event|null |
||
| 953 | */ |
||
| 954 | protected function evaluateSingleEventSetting($event) |
||
| 962 | |||
| 963 | /** |
||
| 964 | * If no event is given and the isShortcut setting is set, the event is displayed using the "Insert Record" |
||
| 965 | * content element and should be loaded from contect object data |
||
| 966 | * |
||
| 967 | * @param Event|null $event |
||
| 968 | * @return Event|null |
||
| 969 | */ |
||
| 970 | protected function evaluateIsShortcutSetting($event) |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Checks if the event pid could be found in the storagePage settings of the detail plugin and |
||
| 982 | * if the pid could not be found it return null instead of the event object. |
||
| 983 | * |
||
| 984 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
| 985 | * @return null|\DERHANSEN\SfEventMgt\Domain\Model\Event |
||
| 986 | */ |
||
| 987 | protected function checkPidOfEventRecord(Event $event) |
||
| 1011 | |||
| 1012 | /** |
||
| 1013 | * Returns the current sys_language_uid |
||
| 1014 | * |
||
| 1015 | * @return int |
||
| 1016 | */ |
||
| 1017 | protected function getSysLanguageUid() |
||
| 1021 | } |
||
| 1022 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.