We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27708 MODX Staff
    • 2,502 Posts
    Based on the help I received from @dev_cw and @sottwell, I have built out a modified and genericized version of my unpubdate plugin.

    The Automatic Unpublish plugin will automatically insert a value for unpub_date based on a document variable date value or a TV with a date value.

    I use it in an events list where there is an event coming up so I set an event date (TV called event_date). This allows the user to set the event date and once the event has happened the document will automatically unpublish. This means the user only needs to set the event date and not worry about another field setting.

    The plugin code has expanded and I’ve added some parameters to it via the config.

    Here’s the code:
    <?php //<-Delete the php open tag
    /*
    * Automatic Unpublish Plugin 
    *
    * Sets unpub_date using another date field as reference.
    *
    * Written By Jay Gilmore - June 6, 2009
    * With assistance of Susan Otwell (sottwell) and Shane Sponagle (dev_cw)
    *
    * Version 1.0.1-alpha
    *
    * Events: OnDocFormSave
    *
    */
    
    if(!$fieldName){
        return;   
    }
    
    if (!$fieldType){
        $fieldType = 'Document Variable';
    }
    
    if($fieldType == 'Template Variable'){
        $fieldPrefix = 'tv'; 
    }
    else{
        $fieldPrefix = '';
    }
    
    if(!$offsetQ){
        $offsetQ = '1';   
    }
    
    if(!$offsetU){
        $offsetU = 'Day';
    }
    
    if($offsetQ){
        $offset = '+'.$offsetQ . ' ' . $offsetU;
    }
    
    else {
        $offset = $offsetU;
    }
    
    $postfield = $fieldPrefix . $fieldName;
    
    if($_POST[$postfield]  ){
    
        $e = &$modx->Event;
        switch ($e->name) {
            case "OnDocFormSave":
                $ud = strtotime($_POST[$postfield]. $offset);
                $unpub_date = $ud;
                $table = $modx->getFullTableName( 'site_content' );
                $fields = array('unpub_date' => $unpub_date);
    
                // Check to see if there is an unpub_date set and skip the update if it is.
                if($_POST['unpub_date'] != $ud){
                    // Update the db with the field value
                    $modx->db->update( $fields, $table, 'id = "'.$id.'"' );
                }
                break;
            default :
                return; 
                break;
        }
    }
    


    Config:
    &fieldName=Field Name;text; &fieldType=Field Type;list;Template Variable, Document Variable; &offsetQ=Offset Quantity;text; &offsetU=Offset Unit;list;Day, Week, Month, Year;
    

    Instructions:
    1. Copy Plugin Code to a New Plugin
    2. Copy Config code to the plugin config field.
    3. Set Config paramaters.
    4. Save.

    I just want people to try it out before I post it as I think there may be some fixes. Please let me know if you need help or to request a change.

    Cheers,

    Jay
      Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
      • 7231
      • 4,205 Posts
      Nice. The only thing that may be nice would be to add a comparison to only do the query if the unpud and the event TV are not ==. This would eliminate the query if not needed for example if you are only editing the pagetitle. grin
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don&#39;t know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 27708 MODX Staff
        • 2,502 Posts
        I don’t know that this makes much of a difference but have updated the plugin to only do the db query if the unpub_date is different.
          Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
          • 20413
          • 2,877 Posts
          Tnx 4 sharing! smiley
            @hawproductions | http://mrhaw.com/

            Infograph: MODX Advanced Install in 7 steps:
            http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

            Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
            http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
            • 33337
            • 3,975 Posts
            Nice stuff Jay smiley

            thanks!
              Zaigham R - MODX Professional | Skype | Email | Twitter

              Digging the interwebs for #MODX gems and bringing it to you. modx.link
              • 16545
              • 358 Posts
              Very nice and useful plugin.... wink

              It also can be used (only with plugin modification) to modify some TV value on scheduled event.
              For example: if we need to expire membership TV (to change it from VIP to Guest).
                • 27708 MODX Staff
                • 2,502 Posts
                Quote from: labasus at Jun 14, 2009, 01:18 AM

                Very nice and useful plugin.... wink

                It also can be used (only with plugin modification) to modify some TV value on scheduled event.
                For example: if we need to expire membership TV (to change it from VIP to Guest).

                Thanks,

                Can you post your mod? I’d like to look at adding the variable/parameter to make it optional.

                Cheers
                  Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
                  • 26931
                  • 2,314 Posts
                  hi there,

                  this is exactly what i was looking for smiley does this plugin work with Evo 1.0.2?

                  1) installed the plugin ( Configuration: Field Name: enddate, Field Type: Template Variable, Offset Quantity: 10, Offset Unit: day )

                  2) created a TV "enddate" ( Input Type: Date, Widget: Date Formatter ... also tried with unixtime )

                  anything i could have missed?

                  thanks, j
                    • 27708 MODX Staff
                    • 2,502 Posts
                    This works w/ Evo 1.0.2 but you need to use the tv id rather than tv name. Content variable names work but this uses POST and you need that ID # as tvs don’t use the TV Name in the forms anymore.

                    There may also be minor issues with the date.

                    I may need to update this as I worked with splittingred on modifying it. I’ll test it this week and let you know if there are other issues.

                    Cheers,

                    Jay
                      Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
                      • 26931
                      • 2,314 Posts
                      This works w/ Evo 1.0.2 but you need to use the tv id rather than tv name. Content variable names work but this uses POST and you need that ID # as tvs don’t use the TV Name in the forms anymore.

                      There may also be minor issues with the date.

                      I may need to update this as I worked with splittingred on modifying it. I’ll test it this week and let you know if there are other issues.
                      great, thanks smiley (right now i use unpub_date and moved it with ManagerManager...works in my case too)