Skip to content

Commit

Permalink
Fix #213 Notification type - thread poll vote (#216)
Browse files Browse the repository at this point in the history
* Fix comment in ThreadAuthorRatedFormatter

* Add public poll vote alert type

* Fix instances, add check for public polls
  • Loading branch information
dvz authored and euantorano committed Sep 27, 2017
1 parent 0a80cf5 commit 06bb7ba
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 5 deletions.
2 changes: 2 additions & 0 deletions inc/languages/english/myalerts.lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
$l['myalerts_post_threadauthor'] = '{1} replied to your thread <b>"{2}"</b>. There may be more posts after this.';
$l['myalerts_subscribed_thread'] = '{1} replied to your subscribed thread <b>"{2}"</b>.';
$l['myalerts_rated_threadauthor'] = '{1} rated your thread <b>"{2}"</b>.';
$l['myalerts_voted_threadauthor'] = '{1} voted in your poll in <b>"{2}"</b>.';

$l['myalerts_setting_rep'] = 'Receive alert for reputation?';
$l['myalerts_setting_pm'] = 'Receive alert for Private Message (PM)?';
Expand All @@ -28,6 +29,7 @@
$l['myalerts_setting_post_threadauthor'] = 'Receive alert when somebody replies to your thread?';
$l['myalerts_setting_subscribed_thread'] = 'Receive alert when somebody replies to one of your subscribed threads?';
$l['myalerts_setting_rated_threadauthor'] = 'Receive alert when somebody rates your thread?';
$l['myalerts_setting_voted_threadauthor'] = 'Receive alert when somebody votes in your thread\'s poll?';
$l['myalerts_settings_save'] = 'Save Settings';
$l['myalerts_settings_updated'] = 'Alert settings updated successfully. Redirecting back to settings page.';
$l['myalerts_settings_updated_title'] = 'Alert Settings Updated.';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* Alert formatter for private thread author reply alerts.
* Alert formatter for private thread author rate alerts.
*/
class MybbStuff_MyAlerts_Formatter_ThreadAuthorRatedFormatter
extends MybbStuff_MyAlerts_Formatter_AbstractFormatter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/**
* Alert formatter for private thread author vote alerts.
*/
class MybbStuff_MyAlerts_Formatter_ThreadAuthorVotedFormatter
extends MybbStuff_MyAlerts_Formatter_AbstractFormatter
{
/**
* @var postParser $parser
*/
private $parser;

/**
* Format an alert into it's output string to be used in both the main
* alerts listing page and the popup.
*
* @param MybbStuff_MyAlerts_Entity_Alert $alert The alert to format.
*
* @return string The formatted alert string.
*/
public function formatAlert(
MybbStuff_MyAlerts_Entity_Alert $alert,
array $outputAlert
) {
$alertContent = $alert->getExtraDetails();
$threadLink = $this->buildShowLink($alert);

return $this->lang->sprintf(
$this->lang->myalerts_voted_threadauthor,
$outputAlert['from_user'],
htmlspecialchars_uni(
$this->parser->parse_badwords($alertContent['t_subject'])
)
);
}

/**
* Init function called before running formatAlert(). Used to load language
* files and initialize other required resources.
*
* @return void
*/
public function init()
{
if (!$this->lang->myalerts) {
$this->lang->load('myalerts');
}

require_once MYBB_ROOT . 'inc/class_parser.php';
$this->parser = new postParser;
}

/**
* Build a link to an alert's content so that the system can redirect to
* it.
*
* @param MybbStuff_MyAlerts_Entity_Alert $alert The alert to build the
* link for.
*
* @return string The built alert, preferably an absolute link.
*/
public function buildShowLink(MybbStuff_MyAlerts_Entity_Alert $alert)
{
$alertContent = $alert->getExtraDetails();

$threadLink = $this->mybb->settings['bburl'] . '/' . get_thread_link(
(int) $alertContent['tid']
);

return $threadLink;
}
}
82 changes: 78 additions & 4 deletions inc/plugins/myalerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ function myalerts_install()
'quoted',
'post_threadauthor',
'subscribed_thread',
'rated_threadauthor'
'rated_threadauthor',
'voted_threadauthor'
);
$alertTypesToAdd = array();

Expand Down Expand Up @@ -1092,7 +1093,7 @@ function myalerts_alert_post_threadauthor(&$post)

$alertTypeManager = MybbStuff_MyAlerts_AlertTypeManager::getInstance();

if (is_null($alertTypeManager) || $alertTypeManager === null) {
if (is_null($alertTypeManager) || $alertTypeManager === false) {
global $cache;

$alertTypeManager = MybbStuff_MyAlerts_AlertTypeManager::createInstance($db, $cache);
Expand Down Expand Up @@ -1172,7 +1173,7 @@ function myalerts_alert_rated_threadauthor()

$alertTypeManager = MybbStuff_MyAlerts_AlertTypeManager::getInstance();

if (is_null($alertTypeManager) || $alertTypeManager === null) {
if (is_null($alertTypeManager) || $alertTypeManager === false) {
global $cache;

$alertTypeManager = MybbStuff_MyAlerts_AlertTypeManager::createInstance($db, $cache);
Expand Down Expand Up @@ -1200,7 +1201,73 @@ function myalerts_alert_rated_threadauthor()
)
);

MybbStuff_MyAlerts_AlertManager::getInstance()->addAlert($alert);
$alertManager = MybbStuff_MyAlerts_AlertManager::getInstance();

if (is_null($alertManager) || $alertManager === false) {
global $cache;

$alertManager = MybbStuff_MyAlerts_AlertManager::createInstance($mybb, $db, $cache, $plugins, $alertTypeManager);
}

$alertManager->addAlert($alert);
}
}
}
}

$plugins->add_hook(
'polls_vote_end',
'myalerts_alert_voted_threadauthor'
);
function myalerts_alert_voted_threadauthor()
{
global $mybb, $db, $cache, $plugins, $poll;

if (!isset($mybb->user['uid']) || $mybb->user['uid'] < 1 || $poll['public'] == 0) {
return;
}

myalerts_create_instances();

$alertTypeManager = MybbStuff_MyAlerts_AlertTypeManager::getInstance();

if (is_null($alertTypeManager) || $alertTypeManager === false) {
global $cache;

$alertTypeManager = MybbStuff_MyAlerts_AlertTypeManager::createInstance($db, $cache);
}

$alertType = $alertTypeManager->getByCode('voted_threadauthor');

if ($alertType != null && $alertType->getEnabled()) {
$thread = get_thread($poll['tid']);

if ($thread['uid'] != $mybb->user['uid']) {
$forumPerms = forum_permissions($thread['fid'], $thread['uid']);

// Check forum permissions
if ($forumPerms['canview'] != 0 || $forumPerms['canviewthreads'] != 0) {
$alert = new MybbStuff_MyAlerts_Entity_Alert(
$thread['uid'],
$alertType,
(int) $tid
);
$alert->setExtraDetails(
array(
'tid' => $poll['tid'],
't_subject' => $thread['subject'],
)
);

$alertManager = MybbStuff_MyAlerts_AlertManager::getInstance();

if (is_null($alertManager) || $alertManager === false) {
global $cache;

$alertManager = MybbStuff_MyAlerts_AlertManager::createInstance($mybb, $db, $cache, $plugins, $alertTypeManager);
}

$alertManager->addAlert($alert);
}
}
}
Expand Down Expand Up @@ -1515,6 +1582,13 @@ function myalerts_register_core_formatters($mybb, $lang)
'rated_threadauthor'
)
);
$formatterManager->registerFormatter(
new MybbStuff_MyAlerts_Formatter_ThreadAuthorVotedFormatter(
$mybb,
$lang,
'voted_threadauthor'
)
);
}

$plugins->add_hook('admin_config_menu', 'myalerts_acp_config_menu');
Expand Down

0 comments on commit 06bb7ba

Please sign in to comment.