diff --git a/retailcrm/controllers/front/Jobs.php b/retailcrm/controllers/front/Jobs.php index 1b890c0..d3143a4 100644 --- a/retailcrm/controllers/front/Jobs.php +++ b/retailcrm/controllers/front/Jobs.php @@ -77,12 +77,9 @@ class RetailcrmJobsModuleFrontController extends ModuleFrontController */ protected function getData() { - RetailcrmJobManager::startJobs(array( - 'RetailcrmAbandonedCartsEvent' => null, - 'RetailcrmIcmlEvent' => new \DateInterval('PT4H'), - 'RetailcrmSyncEvent' => new \DateInterval('PT7M'), - 'RetailcrmInventoriesEvent' => new \DateInterval('PT15M') - )); + if (RetailcrmTools::isWebJobsEnabled()) { + RetailcrmTools::startJobManager(); + } return array('success' => true); } diff --git a/retailcrm/lib/RetailcrmCli.php b/retailcrm/lib/RetailcrmCli.php index 157164d..5e4b186 100644 --- a/retailcrm/lib/RetailcrmCli.php +++ b/retailcrm/lib/RetailcrmCli.php @@ -99,7 +99,15 @@ class RetailcrmCli } $shortopts = "j:"; - $longopts = array("job:", "reset-job-manager", "reset-all"); + $longopts = array( + "job:", + "set-web-jobs:", + "query-web-jobs", + "run-jobs", + "reset-job-manager", + "reset-all" + ); + $options = getopt($shortopts, $longopts); $jobName = isset($options['j']) ? $options['j'] : (isset($options['job']) ? $options['job'] : null); @@ -107,6 +115,12 @@ class RetailcrmCli $this->resetJobManager(); } elseif (isset($options['reset-all'])) { $this->resetAll(); + } elseif (isset($options['query-web-jobs'])) { + $this->queryWebJobs(); + } elseif (isset($options['run-jobs'])) { + RetailcrmTools::startJobManager(); + } elseif (isset($options['set-web-jobs'])) { + $this->setWebJobs(self::getBool($options['set-web-jobs'])); } elseif (empty($jobName)) { $this->help(); } else { @@ -181,6 +195,9 @@ class RetailcrmCli RetailcrmLogger::output(); RetailcrmLogger::output(sprintf('> php %s -j - Runs provided job', $this->cliPath)); RetailcrmLogger::output(sprintf('> php %s --job - Runs provided job', $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 --query-web-jobs - Check web jobs status', $this->cliPath)); RetailcrmLogger::output(); RetailcrmLogger::output( "WARNING: Commands below are dangerous and should be used only when " . @@ -199,6 +216,43 @@ class RetailcrmCli RetailcrmLogger::output(); } + /** + * Sets new web jobs state + * + * @param bool $state + */ + private function setWebJobs($state) + { + $this->loadConfiguration(); + + Configuration::updateGlobalValue(RetailCRM::ENABLE_WEB_JOBS, $state ? '1' : '0'); + RetailcrmLogger::output('Updated web jobs state.'); + $this->queryWebJobs(); + } + + /** + * Prints web jobs status + */ + private function queryWebJobs() + { + $this->loadConfiguration(); + + RetailcrmLogger::output(sprintf( + 'Web jobs status: %s', + RetailcrmTools::isWebJobsEnabled() ? 'true (enabled)' : 'false (disabled)' + )); + } + + /** + * Load PrestaShop configuration if it's not loaded yet + */ + private function loadConfiguration() + { + if (!Configuration::configurationIsLoaded()) { + Configuration::loadConfiguration(); + } + } + /** * Resets JobManager */ @@ -267,6 +321,24 @@ class RetailcrmCli return true; } + /** + * Converts string param from CLI into boolean + * + * @param string $param + */ + private static function getBool($param) + { + if ('true' == $param) { + return true; + } + + if ('false' == $param) { + return false; + } + + return (bool) $param; + } + /** * Returns list of jobs which are allowed to be executed via cli * diff --git a/retailcrm/lib/RetailcrmTools.php b/retailcrm/lib/RetailcrmTools.php index cf53530..f939629 100644 --- a/retailcrm/lib/RetailcrmTools.php +++ b/retailcrm/lib/RetailcrmTools.php @@ -365,6 +365,11 @@ class RetailcrmTools return null; } + public static function isWebJobsEnabled() + { + return '0' !== Configuration::getGlobalValue(RetailCRM::ENABLE_WEB_JOBS); + } + /** * Merge new address to customer, preserves old phone numbers. * @@ -460,4 +465,19 @@ class RetailcrmTools return $code; } + + /** + * Starts JobManager with list of pre-registered jobs + * + * @throws \Exception + */ + public static function startJobManager() + { + RetailcrmJobManager::startJobs(array( + 'RetailcrmAbandonedCartsEvent' => null, + 'RetailcrmIcmlEvent' => new \DateInterval('PT4H'), + 'RetailcrmSyncEvent' => new \DateInterval('PT7M'), + 'RetailcrmInventoriesEvent' => new \DateInterval('PT15M') + )); + } } diff --git a/retailcrm/retailcrm.php b/retailcrm/retailcrm.php index 17c5bc8..52c2917 100644 --- a/retailcrm/retailcrm.php +++ b/retailcrm/retailcrm.php @@ -71,6 +71,7 @@ class RetailCRM extends Module const LATEST_API_VERSION = '5'; const CONSULTANT_SCRIPT = 'RETAILCRM_CONSULTANT_SCRIPT'; const CONSULTANT_RCCT = 'RETAILCRM_CONSULTANT_RCCT'; + const ENABLE_WEB_JOBS = 'RETAILCRM_ENABLE_WEB_JOBS'; /** * @var array $templateErrors