We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51216
    • 35 Posts
    Hi All

    I'm not very clued up on how to develop an extra for MODX however, I just had this idea that I think will be a cool project for someone.

    I find it a little tedious to turn site_status on/off via system settings. Would it be possible to have an extra create a menu item to do this just by clicking on it?

    I managed to add the lexicon key "setting_site_status" as a menu item via the menus editor but nothing further.

    Any feedback welcome!
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You would need to make a full-blown CMP to process that menu item click. A Dashboard widget would be a lot easier, more like a small snippet.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 51216
        • 35 Posts
        Quote from: sottwell at Mar 25, 2016, 09:47 AM
        You would need to make a full-blown CMP to process that menu item click. A Dashboard widget would be a lot easier, more like a small snippet.

        I see. How would I go about creating a dashboard widget for this?
        Could you show me how?
          • 3749
          • 24,544 Posts
          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

          1. Go to System (gear icon) -> Dashboards
          2. Click on the Widgets tab
          3. Click on the "Create New Widget" tab
          4. Fill in the Name: Toggle Site Status
          5. Set the size to "Half"
          6. Set the Widget Type to Inline PHP Widget
          7. 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:


          1. Click on the "Save" button
          2. Click on the "Close" button
          3. Click on the "Dashboards" tab
          4. Right-click on the Dashboard you want to add the widget to and select "Update Dashboard"
          5. Click on "Place Widget"
          6. Use the drop-down menu to select your widget
          7. Click on the "Save" button in the dialog
          8. 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.]
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 51216
            • 35 Posts
            Wow! Great work on the widget Bob! Works like a charm...

            I'm going to play with your code and see what else is possible
              • 3749
              • 24,544 Posts
              I'm glad it worked for you. Thanks for reporting back. smiley
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting