-
-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[skip ci] Updated cms:block commands
- Loading branch information
Showing
3 changed files
with
110 additions
and
88 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/N98/Magento/Command/Cms/Block/AbstractCmsBlockCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,65 @@ | ||
<?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 string | ||
* @deprecated with symfony 6.1 | ||
* @see AsCommand | ||
*/ | ||
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()) . ']' | ||
) | ||
; | ||
} | ||
protected static $defaultName = 'cms:block:list'; | ||
|
||
/** | ||
* 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 $defaultDescription = 'List all cms blocks'; | ||
|
||
/** | ||
* Execute the command | ||
* | ||
* @param InputInterface $input | ||
* @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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* CMS block ToggleCommand | ||
* | ||
* @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; | ||
} | ||
} |