UpdateSiteCommand.php 4.5 KB

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