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

    I could not find the information out there, so i’m posting.

    I am more a front-end guy reaching into back-end now and then, rather than a full-fledget dev.

    For a customer we have an iPhone-App that gets fed by MODx-Revolution via apples .plist-XML-derivat.
    That includes a List of news and a list of podcast episodes.

    We are using push-notifications, so i like to fire some lines of php the moment a new document gets published.
    A Plugin should listen for that, ask if its News or Podcast-Category and send the right push-note out.

    For Debugging i wanted to send me a mail, instead of developing with the notification system. So i created a new plugin, that sends me an email if fired.
    $e = & $modx->Event; // get a reference to the event
    $eventName =  $e->name;
    
    $modx->getService('mail', 'mail.modPHPMailer');
    $modx->mail->set(modMail::MAIL_BODY,'löööft');
    $modx->mail->set(modMail::MAIL_FROM,'[email protected]');
    $modx->mail->set(modMail::MAIL_FROM_NAME,'Johnny Tester');
    $modx->mail->set(modMail::MAIL_SENDER,'Johnny Tester');
    $modx->mail->set(modMail::MAIL_SUBJECT,$eventName);
    $modx->mail->address('to','[email protected]');
    $modx->mail->setHTML(true);
    if (!$modx->mail->send()) {
        $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$err);
    }
    $modx->mail->reset();



    Now, i tried using the OnDocPublished-Event. Did not work.
    Tried OnDocFormSave. Did not work.
    Tried OnWebPageComplete. Did work.

    Now to my question: How can it be, that a plug-in behaves differently according to the event it listened to?

    As i read here, there are different service types, but i have the feeling of reaching a field of the RTFM, that is really sparingly documented.

    Can anybody give me some hints for the right directions?
    Much appreciated, thanks!
    niklas
      • 3749
      • 24,544 Posts
      It shouldn’t matter what the event is, the plugin code should execute when the event fires. However, there are some events that never fire (OnActivateUser is one) and some that fire only in certain circumstances. At present, OnDocPublished fires only when the resource/publish processor is called. I suspect that only happens when you publish a document by right-clicking on it in the tree (I could be wrong).

      How is the document being published?

      You might take a look at the ActivationEmail plugin. It does almost exactly what you want (but with a Tpl chunk). It checks for a change in the users active status as the user object is being saved. With a few minor changes, it should work to check for changes in the published status as a document is being saved.

      [UPDATE] Hmmm. I can’t find any OnBeforeResourceSave event (or anything like it). OnDocFormSave is the only event that could work and I think it will only fire if you save from the Create/Edit Resource panel in the Manger (again, I could be wrong).
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 21635
        • 81 Posts
        It shouldn't matter what the event is, the plugin code should execute when the event fires


        Thanks for your answer.

        Now i happen to notice some really weird behaviour.
        If it is like you said, that it does the event does not influence the behaviour, then i’m really asking myself why so many events won’t fire.
        Here events, that did work

        OnLoadWebDocument
        OnHandleRequest
        OnWebPageComplete
        OnHandleRequest

        Note, that these events won’t help me with my use-case. I just tried above because i kind of wanted to figure out what is happening.
        Events that did _not_ work:

        OnBeforeDocFormSave
        OnBeforeEmptyTrash
        OnEmptyTrash
        OnDocFormRender
        OnDocFormSave
        OnDocPublished
        OnDocUnPublished
        OnManagerLogin
        OnManagerLogout

        There were some more, but for me the biggest puzzle is OnDocFormSave and OnDocPublished.

        Note, that i did all the actions, that the Eventnames suggest, ie. Publish Documents in all forms i know of (context-menu, checkbox in resource edit).

        Using 2.0.7-pl, which worked fine until now.

        Or am i missing sth major?
        Is it the sending of mails, that won’t work with manager-events. ( it’s just front-end events that worked for me).

        strange, strange huh
          • 3749
          • 24,544 Posts
          OnDocFormSave should definitely fire when you save a doc from the Create/Edit resource panel. Maybe the email sending is unreliable for some reason.

          I did think of a possible solution, though it’s not ideal. You could run a cron job that periodically checks the pub status of all (or selected) docs compares it with the status on it’s last run, sends email if it has changes from unpublished to published, and finally, saves the current pub status of the docs.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 21635
            • 81 Posts
            Yes i thought about a similar solution as well, but i consider it a workaround and would love to do the "real thing".

            Is there any way other than sending mails out to check/debug whether an event has been fired?

            I just lack a way of making the modx-processes transparent, so it could become kind of self-explanatory...
              • 3749
              • 24,544 Posts
              You could try something like this. It should put a message in the error log (under reports in the Top Menu).

              $modx->log(modX::LOG_LEVEL_ERROR,'Plugin 1 executing.');
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 21635
                • 81 Posts

                Quote from: BobRay at Feb 12, 2011, 03:37 PM

                You could try something like this. It should put a message in the error log (under reports in the Top Menu).

                $modx->log(modX::LOG_LEVEL_ERROR,'Plugin 1 executing.');

                thank you bob,
                that helped me figuring it out.

                my problem was one of a broken include path. to edit the plugin in a code-editor i included a php script, that was somewhere in the /assets directory.
                that is why the script worked for events from the frontend, because then the include-url was correct.
                as is said... i’m not very deep into modx-coding. smiley

                now moved my projects files to the correct location.
                thanks for helping me out here.
                cheers, niklas