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’m trying to get the following plugin to work to force modx to write a pub_date when one is not selected.

    Can anyone please help with this?

    Here is what I’ve written:
    <?php
    /*
     * Force Date from Parent
     * Written By Jay Gilmore - 14 November 2008
     *
     * Forces Pub Date to be Now() if it is empty.
     *
     * Configuration:
     * check the OnBeforeDocFormSave event
     *
     * Version 1.0
     *
     */
    
    global $content;
    $e = &$modx->Event;
    
    switch($e->name) {
      case 'OnBeforeDocFormSave':
        if ($content['pub_date'] =='') {
            $content['pub_date'] = now();
        }
        break;
    
      	default:
        	return;
        	break;
    }
    ?>
    

    Note: I just put in the php opening tags to trigger syntax highlighting in the forum.

    Any help would be appreciated.

    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
      Jay: not sure how to fix it but it seems that this is not working $content[’pub_date’] ==’’ if I remove the if seemed to work. I will mess with this again later since I need this as well.
        [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’ve also posted a Feature Request because this behavior is both needed and exists in a number of other CMSs. I don’t see that it has any negative issues.

        maybe we need to get the value from POST or $modx->documentObject[’pub_date’] instead of $content[’pub_date’]?

        Thanks for your help on this.
          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
          • 4310
          • 2,310 Posts
          Jay,
          Have you tried :
          $_REQUEST['pub_date']

          I found when using an @EVAL command for a TV that this was one way to return a value.
          Well it was actually for the doc id, but you see what I mean laugh
            • 30765
            • 66 Posts
            Quote from: smashingred at Nov 14, 2008, 12:49 PM

            I’m trying to get the following plugin to work to force modx to write a pub_date when one is not selected.

            Can anyone please help with this?

            Here’s mine:
            /*
             * CorrectDates
             * When a document is published or set to be published,
             * always populate publishedon and pub_date variables correctly
             *
             * Configuration:
             * check the OnDocFormSave and OnDocPublished events
             *
             * Version 0.1, October 2007
             *
             * Version 0.1.1, November 2007
             * Does nothing if page is start page (which can't have publish dates)
             *
             */
            
            function normaliseDates($docid) {
            	global $modx;
            	$dates = $modx->getPageInfo($docid, 0, 'publishedon,pub_date');
            	$pub_date = $dates['pub_date'];
            	$publishedon = $dates['publishedon'];
            	$dbtable = $modx->getFullTableName('site_content');
            	if($publishedon && !$pub_date) {
            		// set pub_date to the same as publishedon
            		$modx->db->update('pub_date=' . $publishedon, $dbtable, 'id=' . $docid);
            	} elseif ($pub_date && !$publishedon) {
            		// set publishedon to now
            		$modx->db->update('publishedon=' . time(), $dbtable, 'id=' . $docid);		
            	}
            }
            
            $e = &$modx->Event;
            switch($e->name) {
            	case 'OnDocFormSave':
            		if ($id != $modx->config['site_start']) normaliseDates($id);
            		break;
            	case 'OnDocPublished':
            		if ($id != $modx->config['site_start']) normaliseDates($docid);
            		break;
            }
            


            Hopefully it helps.

            Cheers
            Matt

              • 17612
              • 7 Posts
              Matt’s code is nicer than mine and it checks for site_start. Thanks Matt!

              What is publishedon used for, and how is it different than pub_date, or why both?

              I’ve always thought it was redundant, but perhaps I don’t understand. Could someone clarify?
                • 30765
                • 66 Posts
                Quote from: lucky at Nov 15, 2008, 12:56 AM

                What is publishedon used for, and how is it different than pub_date, or why both?

                If I recall correctly ... if you just publish something, then publishedon will be set (and not pub_date). If you set something to be published in the future, then pub_date will be set but not publishedon. At least I think that’s right.

                My plugin should set publishedon to be the date a decision was made to publish it; pub_date is the date when it should be published.

                Looking at my plugin code, it seems to have a glaring error: in the third-to-last line, $docid isn’t set, and it looks like it should be $id. But I seem to recall there’s a reason for that -- that $docid was being set somewhere else. All I can say is it works for me.

                Cheers
                Matt
                  • 7231
                  • 4,205 Posts
                  If I recall correctly ... if you just publish something, then publishedon will be set (and not pub_date). If you set something to be published in the future, then pub_date will be set but not publishedon. At least I think that’s right.
                  Oh, I wish i knew about this (I feel stupid since it is just a matter of looking at the table). If there is a publishedon date recorded then a pub_date is not really needed for many of the things I have been using the pub_date for, like sorting by pub_date would be much more efficient if it was sorted by publishedon. OK, now I need to go back and fix some ditto calls  rolleyes

                  Thanks for the cool plugin and thanks for opening my eyes to this  grin

                  PS: the documentation needs to be updated to include publishedon, i think this is why I did not know about it.
                    [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
                    So to sum up. Since there is publishedon this would solve the whole issue w/o using a plugin or anything since it records the date published when published be it now or on a future date?

                    This should be documented for sure. I can add this. But I want to confirm function and application.

                    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
                      • 17612
                      • 7 Posts
                      I’ve been trying to clear this up in my head... it seems publishedon is used to store the date that the document is published, whereas pub_date is meant to be used for scheduling the date it will be published in the future.

                      I’m still not sure I understand the need for both. Wouldn’t just pub_date suffice, regardless of whether or not the date is in the future? We still have the published field, so I think publishedon just adds confusion, for me at least. The user only has access to pub_date, so it should always indicate the publish date (so that it can be altered), and we shouldn’t require a plugin to make that work.

                      publishedon still seems redundant to me. Perhaps a field to tell if the doc was published via scheduling or by immediate user action would be useful.

                      Anyway, Matt’s use of $docid is correct.

                           ...
                                case 'OnDocPublished':
                      		if ($id != $modx->config['site_start']) normaliseDates($docid);
                      		break;
                      }
                      


                      OnDocPublished and OnDocUnPublished return the id as $docid, so it is not an error, just not consistent across all events. I think all of the other document related events return it as $id.