XF 2.0 How to use the thread creator to create a thread - by Cron entries or not logged in user

Or again, if you don't want the user to post their own birthday, pre-define a user in your add-on options and use that user. (this is what most people do)
Good point. I wasn't really thinking in that context. A birthday thread authored by the person who's birthday it is seems odd :) But, either way, $user will need to be an actual user as you can't rely on \XF::visitor() (as you noted, Andy).
 
Almost got it, one more issue.

Here's the code:

PHP:
		// get visitor
		$visitor = \XF::visitor();
		
		foreach ($results as $result)
		{
			// add username to title
			$title = str_replace('{username}', $result['username'], $titleOriginal);
			
			// add username to message
			$message = str_replace('{username}', $result['username'], $messageOriginal);		
			
			$em = \XF::em();
			$forum = $em->find('XF:Forum', $forumId);	
			$user = \XF::app()->find('XF:User', $starterUserId);
			
			// if cron entry is run normally
			if ($visitor['user_id'] == 0)
			{
				\XF::asVisitor($user, function() use ($forum, $title, $message)
				{
					$creator = \XF::service('XF:Thread\Creator', $forum);
					$creator->setContent($title, $message);
					$creator->setPrefix($forum['default_prefix_id']);
					$creator->setIsAutomated();
					$thread = $creator->save();
				});
			}
			
			// if cron entry is run manually
			if ($visitor['user_id'] > 0)
			{
				$creator = \XF::service('XF:Thread\Creator', $forum);
				$creator->setContent($title, $message);
				$creator->setPrefix($forum['default_prefix_id']);
				$creator->setIsAutomated();
				$thread = $creator->save();
			}
			
			$userId = $result['user_id'];
			$user = \XF::app()->find('XF:User', $userId);
			$alertRepo = \XF::app()->repository('XF:UserAlert');
			$alertRepo->alert($user, $starterUserId, $starterUsername, 'post', $thread->first_post_id, 'mention');
		}

When the cron runs normally, the $thread variable is not created. Works fine if the cron is run manually.
 
The result of the asVisitor function will return whatever is returned within the closure, so it should just be changed to this:
PHP:
$thread = \XF::asVisitor($user, function() use ($forum, $title, $message)
{
    $creator = \XF::service('XF:Thread\Creator', $forum);
    $creator->setContent($title, $message);
    $creator->setPrefix($forum['default_prefix_id']);
    $creator->setIsAutomated();
    return $creator->save();
});
Though if I could make a suggestion for some slightly better code.

I think you should drop using the visitor entity at all and just always use whatever the user is that comes from $starterId:
PHP:
        $app = \XF::app();

        foreach ($results as $result)
        {
            // add username to title
            $title = str_replace('{username}', $result['username'], $titleOriginal);
         
            // add username to message
            $message = str_replace('{username}', $result['username'], $messageOriginal);    
         
            $forum = $app->find('XF:Forum', $forumId);
            $starterUser = $app->find('XF:User', $starterUserId);
         
            $thread = \XF::asVisitor($starterUser, function() use ($forum, $title, $message)
            {
                $creator = \XF::service('XF:Thread\Creator', $forum);
                $creator->setContent($title, $message);
                $creator->setPrefix($forum['default_prefix_id']);
                $creator->setIsAutomated();
                return $creator->save();
            });
         
            $user = $app->find('XF:User', $result['user_id']);
            $alertRepo = $app->repository('XF:UserAlert');
            $alertRepo->alertFromUser($user, $starterUser, 'post', $thread->first_post_id, 'mention');
        }
Otherwise it's never totally deterministic as to who is going to create the thread.

(Some other small adjustments made for your consideration too).
 
@Chris D
$alertRepo->alert($user, $user->user_id, $user->username, 'user', $user->user_id, 'upgrade_end');
Here i set the 'resource' in place of 'user' and 'testing' in place of 'upgrade_end',
Then create a new template alert_resource_testing but it not showing the alerts.

Could you please help me ?
 
Top Bottom