We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40088
    • 708 Posts
    Revo 2.3.2
    NewsPublisher 2.0.2

    Is it possible, for example, for the Admin to send a specific member a message from the Manager (Menu > User > Messages) and in turn that message would show up in the member’s account on the front-end? Also, the member could then delete it once read.

    This question has been answered by BobRay. See the first response.

      Todd
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      I don't see why not. After all, it's just a table in the database. The processor files involved are found in core/model/modx/processors/security/message/. They work on a modUserMessage object, which has the usual getter and setter methods.

      I found this article, it's pretty obviously translated by Google Translate or something similar, but you can get the general idea at least. http://sysmagazine.com/posts/132802/ [ed. note: sottwell last edited this post 11 years, 8 months ago.]
        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
        • 3749
        • 24,544 Posts
        I think FormIt might be overkill for that. I would just create a Tpl chunk for displaying one message with a checkbox for deleting it and a hidden field for the ID. Start with something like this (untested):

        [[!ShowMessages? &tpl=`messageTpl` &outerTpl=`messageOuterTpl`]]


        messageOuterTpl:

        <h3>Your Messages</h3>
        <form method='post' id='userform' action='[[~[[*id]]]]'>
        <ul>
            [[+messages]]
        </ul>
        <br />
        <input type="submit" name="submit" value = "submit">
        </form>
        


        messageTpl:

        <li><input type="checkbox" name="messages[]" value="[[+id]]"> [[+subject]] [[+message]]</li>



        ShowMessages snippet:

        /* do nothing if user is not logged in */
        if (!$modx->user->hasSessionContext('web')) {
            return '';
        }
        
        /* process form */
        
        if (isset($_POST['submit'])) {
            if (isset($_POST['messages']) && (!empty($_POST['messages']))) {
                foreach ($_POST['messages'] as $messageId) {
                    $msg = $modx->getObject('modUserMessage', (int) $messageId);
                  if ($msg) {
                      $msg->remove();
                  }
               }
            }
        }
        
        /* Display messages */
        $tpl = $modx->getOption('tpl', $scriptProperties, 'missing Tpl');
        $outerTpl = $modx->getOption('outerTpl', $scriptProperties, 'missing Tpl');
        $uid = $modx->user->get('id');
        $messages = $modx->getCollection('modUserMessage', array('recipient' => $uid));
        if (empty($messages)) {
            return 'No Messages';
        }
        
        $output = $modx->getChunk($outerTpl);
        
        foreach ($messages as $message) {
            $fields = $message->toArray();
            $inner .= $modx->getChunk($tpl, $fields);
        }
        
        $output = str_replace('[[+messages]]', $inner, $output);
        
        return $output;
        

        [ed. note: BobRay last edited this post 11 years, 8 months ago.]
          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
          • 40088
          • 708 Posts
          Bob, this is interesting, thank you.

          First, your snippet throws an error on line 39.

          Second, the checkbox shows on the front-end but the actual message content is missing. It's a great start and it seems like it could be made to work, but I don't know how.
            Todd
            • 3749
            • 24,544 Posts
            This line is wrong:

            $output = str_replace('[[+messages]]', $inner, $output;


            if should be:

            $output = str_replace('[[+messages]]', $inner, $output);


            That could cause both problems.
              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
              • 40088
              • 708 Posts
              Hi Bob,

              That fixed the syntax error but it's still not working, not entirely. The only thing visible is the checkbox. Also, the outer message tpl isn't rendering. And I'm using your exact code (no modifications).
                Todd
                • 3749
                • 24,544 Posts
                I did say "untested" wink

                These are also wrong:

                $tpl = $modx->getOption('tpl', $scriptProperties, 'missing Tpl');
                $outerTpl = $modx->getOption('tpl', $scriptProperties, 'missing Tpl');


                Should be:

                $tpl = $modx->getOption('tpl', $scriptProperties, 'missing Tpl');
                $outerTpl = $modx->getOption('outerTpl', $scriptProperties, 'missing Tpl');
                  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
                  • 40088
                  • 708 Posts
                  [Laughs] Yes you did, Bob. And I appreciate your efforts.

                  It seems to be working except for an unexpected ';' at the end of the outer tpl. [ed. note: todd.b last edited this post 11 years, 8 months ago.]
                    Todd
                    • 3749
                    • 24,544 Posts
                    Found it. Remove the semicolon at the end of this line in the messageTpl chunk:

                    <li><input type="checkbox" name="messages[]"> [[+subject]]  [[+message]]</li>';
                      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
                      • 40088
                      • 708 Posts
                      @BobRay, removing the semicolon didn't fix it. The straight single quotes that wrap the messageTpl seem to be the problem.

                      Also, the delete function doesn't work. The message stays put. [ed. note: todd.b last edited this post 11 years, 8 months ago.]
                        Todd