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
    I am having trouble getting this to work. I have a TV called "event_date" into which the user places a date (converted to unix timestamp format) and I want to be able to update the document’s "unpub_date" content with the same date on save if there is a value.

    Here is the plugin I have created to do this but it doesn’t update the db field as intended. Looking for where I may have gone wrong.

    <?php //<-only there to allow syntax highlighting
    
    $tvValue = $modx->getTemplateVarOutput(array("event_date"), $published='1');
    $id = $modx->documentIdentifier;
    $unpub_date = $tvValue['event_date'];
    $table = $modx->getFullTableName('site_content');
    $e = &$modx->Event;
    switch ($e->name) {
        case "OnDocFormSave":
            $result = $modx->db->update('unpub_date = "' . $unpub_date . '"', $table, 'id = "' . $id . '"');
            break;
        default :
            return; // stop here - this is very important.
            break;
    }
    ?>
    


    Thanks,

    Jay

    UPDATE: The solution is posted here: http://modxcms.com/forums/index.php/topic,36496.msg220582.html#msg220582
      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
      Wouldn’t it be better to use the OnBeforeDocFormSave, setting the POST unpublish date field? You could print_r($_POST) before and after your changes to see exactly what you’re getting.

      Don’t forget that publish and unpublish information also needs to be in the /assets/cache/siteCache.idx.php cache file; I don’t believe that your method would do that. Letting MODx do the saving with a modified POST would.
        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,

        Thanks. I’ll try w/ OnBeforeDocFormSave.
          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 would definitely add the print_r lines in the plugin and check exactly what the POST contains when using the built-in publish/unpublish date widgets as well.
            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 getting a warning because I am trying to modify headers.

            Here is where my plugin code is at:
            <?php //<-Only here for syntax highlighting
            print_r($_POST);  
            $tvValue = $modx->getTemplateVarOutput(array("event_date"), $published='1');
            $unpub_date = $tvValue['event_date'];
            $e = &$modx->Event;
            switch ($e->name) {
                case "OnBeforeDocFormSave":
                    $_POST['unpub_date'] = $unpub_date; 
                    break;
                default :
                    return; // stop here - this is very important.
                    break;
            }
            
            
              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
              If someone has a suggestion, that’d be great. I’ll leave this for now as it seems that the processor won’t do what I need it to do.

              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
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                The header modification warning is only because the page has already been issued by the PHP error system. There should be a PHP error (other than the page header ones) somewhere in the error messages.
                  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
                  There doesn’t seem to be any other error and there are no other errors showing in the view or the system events.

                  I question if this will work. I’ve tried every single combination I could think of and it won’t write the value no matter what I have done. I’ve grabbed the value from the documentObject, I’ve grabbed the value from the post data and I’ve tried to add it via OnDocFormSave and OnBeforeDocFormSave. In addition, I’ve tried to write it to the db too. It just doesn’t seem to work.

                  I think there has to be either an issue with the save.processor.php or the plugin script.

                  I give up.

                    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
                    Hang on... let’s see if I can figure it out. What exactly are you doing? The user edits the document in the Manager and puts this value in the TV, then saves 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
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      To begin with, this is what is in the POST before saving:
                      [unpub_date] => 14-06-2009 08:51:34
                      And here is how the save_content processor handles this:
                      if (empty ($unpub_date)) {
                      	$unpub_date = 0;
                      } else {
                      	list ($d, $m, $Y, $H, $M, $S) = sscanf($unpub_date, "%2d-%2d-%4d %2d:%2d:%2d");
                      	$unpub_date = mktime($H, $M, $S, $m, $d, $Y);
                      	if ($unpub_date < $currentdate) {
                      		$published = 0;
                      	}
                      }

                      If you used the Date input type for your TV you would have what the save_content processor expects, since it uses the same javascript date picker widget. But then in that case, why not just have your editor use the unpublish field in the first place?
                        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