TinyPortal

Development => Block Codes => Topic started by: Greater Realms on July 27, 2013, 10:34:47 PM

Title: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: Greater Realms on July 27, 2013, 10:34:47 PM
Link to my forum: gr.2phatgeeks.com
SMF version: 2.0.4
TP version: 1.107
Default Forum Language: English
Theme name and version: Argentum2 Enhanced, Final. The sexiest of the Argentums. 
Browser Name and Version: Chrome  28.0.1500.72 m
Mods installed: 1.   KeyCAPTCHA for SMF    2.5 
2.   TinyPortal    1.107     
3.   Add Social Media Icons To Profiles    1.1.0     
4.   Ban List    2.0     
5.   Dice Roller BBcode    1.3     
6.   FancyPosts    0.4.2     
7.   SMFSIMPLE Bookmarks
8.     SMF Shop     3.2.1

Hello! I would very much like to display a Who is in the Chat room within a TP module. I am using Blueimp's Ajax chat integrated with SMF: http://frug.github.io/AJAX-Chat/ I've tried to no avail as an utter nubbin to parse the smf mod to see if I can play match-the-theme-edits and I just plain suck.

I am hoping that there is an easier way about it to display who is in the chat via TP. Or at least, someone is patient enough to 's'plain it to a newb like me?

Edited to add 1 new mod: Smf shop
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: Lesmond on July 27, 2013, 10:50:26 PM
It might help if you Could tell us what chat software are you using please?
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: Greater Realms on July 28, 2013, 12:52:15 AM
Ajax chat. Sorry, I only put it in the title. :)
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: IchBin on July 28, 2013, 03:05:30 AM
Took the code right from the mod basically. Give this a try.

global $context, $smcFunc, $scripturl, $txt;

//ajax chat start
$userIDs = chatOnlineUsers();
$context['chat_links'] = array();
if (count($userIDs) > 0){
if (count($userIDs) == 1){
$result = $smcFunc['db_query']('', '
SELECT mem.id_member, mem.real_name, mem.id_group, mg.online_color, mg.id_group
FROM {db_prefix}members AS mem
LEFT JOIN {db_prefix}membergroups AS mg ON
(mg.id_group = IF(mem.id_group = 0, mem.id_post_group, mem.id_group)) WHERE mem.id_member = {int:the_id}',
array(
'the_id' => $userIDs[0],
)
);
} else {
$result = $smcFunc['db_query']('', '
SELECT mem.id_member, mem.real_name, mem.id_group, mg.online_color, mg.id_group
FROM {db_prefix}members AS mem
LEFT JOIN {db_prefix}membergroups AS mg ON
(mg.id_group = IF(mem.id_group = 0, mem.id_post_group, mem.id_group)) WHERE mem.id_member IN ({array_int:the_ids})',
array(
'the_ids' => $userIDs,
)
);
}
while ($row = $smcFunc['db_fetch_assoc']($result)) {
$link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'];
if($row['online_color'] != ""){
$link.= '" style="color: ' . $row['online_color'];
}
$link.= '">' . $row['real_name'] . '</a>';
array_push($context['chat_links'], $link);
}
$smcFunc['db_free_result']($result);
}

if (isset($context['chat_links'])) {
if (count($context['chat_links']) == 0)
echo '<br />' . $txt['chat_no_user'];
elseif (count($context['chat_links']) == 1 )
echo '<br />  1' . $txt['chat_aUser']. ':<br />', implode($context['chat_links']);
else
echo '<br /> ' . count($context['chat_links']). $txt['chat_users']. ':<br />', implode(', ', $context['chat_links']);
}
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: Greater Realms on July 28, 2013, 01:56:25 PM
QuoteFatal error: Call to undefined function chatOnlineUsers() in /home/bariguy/gr.2phatgeeks.com/Sources/Load.php(2207) : eval()'d code(127) : eval()'d code on line 4

I'm assuming that I am doing something wrong here lol--Which shouldn't surprise anyone!

(https://www.tinyportal.net/proxy.php?request=http%3A%2F%2Fi.imgur.com%2Fk6T9VNK.jpg&hash=bc543d9bb9007ad4030c6db6e42e024a091da146)

Thank you both for your help so far!
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: IchBin on July 28, 2013, 03:19:34 PM
I assumed the file was loaded that had the chatOnlineUsers() function in it.

So all you need to do is find out which file in the smf mod has the chatOnlineUsers() function in it. First add $sourcedir to the global line at the top of the code I posted above. Right after the global line, add the include with the name of the file (assuming it's in the Sources/ folder).
require_once($sourcedir . '/NameofFile.php');
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: Greater Realms on July 28, 2013, 04:35:47 PM
So for me to be clear (sorry, you are dealing with yet another complete and total newbie here)

Pretending that www.myawesomesmfforum.com/directory/sources/magicfilewithChatOnlineUsersInIt I would then add:

require_once($sourcedir . 'www.myawesomesmfforum.com/directory/sources/magicfilewithChatOnlineUsersInIt');

To the code you provided into a PHP TP module and if all goes well it should work and no one will die, yes?

Thank you so much. I'll get to work trying to find where that function is.
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: IchBin on July 29, 2013, 01:20:22 AM
No, you add it like I showed you above. You only need to exchange the filename with the actual filename that you find the function in.

$sourcedir will automatically add the path to the Sources directory.
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: Greater Realms on July 29, 2013, 05:53:36 PM
Quote from: IchBin™ on July 29, 2013, 01:20:22 AM
No, you add it like I showed you above. You only need to exchange the filename with the actual filename that you find the function in.

$sourcedir will automatically add the path to the Sources directory.

Thank you for being patient with me and helping!

I found the line chatOnlineUsers in my SSI.php. So the top of the code you originally shared, I added below global:

require_once($sourcedir . '/SSI.php');

Is that correct? I feel like I am missing something super obvious because I have no clue what I am doing--but the above didn't work.

QuoteFatal error: require_once(): Failed opening required '/SSI.php' (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/#######/###/Sources/Load.php(2151) : eval()'d code(127) : eval()'d code on line 2
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: Lesmond on July 29, 2013, 06:14:19 PM
I have been trying this on my test server, I found chatOnlineUsers in index.template.php, I am also getting errors.

here is my code I used in a block..
global $context, $smcFunc, $scripturl, $txt, $sourcedir;
require_once($sourcedir . '/index.template.php');

//ajax chat start
$userIDs = chatOnlineUsers();
$context['chat_links'] = array();
if (count($userIDs) > 0){
if (count($userIDs) == 1){
$result = $smcFunc['db_query']('', '
SELECT mem.id_member, mem.real_name, mem.id_group, mg.online_color, mg.id_group
FROM {db_prefix}members AS mem
LEFT JOIN {db_prefix}membergroups AS mg ON
(mg.id_group = IF(mem.id_group = 0, mem.id_post_group, mem.id_group)) WHERE mem.id_member = {int:the_id}',
array(
'the_id' => $userIDs[0],
)
);
} else {
$result = $smcFunc['db_query']('', '
SELECT mem.id_member, mem.real_name, mem.id_group, mg.online_color, mg.id_group
FROM {db_prefix}members AS mem
LEFT JOIN {db_prefix}membergroups AS mg ON
(mg.id_group = IF(mem.id_group = 0, mem.id_post_group, mem.id_group)) WHERE mem.id_member IN ({array_int:the_ids})',
array(
'the_ids' => $userIDs,
)
);
}
while ($row = $smcFunc['db_fetch_assoc']($result)) {
$link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'];
if($row['online_color'] != ""){
$link.= '" style="color: ' . $row['online_color'];
}
$link.= '">' . $row['real_name'] . '</a>';
array_push($context['chat_links'], $link);
}
$smcFunc['db_free_result']($result);
}

if (isset($context['chat_links'])) {
if (count($context['chat_links']) == 0)
echo '<br />' . $txt['chat_no_user'];
elseif (count($context['chat_links']) == 1 )
echo '<br />  1' . $txt['chat_aUser']. ':<br />', implode($context['chat_links']);
else
echo '<br /> ' . count($context['chat_links']). $txt['chat_users']. ':<br />', implode(', ', $context['chat_links']);
}


here is the error I get...
Fatal error: require_once(): Failed opening required 'D:/Ampp/Ampps/www/smf/Sources/index.template.php' (include_path='.;C:\php\pear') in D:\Ampp\Ampps\www\smf\Sources\Load.php(2161) : eval()'d code(127) : eval()'d code on line 2

Just trying to help, not much good at php :idiot2:
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: Greater Realms on July 29, 2013, 06:51:34 PM
Thank you for helping test/trying too, Lesmond! I'm afraid that if it weren't for hard working people who make mods and ad ons like TP I wouldn't get anywhere  :uglystupid2:

Your code gives me the same error you are getting as well!
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: IchBin on July 29, 2013, 08:38:41 PM
Greater Realms, SSI.php is already include in the TP code somewhere, so there's no need to include the file. You should be able to remove the include line.

Les, if it's in index.template.php for you (weird lol), then you should also be able to remove the include. Since index.template.php is included already in the process.
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: Greater Realms on July 29, 2013, 08:45:23 PM

I am sort of going cross eyed at the moment.

QuoteGreater Realms, SSI.php is already include in the TP code somewhere, so there's no need to include the file. You should be able to remove the include line.

Does...that mean I don't need to use require_once($sourcedir . '/somefile.php');? I am so sorry for being this dense.  :uglystupid2:

Edited # 2: Had an error with Ajax chat, crisis averted. Still too dense to figure out how to place a Who's chatting. May need more coffee  O0

Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: Lesmond on July 29, 2013, 09:49:08 PM
Quote from: IchBin™ on July 29, 2013, 08:38:41 PM
Les, if it's in index.template.php for you (weird lol), then you should also be able to remove the include. Since index.template.php is included already in the process.
Brad the mod adds that bit to index.template.php
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: IchBin on July 30, 2013, 05:35:05 AM
What I'm telling you Greater Realms, is that TinyPortal already uses things from the SSI.php file. So the file is already included for every page that you open in the forum because TP is already including it in it's own code. There's no need to include a file that is already included. In other words, you do not need that single line of code.

According to the mod instructions on the SMF mod site located here:
http://custom.simplemachines.org/mods/index.php?action=parse;mod=1319;attach=138620;smf_version=2.0_RC3

The chatOnlineUsers() function is actually in the Load.php file. So I'm guessing you guys have an old version of the mod or something. The function might be called in the index.template.php file Les, but the actual function is not declared in that file. It's either that, or you guys are not searching for the right function name.

Let me know if the code works without the include (require_once) line.
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: Greater Realms on July 30, 2013, 05:50:30 AM
Thank you. That's what I was asking you so I could be clear.

I am not using the mod. I am sorry if I was not clear. I am using a version directly from the actual Ajax Chat creators site integrated for SMF found here: http://frug.github.io/AJAX-Chat/ because it is kept updated while the actual SMF mod offered on the SMF forums is not

The code did not work before you suggested the include, still doesn't work without it and it still gives me the same error:

QuoteFatal error: Call to undefined function chatOnlineUsers() in /home/bariguy/gr.2phatgeeks.com/Sources/Load.php(2151) : eval()'d code(127) : eval()'d code on line 4

QuoteIt's either that, or you guys are not searching for the right function name.

I wanted to reiterate that I cannot script or code my way out of a wet paper bag. When you say stuff like this 99 times out of 100 I don't understand because I don't even know where to start...This is why I'm here, unfortunately for you.
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: IchBin on July 31, 2013, 02:21:41 AM
Ah ok, didn't realize you weren't using the mod. In that case, we'll just copy the whole function into the block too.

Add this code in with the other code I provided at the end.

function chatOnlineUsers(){
global $smcFunc;

$userIDs = array();
$result = $smcFunc['db_query']('', '
SELECT userID
FROM {db_prefix}ajaxchat_online WHERE NOW() <= DATE_ADD(dateTime, interval 2 MINUTE)' ,
  array()
);
while($row = $smcFunc['db_fetch_assoc']($result)) {
array_push($userIDs, $row['userID']);
}
$smcFunc['db_free_result']($result);
return array_unique($userIDs);
}
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: Lesmond on July 31, 2013, 12:46:36 PM
I still have an error Brad..

The error ...
Table 'smf542.smf_ajaxchat_online' doesn't exist
File: D:\Ampp\Ampps\www\smf\Sources\Load.php(2158) : eval()'d code(127) : eval()'d code
Line: 55


heres the code I have used..
global $context, $smcFunc, $scripturl, $txt;

//ajax chat start
$userIDs = chatOnlineUsers();
$context['chat_links'] = array();
if (count($userIDs) > 0){
if (count($userIDs) == 1){
$result = $smcFunc['db_query']('', '
SELECT mem.id_member, mem.real_name, mem.id_group, mg.online_color, mg.id_group
FROM {db_prefix}members AS mem
LEFT JOIN {db_prefix}membergroups AS mg ON
(mg.id_group = IF(mem.id_group = 0, mem.id_post_group, mem.id_group)) WHERE mem.id_member = {int:the_id}',
array(
'the_id' => $userIDs[0],
)
);
} else {
$result = $smcFunc['db_query']('', '
SELECT mem.id_member, mem.real_name, mem.id_group, mg.online_color, mg.id_group
FROM {db_prefix}members AS mem
LEFT JOIN {db_prefix}membergroups AS mg ON
(mg.id_group = IF(mem.id_group = 0, mem.id_post_group, mem.id_group)) WHERE mem.id_member IN ({array_int:the_ids})',
array(
'the_ids' => $userIDs,
)
);
}
while ($row = $smcFunc['db_fetch_assoc']($result)) {
$link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'];
if($row['online_color'] != ""){
$link.= '" style="color: ' . $row['online_color'];
}
$link.= '">' . $row['real_name'] . '</a>';
array_push($context['chat_links'], $link);
}
$smcFunc['db_free_result']($result);
}

if (isset($context['chat_links'])) {
if (count($context['chat_links']) == 0)
echo '<br />' . $txt['chat_no_user'];
elseif (count($context['chat_links']) == 1 )
echo '<br />  1' . $txt['chat_aUser']. ':<br />', implode($context['chat_links']);
else
echo '<br /> ' . count($context['chat_links']). $txt['chat_users']. ':<br />', implode(', ', $context['chat_links']);
}
function chatOnlineUsers(){
global $smcFunc;

$userIDs = array();
$result = $smcFunc['db_query']('', '
SELECT userID
FROM {db_prefix}ajaxchat_online WHERE NOW() <= DATE_ADD(dateTime, interval 2 MINUTE)' ,
  array()
);
while($row = $smcFunc['db_fetch_assoc']($result)) {
array_push($userIDs, $row['userID']);
}
$smcFunc['db_free_result']($result);
return array_unique($userIDs);
}


I am not sure if its because the ajaxchat doesn't add the smf_  prefix to the tables in the database
Title: Re: Possible to create Who's Chatting TP module for Integrated Ajax Chat?
Post by: IchBin on August 02, 2013, 06:13:50 AM
If it doesn't add the smf_ prefix then just take out the {db_prefix} and add the prefix that the mod adds. But this is code that is directly from the mod. I haven't wrote any of the code. So if it's not working, then you are either using a different mod, or an outdated mod.