Hi.
I received a request to share my solution for a custom command to close/open the chat. So I will post this so everyone can use my modification.
If you don't have added any custom commands, follow Guide no:1.
If you already have one or more custom commands, follow Guide no:2.
Guide no:1(no previously added custom commands)
1. Open 'chat/lib/class/CustomAJAXChat.php'.
2. At the bottom of the opened file(before the last '}' and '?>'(if present), add this code:
function parseCustomCommands($text, $textParts) {
if($this->getUserRole() == AJAX_CHAT_ADMIN) {
switch($textParts[0]) {
case '/toggleChat':
$config_file = AJAX_CHAT_PATH.'lib/config.php';
$file_contents = file_get_contents($config_file);
$open = "\$config['chatClosed'] = false;";
$closed = "\$config['chatClosed'] = true;";
if(strpos($file_contents, $open)) {
$file_contents = str_replace($open, $closed, $file_contents);
file_put_contents($config_file, $file_contents);
$this->insertChatBotMessage($this->getPrivateMessageID(), '/close');
} else {
$file_contents = str_replace($closed, $open, $file_contents);
file_put_contents($config_file, $file_contents);
$this->insertChatBotMessage($this->getPrivateMessageID(), '/open');
}
return true;
default: return false;
}
}
}
3. Open 'chat/js/custom.js' and add this code:
ajaxChat.replaceCustomCommands = function(text, textParts) {
switch(textParts[0]) {
case '/open': // Chat open confirmation message
text = text.replace('/open', ' ');
return '<span class="chatBotMessage">' + this.lang['chatOpened'] + '</span>';
break;
case '/close': // Chat close confirmation message
text = text.replace('/close', ' ');
return '<span class="chatBotErrorMessage">' + this.lang['chatClosed'] + '</span>';
break;
}
}
4. Open 'chat/js/language/*your preferred language files*.js' and find the first code in the file:
Add this code directly after this, before the first translated word:
chatOpened: 'Chat is opened!',
chatClosed: 'Chat is closed!',
(Make sure to open all language files you are using and add the code above.)
5. Go to "CONCLUSION" below the next guide!
Guide no:2(one or more previously added custom commands)
1. Open 'chat/lib/class/CustomAJAXChat.php'.
2. Find:
function parseCustomCommands($text, $textParts) {
In that code search for:
After the closing you should add your code(3)!
Example on were to put the code:
function parseCustomCommands($text, $textParts) {
switch($textParts[0]) {
*A bunch of code here*
*Look for the last closing "}", add the new code after the closing.*
} <--- * The last closing for "switch" *
<--- *Here is were you will add your new code!*
}
3. Now add this code at the position mentioned above:
if($this->getUserRole() == AJAX_CHAT_ADMIN) {
switch($textParts[0]) {
case '/toggleChat':
$config_file = AJAX_CHAT_PATH.'lib/config.php';
$file_contents = file_get_contents($config_file);
$open = "\$config['chatClosed'] = false;";
$closed = "\$config['chatClosed'] = true;";
if(strpos($file_contents, $open)) {
$file_contents = str_replace($open, $closed, $file_contents);
file_put_contents($config_file, $file_contents);
$this->insertChatBotMessage($this->getPrivateMessageID(), '/close');
} else {
$file_contents = str_replace($closed, $open, $file_contents);
file_put_contents($config_file, $file_contents);
$this->insertChatBotMessage($this->getPrivateMessageID(), '/open');
}
return true;
default: return false;
}
}
4. Open 'chat/js/custom.js'. Check if you have the function:
ajaxChat.replaceCustomCommands = function(text, textParts) {
If
NOT, add this code:
ajaxChat.replaceCustomCommands = function(text, textParts) {
switch(textParts[0]) {
case '/open': // Chat open confirmation message
text = text.replace('/open', ' ');
return '<span class="chatBotMessage">' + this.lang['chatOpened'] + '</span>';
break;
case '/close': // Chat close confirmation message
text = text.replace('/close', ' ');
return '<span class="chatBotErrorMessage">' + this.lang['chatClosed'] + '</span>';
break;
}
}
If PRESENT, then find this:
Before that code, add this:
case '/open': // Chat open confirmation message
text = text.replace('/open', ' ');
return '<span class="chatBotMessage">' + this.lang['chatOpened'] + '</span>';
break;
case '/close': // Chat close confirmation message
text = text.replace('/close', ' ');
return '<span class="chatBotErrorMessage">' + this.lang['chatClosed'] + '</span>';
break;
5. Open 'chat/js/language/*your preferred language files*.js' and find the first code in the file:
Add this code directly after this, before the first translated word:
chatOpened: 'Chat is opened!',
chatClosed: 'Chat is closed!',
(Make sure to open all language files you are using and add the code above and change the text inside the ' ' to the correct translation for the language file.)
CONCLUSION:
The code checks if the chat is opened or closed and triggers the opposite eg. if chat is opened, it closes. If chat is closed, it opens.
Now everything is done and you can use the command '/toggleChat' to toggle between opened or closed chat. Only Admins can use this command. When the command is issued, the chat automatically log out all users including Mods. Only exception is Admins. I don't provide code for changing this, because this is built in behavior in the chat and requires changing code inside 'AjaxChat.php', which this guide don't cover. But if you like to change this, you can do so by searching for the function 'isChatOpen()'. This function controls this behavior.
Hope I didn't make to much typos or errors. I have the code embedded in a lot of other custom changes, so was a bit messy to pick out the parts for this command only. Hopefully I haven't missed anything.
Any questions, please throw them at me, will try to help as far as I can.
Best regards,
FireWire