Skip to content

Commit

Permalink
[skip ci] Updated cms:block commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sreichel committed Oct 16, 2023
1 parent 12f48b3 commit e8da83f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 88 deletions.
29 changes: 29 additions & 0 deletions src/N98/Magento/Command/Cms/Block/AbstractCmsBlockCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace N98\Magento\Command\Cms\Block;

use Symfony\Component\Validator\Exception\UnexpectedValueException;
use Mage_Cms_Model_Block;
use N98\Magento\Command\AbstractMagentoCommand;

/**
* @package N98\Magento\Command\Cms\Block
*/
class AbstractCmsBlockCommand extends AbstractMagentoCommand
{
/**
* Get an instance of cms/block
*
* @return Mage_Cms_Model_Block
*/
protected function _getBlockModel(): Mage_Cms_Model_Block
{
$model = $this->_getModel('cms/block');
if (!$model instanceof Mage_Cms_Model_Block) {
throw new UnexpectedValueException($model, Mage_Cms_Model_Block::class);
}
return $model;
}
}
92 changes: 41 additions & 51 deletions src/N98/Magento/Command/Cms/Block/ListCommand.php
Original file line number Diff line number Diff line change
@@ -1,80 +1,70 @@
<?php

declare(strict_types=1);

namespace N98\Magento\Command\Cms\Block;

use N98\Magento\Command\AbstractMagentoCommand;
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
use N98\Util\Console\Helper\TableHelper;
use Mage_Cms_Model_Block;
use Mage_Core_Exception;
use N98\Magento\Command\AbstractMagentoCommandFormatInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* CMS Block ListCommand
* List CMS blocks command
*
* @package N98\Magento\Command\Cms\Block
*/
class ListCommand extends AbstractMagentoCommand
class ListCommand extends AbstractCmsBlockCommand implements AbstractMagentoCommandFormatInterface
{
/**
* Configure command
* @var array<int, array<string, int|string>>|null
*/
protected function configure()
{
$this
->setName('cms:block:list')
->setDescription('List all cms blocks')
->addOption(
'format',
null,
InputOption::VALUE_OPTIONAL,
'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
)
;
}
private ?array $data = null;

/**
* Get an instance of cms/block
*
* @return \Mage_Cms_Model_Block
* @var string
* @deprecated with symfony 6.1
* @see AsCommand
*/
protected function _getBlockModel()
{
return $this->_getModel('cms/block', '\Mage_Cms_Model_Block');
}
protected static $defaultName = 'cms:block:list';

/**
* Execute the command
*
* @param InputInterface $input
* @var string
* @deprecated with symfony 6.1
* @see AsCommand
*/
protected static $defaultDescription = 'List all cms blocks';

/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
* @return array<int, array<string, int|string>>
* @throws Mage_Core_Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int
public function getData(InputInterface $input, OutputInterface $output): array
{
$this->detectMagento($output, true);
if (!$this->initMagento()) {
return 0;
}

$cmsBlockCollection = $this->_getBlockModel()->getCollection()->addFieldToSelect('*');
if (is_null($this->data)) {
/** @var Mage_Cms_Model_Block[] $cmsBlockCollection */
$cmsBlockCollection = $this->_getBlockModel()->getCollection()->addFieldToSelect('*');

/** @var \Mage_Cms_Model_Resource_Block $resourceModel */
$resourceModel = $this->_getBlockModel()->getResource();
$resourceModel = $this->_getBlockModel()->getResource();

$table = [];
foreach ($cmsBlockCollection as $cmsBlock) {
$storeIds = implode(',', $resourceModel->lookupStoreIds($cmsBlock->getId()));
$this->data = [];
foreach ($cmsBlockCollection as $cmsBlock) {
$storeIds = implode(',', $resourceModel->lookupStoreIds((int)$cmsBlock->getId()));

$table[] = [$cmsBlock->getData('block_id'), $cmsBlock->getData('identifier'), $cmsBlock->getData('title'), $cmsBlock->getData('is_active') ? 'active' : 'inactive', $storeIds];
$this->data[] = [
'block_id' => $cmsBlock->getBlockId(),
'title' => $cmsBlock->getTitle(),
'identifier' => $cmsBlock->getIdentifier(),
'is_active' => $cmsBlock->getIsActive() ? 'active' : 'inactive',
'store_ids' => $storeIds
];
}
}

/* @var TableHelper $tableHelper */
$tableHelper = $this->getHelper('table');
$tableHelper
->setHeaders(['block_id', 'identifier', 'title', 'is_active', 'store_ids'])
->renderByFormat($output, $table, $input->getOption('format'));
return 0;
return $this->data;
}
}
82 changes: 45 additions & 37 deletions src/N98/Magento/Command/Cms/Block/ToggleCommand.php
Original file line number Diff line number Diff line change
@@ -1,72 +1,80 @@
<?php

declare(strict_types=1);

namespace N98\Magento\Command\Cms\Block;

use N98\Magento\Command\AbstractMagentoCommand;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;

/**
* CMS Block ToggleCommand
* Toggle CMS block command
*
* @package N98\Magento\Command\Cms\Block
*/
class ToggleCommand extends AbstractMagentoCommand
class ToggleCommand extends AbstractCmsBlockCommand
{
private const COMMAND_ARGUMENT_BLOCK_ID = 'block_id';

/**
* Configure command
* @var string
* @deprecated with symfony 6.1
* @see AsCommand
*/
protected function configure()
{
$this
->setName('cms:block:toggle')
->addArgument('block_id', InputArgument::REQUIRED, 'Block ID or Identifier')
->setDescription('Toggle a cms block')
;
}
protected static $defaultName = 'cms:block:toggle';

/**
* @var string
* @deprecated with symfony 6.1
* @see AsCommand
*/
protected static $defaultDescription = 'Toggle a cms block.';

/**
* Get an instance of cms/block
*
* @return \Mage_Cms_Model_Block
* @return void
*/
protected function _getBlockModel()
protected function configure()
{
return $this->_getModel('cms/block', '\Mage_Cms_Model_Block');
$this->addArgument(
self::COMMAND_ARGUMENT_BLOCK_ID,
InputArgument::REQUIRED,
'Block ID or Identifier'
);
}

/**
* Execute the command
*
* @param InputInterface $input
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
* @throws Throwable
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->detectMagento($output, true);
if (!$this->initMagento()) {
return 0;
}
$blockId = $input->getArgument('block_id');
if (is_numeric($blockId)) {
$block = $this->_getBlockModel()->load($blockId);
} else {
$block = $this->_getBlockModel()->load($blockId, 'identifier');
}
$this->detectMagento($output);
$this->initMagento();

$blockId = $this->getArgumentString($input, self::COMMAND_ARGUMENT_BLOCK_ID);
$block = $this->_getBlockModel()->load($blockId, is_numeric($blockId) ? null : 'identifier');

if (!$block->getId()) {
return (int) $output->writeln('<error>Block was not found</error>');
$output->writeln('<error>Block was not found</error>');
return Command::INVALID;
}
$newStatus = !$block->getIsActive();

$block
->setIsActive($newStatus)
->setIsActive(!$block->getIsActive())
->save();

$output->writeln(sprintf(
'<comment>Block</comment> <info>%s</info>',
$newStatus ? 'enabled' : 'disabled'
'<comment>Block "%s"</comment> <info>%s</info>',
$block->getTitle(),
$block->getIsActive() ? 'enabled' : 'disabled'
));
return 0;

return Command::SUCCESS;
}
}

0 comments on commit e8da83f

Please sign in to comment.