Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Commit

Permalink
Add one-way jobs to plgJobToggleOffline
Browse files Browse the repository at this point in the history
Add jobs setOffline and setOnline. These jobs enable a single
directive rather than the blind toggle in toggleOffline.
  • Loading branch information
ditsuke committed Aug 15, 2021
1 parent 15fcf40 commit 77c11e2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
PLG_JOB_TOGGLE_OFFLINE="Job - Toggle Offline"
PLG_JOB_TOGGLE_OFFLINE_DESC="Toggles the site's offline status on each run"
PLG_JOB_TOGGLE_OFFLINE_DESC="Toggles the site's status on each run."
PLG_JOB_TOGGLE_OFFLINE_ERROR_CONFIGURATION_PHP_NOTUNWRITABLE="Could not make configuration.php un-writable."
PLG_JOB_TOGGLE_OFFLINE_ERROR_CONFIGURATION_PHP_NOTWRITABLE="Could not make configuration.php writable"
PLG_JOB_TOGGLE_OFFLINE_ERROR_CONFIGURATION_PHP_NOTWRITABLE="Could not make configuration.php writable."
PLG_JOB_TOGGLE_OFFLINE_ERROR_WRITE_FAILED="Could not write to the configuration file!"
PLG_JOB_TOGGLE_OFFLINE_JOB_LOG_MESSAGE="Job> ToggleOffline return code is: %1$d. Processing Time: %2$.2f seconds"
PLG_JOB_TOGGLE_OFFLINE_JOB_LOG_SITE_STATUS="Site is now: %1$s"
PLG_JOB_TOGGLE_OFFLINE_TITLE="Toggle Offline"
PLG_JOB_TOGGLE_OFFLINE_XML_DESCRIPTION="Toggles the site's offline status on each run"
PLG_JOB_TOGGLE_OFFLINE_JOB_LOG_MESSAGE="Job> ToggleOffline return code is: %1$d. Processing Time: %2$.2f seconds."
PLG_JOB_TOGGLE_OFFLINE_JOB_LOG_SITE_STATUS="Site was %1$s, is now %2$s."
PLG_JOB_TOGGLE_OFFLINE_SET_OFFLINE_DESC="Sets site offline to online on each run."
PLG_JOB_TOGGLE_OFFLINE_SET_OFFLINE_JOB_LOG_MESSAGE="Job> SetOffline return code is: %1$d. Processing Time: %2$.2f seconds."
PLG_JOB_TOGGLE_OFFLINE_SET_OFFLINE_TITLE="Get Site Offline."
PLG_JOB_TOGGLE_OFFLINE_SET_ONLINE_DESC="Sets site status to online on each run."
PLG_JOB_TOGGLE_OFFLINE_SET_ONLINE_JOB_LOG_MESSAGE="Job> SetOnline return code is: %1$d. Processing Time: %2$.2f seconds."
PLG_JOB_TOGGLE_OFFLINE_SET_ONLINE_TITLE="Get Site Online."
PLG_JOB_TOGGLE_OFFLINE_TITLE="Toggle Offline."
PLG_JOB_TOGGLE_OFFLINE_XML_DESCRIPTION="Toggles the site's offline status on each run."
38 changes: 31 additions & 7 deletions plugins/job/toggleoffline/toggleoffline.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,20 @@ class PlgJobToggleoffline extends CMSPlugin implements SubscriberInterface
*/
protected const JOBS_MAP = [
'plg_job_toggle_offline' => [
'langConstPrefix' => 'PLG_JOB_TOGGLE_OFFLINE'
]
'langConstPrefix' => 'PLG_JOB_TOGGLE_OFFLINE',
'toggle' => true
],
'plg_job_toggle_offline_set_online' => [
'langConstPrefix' => 'PLG_JOB_TOGGLE_OFFLINE_SET_ONLINE',
'toggle' => false,
'offline' => false
],
'plg_job_toggle_offline_set_offline' => [
'langConstPrefix' => 'PLG_JOB_TOGGLE_OFFLINE_SET_OFFLINE',
'toggle' => false,
'offline' => true
],

];

/**
Expand Down Expand Up @@ -101,12 +113,24 @@ public function toggleOffline(CronRunEvent $event): void

$config = ArrayHelper::fromObject(new JConfig);

$config['offline'] = !$config['offline'];
$status = $config['offline'] ? 'offline' : 'online';
$this->writeConfigFile(new Registry($config));
$this->addJobLog(Text::sprintf('PLG_JOB_TOGGLE_OFFLINE_JOB_LOG_SITE_STATUS', $status));
$toggle = self::JOBS_MAP[$event->getJobId()]['toggle'];
$oldStatus = $config['offline'] ? 'offline' : 'online';

if ($toggle)
{
$config['offline'] = !$config['offline'];
}
else
{
$offline = self::JOBS_MAP[$event->getJobId()]['offline'];
$config['offline'] = $offline;
}

$newStatus = $config['offline'] ? 'offline' : 'online';
$exit = $this->writeConfigFile(new Registry($config));
$this->addJobLog(Text::sprintf('PLG_JOB_TOGGLE_OFFLINE_JOB_LOG_SITE_STATUS', $oldStatus, $newStatus));

$this->jobEnd($event, 0);
$this->jobEnd($event, $exit);
}

/**
Expand Down

0 comments on commit 77c11e2

Please sign in to comment.