We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16278
    • 928 Posts
    $_POST[’unpub_date’] = $unpub_date;
    No can do that - $_POST array is read only shocked
    KP
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      My bad. The OnBeforeDocFormSave event is triggered just before the insert/update query, long after the POST array has been broken down into individual variables, so you can change the $unpub_date variable. The event is triggered after the date processing code, so you’d need to make sure your date value is a valid timestamp.
      http://il2.php.net/mktime
      http://il2.php.net/manual/en/function.strtotime.php
        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
        • 7231
        • 4,205 Posts
        There may be a problem getting the TV value using the API call having it set to only return published documents and using the onBeforeDocFormSave since if it is a new document it will only be set as published after saving. You can get the TV value using $_POST[’tvevent_date’] without using the API.

        To record the value you can do an insert query to update the pub_date value, but will need to change the plugin event trigger to onDocFormSave so that it is triggered after the data is recorded. Otherwise the racord will be overwritten by the document defined values.
        <?php //<-Only here for syntax highlighting
        $e = &$modx->Event;
        switch ($e->name) {
            case "OnDocFormSave":
                //*** USE DB API TO UPDATE THE PUB_DATE FIELD HERE
                $unpub_date = $_POST['tvevent_date'];
                $table = $modx->getFullTableName( 'site_content' );
                $fields = array('unpub_date' => $unpub_date);
                $modx->db->update( $fields, $table, 'id = "'.$id.'"' );
                break;
            default :
                return; // stop here - this is very important.
                break;
        }


        ** this is untested...just an idea.
          [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
          Shane,

          Didn’t work. I seriously don’t think this action can work. If someone wants to prove me wrong go for it but I can’t write an unpub_date be it to the db or the $_POST data.

          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
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            I’m a bit confused as to exactly why you would want to do this. Can’t your editor just use the regular unpublish field when editing the document?
              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
              • 27708 MODX Staff
              • 2,502 Posts
              Susan,

              I am going to instruct them to to it manually. The idea was to enforce a standard of unpublising the event document 1 day after the event date without the client having to manually edit.

              Thanks for all the help.
                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
                Quote from: sottwell at Jun 07, 2009, 11:53 PM

                I’m a bit confused as to exactly why you would want to do this. Can’t your editor just use the regular unpublish field when editing the document?
                Some of my clients have a hard time finding the pub date otpions since they are in the second tab. Can ManagerManager be used to move these fields to the main tab? that may be a solution.

                As for it not working...It seems that $_POST[’tvevent_time’] has the time selector format and has not been converted to timestamp yet. Add a strtotime to the code and it will work. This worked on my test server. You may want to avoid the query if the tv date is already the same as the unpub date.

                //<?php 
                $e = &$modx->Event;
                switch ($e->name) {
                    case "OnDocFormSave":
                        $unpub_date = strtotime($_POST['tveventdate']);
                        $table = $modx->getFullTableName( 'site_content' );
                        $fields = array('unpub_date' => $unpub_date);
                        $modx->db->update( $fields, $table, 'id = "'.$id.'"' );
                        break;
                    default :
                        return; // stop here - this is very important.
                        break;
                }


                EDIT: to set it to +1 day automatically add the +1 day parameter to the strtotime....
                         $unpub_date = strtotime('+1 day', $_POST['tveventdate']);

                  [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
                  Shane,

                  Thanks. Main plugin works perfectly. The +1 day parameter doesn’t seem to be working though.

                  It may be due to a version issue. I will review the strtotime usage and see if I can’t make it work.

                  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
                    • 27708 MODX Staff
                    • 2,502 Posts
                    I got it working. Shane your syntax for adding the day to the strtotime was off.

                    So here is the working version:
                    //<?php
                    $e = &$modx->Event;
                    switch ($e->name) {
                        case "OnDocFormSave":
                            $unpub_date = strtotime($_POST['tvevent_date']. '+1 day');
                            $table = $modx->getFullTableName( 'site_content' );
                            $fields = array('unpub_date' => $unpub_date);
                            $modx->db->update( $fields, $table, 'id = "'.$id.'"' );
                            break;
                        default :
                            return; // stop here - this is very important.
                            break;
                    }
                    


                    The strtotime should have been marked up like this:
                    //<?php
                    $unpub_date = strtotime($_POST['tvevent_date']. '+1 day');


                    It works like a charm.

                    I think this could come in really handy I may add some user settings and post it to the repo.

                    CHeers all.
                      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