Skip to content

Commit

Permalink
Added help arguments. Improved multistore checks. Added query-shops a…
Browse files Browse the repository at this point in the history
…rgument
  • Loading branch information
max-baranikov authored Dec 22, 2021
1 parent 41794dc commit f5bfcc0
Showing 1 changed file with 65 additions and 11 deletions.
76 changes: 65 additions & 11 deletions retailcrm/lib/RetailcrmCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ public function execute($signalsHandler = null)
RetailcrmLogger::output('WARNING: cannot handle signals properly, force stop can cause problems!');
}

$shortopts = 'j:s:';
$shortopts = 'hj:s:';
$longopts = [
'help',
'job:',
'shop:',
'query-shops',
'set-web-jobs:',
'query-web-jobs',
'run-jobs',
Expand All @@ -124,11 +126,15 @@ public function execute($signalsHandler = null)
RetailcrmTools::startJobManager();
} elseif (isset($options['set-web-jobs'])) {
$this->setWebJobs(self::getBool($options['set-web-jobs']), $shopId);
} elseif (empty($jobName)) {
$this->help();
} else {
} elseif (isset($options['query-shops'])) {
$this->queryShops();
} elseif (!empty($jobName)) {
$this->setCleanupOnShutdown();
$this->runJob($jobName, $shopId);
} elseif (isset($options['help']) || isset($options['h'])) {
$this->printHelp();
} else {
$this->printArgumentError('Unknown argument!');
}
}

Expand Down Expand Up @@ -198,7 +204,7 @@ private function printStack($exception, $header = 'Error while executing a job:
/**
* Prints CLI help
*/
private function help()
private function printHelp()
{
RetailcrmLogger::output('Available jobs:');
RetailcrmLogger::output();
Expand All @@ -215,6 +221,9 @@ private function help()
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 --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(
'NOTICE: If you have MultiShop feature enabled, you can additionally ' .
Expand Down Expand Up @@ -242,6 +251,15 @@ private function help()
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
*
Expand All @@ -250,13 +268,12 @@ private function help()
*/
private function setWebJobs($state, $shopId = null)
{
if (null === $shopId) {
RetailcrmLogger::output('You must specify shop id');
$shopId = $this->setShopId($shopId);

if (null === $shopId) {
return;
}

RetailcrmContextSwitcher::setShopContext($shopId);
$this->loadConfiguration();

Configuration::updateValue(RetailCRM::ENABLE_WEB_JOBS, $state ? '1' : '0');
Expand All @@ -271,13 +288,12 @@ private function setWebJobs($state, $shopId = null)
*/
private function queryWebJobs($shopId = null)
{
if (null === $shopId) {
RetailcrmLogger::output('You must specify shop id');
$shopId = $this->setShopId($shopId);

if (null === $shopId) {
return;
}

RetailcrmContextSwitcher::setShopContext($shopId);
$this->loadConfiguration();

RetailcrmLogger::output(sprintf(
Expand All @@ -286,6 +302,44 @@ private function queryWebJobs($shopId = null)
));
}

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
*/
Expand Down

0 comments on commit f5bfcc0

Please sign in to comment.