We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22851
    • 805 Posts
    Hi all.

    I am currently using MODx version 0.9.6 (although I will be upgrading to 0.9.6.1 with security patch soon.) I have have written some php that I would like launched every time a new document is published, whether that be directly by selecting ’published’ in the manager or automatically when the date passes the pub_date.

    I put the code I want executed in a plugin which I set to launch on OnDocPublished, but it’s not working. So, I tried putting a simple php mail command in the plugin instead to notify me when the plugin was being executed. It works if I set the event to launch on OnDocFormSave, for example, but not on OnDocPublished.

    I don’t know what’s going on here. Is anyone able to help me out?

    Thanks,

    Paul
      YAMS: Yet Another Multilingual Solution for MODx
      YAMS Forums | Latest: YAMS 1.1.9 | YAMS Documentation
      Please consider donating if you appreciate the time and effort spent developing and supporting YAMS.
      • 22303 MODX Staff
      • 10,725 Posts
      That event is only executed when you use the right-click context menu in the document tree to "Publish" it.
        • 22851
        • 805 Posts
        Ahh. I see. That would explain it. Would you (or anyone) know if there is an event that fires when a document is published any old how then?

        Basically, I have got a little XMLRPC ping script that notifies different blog and bookmarking sites that a new blog item has been published. Sometimes we write blog items in advance and get them to be published automatically. I would like the XMLRPC ping script to be executed at the same time the documents are published, whether it happens automatically or not.

        Cheers,

        Paul
          YAMS: Yet Another Multilingual Solution for MODx
          YAMS Forums | Latest: YAMS 1.1.9 | YAMS Documentation
          Please consider donating if you appreciate the time and effort spent developing and supporting YAMS.
          • 30765
          • 66 Posts
          Quote from: PMS at Jan 04, 2008, 07:53 PM
          Would you (or anyone) know if there is an event that fires when a document is published any old how then?

          Try ’OnDocFormSave’ (although you might need to check that the document has been published). OnDocFormSave doesn’t fire when a document is published with right-click, so you need to check for both cases.

          Cheers
          Matt

            • 28042 ☆ A M B ☆
            • 24,524 Posts
            There could be an event trap in the parser’s checkPublishStatus function, just before the actual UPDATE query. A plugin using that event would do a SELECT query using the same criteria to get the data on the docs about to be published, run the UPDATE, and if the UPDATE were successful do whatever the user wanted with the information that documents x, y and z were published. Since the plugin ran the UPDATE query, the parser’s UPDATE query wouldn’t do anything, and the rest of the function would proceed as usual.
            Line 527:
                    if ($cacheRefreshTime <= $timeNow && $cacheRefreshTime != 0) {
            // $this->invokeEvent("OnBeforeDocAutoPublished"); 
                        // now, check for documents that need publishing
                        $sql = "UPDATE ".$this->getFullTableName("site_content")." SET published=1, publishedon=".time()." WHERE ".$this->getFullTableName("site_content").".pub_date < $timeNow AND ".$this->getFullTableName("site_content").".pub_date!=0 AND published=0";
                        if (@ !$result= $this->dbQuery($sql)) {
                            $this->messageQuit("Execution of a query to the database failed", $sql);
                        }
            

              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
              • 22851
              • 805 Posts
              Thanks, Tuatara. That’s a good idea, which I have now put in place as it’s better than nothing. It doesn’t work with the automatic publishing of documents by date though.

              Sottwell. That would be perfect. I’m surprised that no-one has requested an event like that before. I can imagine that it would be useful for automated email notifications and the like, not just XMLRPC pings.

              Say I wanted that feature now, I guess that would mean having to modify the core code in at least two places? 1) in the parser’s checkPublishStatus function as you indicated in the code snippet (although I don’t know which file that is) and 2) the part of the code which makes that event accessible in the manager interface. I know virtually nothing about modx’s inner workings for the moment, but I am guessing that part 2) is not as simple as the one-liner that part 1) is.

              I don’t want to modify the core code myself, especially if it’s more than one or two lines that are easy to keep track of. How would I go about officially requesting that this event be added to the next release of MODx? If it’s easy and it was agreed to add this event, could the mods be made to the latest development version on SVN so that I could download the relevant files and start using the functionality immediately? I have never had to deal with a modification of the core code before, so I don’t know the best way of proceeding.

              Thank you,

              Paul
                YAMS: Yet Another Multilingual Solution for MODx
                YAMS Forums | Latest: YAMS 1.1.9 | YAMS Documentation
                Please consider donating if you appreciate the time and effort spent developing and supporting YAMS.
                • 30765
                • 66 Posts
                Quote from: PMS at Jan 05, 2008, 05:40 PM
                It doesn’t work with the automatic publishing of documents by date though.

                That’s a separate issue:

                http://modxcms.com/forums/index.php/topic,20401.msg127007.html#msg127007
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Quote from: PMS at Jan 05, 2008, 05:40 PM

                  I’m surprised that no-one has requested an event like that before. I can imagine that it would be useful for automated email notifications and the like, not just XMLRPC pings.
                  This is why the new core was rewritten in a proper object-oriented form, so you don’t have to use events to do such things. The new API can apply consistent logic when you use the set() method to set the value of a field, e.g. $resource->set(’published’, true) could be extended to apply your logic or we can have it invoke the event there and just make sure everyone calls this function when modifying the data. So even if you do choose the event/plugin route, you still have all logic connected to the event in one function in a single class file, so you are sure the event is always triggered, regardless of who or how the data is manipulated. This centralization of the logic is one of the key attributes of a Model in the world of MVC design patterns, which 0.9.7 will introduce MODx users to.

                  Quote from: PMS at Jan 05, 2008, 05:40 PM

                  I don’t want to modify the core code myself, especially if it’s more than one or two lines that are easy to keep track of. How would I go about officially requesting that this event be added to the next release of MODx? If it’s easy and it was agreed to add this event, could the mods be made to the latest development version on SVN so that I could download the relevant files and start using the functionality immediately? I have never had to deal with a modification of the core code before, so I don’t know the best way of proceeding.
                  Hmmm, I believe triggering this event with the current auto-publishing mechanism could have negative implications to the performance of MODx sites if not implemented carefully, and this would require a thorough test and review by proof of concept. I would go ahead and just make your modifications and if they prove useful and successful for you, submit them back to the project as a patch attached to a feature request (if you know how to make a patch, otherwise, you could attach the changed files, though that is less preferable). If you don’t feel confident, get someone with skills you trust to help you make, test, and submit the changes back.

                  In this case, with the rewritten core coming in 0.9.7, I think you can safely make and track these couple of changes you need until 0.9.7 is released (i.e. so you can maintain it while the 0.9.6.x series remains the stable branch). Then, you’ll likely discover much better ways to do this, although I can tell you that auto-publishing is one of the features that is still on the list of refactorings to be completed for 0.9.7 release, as it currently breaks the ability of the new engine to produce pages from cached data result sets without ever having to make a connection to the database. This is due to it delegating the decision to the UPDATE statement (i.e. result cannot be determined without executing the query).
                    • 22851
                    • 805 Posts
                    Quote from: tuatara at Jan 05, 2008, 05:46 PM

                    That’s a separate issue:

                    http://modxcms.com/forums/index.php/topic,20401.msg127007.html#msg127007

                    I think that you got the wrong end of the stick there. I was meaning that when a document gets published by way of MODx noticing that the pub_date has been passed, no specific event is fired, and so currently you can’t get MODx to do anything extra at that moment. Unless I am missing something, that’s a separate issue to the one being described in the link you posted. It was useful reading anyway, since I may well have to use that plugin someday, so thanks!

                    OpenGeek:

                    Thanks for the comprehensive answer. The new core sounds great. It’s good to hear that the best CMS is going to be getting even better.

                    I’m not sure what I’ll do until 0.9.7. It’s unlikely that I’ll modify the current core to make the additional event available in the manager, since I don’t have enough experience to do so... but I’ll take a look anyway in case it’s easier that I think. If I do get something working, then I’ll post back here first before doing anything else.

                    Cheers,

                    Paul
                      YAMS: Yet Another Multilingual Solution for MODx
                      YAMS Forums | Latest: YAMS 1.1.9 | YAMS Documentation
                      Please consider donating if you appreciate the time and effort spent developing and supporting YAMS.
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      You need to do two things:

                      1. in the file manager/includes/document.parser.class.inc.php add the line as I showed below
                              if ($cacheRefreshTime <= $timeNow && $cacheRefreshTime != 0) {
                                   /* event added 01/2007 by sottwell */
                                   $this->invokeEvent("OnBeforeDocAutoPublished"); 
                                   /* end of addition */
                                  // now, check for documents that need publishing
                                  $sql = "UPDATE ".$this->getFullTableName("site_content")." SET published=1, publishedon=".time()." WHERE ".$this->getFullTableName("site_content").".pub_date < $timeNow AND ".$this->getFullTableName("site_content").".pub_date!=0 AND published=0";
                                  if (@ !$result= $this->dbQuery($sql)) {
                                      $this->messageQuit("Execution of a query to the database failed", $sql);
                                  }

                      Then in the database, in the system_eventnames table, add a line
                      name: OnBeforeDocAutoPublished 
                      service: 5
                      

                      Now the event will appear in the block of events Template Service Events, along with the OnDocPublished and OnDocUnpublished events.

                      Your plugin would need to look something like this:
                       // not sure if this variable would be available to our plugin
                      $timeNow= time() + $modx->config['server_offset_time'];
                      // add any other fields you want to the SELECT query
                      $sql = "SELECT pagetitle FROM  ".$modx->getFullTableName("site_content")." sc WHERE sc.pub_date < $timeNow AND sc.pub_date!=0 AND published=0";
                      $docs_being_published = $modx->db->query($sql);
                      if($modx->db->getRecordCount($res) > 0) {
                          $sql = "UPDATE ".$modx->getFullTableName("site_content") sc ." SET published=1, publishedon=".time()." WHERE sc.pub_date < $timeNow AND sc.pub_date!=0 AND published=0";
                          if (@ $result= $modx->db->query($sql)) {
                              // do whatever you want with the data in the $docs_being_published object here...
                          } // end if update was successful
                      } // end if any docs were to be auto-published
                      

                      I’m sure somebody can come up with a better plugin than that, but this should be the general idea.
                        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