Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @copyright Copyright (C) eZ Systems AS. All rights reserved.
- * @license For full copyright and license information view LICENSE file distributed with this source code.
- */
- namespace eZ\Bundle\EzPublishCoreBundle\Command;
- use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
- use Symfony\Component\Console\Helper\Table;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\Console\Style\SymfonyStyle;
- class LoadReverseRelationListCommand extends ContainerAwareCommand
- {
- /** @var \eZ\Publish\API\Repository\ContentService */
- private $contentService;
- /** @var \eZ\Publish\API\Repository\UserService */
- private $userService;
- /** @var \Symfony\Component\Console\Style\SymfonyStyle */
- private $io;
- /** @var \eZ\Publish\API\Repository\PermissionResolver */
- private $permissionResolver;
- protected function configure()
- {
- $this->setName('support-toolkit:load-reverse-relation-list')
- ->setDescription('Load reverse relation list.')
- ->addOption(
- 'contentId',
- 'c',
- InputOption::VALUE_REQUIRED,
- 'Content ID'
- )
- ->addOption(
- 'offset',
- 'o',
- InputOption::VALUE_OPTIONAL,
- 'Offset',
- 0
- )
- ->addOption(
- 'limit',
- 'l',
- InputOption::VALUE_OPTIONAL,
- 'Limit',
- 10
- )
- ->addOption(
- 'userId',
- 'u',
- InputOption::VALUE_OPTIONAL,
- 'User ID',
- 14
- )
- ->setHelp(
- <<<EOT
- The command <info>%command.name%</info> allows you to count content's reverse relations.
- Arguments can be combined.
- Required arguments:
- - Content ID
- <comment>support-toolkit:create-folders --contentId=10</comment> or <comment>support-toolkit:create-folders -c 10</comment>
- Optional arguments:
- - Offset:
- <comment>support-toolkit:create-folders --offset=5</comment> or <comment>support-toolkit:create-folders -o 5</comment>
- - Limit:
- <comment>support-toolkit:create-folders --limit=10</comment> or <comment>support-toolkit:create-folders -l 5</comment>
- - User Id:
- <comment>support-toolkit:create-folders --userId=14</comment> or <comment>support-toolkit:create-folders -u 14</comment>
- EOT
- );
- }
- protected function initialize(InputInterface $input, OutputInterface $output)
- {
- $this->io = new SymfonyStyle($input, $output);
- $this->io->title('Load reverse relation list');
- /** @var \eZ\Publish\API\Repository\Repository $repository */
- $repository = $this->getContainer()->get('ezpublish.api.repository');
- $this->contentService = $repository->getContentService();
- $this->userService = $repository->getUserService();
- $this->permissionResolver = $repository->getPermissionResolver();
- }
- /**
- * {@inheritdoc}
- */
- protected function interact(InputInterface $input, OutputInterface $output)
- {
- foreach ($input->getOptions() as $option => $value) {
- if (null === $value && $option !== 'siteaccess') {
- $input->setOption($option, $this->io->ask(sprintf('%s', ucfirst($option))));
- }
- }
- }
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $contentId = $input->getOption('contentId');
- $offset = $input->getOption('offset');
- $limit = $input->getOption('limit');
- $userId = $input->getOption('userId');
- try {
- $user = $this->userService->loadUser($userId);
- $this->permissionResolver->setCurrentUserReference($user);
- $content = $this->contentService->loadContent($contentId);
- $list = $this->contentService->loadReverseRelationList($content->contentInfo, $offset, $limit);
- $page = $offset/$limit + 1;
- $output->writeln(sprintf('<info>Content with ID: %d has %d reverse relation in total</info>',
- $content->id, $list->totalCount
- ));
- $output->writeln(sprintf('<info>On page %d: </info>',
- $page
- ));
- $rows = [];
- foreach ($list->items as $item) {
- if ($item->hasRelation()) {
- $rows[] = [
- $item->getRelation()->sourceContentInfo->id,
- $item->getRelation()->sourceContentInfo->name,
- ];
- } else {
- /* @var \eZ\Publish\API\Repository\Values\Content\RelationList\Item\UnauthorizedRelationListItem $item */
- $rows[] = [
- $item->getPayload()['contentId'],
- 'User has no access',
- ];
- }
- }
- $table = new Table($output);
- $table
- ->setHeaders(['Content ID', 'Name'])
- ->setRows($rows)
- ;
- $table->render();
- } catch (\Exception $e) {
- $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement