ElkArte Community

Project Support => Support => Topic started by: ahrasis on November 25, 2014, 02:26:34 am

Title: Adding Buddy BG
Post by: ahrasis on November 25, 2014, 02:26:34 am
Hi,

I attempting to port another simple mod of mine (HLBM) here. This mod will need to change this line:
Code: [Select]
<div class="post_wrapper ', $message['approved'] ? ($message['alternate'] == 0 ? 'windowbg' : 'windowbg2') : 'approvebg', '">
To:
Code: [Select]
<div class="post_wrapper ', $message['approved'] && $message['member']['is_buddy'] ? 'buddybg' : ($message['approved'] ? ($message['alternate'] == 0 ? 'windowbg' : 'windowbg2') : 'approvebg'), '">

I cannot see is_buddy anywhere in display controller. Is there any is_buddy in Elk? Can this be done via hook? How?
Title: Re: Adding Buddy BG
Post by: emanuele on November 25, 2014, 09:35:22 am
is_buddy is loaded in loadMemberContext, so, yes it's available.

Not that I can see.
Title: Re: Adding Buddy BG
Post by: Spuds on November 25, 2014, 10:13:08 am
I think it may be in the $message array, $message['is_buddy'] perhaps?

The only way I can think to do it without edits would be with some JS.  In a hook you keep track of the is buddy flags and output a JS object with the id's, and then have some JS function that on load reads that and changes the backgrounds.  Thats just a thought.
Title: Re: Adding Buddy BG
Post by: ahrasis on November 26, 2014, 02:00:52 am
Thank you @emanuele and @Spuds . I think I get the idea a bit. Javascript may be the only solution to avoid modifying the message tempalte.

I am thinking rather than injecting script in the header, it will be better to add it via template layer somewhere after the message itself at the bottom.

Current idea of template code:
Code: [Select]
function template_HLBM_below()
{
// I don't think $message can be used here. Help!!! I cannot get it to work.
if ($message['approved'] && $message['member']['is_buddy']) {
echo '
<script>
function changeColor(buddybg) {
var hlbm = document.getElementById('forumposts').getElementsByClassName('post_wrapper windowbg');
hlbm.style.color = buddybg;
// Not finish. Then what? What about windowbg2? Repeat this again?
// Or use this one instead???
document.getElementById('forumposts').className = post_wrapper windowbg
document.getElementById('forumposts').className.replace(post_wrapper buddybg)
// If correct, repeat for windowbg2???
document.getElementById('forumposts').className = post_wrapper windowbg2
document.getElementById('forumposts').className.replace(post_wrapper buddybg)
}
</script>';
}
}

The javascript is supposed to get the class windowbg and windowbg2 if the message is approved and is a buddy message. But I cannot test it as I don't think $message can be simply used just like that. Anything that can make it global and use here?
Title: Re: Adding Buddy BG
Post by: TE on November 26, 2014, 05:31:17 am
Add a hook in elkarte_settings for
 
Code: [Select]
integrate_prepare_display_context
Example:
Code: [Select]
colorize_buddy|SUBSDIR/ColorizeBuddy.subs.php

Code: (ColorizeBuddy.subs.php) [Select]
<?php
function colorize_buddy(&$output, &$message)
{
global $user_info;

if (in_array($message['id_member'], $user_info['buddies']))
{
addInlineJavascript('
$( "#msg'. $message['id_msg'] . '" ).parent().css( "background-color", "#ccc" );
', true);
}
}
Title: Re: Adding Buddy BG
Post by: emanuele on November 26, 2014, 07:42:30 am
Or:
Code: [Select]
<?php
function colorize_buddy(&$output, &$message)
{
global $user_info;

if (in_array($message['id_member'], $user_info['buddies']))
{
addInlineJavascript('
$( "#msg'. $message['id_msg'] . '" ).parent().addClass("buddybg");
', true);
}
}
So that styling can go to the css. ;)

I would be tempted to do something more complex, but probably is not worth at the moment. O:-)

Though, that could be an interesting spot to "do some changes"... O:-) O:-) O:-) O:-)
Title: Re: Adding Buddy BG
Post by: ahrasis on November 27, 2014, 12:09:20 am
You are all very wonderful and helpful indeed. Thank you very much guys.
Title: Re: Adding Buddy BG
Post by: ahrasis on November 27, 2014, 01:41:38 am
After testing the code, I realize that the code is targeting this code's parent:
Code: [Select]
<a class="post_anchor" id="msg' . $message['id'] . '"></a>

Unfortunately, if the said buddy message is on the top of the display page, it ends up wrongly changing this code's parent:
Code: [Select]
<a id="msg', $context['first_message'], '">

How to avoid it?
Title: Re: Adding Buddy BG
Post by: TE on November 27, 2014, 02:00:49 am
Ah, sorry.. didn't knew the anchor wasn't present there... You can also use the ID #info_xx (that's the post's header) and jump from there to the parent three levels up.

Code: [Select]
$( "#info_'. $message['id_msg'] . '" ).parent().parent().parent().css( "background-color", "#ccc" );
Title: Re: Adding Buddy BG
Post by: ahrasis on November 27, 2014, 04:23:07 am
Work like a charm. Thank you TE.

Addon is ready and already posted in the addon board.
Title: Re: Adding Buddy BG
Post by: emanuele on November 27, 2014, 07:27:38 am
https://github.com/emanuele45/Dialogo/commit/909193e7169e3def9ec20abbaa0dca440dc43d7d

O:-)
I'd push this to 1.0.2.

And, with a bit of tweaking, it may help @phantom as well with his Topic Author (http://www.elkarte.net/community/index.php?topic=2208.0). :D
Title: Re: Adding Buddy BG
Post by: ahrasis on November 28, 2014, 06:18:43 am
Great!

But how do I make use of this? And just out of my curiosity, why is $message['classes'] positioned before the default class? Shouldn't it be after to override it?
Title: Re: Adding Buddy BG
Post by: TE on November 28, 2014, 06:46:36 am
You don't need the JS part anymore, just modify the $Output Array..

Code: [Select]
function colorize_buddy(&$output, &$message)
{
global $user_info;

if (in_array($message['id_member'], $user_info['buddies']))
$output['classes'] = array("buddybg");
}
Untested though  :D
Title: Re: Adding Buddy BG
Post by: ahrasis on November 28, 2014, 06:58:55 am
Ok. That easier. But will that replaces the default class or adds a new class? I kinda think it adds a new class but shouldn't it be after and not before the default class (windowbg / windowbg2 / approvebg) for it to work?
Title: Re: Adding Buddy BG
Post by: emanuele on November 28, 2014, 01:01:00 pm
Just to be picky:
Code: [Select]
function colorize_buddy(&$output, &$message)
{
global $user_info;

if (in_array($message['id_member'], $user_info['buddies']))

$output['classes'][] = "buddybg";
}
:D

Quote from: ahrasis – But will that replaces the default class or adds a new class?
Adds new.

Quote from: ahrasis – I kinda think it adds a new class but shouldn't it be after and not before the default class (windowbg / windowbg2 / approvebg) for it to work?
AFAIK the order of the classes are used is not relevant.
What is relevant is the order the classes are declared in the stylesheet (i.e. in index.css for example). ;)