UpdateSiteCommand.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 an existing organization's website
  15. * with the latest data from the Opentalent DB
  16. *
  17. * @package Opentalent\OtAdmin\Command
  18. */
  19. class UpdateSiteCommand extends Command
  20. {
  21. /**
  22. * -- This method is expected by Typo3, do not rename ou remove --
  23. *
  24. * Allows to configure the command.
  25. * Allows to add a description, a help text, and / or define arguments.
  26. *
  27. */
  28. protected function configure()
  29. {
  30. $this
  31. ->setName("ot:site:update")
  32. ->setDescription("Update an organization website")
  33. ->setHelp("This CLI command update an existing organization's website
  34. with the latest data from the Opentalent DB")
  35. ->addOption(
  36. 'all',
  37. null,
  38. InputOption::VALUE_NONE,
  39. "Update all of the organization websites"
  40. )
  41. ->addArgument(
  42. 'organization-id',
  43. InputArgument::OPTIONAL,
  44. "The organization's id in the opentalent DB"
  45. );
  46. }
  47. /**
  48. * -- This method is expected by Typo3, do not rename ou remove --
  49. *
  50. * @param InputInterface $input
  51. * @param OutputInterface $output
  52. * @throws \Exception
  53. */
  54. protected function execute(InputInterface $input, OutputInterface $output)
  55. {
  56. $org_id = $input->getArgument('organization-id');
  57. $all = $input->getOption('all');
  58. $hard = $input->getOption('hard');
  59. if ($all && $org_id) {
  60. throw new \InvalidArgumentException("You can not pass both an organization id and the --all option");
  61. }
  62. if (!$all && !$org_id) {
  63. throw new \InvalidArgumentException("You shall either pass an organization id or use the --all option");
  64. }
  65. $io = new SymfonyStyle($input, $output);
  66. $siteController = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  67. if ($all) {
  68. $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
  69. $queryBuilder = $connectionPool->getQueryBuilderForTable('ot_websites');
  70. $sites = $queryBuilder
  71. ->select('organization_id')
  72. ->from('ot_websites')
  73. ->where($queryBuilder->expr()->eq('deleted', 0))
  74. ->andWhere($queryBuilder->expr()->gt('organization_id', 0))
  75. ->execute()
  76. ->fetchAll();
  77. $io->progressStart(count($sites));
  78. foreach ($sites as $site) {
  79. $org_id = $site['organization_id'];
  80. try {
  81. $siteController->updateSiteAction($org_id);
  82. } catch (\Throwable $e) {
  83. $io->error('Organization Id: ' . $org_id . ' - ' . $e->getMessage());
  84. }
  85. $io->progressAdvance(1);
  86. }
  87. $io->progressFinish();
  88. $io->success(sprintf("The websites have all been updated"));
  89. } else {
  90. $rootUid = $siteController->updateSiteAction($org_id, $hard);
  91. $io->success(sprintf("The website with root uid " . $rootUid . " has been updated"));
  92. }
  93. }
  94. }