Skip to content

Commit

Permalink
And the javascript to handle the notification button
Browse files Browse the repository at this point in the history
Signed-off-by: emanuele <emanuele45@gmail.com>
  • Loading branch information
emanuele45 committed May 11, 2013
1 parent 180ec73 commit a716a27
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
3 changes: 2 additions & 1 deletion sources/controllers/Display.controller.php
Expand Up @@ -895,10 +895,11 @@ function action_index()
}

// Build the normal button array.
addJavascriptVar('notification_topic_notice', $context['is_marked_notify'] ? $txt['notification_disable_topic'] : $txt['notification_enable_topic'], true);
$context['normal_buttons'] = array(
'reply' => array('test' => 'can_reply', 'text' => 'reply', 'image' => 'reply.png', 'lang' => true, 'url' => $scripturl . '?action=post;topic=' . $context['current_topic'] . '.' . $context['start'] . ';last_msg=' . $context['topic_last_message'], 'active' => true),
'add_poll' => array('test' => 'can_add_poll', 'text' => 'add_poll', 'image' => 'add_poll.png', 'lang' => true, 'url' => $scripturl . '?action=editpoll;add;topic=' . $context['current_topic'] . '.' . $context['start']),
'notify' => array('test' => 'can_mark_notify', 'text' => $context['is_marked_notify'] ? 'unnotify' : 'notify', 'image' => ($context['is_marked_notify'] ? 'un' : '') . 'notify.png', 'lang' => true, 'custom' => 'onclick="return confirm(\'' . ($context['is_marked_notify'] ? $txt['notification_disable_topic'] : $txt['notification_enable_topic']) . '\');"', 'url' => $scripturl . '?action=notify;sa=' . ($context['is_marked_notify'] ? 'off' : 'on') . ';topic=' . $context['current_topic'] . '.' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id']),
'notify' => array( 'test' => 'can_mark_notify', 'text' => $context['is_marked_notify'] ? 'unnotify' : 'notify', 'image' => ($context['is_marked_notify'] ? 'un' : '') . 'notify.png', 'lang' => true, 'custom' => 'onclick="return notifyButton(this);"', 'url' => $scripturl . '?action=notify;sa=' . ($context['is_marked_notify'] ? 'off' : 'on') . ';topic=' . $context['current_topic'] . '.' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id']),
'mark_unread' => array('test' => 'can_mark_unread', 'text' => 'mark_unread', 'image' => 'markunread.png', 'lang' => true, 'url' => $scripturl . '?action=markasread;sa=topic;t=' . $context['mark_unread_time'] . ';topic=' . $context['current_topic'] . '.' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id']),
'disregard' => array('test' => 'can_disregard', 'text' => ($context['topic_disregarded'] ? 'un' : '') . 'disregard', 'image' => ($context['topic_disregarded'] ? 'un' : '') . 'disregard.png', 'lang' => true, 'url' => $scripturl . '?action=disregardtopic;topic=' . $context['current_topic'] . '.' . $context['start'] . ';sa=' . ($context['topic_disregarded'] ? 'off' : 'on') . ';' . $context['session_var'] . '=' . $context['session_id']),
'send' => array('test' => 'can_send_topic', 'text' => 'send_topic', 'image' => 'sendtopic.png', 'lang' => true, 'url' => $scripturl . '?action=emailuser;sa=sendtopic;topic=' . $context['current_topic'] . '.0'),
Expand Down
14 changes: 8 additions & 6 deletions sources/controllers/Notify.controller.php
Expand Up @@ -93,26 +93,28 @@ function action_notify_api()
// Our topic functions are here
require_once(SUBSDIR . '/Topic.subs.php');

// Attempt to turn notifications on.
// Attempt to turn notifications on/off.
setTopicNotification($user_info['id'], $topic, $_GET['sa'] == 'on');

$text = $_GET['sa'] == 'on' ? $txt['unnotify'] : $txt['notify'];
$url = $scripturl . '?action=notify;sa=' . ($_GET['sa'] == 'on' ? 'off' : 'on') . ';topic=' . $topic . '.' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';api' . (isset($_REQUEST['json']) ? ';json' : '');
$return = array(
'text' => $_GET['sa'] == 'on' ? $txt['unnotify'] : $txt['notify'],
'url' => $scripturl . '?action=notify;sa=' . ($_GET['sa'] == 'on' ? 'off' : 'on') . ';topic=' . $topic . '.' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';api' . (isset($_REQUEST['json']) ? ';json' : ''),
'confirm' => $_GET['sa'] == 'on' ? $txt['notification_disable_topic'] : $txt['notification_enable_topic']
);

if (isset($_REQUEST['json']))
{
die(json_encode(array('text' => $text, 'url' => $url)));
die(json_encode($return));
}
loadTemplate('Xml');

$context['template_layers'] = array();
$context['sub_template'] = 'generic_xml_buttons';

$context['xml_data'] = array(
array(
'text' => $text,
'url' => $url,
),
$return,
);
}

Expand Down
5 changes: 3 additions & 2 deletions themes/default/Xml.template.php
Expand Up @@ -339,8 +339,9 @@ function template_generic_xml_buttons()
foreach ($context['xml_data'] as $button)
echo '
<button>
<text>', cleanXml($button['text']), '</text>
<url>', cleanXml($button['url']), '</text>
<text><![CDATA[', cleanXml($button['text']), ']]></text>
<url><![CDATA[', cleanXml($button['url']), ']]></url>', !empty($button['confirm']) ? '
<confirm><![CDATA[' . cleanXml($button['confirm']) . ']]></confirm>' : '', '
</button>';
echo '
</buttons>
Expand Down
36 changes: 36 additions & 0 deletions themes/default/scripts/script.js
Expand Up @@ -1685,3 +1685,39 @@ function doAutoSubmit()

setTimeout("doAutoSubmit();", 1000);
}

function toggleButtonAJAX(btn, text_confirm)
{
ajax_indicator(true);

var oXMLDoc = getXMLDocument(btn.href + ';api');

if (oXMLDoc.responseXML)
{
var text = oXMLDoc.responseXML.getElementsByTagName('elk')[0].getElementsByTagName('text')[0].firstChild.nodeValue.removeEntities();
var url = oXMLDoc.responseXML.getElementsByTagName('elk')[0].getElementsByTagName('url')[0].firstChild.nodeValue.removeEntities();
var confirm_elem = oXMLDoc.responseXML.getElementsByTagName('elk')[0].getElementsByTagName('confirm');
if (confirm_elem.length == 1)
var confirm_text = confirm_elem[0].firstChild.nodeValue.removeEntities();

$('.' + btn.className).each(function() {
// @todo: the span should be moved somewhere in themes.js?
$(this).html('<span>' + text + '</span>');
$(this).attr('href', url);
if (typeof(confirm_text) != 'undefined')
eval(text_confirm + '= \'' + confirm_text.replace(/[\\']/g, '\\$&') + '\'');
});
}

ajax_indicator(false);

}

function notifyButton(btn)
{
if (typeof(notification_topic_notice) != 'undefined' && !confirm(notification_topic_notice))
return false;

toggleButtonAJAX(btn, 'notification_topic_notice');
return false;
}

0 comments on commit a716a27

Please sign in to comment.