Added help arguments. Improved multistore checks. Added query-shops argument

This commit is contained in:
max-baranikov 2021-12-22 12:00:28 +03:00 committed by GitHub
parent 41794dca51
commit f5bfcc0961
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -99,10 +99,12 @@ class RetailcrmCli
RetailcrmLogger::output('WARNING: cannot handle signals properly, force stop can cause problems!'); RetailcrmLogger::output('WARNING: cannot handle signals properly, force stop can cause problems!');
} }
$shortopts = 'j:s:'; $shortopts = 'hj:s:';
$longopts = [ $longopts = [
'help',
'job:', 'job:',
'shop:', 'shop:',
'query-shops',
'set-web-jobs:', 'set-web-jobs:',
'query-web-jobs', 'query-web-jobs',
'run-jobs', 'run-jobs',
@ -124,11 +126,15 @@ class RetailcrmCli
RetailcrmTools::startJobManager(); RetailcrmTools::startJobManager();
} elseif (isset($options['set-web-jobs'])) { } elseif (isset($options['set-web-jobs'])) {
$this->setWebJobs(self::getBool($options['set-web-jobs']), $shopId); $this->setWebJobs(self::getBool($options['set-web-jobs']), $shopId);
} elseif (empty($jobName)) { } elseif (isset($options['query-shops'])) {
$this->help(); $this->queryShops();
} else { } elseif (!empty($jobName)) {
$this->setCleanupOnShutdown(); $this->setCleanupOnShutdown();
$this->runJob($jobName, $shopId); $this->runJob($jobName, $shopId);
} elseif (isset($options['help']) || isset($options['h'])) {
$this->printHelp();
} else {
$this->printArgumentError('Unknown argument!');
} }
} }
@ -198,7 +204,7 @@ class RetailcrmCli
/** /**
* Prints CLI help * Prints CLI help
*/ */
private function help() private function printHelp()
{ {
RetailcrmLogger::output('Available jobs:'); RetailcrmLogger::output('Available jobs:');
RetailcrmLogger::output(); RetailcrmLogger::output();
@ -215,6 +221,9 @@ class RetailcrmCli
RetailcrmLogger::output(sprintf('> php %s --run-jobs - Run default jobs routine', $this->cliPath)); RetailcrmLogger::output(sprintf('> php %s --run-jobs - Run default jobs routine', $this->cliPath));
RetailcrmLogger::output(sprintf('> php %s --set-web-jobs true / false - Enable or disable web jobs', $this->cliPath)); RetailcrmLogger::output(sprintf('> php %s --set-web-jobs true / false - Enable or disable web jobs', $this->cliPath));
RetailcrmLogger::output(sprintf('> php %s --query-web-jobs - Check web jobs status', $this->cliPath)); RetailcrmLogger::output(sprintf('> php %s --query-web-jobs - Check web jobs status', $this->cliPath));
RetailcrmLogger::output(sprintf('> php %s --query-shops - Get list of shops with ids (for MultiShop)', $this->cliPath));
RetailcrmLogger::output(sprintf('> php %s -h - Shows this page', $this->cliPath));
RetailcrmLogger::output(sprintf('> php %s --help - Shows this page', $this->cliPath));
RetailcrmLogger::output(); RetailcrmLogger::output();
RetailcrmLogger::output( RetailcrmLogger::output(
'NOTICE: If you have MultiShop feature enabled, you can additionally ' . 'NOTICE: If you have MultiShop feature enabled, you can additionally ' .
@ -242,6 +251,15 @@ class RetailcrmCli
RetailcrmLogger::output(); RetailcrmLogger::output();
} }
private function printArgumentError($message = '')
{
if (!empty($message)) {
RetailcrmLogger::output($message);
}
RetailcrmLogger::output('Use -h or --help to get more info about CLI');
}
/** /**
* Sets new web jobs state * Sets new web jobs state
* *
@ -250,13 +268,12 @@ class RetailcrmCli
*/ */
private function setWebJobs($state, $shopId = null) private function setWebJobs($state, $shopId = null)
{ {
if (null === $shopId) { $shopId = $this->setShopId($shopId);
RetailcrmLogger::output('You must specify shop id');
if (null === $shopId) {
return; return;
} }
RetailcrmContextSwitcher::setShopContext($shopId);
$this->loadConfiguration(); $this->loadConfiguration();
Configuration::updateValue(RetailCRM::ENABLE_WEB_JOBS, $state ? '1' : '0'); Configuration::updateValue(RetailCRM::ENABLE_WEB_JOBS, $state ? '1' : '0');
@ -271,13 +288,12 @@ class RetailcrmCli
*/ */
private function queryWebJobs($shopId = null) private function queryWebJobs($shopId = null)
{ {
if (null === $shopId) { $shopId = $this->setShopId($shopId);
RetailcrmLogger::output('You must specify shop id');
if (null === $shopId) {
return; return;
} }
RetailcrmContextSwitcher::setShopContext($shopId);
$this->loadConfiguration(); $this->loadConfiguration();
RetailcrmLogger::output(sprintf( RetailcrmLogger::output(sprintf(
@ -286,6 +302,44 @@ class RetailcrmCli
)); ));
} }
private function queryShops()
{
$isFeatureActive = Shop::isFeatureActive();
RetailcrmLogger::output(sprintf(
'Multistore status: %s',
$isFeatureActive ? 'true (enabled)' : 'false (disabled)'
));
$shops = RetailcrmContextSwitcher::getShops();
RetailcrmLogger::output(
"\nShop ID\t| Shop Name"
);
foreach ($shops as $shop) {
RetailcrmLogger::output(sprintf(
"%s\t- %s",
$shop['id_shop'],
$shop['name']
));
}
}
private function setShopId($shopId)
{
if (null === $shopId) {
$shopId = Shop::getContextShopID();
}
if (null === $shopId) {
$this->printArgumentError('You must specify shop id');
} else {
RetailcrmContextSwitcher::setShopContext($shopId);
}
return $shopId;
}
/** /**
* Load PrestaShop configuration if it's not loaded yet * Load PrestaShop configuration if it's not loaded yet
*/ */