Name

sn_ex_sp.PopularTopicsUtilSNC

Description

WARNING Customers should NOT modify this script The purpose of this script include is to provide default behaviours for the PopularTopicsUtil script include. To change the behaviour of these methods (or add new methods), Customers should override/add new methods to the PopularTopicsUtil script include.

Script

var PopularTopicsUtilSNC = Class.create();
PopularTopicsUtilSNC.prototype = {
  initialize: function() {
      this.popularTopicsIndex = {};
      this.defaultRedirectionPage = 'emp_taxonomy_topic';
      this.URL_ENABLED = gs.getProperty("sn_ex_sp.enable.image.url.support");
  },

  /**
   * Returns default topic icon image name
   **/
  _fetchDefaultIconImage: function() {
      return new TopicPageUtil().fetchDefaultIconImage();
  },
  /**
   * Fill topics from topics table if popular topics < limit
   **/
  _fillTopics: function(popularTopics, taxonomyId, count, isMobile) {
      var taxonomyService = new sn_taxonomy.Taxonomy(taxonomyId);
      var maxLevel = 0;
      var noOfTopics = 0;
      var countOfTopicsInResp = 0;
      while (noOfTopics < count) {
          var topics = taxonomyService.getTopics(true, maxLevel, isMobile);
          if (countOfTopicsInResp === topics.length) {
              break;
          }
          countOfTopicsInResp = topics.length;
          topics.every(function(topic) {
              //If topic doesnt already exist then add it
              if (!this.popularTopicsIndex[topic.sys_id]) {
                  var icon;
                  if(this.URL_ENABLED && this.URL_ENABLED === "true")
                      icon = new sn_ex_sp.TopicPageUtil().getIcon(topic.sys_id);
                  else
                      icon = sn_taxonomy.Topic.getIcon(topic.sys_id);
                  var pageToRedirect = topic.template && topic.template.length > 0 ? topic.template : this.defaultRedirectionPage;
                  popularTopics.push({
                      topic: topic.name,
                      topicId: topic.sys_id,
                      icon: icon || icon.trim().length !== 0 ? icon : this._fetchDefaultIconImage(),
                      topicUrl: '?id='+ pageToRedirect +'&topic_id=' + topic.sys_id
                  });
                  this.popularTopicsIndex[topic.sys_id] = true;
                  noOfTopics++;
              }
              if (noOfTopics === count) {
                  return false;
              }
              return true;
          }, this);
          maxLevel++;

      }


  },

  /**
   * Return image's url if available, otherwise return the image (uploaded or default)
   */
  _getImage: function(url, image, size, defaultImage) {
      if(this.URL_ENABLED && this.URL_ENABLED==="true" && url)
          return url;
      if(image){
          if(size)
              return image + '?t=' + size;
          return image;
      }
      return defaultImage;
  },

  /**
   * Returns popular topics
   * @param taxonomyId
   * @param limit: the number of topics
   * @param isMobile
   **/
  getPopularTopics: function(taxonomy, limit, isMobile) {
      var popularTopics = [];
      var startIndex = 0;
      var noOfItems = 0;
      var endIndex = limit * 2;
      while (noOfItems < limit) {
          var topicMetricGr = new GlideRecord('sn_ex_sp_topic_metric');
          topicMetricGr.addQuery('topic.taxonomy', taxonomy);
          topicMetricGr.addQuery('topic.active', true);
          topicMetricGr.orderByDesc('popularity');
          topicMetricGr.orderBy('topic.order');
          topicMetricGr.orderBy('topic.name');
          topicMetricGr.chooseWindow(startIndex, endIndex);
          topicMetricGr.query();
          if (!topicMetricGr.hasNext()) {
              break;
          }
          while (topicMetricGr.next() && noOfItems < limit) {
              var topic = topicMetricGr.topic;
              var canView;
              if (!GlidePluginManager.isActive('com.glideapp.knowledge.i18n2'))
                  canView = new TopicSecurityUtil().canViewTopic(topic.sys_id, false);
              else 
                  canView = new TopicSecurityUtil().canViewTopicAdvanced(topic.sys_id, false);
                  
              var pageToRedirect = topic.template ? topic.template.getDisplayValue() : this.defaultRedirectionPage;
              if (canView) {
                  popularTopics.push({
                      topic: topic.name.getDisplayValue(),
                      topicId: topic.sys_id.getDisplayValue(),
                      icon: this._getImage(topic.icon_url.toString(), topic.icon.getDisplayValue(), 'small', this._fetchDefaultIconImage()),
                      topicUrl: '?id='+ pageToRedirect + '&topic_id=' + topic.sys_id
                  });
                  this.popularTopicsIndex[topic.sys_id] = true;
                  noOfItems++;
              }

          }
          startIndex = endIndex + 1;
          endIndex = endIndex + (limit * 2);
      }
      return popularTopics;
  },
  type: 'PopularTopicsUtilSNC'
};

Sys ID

21ecf2566b387010d70bc141ee44afca

Offical Documentation

Official Docs: