Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  5. * @license For full copyright and license information view LICENSE file distributed with this source code.
  6. */
  7. namespace eZ\Bundle\EzPublishCoreBundle\Command;
  8.  
  9. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  10. use Symfony\Component\Console\Helper\Table;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Console\Style\SymfonyStyle;
  15.  
  16. class LoadReverseRelationListCommand extends ContainerAwareCommand
  17. {
  18. /** @var \eZ\Publish\API\Repository\ContentService */
  19. private $contentService;
  20.  
  21. /** @var \eZ\Publish\API\Repository\UserService */
  22. private $userService;
  23.  
  24. /** @var \Symfony\Component\Console\Style\SymfonyStyle */
  25. private $io;
  26.  
  27. /** @var \eZ\Publish\API\Repository\PermissionResolver */
  28. private $permissionResolver;
  29.  
  30. protected function configure()
  31. {
  32. $this->setName('support-toolkit:load-reverse-relation-list')
  33. ->setDescription('Load reverse relation list.')
  34. ->addOption(
  35. 'contentId',
  36. 'c',
  37. InputOption::VALUE_REQUIRED,
  38. 'Content ID'
  39. )
  40. ->addOption(
  41. 'offset',
  42. 'o',
  43. InputOption::VALUE_OPTIONAL,
  44. 'Offset',
  45. 0
  46. )
  47. ->addOption(
  48. 'limit',
  49. 'l',
  50. InputOption::VALUE_OPTIONAL,
  51. 'Limit',
  52. 10
  53. )
  54. ->addOption(
  55. 'userId',
  56. 'u',
  57. InputOption::VALUE_OPTIONAL,
  58. 'User ID',
  59. 14
  60. )
  61. ->setHelp(
  62. <<<EOT
  63. The command <info>%command.name%</info> allows you to count content's reverse relations.
  64. Arguments can be combined.
  65. Required arguments:
  66. - Content ID
  67. <comment>support-toolkit:create-folders --contentId=10</comment> or <comment>support-toolkit:create-folders -c 10</comment>
  68. Optional arguments:
  69. - Offset:
  70. <comment>support-toolkit:create-folders --offset=5</comment> or <comment>support-toolkit:create-folders -o 5</comment>
  71. - Limit:
  72. <comment>support-toolkit:create-folders --limit=10</comment> or <comment>support-toolkit:create-folders -l 5</comment>
  73. - User Id:
  74. <comment>support-toolkit:create-folders --userId=14</comment> or <comment>support-toolkit:create-folders -u 14</comment>
  75. EOT
  76. );
  77. }
  78.  
  79. protected function initialize(InputInterface $input, OutputInterface $output)
  80. {
  81. $this->io = new SymfonyStyle($input, $output);
  82. $this->io->title('Load reverse relation list');
  83.  
  84. /** @var \eZ\Publish\API\Repository\Repository $repository */
  85. $repository = $this->getContainer()->get('ezpublish.api.repository');
  86. $this->contentService = $repository->getContentService();
  87. $this->userService = $repository->getUserService();
  88. $this->permissionResolver = $repository->getPermissionResolver();
  89. }
  90.  
  91. /**
  92. * {@inheritdoc}
  93. */
  94. protected function interact(InputInterface $input, OutputInterface $output)
  95. {
  96. foreach ($input->getOptions() as $option => $value) {
  97. if (null === $value && $option !== 'siteaccess') {
  98. $input->setOption($option, $this->io->ask(sprintf('%s', ucfirst($option))));
  99. }
  100. }
  101. }
  102.  
  103. protected function execute(InputInterface $input, OutputInterface $output)
  104. {
  105. $contentId = $input->getOption('contentId');
  106. $offset = $input->getOption('offset');
  107. $limit = $input->getOption('limit');
  108. $userId = $input->getOption('userId');
  109.  
  110. try {
  111. $user = $this->userService->loadUser($userId);
  112. $this->permissionResolver->setCurrentUserReference($user);
  113.  
  114. $content = $this->contentService->loadContent($contentId);
  115.  
  116. $list = $this->contentService->loadReverseRelationList($content->contentInfo, $offset, $limit);
  117. $page = $offset/$limit + 1;
  118. $output->writeln(sprintf('<info>Content with ID: %d has %d reverse relation in total</info>',
  119. $content->id, $list->totalCount
  120. ));
  121. $output->writeln(sprintf('<info>On page %d: </info>',
  122. $page
  123. ));
  124.  
  125. $rows = [];
  126. foreach ($list->items as $item) {
  127. if ($item->hasRelation()) {
  128. $rows[] = [
  129. $item->getRelation()->sourceContentInfo->id,
  130. $item->getRelation()->sourceContentInfo->name,
  131. ];
  132. } else {
  133. /* @var \eZ\Publish\API\Repository\Values\Content\RelationList\Item\UnauthorizedRelationListItem $item */
  134. $rows[] = [
  135. $item->getPayload()['contentId'],
  136. 'User has no access',
  137. ];
  138. }
  139.  
  140. }
  141.  
  142. $table = new Table($output);
  143. $table
  144. ->setHeaders(['Content ID', 'Name'])
  145. ->setRows($rows)
  146. ;
  147. $table->render();
  148. } catch (\Exception $e) {
  149. $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
  150. }
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement