We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    Is there a way to set/reset a TV value with a snippet over multiple pages?

    I have a TV "counter" for each page that needs to be reset [to 0] every month on every page.

    How would I go about doing this?

    -thanks
    -sean
      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 3749
      • 24,544 Posts
      This should do it if you are using MODx Revolution (untested):

      <?php
      /* ResetCounters snippet */
      
      $tvId = ##; // set to the ID of the 'counter' TV.
      
      /* Get the templateVarResource objects */
      
      $tvrs = $modx->getCollection('modTemplateVarResource', array('tmplvarid' => $tvId));
      
      if (! empty ($tvrs)) {
      
          foreach($tvrs as $tvr) {
              $tvr->set('value', 0);
              $tvr->save();
         }
      
      } else {
      
          return 'No templateVarResource objects found';
      }
      
      return 'TVs reset';
      


      Just put the tag [[ResetCounters]] on a page that is unpublished and/or hidden from menus and preview that page once a month.
        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
        • 26503
        • 620 Posts
        Thanks Bob;

        I was looking at resetting all the vars using a database query, this is much cleaner... though would have to be scheduled VIA a cron job, which I was hoping to avoid...

        -thanks
        -sean
          *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

          Sean Kimball CLP, CLS.
          Technical Director / Sr. Developer | BigBlock Studios
          ._______________________________________________.
          Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
          27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
          phone/fax: 905-426-5525
          • 3749
          • 24,544 Posts
          Since you want it done on the first of every month, you could store the current month somewhere (e.g. a file or a system setting), then check it in index.php. When the current month doesn’t match the stored month -- run the snippet and reset the stored month. The first person to visit the site after midnight on the 1st of each month would trigger it. I doubt if it would take more than a second or two to run.
            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
            • 27106
            • 147 Posts
            Just a note to point out that if you are using a TV to flag the items you want included in each EmailResource mailing, you can adapt Bob's snippet to reset the TV after the mailing. That is, if you have a checkbox labelled "Include in mailing" which can be ticked for all the items in your mailing, then this is a way to set those checkboxes to blank again before your next new mailing.

            Bob, one day when you have nothing better to do (!) this would be a nice optional add-on to EmailResource.
              David Walker
              Principal, Shorewalker DMS
              Phone: 03 8899 7790
              Mobile: 0407 133 020
              • 31104
              • 108 Posts
              Quote from: BobRay at Aug 08, 2011, 05:52 PM
              This should do it if you are using MODx Revolution (untested):

              <!--?php
              /* ResetCounters snippet */
              
              $tvId = ##; // set to the ID of the 'counter' TV.
              
              /* Get the templateVarResource objects */
              
              $tvrs = $modx--->getCollection('modTemplateVarResource', array('tmplvarid' => $tvId));
              
              if (! empty ($tvrs)) {
              
                  foreach($tvrs as $tvr) {
                      $tvr->set('value', 0);
                      $tvr->save();
                 }
              
              } else {
              
                  return 'No templateVarResource objects found';
              }
              
              return 'TVs reset';
              


              Just put the tag [[ResetCounters]] on a page that is unpublished and/or hidden from menus and preview that page once a month.

              Marvelous, thank you Bob!
                • 3749
                • 24,544 Posts
                Glad I could help.


                I should have said to use this tag (with the exclamation point):

                [[!ResetCounters]] 



                If you can set up a cron job (usually possible in cPanel), and the page is published, but not shown in menus, you should be able to create a monthly one that visits the page:

                wget -O - http://yoursite.com/url/of/reset/page >/dev/null 2>&1


                If cPanel doesn't help you fill in the date options, the full line would be:

                0 0 1 * * wget -O - http://yoursite.com/url/of/reset/page >/dev/null 2>&1


                (0 0 1 * * means: minute 0, hour 0, of day 1, for every month, and every year.)

                That will execute just after midnight on the first day of each month. Note that the first two characters are zeros, but the -O is the capital letter O.
                  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