/**
  * Modify flow of discussion by pinning accepted answers.
  *
  * @param $Sender
  * @param $Args
  */
 public function DiscussionController_BeforeDiscussionRender_Handler($Sender, $Args)
 {
     if ($Sender->Data('Discussion.QnA')) {
         $Sender->CssClass .= ' Question';
     }
     if (strcasecmp($Sender->Data('Discussion.QnA'), 'Accepted') != 0) {
         return;
     }
     // Find the accepted answer(s) to the question.
     $CommentModel = new CommentModel();
     $Answers = $CommentModel->GetWhere(array('DiscussionID' => $Sender->Data('Discussion.DiscussionID'), 'Qna' => 'Accepted'))->Result();
     if (class_exists('ReplyModel')) {
         $ReplyModel = new ReplyModel();
         $Discussion = NULL;
         $ReplyModel->JoinReplies($Discussion, $Answers);
     }
     $Sender->SetData('Answers', $Answers);
     // Remove the accepted answers from the comments.
     // Allow this to be skipped via config.
     if (C('QnA.AcceptedAnswers.Filter', TRUE)) {
         if (isset($Sender->Data['Comments'])) {
             $Comments = $Sender->Data['Comments']->Result();
             $Comments = array_filter($Comments, function ($Row) {
                 return strcasecmp(GetValue('QnA', $Row), 'accepted');
             });
             $Sender->Data['Comments'] = new Gdn_DataSet(array_values($Comments));
         }
     }
 }
 /**
  *
  *
  * @param $Filename
  * @param $Get
  * @return bool|string
  */
 public function filenameRedirect($Filename, $Get)
 {
     trace(['Filename' => $Filename, 'Get' => $Get], 'Testing');
     $Filename = strtolower($Filename);
     array_change_key_case($Get);
     if (!isset(self::$Files[$Filename])) {
         return false;
     }
     $Row = self::$Files[$Filename];
     if (is_callable($Row)) {
         // Use a callback to determine the translation.
         $Row = call_user_func_array($Row, [&$Get]);
     }
     trace($Get, 'New Get');
     // Translate all of the get parameters into new parameters.
     $Vars = array();
     foreach ($Get as $Key => $Value) {
         if (!isset($Row[$Key])) {
             continue;
         }
         $Opts = (array) $Row[$Key];
         if (isset($Opts['Filter'])) {
             // Call the filter function to change the value.
             $R = call_user_func($Opts['Filter'], $Value, $Opts[0]);
             if (is_array($R)) {
                 if (isset($R[0])) {
                     // The filter can change the column name too.
                     $Opts[0] = $R[0];
                     $Value = $R[1];
                 } else {
                     // The filter can return return other variables too.
                     $Vars = array_merge($Vars, $R);
                     $Value = null;
                 }
             } else {
                 $Value = $R;
             }
         }
         if ($Value !== null) {
             $Vars[$Opts[0]] = $Value;
         }
     }
     trace($Vars, 'Translated Arguments');
     // Now let's see what kind of record we have.
     // We'll check the various primary keys in order of importance.
     $Result = false;
     if (isset($Vars['CommentID'])) {
         trace("Looking up comment {$Vars['CommentID']}.");
         $CommentModel = new CommentModel();
         // If a legacy slug is provided (assigned during a merge), attempt to lookup the comment using it
         if (isset($Get['legacy']) && Gdn::Structure()->Table('Comment')->ColumnExists('ForeignID')) {
             $Comment = $CommentModel->GetWhere(['ForeignID' => $Get['legacy'] . '-' . $Vars['CommentID']])->FirstRow();
         } else {
             $Comment = $CommentModel->GetID($Vars['CommentID']);
         }
         if ($Comment) {
             $Result = CommentUrl($Comment, '//');
         }
     } elseif (isset($Vars['DiscussionID'])) {
         trace("Looking up discussion {$Vars['DiscussionID']}.");
         $DiscussionModel = new DiscussionModel();
         $DiscussionID = $Vars['DiscussionID'];
         $Discussion = false;
         if (is_numeric($DiscussionID)) {
             // If a legacy slug is provided (assigned during a merge), attempt to lookup the discussion using it
             if (isset($Get['legacy']) && Gdn::Structure()->Table('Discussion')->ColumnExists('ForeignID')) {
                 $Discussion = $DiscussionModel->GetWhere(['ForeignID' => $Get['legacy'] . '-' . $DiscussionID])->FirstRow();
             } else {
                 $Discussion = $DiscussionModel->GetID($Vars['DiscussionID']);
             }
         } else {
             // This is a slug style discussion ID. Let's see if there is a UrlCode column in the discussion table.
             $DiscussionModel->DefineSchema();
             if ($DiscussionModel->Schema->FieldExists('Discussion', 'UrlCode')) {
                 $Discussion = $DiscussionModel->GetWhere(['UrlCode' => $DiscussionID])->FirstRow();
             }
         }
         if ($Discussion) {
             $Result = DiscussionUrl($Discussion, self::pageNumber($Vars, 'Vanilla.Comments.PerPage'), '//');
         }
     } elseif (isset($Vars['UserID'])) {
         trace("Looking up user {$Vars['UserID']}.");
         $User = Gdn::UserModel()->GetID($Vars['UserID']);
         if ($User) {
             $Result = Url(UserUrl($User), '//');
         }
     } elseif (isset($Vars['TagID'])) {
         $Tag = TagModel::instance()->GetID($Vars['TagID']);
         if ($Tag) {
             $Result = TagUrl($Tag, self::pageNumber($Vars, 'Vanilla.Discussions.PerPage'), '//');
         }
     } elseif (isset($Vars['CategoryID'])) {
         trace("Looking up category {$Vars['CategoryID']}.");
         // If a legacy slug is provided (assigned during a merge), attempt to lookup the category ID based on it
         if (isset($Get['legacy']) && Gdn::Structure()->Table('Category')->ColumnExists('ForeignID')) {
             $CategoryModel = new CategoryModel();
             $Category = $CategoryModel->GetWhere(['ForeignID' => $Get['legacy'] . '-' . $Vars['CategoryID']])->FirstRow();
         } else {
             $Category = CategoryModel::Categories($Vars['CategoryID']);
         }
         if ($Category) {
             $Result = categoryUrl($Category, self::pageNumber($Vars, 'Vanilla.Discussions.PerPage'), '//');
         }
     } elseif (isset($Vars['CategoryCode'])) {
         trace("Looking up category {$Vars['CategoryCode']}.");
         $category = CategoryModel::instance()->getByCode($Vars['CategoryCode']);
         if ($category) {
             $pageNumber = self::pageNumber($Vars, 'Vanilla.Discussions.PerPage');
             if ($pageNumber > 1) {
                 $pageParam = '?Page=' . $pageNumber;
             } else {
                 $pageParam = null;
             }
             $Result = categoryUrl($category, '', '//') . $pageParam;
         }
     }
     return $Result;
 }