Interesting idea.
Your dashboard widget could have a button with some JavaScript to make an Ajax call to a processor that toggled the site status. Or, it could be an HTML form that toggled the site_status System Setting when submitted.
The actual code to toggle the status would look like this (tested):
<?php
/* ToggleSiteStatus */
$setting = $modx->getObject('modSystemSetting', array('key' => 'site_status'));
$status = $setting->get('value');
$newStatus = empty($status) ? '1' : '0';
$setting->set('value', $newStatus);
$setting->save();
/* Clear System Settings cache */
$cm = $modx->getCacheManager();
$cm->refresh(array('system_settings' => array()));
You'd probably want the text of the submit button in your widget or CMP to toggle as well.
It's not as elegant, but you could put the code above in a snippet, put an uncached tag for that snippet on a page, and view the page when you want to toggle the site_status.
You don't want to put the code itself in a PHP widget or use the snippet tag in an HTML widget because then the site_status would toggle every time you logged in or viewed the dashboard.
[Update]
OK, here's a quick-and-dirty widget that will toggle the site_status setting from the Dashboard.
Create the Widget
- Go to System (gear icon) -> Dashboards
- Click on the Widgets tab
- Click on the "Create New Widget" tab
- Fill in the Name: Toggle Site Status
- Set the size to "Half"
- Set the Widget Type to Inline PHP Widget
- Paste this code into the Widget Content Field:
<?php
$output = '';
if (isset($_POST['submitVarToggle'])) {
$setting = $modx->getObject('modSystemSetting', array('key' => 'site_status'));
$status = $setting->get('value');
$newStatus = empty($status) ? '1' : '0';
$setting->set('value', $newStatus);
$setting->save();
/* Clear System Settings cache */
$cm = $modx->getCacheManager();
$cm->refresh(array('system_settings' => array()));
$statusMessage = $newStatus ? ' Take Site Offline ' : ' Bring Site Online ';
} else {
$currentStatus = $modx->getOption('site_status');
$statusMessage = $currentStatus? ' Take Site Offline ' : ' Bring Site Online ';
}
$output .= '
<form action = "" method="post">
<input style="padding:10px" type="submit" name="submitVarToggle" value="' . $statusMessage . '" />
</form>';
// $output .= "<br /><br />" . print_r($_POST, true);
return $output;
Connect the widget to a dashboard:
- Click on the "Save" button
- Click on the "Close" button
- Click on the "Dashboards" tab
- Right-click on the Dashboard you want to add the widget to and select "Update Dashboard"
- Click on "Place Widget"
- Use the drop-down menu to select your widget
- Click on the "Save" button in the dialog
- Click on the "Save" button at the upper right
This will reload the Manager page whenever you toggle the status. The only way to avoid that would be with JavaScript, which would be more complicated.
[ed. note: BobRay last edited this post 10 years, 6 months ago.]