We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 2453
    • 26 Posts
    Hi! I’m wondering how I might go about having a TVs value become the unpublish date for a specific template.

    I found http://modxcms.com/forums/index.php?topic=19893.0, but am having trouble adapting it. My understanding is this: I would set up a plug-in for that template, have it run whenever it saves, and there would be php code that would make the correct adjustments. Am I on the right track, or any ideas on how I might go about this?

    I’m using Revolution.
      • 2453
      • 26 Posts
      I think I’ll have to tell the editor to set the unpublish date manually.

      So much to lean, not enough time to learn it!

      Thanks.
        • 33968
        • 863 Posts
        Hi, I think you are on the right track and that post seems to be a good example to follow. I’ve not done much custom coding in Revo yet but a plugin associated to ’OnDocFormSave’ is likely the way to go.

        Why not post your attempt here and we can have a look at it?
          • 33968
          • 863 Posts
          I had a go at this as it seemed like a straightforward one to get started on.

          Just aiming to set the Unpublish date to one day after the publish date:
          <?php
          
          $pdate = $resource->get('publishedon');
          $update = $resource->get('unpub_date');
          $offset = 86400; // 1 day = 86400 sec
          
          // calculate new unpublish date
          $pdate = strtotime($pdate);
          $nupdate = $pdate + $offset;
          
          // save new unpublish date into field
          $update->set('unpub_date', $nupdate);
          $update->save();


          I can get the correct output with a snippet but when converted to a plugin (OnDocFormSave checked) just results in the eternal ’Please wait...’ message.

          Any ideas?
            • 3749
            • 24,544 Posts
            Quote from: lucas at Mar 18, 2011, 12:43 PM

            I had a go at this as it seemed like a straightforward one to get started on.

            Just aiming to set the Unpublish date to one day after the publish date:
            <?php
            // save new unpublish date into field
            $update->set('unpub_date', $nupdate);
            $update->save();


            Try this:

            <?php
            // save new unpublish date into field
            $resource->set('unpub_date', $nupdate);
            // You shouldn't need this: $resource->save();


              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
              • 33968
              • 863 Posts
              Hi Bob, all works fine now. Thanks!
              I take it that ’save()’ is not required because this occurs when the manager form ’save’ button is pressed anyway?

              Don’t know what happened to the OP smiley
                • 3749
                • 24,544 Posts
                Quote from: lucas at Mar 23, 2011, 02:34 PM

                Hi Bob, all works fine now. Thanks!
                I take it that ’save()’ is not required because this occurs when the manager form ’save’ button is pressed anyway?

                Right, the plugin code occurs between the button click and the actual save.
                  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
                  • 17667
                  • 108 Posts
                  I am interested in doing something similar. When I set up the plugin with this code how do I have it run only for a select template?
                    • 3749
                    • 24,544 Posts
                    Put this at the top:

                    if ($resource->get('template') != ##) {
                        return;
                    }
                    

                    Replace ## with the ID of the template.
                      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
                      • 17667
                      • 108 Posts
                      Excellent, this forum rocks.

                      For anyone working with MODX Revo and looking for a similar solution, I have a template with a TV where you add an integer for the number of days after the Publish Date you want to automatically set the Unpublish date. I created a new plugin. Under System Events I checked OnDocFormSave. Then for the plugin code I entered:

                      // only run this code for the template with the ID of 6
                      if ($resource->get('template') != 6) {
                      	return;
                      }
                      
                      // get the publish date
                      $publish_date = $resource->get('publishedon');
                      
                      // get the template variable with the number of days after publish date to set unpublish date
                      $unpublish_days = $resource->getTVValue('unpublish_days');
                      
                      // calculate unpublish date
                      $publish_date = strtotime($publish_date);
                      $offset = 86400 * $unpublish_days;
                      $new_unpublish_date = $publish_date + $offset;
                      
                      // save new unpublish date into field
                      $resource->set('unpub_date', $new_unpublish_date);