UpdateRoutingIndexCommand.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * @return int
  51. * @throws \TYPO3\CMS\Extbase\Object\Exception
  52. */
  53. protected function execute(InputInterface $input, OutputInterface $output)
  54. {
  55. $org_id = $input->getArgument('organization-id');
  56. $all = $input->getOption('all');
  57. if ($all && $org_id) {
  58. throw new \InvalidArgumentException("You can not pass both an organization id and the --all option");
  59. }
  60. if (!$all && !$org_id) {
  61. throw new \InvalidArgumentException("You shall either pass an organization id or use the --all option");
  62. }
  63. $io = new SymfonyStyle($input, $output);
  64. $siteController = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  65. if ($all) {
  66. $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
  67. $queryBuilder = $connectionPool->getQueryBuilderForTable('ot_websites');
  68. $sites = $queryBuilder
  69. ->select('organization_id')
  70. ->from('ot_websites')
  71. ->where($queryBuilder->expr()->eq('deleted', 0))
  72. ->andWhere($queryBuilder->expr()->gt('organization_id', 0))
  73. ->execute()
  74. ->fetchAll();
  75. $io->progressStart(count($sites));
  76. foreach ($sites as $site) {
  77. $org_id = $site['organization_id'];
  78. try {
  79. $siteController->updateRoutingIndexAction($org_id);
  80. } catch (\Throwable $e) {
  81. $io->error('Organization Id: ' . $org_id . ' - ' . $e->getMessage());
  82. }
  83. $io->progressAdvance(1);
  84. }
  85. $io->progressFinish();
  86. $io->success(sprintf("The routing index has all been fully updated"));
  87. } else {
  88. $rootUid = $siteController->updateRoutingIndexAction($org_id);
  89. $io->success(sprintf("The website with root uid " . $rootUid . " routing index has been updated"));
  90. }
  91. return 0;
  92. }
  93. }