XF 2.1 Possible Relation Needed

Ozzy47

Well-known member
Working on a addon that sends an email (via a cron) to selected staff when there is a new moderated thread in the mod queue.
Here is the PHP.
PHP:
<?php

namespace OzzModz\ModeratedPost\Cron;

class ModeratedPost
{
    public static function ModeratedPostEmail()
    {   
        $options = \XF::options();
          
        $sendToUsers = $options->ozzmodzModeratedPost_users;

        if (!empty($sendToUsers))
        {
            $finder = \XF::finder('XF:Thread');
            $posts = $finder
                ->where('post_date', '>', time() - 10 * 60)
                ->where('discussion_state', 'moderated')
                ->order('post_date', 'ASC')
                ->fetch();
 
            foreach ($posts AS $post)
            {
                foreach ($sendToUsers AS $sendToUser)
                {
                    $mail = \XF::app()->mailer()->newMail();

                    $user = \XF::app()->find('XF:User', $sendToUser);

                    if (!empty($user))
                    {
                        $mail->setToUser($user);

                        $mail->setTemplate('ozzmodzModeratedPost_thread_email', [
                            'post' => $post
                        ]);

                        $mail->queue();
                    }
                }
            }
        }
    }
}

Here is the template.
HTML:
<mail:subject>
    {{ phrase('ozzmodzModeratedPost_new_moderated_thread', {
        'title': prefix('thread', $thread, 'escaped') . $thread.title
    }) }}
</mail:subject>

{{ phrase('ozzmodzModeratedPost_poster_new_moderated_thread', {
    'receiver': $receiver.username,
    'poster': username_link_email($post.User, $post.username),
    'site': '<a href="' . link('canonical:index') . '">' . $xf.options.boardTitle . '</a>'
}) }}

<h2><a href="{{ link('canonical:posts', $post) }}">{{ prefix('thread', $thread, 'escaped') }}{$thread.title}</a></h2>

<xf:if is="$xf.options.emailWatchedThreadIncludeMessage">
    <div class="message">{{ bb_code_type('emailHtml', $post.message, 'post', $post) }}</div>
</xf:if>

<xf:macro template="thread_forum_macros" name="go_thread_bar" arg-thread="{$thread}" arg-watchType="forums" />

Now when the email comes through, it is partially correct. But what is not being included is the thread title, the message and the go thread bar is not going to the thread.

What do I have to change in the PHP file (and possibly template) to get this to work as wanted?
 
You mixed up $post and thread quite a bunch of times. All your described places call $thread but it's never set, as you passed it as $post.
 
Oddly enough, that is exactly how the watched forum email template is. That is why I think my finder needs work.
 
Oddly enough, that is exactly how the watched forum email template is. That is why I think my finder needs work.

Doesn't really matter how that is or is not. 🤔 Your template is calling to $thread multiple times, which you never passed as parameter, because you labelled it as $post.
 
Also, this code seems pretty inefficient - you are querying the target users for every single thread (not post, your finder does not return posts).
 
I have to echo basically the other comments.

If you want something to happen every time the state of a thread changes you would do this in the entity _postSave method as soon as it happens. Not via a scheduled cron sometime later.
 
Top Bottom