Hello
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?
You'll be able to use the HTML and JavaScript in CB Content Module, but not the PHP. So if you're able to replace the PHP needed with substitutions yes that'll work fine. Looks like the PHP is just using user id and name so that should be easy enough with [user_id] and [name].
Kyle (Krileon) Community Builder Team Member Before posting on forums:
Read FAQ thoroughly
+
Read our Documentation
+
Search the forums CB links:
Documentation
-
Localization
-
CB Quickstart
-
CB Paid Subscriptions
-
Add-Ons
-
Forge
-- If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
-- If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please
send me a private message
with your thread and will reply when possible!
-- Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
-- My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.