<?php
class MonthlyEmails_Model_Email extends XenForo_Model
{
public function sendMonthlyEmails()
{
$users = $this->getModelFromCache('XenForo_Model_User')->getUsers(array(
'user_status' => 'valid',
'receive_admin_email' => 1,
), array(
'join' => XenForo_Model_User::FETCH_USER_FULL
));
$viewParams = array(
'options' => XenForo_Application::get('options'),
'threads' => $this->_getRecentPublicThreads()
);
foreach ($users as $user)
{
$mail = XenForo_Mail::create('monthly_email', array_merge($viewParams, array('user' => $user)), $user['language_id']);
$mail->enableAllLanguagePreCache();
$mail->queue($user['email'], $user['username']);
}
}
protected function _getRecentPublicThreads()
{
$nodeModel = $this->getModelFromCache('XenForo_Model_Node');
$nodes = $nodeModel->getAllNodes(false, true);
$nodePermissions = $nodeModel->getNodePermissionsForPermissionCombination(1);
$nodeHandlers = $nodeModel->getNodeHandlersForNodeTypes(
$nodeModel->getUniqueNodeTypeIdsFromNodeList($nodes)
);
$nodes = $nodeModel->getViewableNodesFromNodeList($nodes, $nodeHandlers, $nodePermissions);
$nodes = $nodeModel->mergeExtraNodeDataIntoNodeList($nodes, $nodeHandlers);
$nodes = $nodeModel->prepareNodesWithHandlers($nodes, $nodeHandlers);
$postIds = array();
foreach ($nodes as $node)
{
if (!empty($node['last_post_id']))
$postIds[$node['last_post_id']] = $node['last_post_date'];
}
arsort($postIds);
$postIds = array_slice(array_keys($postIds), 0, 3);
$threads = $this->getModelFromCache('XenForo_Model_Post')->getPostsAndParentData($postIds);
return $threads[1];
}
}