/** * * @param DiscussionController $Sender * @param array $Args */ public function DiscussionController_QnA_Create($Sender, $Args = array()) { $Comment = Gdn::SQL()->GetWhere('Comment', array('CommentID' => $Sender->Request->Get('commentid')))->FirstRow(DATASET_TYPE_ARRAY); if (!$Comment) { throw NotFoundException('Comment'); } $Discussion = Gdn::SQL()->GetWhere('Discussion', array('DiscussionID' => $Comment['DiscussionID']))->FirstRow(DATASET_TYPE_ARRAY); // Check for permission. if (!(Gdn::Session()->UserID == GetValue('InsertUserID', $Discussion) || Gdn::Session()->CheckPermission('Garden.Moderation.Manage'))) { throw PermissionException('Garden.Moderation.Manage'); } if (!Gdn::Session()->ValidateTransientKey($Sender->Request->Get('tkey'))) { throw PermissionException(); } switch ($Args[0]) { case 'accept': $QnA = 'Accepted'; break; case 'reject': $QnA = 'Rejected'; break; } if (isset($QnA)) { $DiscussionSet = array('QnA' => $QnA); $CommentSet = array('QnA' => $QnA); if ($QnA == 'Accepted') { $CommentSet['DateAccepted'] = Gdn_Format::ToDateTime(); $CommentSet['AcceptedUserID'] = Gdn::Session()->UserID; if (!$Discussion['DateAccepted']) { $DiscussionSet['DateAccepted'] = Gdn_Format::ToDateTime(); $DiscussionSet['DateOfAnswer'] = $Comment['DateInserted']; } } // Update the comment. Gdn::SQL()->Put('Comment', $CommentSet, array('CommentID' => $Comment['CommentID'])); // Update the discussion. if ($Discussion['QnA'] != $QnA && (!$Discussion['QnA'] || in_array($Discussion['QnA'], array('Unanswered', 'Answered', 'Rejected')))) { Gdn::SQL()->Put('Discussion', $DiscussionSet, array('DiscussionID' => $Comment['DiscussionID'])); } // Determine QnA change if ($Comment['QnA'] != $QnA) { $Change = 0; switch ($QnA) { case 'Rejected': $Change = -1; if ($Comment['QnA'] != 'Accepted') { $Change = 0; } break; case 'Accepted': $Change = 1; break; default: if ($Comment['QnA'] == 'Rejected') { $Change = 0; } if ($Comment['QnA'] == 'Accepted') { $Change = -1; } break; } } // Apply change effects if ($Change) { // Update the user $UserID = GetValue('InsertUserID', $Comment); $this->RecalculateUserQnA($UserID); // Update reactions if ($this->Reactions) { include_once Gdn::Controller()->FetchViewLocation('reaction_functions', '', 'plugins/Reactions'); $Rm = new ReactionModel(); // If there's change, reactions will take care of it $Rm->React('Comment', $Comment['CommentID'], 'AcceptAnswer'); } } // Record the activity. if ($QnA == 'Accepted') { $Activity = array('ActivityType' => 'AnswerAccepted', 'NotifyUserID' => $Comment['InsertUserID'], 'HeadlineFormat' => '{ActivityUserID,You} accepted {NotifyUserID,your} answer.', 'RecordType' => 'Comment', 'RecordID' => $Comment['CommentID'], 'Route' => CommentUrl($Comment, '/'), 'Emailed' => ActivityModel::SENT_PENDING, 'Notified' => ActivityModel::SENT_PENDING); $ActivityModel = new ActivityModel(); $ActivityModel->Save($Activity); } } Redirect("/discussion/comment/{$Comment['CommentID']}#Comment_{$Comment['CommentID']}"); }
/** * Join external records to an array. * * @param array &$Data The data to join. * In order to join records each row must have the a RecordType and RecordID column. * @param string $Column The name of the column to put the record in. * If this is blank then the record will be merged into the row. * @param bool $Unset Whether or not to unset rows that don't have a record. * @since 2.3 */ function joinRecords(&$Data, $Column = '', $Unset = false) { $IDs = array(); $AllowedCats = DiscussionModel::CategoryPermissions(); if ($AllowedCats === false) { // This user does not have permission to view anything. $Data = array(); return; } // Gather all of the ids to fetch. foreach ($Data as &$Row) { if (!$Row['RecordType']) { continue; } $RecordType = ucfirst(StringEndsWith($Row['RecordType'], '-Total', true, true)); $Row['RecordType'] = $RecordType; $ID = $Row['RecordID']; $IDs[$RecordType][$ID] = $ID; } // Fetch all of the data in turn. $JoinData = array(); foreach ($IDs as $RecordType => $RecordIDs) { if ($RecordType == 'Comment') { Gdn::SQL()->Select('d.Name, d.CategoryID')->Join('Discussion d', 'd.DiscussionID = r.DiscussionID'); } $Rows = Gdn::SQL()->Select('r.*')->WhereIn($RecordType . 'ID', array_values($RecordIDs))->Get($RecordType . ' r')->ResultArray(); $JoinData[$RecordType] = Gdn_DataSet::Index($Rows, array($RecordType . 'ID')); } // Join the rows. $Unsets = array(); foreach ($Data as $Index => &$Row) { $RecordType = $Row['RecordType']; $ID = $Row['RecordID']; if (!isset($JoinData[$RecordType][$ID])) { if ($Unset) { $Unsets[] = $Index; } continue; // orphaned? } $Record = $JoinData[$RecordType][$ID]; if ($AllowedCats !== true) { // Check to see if the user has permission to view this record. $CategoryID = GetValue('CategoryID', $Record, -1); if (!in_array($CategoryID, $AllowedCats)) { $Unsets[] = $Index; continue; } } switch ($RecordType) { case 'Discussion': $Url = DiscussionUrl($Record, '', '/') . '#latest'; break; case 'Comment': $Url = CommentUrl($Record, '/'); $Record['Name'] = sprintf(T('Re: %s'), $Record['Name']); break; default: $Url = ''; } $Record['Url'] = $Url; if ($Column) { $Row[$Column] = $Record; } else { $Row = array_merge($Row, $Record); } } foreach ($Unsets as $Index) { unset($Data[$Index]); } // Join the users. Gdn::UserModel()->JoinUsers($Data, array('InsertUserID')); if (!empty($Unsets)) { $Data = array_values($Data); } }
exit; } /* Copyright 2014 Zachary Doll */ $Contents = $this->_Content; echo '<ul class="DataList Compact BlogList">'; foreach ($Contents as $Content) { static $UserPhotoFirst = NULL; if ($UserPhotoFirst === NULL) { $UserPhotoFirst = C('Vanilla.Comment.UserPhotoFirst', TRUE); } $ContentType = GetValue('ItemType', $Content); $ContentID = GetValue("{$ContentType}ID", $Content); $Author = GetValue('Author', $Content); switch (strtolower($ContentType)) { case 'comment': $ContentURL = CommentUrl($Content); break; case 'discussion': $ContentURL = DiscussionUrl($Content); break; } ?> <li id="<?php echo "{$ContentType}_{$ContentID}"; ?> " class="Item"> <h3><?php echo Anchor(Gdn_Format::Text($Content['Name'], FALSE), $ContentURL); ?> </h3> <div class="Item-Header">
function WritePromotedContent($Content, $Sender) { static $UserPhotoFirst = NULL; if ($UserPhotoFirst === NULL) { $UserPhotoFirst = C('Vanilla.Comment.UserPhotoFirst', TRUE); } $ContentType = GetValue('ItemType', $Content); $ContentID = GetValue("{$ContentType}ID", $Content); $Author = GetValue('Author', $Content); switch (strtolower($ContentType)) { case 'comment': $ContentURL = CommentUrl($Content); break; case 'discussion': $ContentURL = DiscussionUrl($Content); break; } ?> <div id="<?php echo "Promoted_{$ContentType}_{$ContentID}"; ?> " class="<?php echo CssClass($Content); ?> "> <div class="AuthorWrap"> <span class="Author"> <?php if ($UserPhotoFirst) { echo UserPhoto($Author); echo UserAnchor($Author, 'Username'); } else { echo UserAnchor($Author, 'Username'); echo UserPhoto($Author); } $Sender->FireEvent('AuthorPhoto'); ?> </span> <span class="AuthorInfo"> <?php echo ' ' . WrapIf(htmlspecialchars(GetValue('Title', $Author)), 'span', array('class' => 'MItem AuthorTitle')); echo ' ' . WrapIf(htmlspecialchars(GetValue('Location', $Author)), 'span', array('class' => 'MItem AuthorLocation')); $Sender->FireEvent('AuthorInfo'); ?> </span> </div> <div class="Meta CommentMeta CommentInfo"> <span class="MItem DateCreated"> <?php echo Anchor(Gdn_Format::Date($Content['DateInserted'], 'html'), $Permalink, 'Permalink', array('rel' => 'nofollow')); ?> </span> <?php // Include source if one was set if ($Source = GetValue('Source', $Content)) { echo Wrap(sprintf(T('via %s'), T($Source . ' Source', $Source)), 'span', array('class' => 'MItem Source')); } $Sender->FireEvent('ContentInfo'); ?> </div> <div class="Title"><?php echo Anchor(Gdn_Format::Text($Content['Name'], FALSE), $ContentURL, 'DiscussionLink'); ?> </div> <div class="Body"><?php echo Anchor(strip_tags(Gdn_Format::To($Content['Body'], $Content['Format'])), $ContentURL, 'BodyLink'); ?> </div> </div> <?php }
/** * Pre-process content into a uniform format for output * * @param Array $content By reference */ protected function processContent(&$content) { foreach ($content as &$item) { $contentType = val('RecordType', $item); $userID = val('InsertUserID', $item); $itemProperties = array(); $itemFields = array('DiscussionID', 'DateInserted', 'DateUpdated', 'Body', 'Format', 'RecordType', 'Url', 'CategoryID', 'CategoryName', 'CategoryUrl'); switch (strtolower($contentType)) { case 'comment': $itemFields = array_merge($itemFields, array('CommentID')); // Comment specific $itemProperties['Name'] = sprintf(t('Re: %s'), valr('Discussion.Name', $item, val('Name', $item))); $url = CommentUrl($item); break; case 'discussion': $itemFields = array_merge($itemFields, array('Name', 'Type')); $url = DiscussionUrl($item); break; } $item['Url'] = $url; if ($categoryId = val('CategoryID', $item)) { $category = CategoryModel::categories($categoryId); $item['CategoryName'] = val('Name', $category); $item['CategoryUrl'] = CategoryUrl($category); } $itemFields = array_fill_keys($itemFields, true); $filteredItem = array_intersect_key($item, $itemFields); $itemProperties = array_merge($itemProperties, $filteredItem); $item = $itemProperties; // Attach User $userFields = array('UserID', 'Name', 'Title', 'Location', 'PhotoUrl', 'RankName', 'Url', 'Roles', 'RoleNames'); $user = Gdn::userModel()->getID($userID); $roleModel = new RoleModel(); $roles = $roleModel->GetByUserID($userID)->resultArray(); $roleNames = ''; foreach ($roles as $role) { $roleNames[] = val('Name', $role); } // check $rankName = null; if (class_exists('RankModel')) { $rankName = val('Name', RankModel::Ranks(val('RankID', $user)), null); } $userProperties = array('Url' => url(userUrl($user), true), 'PhotoUrl' => UserPhotoUrl($user), 'RankName' => $rankName, 'RoleNames' => $roleNames, 'CssClass' => val('_CssClass', $user)); $user = (array) $user; $userFields = array_fill_keys($userFields, true); $filteredUser = array_intersect_key($user, $userFields); $userProperties = array_merge($filteredUser, $userProperties); $item['Author'] = $userProperties; } }
/** * * * @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; }