UpdateSiteCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. ->addOption(
  47. 'deep',
  48. 'd',
  49. InputOption::VALUE_NONE,
  50. "Performs a deep update (recreate the site config file, reset the be_users permissions)"
  51. );
  52. }
  53. /**
  54. * -- This method is expected by Typo3, do not rename ou remove --
  55. *
  56. * @param InputInterface $input
  57. * @param OutputInterface $output
  58. * @throws \Exception
  59. */
  60. protected function execute(InputInterface $input, OutputInterface $output)
  61. {
  62. $org_id = $input->getArgument('organization-id');
  63. $all = $input->getOption('all');
  64. $deep = $input->getOption('deep');
  65. if ($all && $org_id) {
  66. throw new \InvalidArgumentException("You can not pass both an organization id and the --all option");
  67. }
  68. if (!$all && !$org_id) {
  69. throw new \InvalidArgumentException("You shall either pass an organization id or use the --all option");
  70. }
  71. $io = new SymfonyStyle($input, $output);
  72. $siteController = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  73. $msg_deep = $deep ? 'deeply ' : '';
  74. if ($all) {
  75. $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
  76. $queryBuilder = $connectionPool->getQueryBuilderForTable('ot_websites');
  77. $sites = $queryBuilder
  78. ->select('organization_id')
  79. ->from('ot_websites')
  80. ->execute()
  81. ->fetchAll();
  82. $io->progressStart(count($sites));
  83. foreach ($sites as $site) {
  84. $org_id = $site['organization_id'];
  85. try {
  86. $siteController->updateSiteAction($org_id, $deep);
  87. } catch (\Throwable $e) {
  88. $io->error('Organization Id: ' . $org_id . ' - ' . $e->getMessage());
  89. }
  90. $io->progressAdvance(1);
  91. }
  92. $io->progressFinish();
  93. $io->success(sprintf("The websites have all been " . $msg_deep . "updated"));
  94. } else {
  95. $rootUid = $siteController->updateSiteAction($org_id, $deep);
  96. $io->success(sprintf("The website with root uid " . $rootUid . " has been " . $msg_deep . "updated"));
  97. }
  98. }
  99. }