TermController.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Meramo\Begriffmgt\Controller;
  3. use Meramo\Begriffmgt\Domain\Model\Term;
  4. use Meramo\Begriffmgt\Domain\Repository\TermRepository;
  5. use TYPO3\CMS\Core\Messaging\FlashMessage;
  6. use TYPO3\CMS\Core\Utility\GeneralUtility;
  7. use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
  8. class TermController extends ActionController
  9. {
  10. public function __construct(
  11. TermRepository $termRepository
  12. ) {
  13. $this->termRepository = $termRepository;
  14. }
  15. public function createAction($terms, $categoryObj, $typeObj, $urlObj): void {
  16. $objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
  17. $flashMessageService = $objectManager->get('TYPO3\\CMS\\Core\\Messaging\\FlashMessageService');
  18. $messageQueue = $flashMessageService->getMessageQueueByIdentifier('begriffmgt');
  19. foreach ($terms as $term) {
  20. if($term !== '' && !($this->termRepository->findByTitle($term))) {
  21. $termObj = new Term();
  22. $termObj->setTerm($term);
  23. $termObj->setCategory($categoryObj);
  24. $termObj->setType($typeObj);
  25. $termObj->setUrl($urlObj);
  26. $this->termRepository->add($termObj);
  27. }
  28. elseif ($term !== '' && $this->termRepository->findByTitle($term)) {
  29. $termObj = $this->termRepository->findByUid(($this->termRepository->findByTitle($term))['uid']);
  30. $termObj->setCategory($categoryObj);
  31. $termObj->setType($typeObj);
  32. $termObj->setUrl($urlObj);
  33. $this->termRepository->update($termObj);
  34. $flashMessage = GeneralUtility::makeInstance(
  35. FlashMessage::class,
  36. 'Begriff ' . $term . ' wurde aktualisiert. Begriff war bereits vorhanden. Bitte Liste prüfen!',
  37. 'Update', // the header of the flash message
  38. FlashMessage::OK, // the severity of the flash message
  39. TRUE // whether the flash message should be stored in the session (to survive redirects)
  40. );
  41. $messageQueue->addMessage($flashMessage);
  42. }
  43. }
  44. }
  45. public function deleteAction(int $termId): void {
  46. $term = $this->termRepository->findByUid($termId);
  47. if ($term) {
  48. $this->termRepository->remove($term);
  49. }
  50. }
  51. }