i need to embed a hosted chat system in my site and the provider gave me the following code to enter between the body tags of my template.
Code:
<?php
$chat_id = $chat_name = $chat_avatar = $chat_link = $chat_displayname = $chat_role = "";
$chat_friends = '';
$user = JFactory::getUser();
if (!$user->guest) {
$chat_id = $user->id;
$chat_name = $user->username;
$chat_displayname = $user->name;
if(class_exists('JAccess')) {
$user = JFactory::getUser();
$groups = JAccess::getGroupsByUser($user->id, false);
$groups_list = "(" . implode(",", $groups) . ")";
$db = JFactory::getDbo();
$query = $db->getQuery(true) ->select($db->qn("title")) ->from("#__usergroups") ->where($db->qn("id") . " IN " .
$groups_list); $db->setQuery($query);
$rows = $db->loadObjectList();
if(!empty($rows) && is_object($rows[0]) && property_exists($rows[0], "title")){
$chat_role = $rows[0]->title;
}
}
}
?>
<?php if (!empty($user->id)) : ?> // Check if the user is logged in. You can also check if the user is a premium user or not
<div id="cometchat"></div>
<script>
let authKey = "YOUR_AUTH_KEY"
window.addEventListener('DOMContentLoaded', (event) => {
CometChatWidget.init({
"appID": "YOUR_COMETCHAT_APP_ID",
"appRegion": "YOUR_COMETCHAT_APP_REGION",
"authKey": authKey
}).then(response => {
console.log("Initialization completed successfully");
//You can now call login function.
CometChatWidget.login({
"uid": "<?php echo $user->id ?>"
}).then(response => {
launchWidget()
}, error => {
console.log("User login failed with error:", error);
if(error.code === 'ERR_UID_NOT_FOUND'){
console.log("Please create user with UID <?php echo $user->id ?>")
var user = new CometChatWidget.CometChat.User("<?php echo $user->id ?>");
user.setName("<?php echo $user->name ?>");
CometChatWidget.CometChat.createUser(user, authKey).then(
user => {
console.log("user created", user);
CometChatWidget.login({"uid": "<?php echo $user->id ?>"
}).then(response => {
launchWidget()
}, error => {
});
}, error => {
console.log("error", error);
})
}
//Check the reason for error and take appropriate action.
});
}, error => {
console.log("Initialization failed with error:", error);
//Check the reason for error and take appropriate action.
});
});
function launchWidget() {
CometChatWidget.launch({
"widgetID": "YOUR_COMETCHAT_WIDGET_ID",
"target": "#cometchat",
"roundedCorners": "true",
"height": "600px",
"width": "800px",
"docked": true
});
}
</script>
<?php endif; ?>
Is it possible to use this code in cb_content module so that we can easily substitute the user details and show the module only in the menu items we want?