We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22427
    • 793 Posts
    Is there no way to view the sent messages?
      • 3749
      • 24,544 Posts
      If the message is addressed to you, you'll see it when you go to the 'Messages' section, but AFAIK, you can't see other people's messages unless you either log in as them or look in the DB.

      This snippet would show them all:

      $messages = $modx->getCollection('modUserMessage');
      $output = '';
      foreach ($messages as $message) {
          $fields = $message->toArray();
          $output .= $modx->getChunk('messageTpl', $fields);
      }
      
      return $output;



      messageTpl chunk:

      <p>[[+id]] -- [[+subject]] -- [[+message]]</p>


        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
        • 22427
        • 793 Posts
        Thank you, Bob - very helpful.
        I would like to have the author of the message in the output. Adding [[+sender]] to the messageTpl of course gives just his id.
        How do I get his username? (Tried a few things, but to no avail.)
          • 3749
          • 24,544 Posts
          Here's what I have for doing that in code:


          foreach ($messages as $message) {
              /** @var $message xPDOObject */
              $fields = $message->toArray ('', true);
              $query = $modx->newQuery('modUser', array(
                  'id' => $fields['sender'],
              ));
              $query->select('username');
              $username = $modx->getValue($query->prepare());
              $fields['sender'] = $username;
              $fields['class'] = $fields['read']? 'read' : 'unread';
              $fields['read'] = $fields['read'] ? 'Yes' : 'No';
              $inner .= $modx->getChunk($tpl, $fields);
          }


            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
            • 22427
            • 793 Posts
            I tried it this way, but there was no output:
            $output = '';
            foreach ($messages as $message) {
                /** @var $message xPDOObject */
                $fields = $message->toArray ('', true);
                $query = $modx->newQuery('modUser', array(
                    'id' => $fields['sender'],
                ));
                $query->select('username');
                $username = $modx->getValue($query->prepare());
                $fields['sender'] = $username;
                $fields['class'] = $fields['read']? 'read' : 'unread';
                $fields['read'] = $fields['read'] ? 'Yes' : 'No';
                $output .= $modx->getChunk('messageTpl', $fields);
            };
            return $output;

            To test the query, I tried
            $output = '';
            foreach ($messages as $message) {
                /** @var $message xPDOObject */
                $fields = $message->toArray ('', true);
                $query = $modx->newQuery('modUser', array(
                    'id' => $fields['sender'],
                ));
                $query->select('username');
                $username = $modx->getValue($query->prepare());
                $output .= $username . ' ';
            };
            return $output;
            The output remains empty.
              • 22427
              • 793 Posts
              Stupid me - I forgot the line
              $messages = $modx->getCollection('modUserMessage');
              

              Here's the complete snippet code:
              $output = '';
              $messages = $modx->getCollection('modUserMessage');
              foreach ($messages as $message) {
                  /** @var $message xPDOObject */
                  $fields = $message->toArray ('', true);
                  $query = $modx->newQuery('modUser', array(
                      'id' => $fields['sender'],
                  ));
                  $query->select('username');
                  $username = $modx->getValue($query->prepare());
                  $fields['sender'] = $username;
                  $fields['class'] = $fields['read']? 'read' : 'unread';
                  $fields['read'] = $fields['read'] ? 'Yes' : 'No';
                  $output .= $modx->getChunk('messageTpl', $fields);
              };
              return $output;

              It works like a charm. Thanks Bob, I learnt something about xPDO!
                • 3749
                • 24,544 Posts
                I'm glad it's working for you.

                BTW, I'm working on an extra to let users see and delete their messages in the front end (and possibly reply to them). It may be a while before it's released, though.
                  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
                  • 22427
                  • 793 Posts
                  Since the messages in the grid are not sortable* (see https://forums.modx.com/thread/95945/package-manager-grid-columns-not-sortable), it would be helpful if at least they would be sorted by date_sent in DESC order by default.

                  * Perhaps this is fixed in 2.3.3-pl?
                    • 3749
                    • 24,544 Posts
                    Yes, I already have them sorted in DESC order.

                    I think adding this line between lines 8 and 9 would do it for the snippet above:

                    $query->sortby('date_sent', 'DESC');


                    You could also add that line below line 32 of the core\model\modx\processors\security\message\getlist.class.php file to have them sorted in the Manager.
                      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