DashboardController.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. declare(strict_types=1);
  3. namespace Meramo\Begriffmgt\Controller;
  4. use Meramo\Begriffmgt\Domain\Model\Category;
  5. use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
  6. use Meramo\Begriffmgt\Domain\Repository\TermRepository;
  7. use Meramo\Begriffmgt\Domain\Model\Term;
  8. use Meramo\Begriffmgt\Domain\Repository\CategoryRepository;
  9. use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
  10. class DashboardController extends ActionController
  11. {
  12. protected $termRepository;
  13. protected $categoryRepository;
  14. public function __construct(TermRepository $termRepository, CategoryRepository $categoryRepository) {
  15. $this->termRepository = $termRepository;
  16. $this->categoryRepository = $categoryRepository;
  17. }
  18. public function indexAction(): void
  19. {
  20. $terms = $this->termRepository->findAll();
  21. $categories = $this->categoryRepository->findAll();
  22. $this->view->assignMultiple([
  23. 'terms' => $terms,
  24. 'categories' => $categories,
  25. ]);
  26. }
  27. public function newAction(): void {
  28. }
  29. public function listAction(): void {
  30. $terms = $this->termRepository->findAll();
  31. $this->view->assign('terms', $terms);
  32. }
  33. public function createAction(string $termList, string $categoryTitle): void {
  34. $terms = array_map('trim', explode(',', $termList));
  35. $category = $this->createCategoryAction($categoryTitle);
  36. $categoryObj = $this->categoryRepository->findByUid($category);
  37. foreach ($terms as $term) {
  38. $termObj = new Term();
  39. $termObj->setTerm($term);
  40. $termObj->setCategory($categoryObj);
  41. $this->termRepository->add($termObj);
  42. }
  43. $this->redirect('index');
  44. }
  45. public function createCategoryAction(string $title) {
  46. $category = new Category();
  47. $category->setTitle($title);
  48. $this->categoryRepository->add($category);
  49. $this->objectManager->get(PersistenceManager::class)->persistAll();
  50. return $category->getUid();
  51. }
  52. }