HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Unsupported comments?

Useful, thank you for sharing but comments 're not supported, isn't? then no easy way to change the author of a comment?

Tagged:

Comments

  • hgtonighthgtonight ∞ · New Moderator

    No easy way; but there is a way. Fork it and add the necessary code!

    public function base_commentOptions_handler($sender, $args) {
        $comment = $args['Comment'];
        if (CheckPermission('Garden.Settings.Manage')) {
            $Label = T('Change Author');
            $Url = "/discussion/commentauthor/{$comment->CommentID}";
            // Deal with inconsistencies in how options are passed
            if (isset($sender->Options)) {
                $sender->Options .= Wrap(Anchor($Label, $Url, 'ChangeAuthor'), 'li');
            }
            else {
                $args['CommentOptions']['ChangeAuthor'] = array(
                    'Label' => $Label,
                    'Url' => $Url,
                    'Class' => 'ChangeAuthor'
               );
            }
        }
    }
    
    public function discussionController_commentAuthor_create($sender, $commentID) {
        $sender->Permission('Garden.Settings.Manage');
    
        $comment = $sender->CommentModel->GetID($commentID);
        if (!$comment) {
            throw NotFoundException('Comment');
        }
    
        if ($sender->Form->AuthenticatedPostBack()) {
            // Change the author
            $Name = $sender->Form->GetFormValue('Author', '');
            $UserModel = new UserModel();
            if (trim($Name) != '') {
                $User = $UserModel->GetByUsername(trim($Name));
                if (is_object($User)) {
                    if ($comment->InsertUserID == $User->UserID) {
                        $sender->Form->AddError('That user is already the comment author.');
                    }
                    else {
                        // Change comment InsertUserID
                        $sender->CommentModel->SetField($commentID, 'InsertUserID', $User->UserID);
                        // Update users' comment counts
                        $sender->CommentModel->updateUserCommentCounts($comment->DiscussionID);
                        $sender->CommentModel->updateUser($comment->InsertUserID);
                        $sender->CommentModel->updateUser($User->UserID);
                        // Go to the updated comment
                        Redirect(commentUrl($comment));
                    }
                }
            }
            else {
                $sender->Form->AddError('No user with that name was found.');
            }
        }
        else {
           // Form to change the author
           $sender->SetData('Title', 'Change Comment Author');
        }
    
        $sender->Render('changeauthor', '', 'plugins/AuthorSelector');
    }
    

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • R_JR_J Ex-Fanboy Munich Admin

    lol

    I was even thinking about a pull request:

        public function discussionController_commentOptions_handler($sender, $args) {
            if (
                !Gdn::session()->checkPermission(
                    'Vanilla.Discussions.Edit',
                    true,
                    'Category',
                    $args['Discussion']->PermissionCategoryID
                )
            ) {
                return;
            }
            $args['CommentOptions']['ChangeAuthor'] = [
                'Label' => t('Change Author'),
                'Url' => '/discussion/author?commentid='.$args['Comment']->CommentID,
                'Class' => 'ChangeAuthor'
            ];
        }
    
       /**
        * Handle discussion option menu Change Author action.
        */
        public function discussionController_author_create($sender, $args) {
            $requestArguments = $sender->Request->getRequestArguments('get');
            $recordType = 'discussion';
    
            $discussionID = val('discussionid', $requestArguments, '');
            if ($discussionID === '') {
                $commentID = val('commentid', $requestArguments, '');
                $comment = $sender->CommentModel->getID($commentID);
                if (!$comment) {
                    throw NotFoundException('Comment');
                }
                $discussionID = $comment->DiscussionID;
                $recordType = 'comment';
            }
            $discussion = $sender->DiscussionModel->getID($discussionID);
            if (!$discussion) {
                throw notFoundException('Discussion');
            }
    
            // Check edit permission
            $sender->permission(
                'Vanilla.Discussions.Edit',
                true,
                'Category',
                $discussion->PermissionCategoryID
            );
            do {
                if (!$sender->Form->authenticatedPostBack()) {
                    break;
                }
                // Change the author
                $user = Gdn::userModel()->getByUsername(
                    trim($sender->Form->getFormValue('Author', ''))
                );
                if (!is_object($user)) {
                    $sender->Form->addError('No user with that name was found.');
                    break;
                }
    
                if (${"$recordType"}->InsertUserID == $user->UserID) {
                    $sender->Form->addError(
                        'That user is already the '.$recordType.' author.'
                    );
                    break;
                }
    
                switch ($recordType) {
                    case 'comment':
                        $sender->CommentModel->setField($commentID, 'InsertUserID', $user->UserID);
                        // Update users' comment counts
                        $sender->CommentModel->updateUser($comment->InsertUserID);
                        $sender->CommentModel->updateUser($user->UserID, true); // Increment
                        redirect(discussionUrl($discussion).'#Comment_'.$commentID);
                        break;
                    case 'discussion':
                        $sender->DiscussionModel->setField($discussionID, 'InsertUserID', $user->UserID);
                        // Update users' discussion counts
                        $sender->DiscussionModel->UpdateUserDiscussionCount($discussion->InsertUserID);
                        $sender->DiscussionModel->UpdateUserDiscussionCount($user->UserID, true); // Increment
                        redirect(discussionUrl($discussion));
                        break;
                    default:
                        break;
                }
            } while (false);
    
            // Form to change the author
            $sender->setData('Title', $discussion->Name);
            $sender->render('changeauthor', '', 'plugins/AuthorSelector');
        }
    

    But I've seen that I forgot updating discussions comment counts.

    And trying my code on my test forum with enabled cache, gives me strange behavior with the comment count. CommentModel->updateUser doesn't update the cached user, while the DiscussionModel does. Don't know if this can be considered an issue or not. @Linc: what is your opinion on that?

  • LincLinc Detroit Admin

    In a blog-like setup, the person creating a discussion might actually be someone like an editor, which is the scenario I envisioned for this addon. I don't understand the use case of changing a comment's author.

  • R_JR_J Ex-Fanboy Munich Admin

    Sorry, I could have been more clearer:
    discussionModel->updateUserDiscussionCount() makes a call to userModel->updateUserCache while commentModel->updateUser does not. Is that worth making a pull request?

  • hgtonighthgtonight ∞ · New Moderator
    edited June 2016

    @Linc said:
    I don't understand the use case of changing a comment's author.

    Me either.

    @R_J said:
    Sorry, I could have been more clearer:
    discussionModel->updateUserDiscussionCount() makes a call to userModel->updateUserCache while commentModel->updateUser does not.

    insightful

    Is that worth making a pull request?

    Always.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • PamelaPamela ✭✭

    Great reply ;-) many thanks @hgtonight and @R_J then I 'll try it A.S.A.P.

  • PamelaPamela ✭✭

    @Linc said:
    I don't understand the use case of changing a comment's author.

    Thank you @Linc to take time to reply... We (graphical & musical artists collective) 're using Vanilla as a classic forums board, so I would like change personal name of one of us to our crew name for example ;-) if it's understandable in English...

  • LincLinc Detroit Admin

    Ah, so maybe more of a group account, where folks can do a "Post As..." option and change to the collective before posting, a la Facebook page management. That feature makes sense. Adding the retroactive switching to this plugin makes less sense.

Sign In or Register to comment.