/**
  * Get data for a single discussion by ForeignID.
  *
  * @since 2.0.18
  * @access public
  *
  * @param int $ForeignID Foreign ID of discussion to get.
  * @return object SQL result.
  */
 public function getForeignID($ForeignID, $Type = '')
 {
     $Hash = ForeignIDHash($ForeignID);
     $Session = Gdn::session();
     $this->fireEvent('BeforeGetForeignID');
     $this->SQL->select('d.*')->select('ca.Name', '', 'Category')->select('ca.UrlCode', '', 'CategoryUrlCode')->select('ca.PermissionCategoryID')->select('w.DateLastViewed, w.Dismissed, w.Bookmarked')->select('w.CountComments', '', 'CountCommentWatch')->select('w.Participated')->select('d.DateLastComment', '', 'LastDate')->select('d.LastCommentUserID', '', 'LastUserID')->select('lcu.Name', '', 'LastName')->select('iu.Name', '', 'InsertName')->select('iu.Photo', '', 'InsertPhoto')->from('Discussion d')->join('Category ca', 'd.CategoryID = ca.CategoryID', 'left')->join('UserDiscussion w', 'd.DiscussionID = w.DiscussionID and w.UserID = ' . $Session->UserID, 'left')->join('User iu', 'd.InsertUserID = iu.UserID', 'left')->join('Comment lc', 'd.LastCommentID = lc.CommentID', 'left')->join('User lcu', 'lc.InsertUserID = lcu.UserID', 'left')->where('d.ForeignID', $Hash);
     if ($Type != '') {
         $this->SQL->where('d.Type', $Type);
     }
     $Discussion = $this->SQL->get()->firstRow();
     if (c('Vanilla.Views.Denormalize', false)) {
         $this->AddDenormalizedViews($Discussion);
     }
     return $Discussion;
 }
 /**
  * Takes a set of discussion identifiers and returns their comment counts in the same order.
  */
 public function GetCommentCounts()
 {
     $this->AllowJSONP(TRUE);
     $vanilla_identifier = GetValue('vanilla_identifier', $_GET);
     if (!is_array($vanilla_identifier)) {
         $vanilla_identifier = array($vanilla_identifier);
     }
     $vanilla_identifier = array_unique($vanilla_identifier);
     $FinalData = array_fill_keys($vanilla_identifier, 0);
     $Misses = array();
     $CacheKey = 'embed.comments.count.%s';
     $OriginalIDs = array();
     foreach ($vanilla_identifier as $ForeignID) {
         $HashedForeignID = ForeignIDHash($ForeignID);
         // Keep record of non-hashed identifiers for the reply
         $OriginalIDs[$HashedForeignID] = $ForeignID;
         $RealCacheKey = sprintf($CacheKey, $HashedForeignID);
         $Comments = Gdn::Cache()->Get($RealCacheKey);
         if ($Comments !== Gdn_Cache::CACHEOP_FAILURE) {
             $FinalData[$ForeignID] = $Comments;
         } else {
             $Misses[] = $HashedForeignID;
         }
     }
     if (sizeof($Misses)) {
         $CountData = Gdn::SQL()->Select('ForeignID, CountComments')->From('Discussion')->Where('Type', 'page')->WhereIn('ForeignID', $Misses)->Get()->ResultArray();
         foreach ($CountData as $Row) {
             // Get original identifier to send back
             $ForeignID = $OriginalIDs[$Row['ForeignID']];
             $FinalData[$ForeignID] = $Row['CountComments'];
             // Cache using the hashed identifier
             $RealCacheKey = sprintf($CacheKey, $Row['ForeignID']);
             Gdn::Cache()->Store($RealCacheKey, $Row['CountComments'], array(Gdn_Cache::FEATURE_EXPIRY => 60));
         }
     }
     $this->SetData('CountData', $FinalData);
     $this->DeliveryMethod = DELIVERY_METHOD_JSON;
     $this->DeliveryType = DELIVERY_TYPE_DATA;
     $this->Render();
 }