UpdateRoutingIndexCommand.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Opentalent\OtAdmin\Command;
  3. use Opentalent\OtAdmin\Controller\SiteController;
  4. use Symfony\Component\Console\Command\Command;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Input\InputOption;
  8. use Symfony\Component\Console\Output\OutputInterface;
  9. use Symfony\Component\Console\Style\SymfonyStyle;
  10. use TYPO3\CMS\Core\Database\ConnectionPool;
  11. use TYPO3\CMS\Core\Utility\GeneralUtility;
  12. use TYPO3\CMS\Extbase\Object\ObjectManager;
  13. /**
  14. * This CLI command updates the routing index for the given website
  15. *
  16. * @package Opentalent\OtAdmin\Command
  17. */
  18. class UpdateRoutingIndexCommand extends Command
  19. {
  20. /**
  21. * -- This method is expected by Typo3, do not rename ou remove --
  22. *
  23. * Allows to configure the command.
  24. * Allows to add a description, a help text, and / or define arguments.
  25. *
  26. */
  27. protected function configure()
  28. {
  29. $this
  30. ->setName("ot:site:index")
  31. ->setDescription("Update the routes index for the given website(s)")
  32. ->setHelp("This CLI command updates the routing index for the given website")
  33. ->addOption(
  34. 'all',
  35. null,
  36. InputOption::VALUE_NONE,
  37. "Update all of the organization websites"
  38. )
  39. ->addArgument(
  40. 'organization-id',
  41. InputArgument::OPTIONAL,
  42. "The organization's id in the opentalent DB"
  43. );
  44. }
  45. /**
  46. * -- This method is expected by Typo3, do not rename ou remove --
  47. *
  48. * @param InputInterface $input
  49. * @param OutputInterface $output
  50. * @throws \Exception
  51. */
  52. protected function execute(InputInterface $input, OutputInterface $output)
  53. {
  54. $org_id = $input->getArgument('organization-id');
  55. $all = $input->getOption('all');
  56. if ($all && $org_id) {
  57. throw new \InvalidArgumentException("You can not pass both an organization id and the --all option");
  58. }
  59. if (!$all && !$org_id) {
  60. throw new \InvalidArgumentException("You shall either pass an organization id or use the --all option");
  61. }
  62. $io = new SymfonyStyle($input, $output);
  63. $siteController = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  64. if ($all) {
  65. $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
  66. $queryBuilder = $connectionPool->getQueryBuilderForTable('ot_websites');
  67. $sites = $queryBuilder
  68. ->select('organization_id')
  69. ->from('ot_websites')
  70. ->execute()
  71. ->fetchAll();
  72. $io->progressStart(count($sites));
  73. foreach ($sites as $site) {
  74. $org_id = $site['organization_id'];
  75. try {
  76. $siteController->updateRoutingIndexAction($org_id);
  77. } catch (\Throwable $e) {
  78. $io->error('Organization Id: ' . $org_id . ' - ' . $e->getMessage());
  79. }
  80. $io->progressAdvance(1);
  81. }
  82. $io->progressFinish();
  83. $io->success(sprintf("The routing index has all been fully updated"));
  84. } else {
  85. $rootUid = $siteController->updateRoutingIndexAction($org_id);
  86. $io->success(sprintf("The website with root uid " . $rootUid . " routing index has been updated"));
  87. }
  88. }
  89. }