Skip to content

Commit

Permalink
Iblock schema (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Kolobaev authored and niksamokhvalov committed Dec 18, 2017
1 parent 59e2075 commit c2af967
Show file tree
Hide file tree
Showing 10 changed files with 786 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Notamedia\ConsoleJedi\Environment\Command\InitCommand;
use Notamedia\ConsoleJedi\Module\Command as Module;
use Notamedia\ConsoleJedi\Search\Command\ReIndexCommand;
use Notamedia\ConsoleJedi\Iblock\Command\ExportCommand;
use Notamedia\ConsoleJedi\Iblock\Command\ImportCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -151,6 +153,9 @@ protected function getBitrixCommands()
new ClearCommand(),
new InitCommand(),
new ReIndexCommand(),
new ExportCommand(),
new ImportCommand(),
new ReIndexCommand(),
],
Module\ModuleCommand::getCommands()
);
Expand Down
118 changes: 118 additions & 0 deletions src/Iblock/Command/ExportCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Notamedia\ConsoleJedi\Iblock\Command;

use Notamedia\ConsoleJedi\Application\Command\BitrixCommand;
use Notamedia\ConsoleJedi\Iblock\Exception\IblockException;
use Notamedia\ConsoleJedi\Iblock\Exporter;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\FormatterHelper;
use Bitrix\Main\Loader;

/**
* Command export information block(s) in xml file(s)
*/
class ExportCommand extends BitrixCommand
{
use MigrationCommandTrait;

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('iblock:export')
->setDescription('Export information block(s) to xml')
->addArgument(
'type',
InputArgument::REQUIRED,
'Information block type'
)
->addArgument(
'code',
InputArgument::REQUIRED,
'Information block code'
)
->addArgument(
'dir',
InputArgument::OPTIONAL,
'Directory to export'
)
->addOption(
'sections',
's',
InputOption::VALUE_OPTIONAL,
'Export sections [ "active", "all", "none" ]',
'none'
)
->addOption(
'elements',
'e',
InputOption::VALUE_OPTIONAL,
'Export elements [ "active", "all", "none" ]',
'none'
);
}

/**
* {@inheritdoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
Loader::includeModule('iblock');
}

/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$this->setDir($input);
$this->setIblocks($input);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$formatter = new FormatterHelper();

if (count($this->errors) > 0) {
$output->writeln($formatter->formatBlock($this->errors, 'error'));
return false;
}

$exporter = new Exporter();
$exporter
->setSections($input->getOption('sections'))
->setElements($input->getOption('elements'));

foreach ($this->iblocks as $iblock) {

try {
$xml_id = \CIBlockCMLExport::GetIBlockXML_ID($iblock['ID']);
$path = implode(DIRECTORY_SEPARATOR, [$this->dir, $xml_id]) . $this->extension;

$exporter
->setPath($path)
->setId($iblock['ID'])
->execute();

$output->writeln(sprintf('<info>%s</info> iblock %s %s', 'success', $iblock['CODE'], $path));

} catch (IblockException $e) {
$output->writeln(sprintf('<error>%s</error> iblock %s', 'fail', $iblock['CODE']));
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln($e->getMessage());
}
}
}

return true;
}
}
117 changes: 117 additions & 0 deletions src/Iblock/Command/ImportCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Notamedia\ConsoleJedi\Iblock\Command;

use Notamedia\ConsoleJedi\Application\Command\BitrixCommand;
use Notamedia\ConsoleJedi\Iblock\Exception\IblockException;
use Notamedia\ConsoleJedi\Iblock\Importer;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\FormatterHelper;
use Bitrix\Main\Loader;

/**
* Command import information block(s) from xml file(s)
*/
class ImportCommand extends BitrixCommand
{
use MigrationCommandTrait;

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('iblock:import')
->setDescription('Import information block(s) from xml')
->addArgument(
'type',
InputArgument::REQUIRED,
'Information block type'
)
->addArgument(
'sites',
InputArgument::REQUIRED,
'Sites to which the information block will be bound (if it is to be created)'
)
->addArgument(
'dir',
InputArgument::OPTIONAL,
'Directory to import'
)
->addOption(
'sections',
's',
InputOption::VALUE_OPTIONAL,
'If an existing section is no longer in the source file [ leave: "N", deactivate: "A", delete: "D" ]',
'A'
)
->addOption(
'elements',
'e',
InputOption::VALUE_OPTIONAL,
'If an existing element is no longer in the source file [ leave: "N", deactivate: "A", delete: "D" ]',
'A'
);

}

/**
* {@inheritdoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
Loader::includeModule('iblock');
}


/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$this->setDir($input);
$this->setType($input);
$this->setSites($input);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$formatter = new FormatterHelper();

if (count($this->errors) > 0) {
$output->writeln($formatter->formatBlock($this->errors, 'error'));
return false;
}

$importer = new Importer();
$importer
->setType($this->type)
->setSites($this->sites)
->setActionSection($input->getOption('sections'))
->setActionElement($input->getOption('elements'));;

foreach (glob(implode(DIRECTORY_SEPARATOR . '*', [$this->dir, $this->extension])) as $file) {

try {
$importer
->setPath($file)
->execute();

$output->writeln(sprintf('<info>%s</info> file %s', 'success', $file));

} catch (IblockException $e) {
$output->writeln(sprintf('<error>%s</error> file %s', 'fail', $file));
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln($e->getMessage());
}
}
}
}
}
Loading

0 comments on commit c2af967

Please sign in to comment.