Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Are these events missing from Vanilla 2?

derekdonderekdon New
edited January 2010 in Vanilla 2.0 - 2.8
I'm trying to update the PostAnonymously plugin to work with Vanilla 2.

In vanilla 2 are there replacement events for PreSaveDiscussion, PreSaveComment, PostSaveDiscussion, PostSaveComment?

I need to temporarily change the UserID prior to saving the discussion/comment data should the user be posting anonymously, and revert it back after the save has completed. I have the extra Post Anonymously checkbox in the form via using a Base_BeforeFormButtons_Handler in my version of this plugin, but I just can't seem to find the right hook to do the ID switch. Do I need to override the PostController's Discussion and Comment methods, or their Models methods entirely?

Cheers,

Derek.

Comments

  • MarkMark Vanilla Staff
    It's entirely possible that there aren't events like that in Vanilla 2 yet. If you want to add events to the core, you can either tell us where you want them, or you can make the changes yourself and send us a pull request on GitHub.

    It's also worth noting that there might be an easier way to accomplish this task with Vanilla 2. I don't know the ins & outs of your particular situation, but generally speaking, programming plugins should be a lot easier with Vanilla 2 (and take less code).
  • Thanks Mark.

    I got the hook working in the end by adding two new events to the core. There might be an easier way to do it but I'm still getting to grips with the system.

    In class.discussionmodel.php

    public function Save($FormPostValues, $CommentModel)
    {
    //Added the following five lines
    $AnonymousPost = ArrayValue('AnonymousPost', $FormPostValues);
    $AnonymousPost = is_numeric($AnonymousPost) && $AnonymousPost > -1 && $AnonymousPost < 2 ? $AnonymousPost : 0;
    $this->EventArguments['AnonymousPost'] = $AnonymousPost;
    $this->FireEvent('BeforeSaveDiscussion'); // Plugin hook

    $Session = Gdn::Session();
    ...

    In class.commentmodel.php

    public function Save($FormPostValues)
    {
    //Added the following five lines
    $AnonymousPost = ArrayValue('AnonymousPost', $FormPostValues);
    $AnonymousPost = is_numeric($AnonymousPost) && $AnonymousPost > -1 && $AnonymousPost < 2 ? $AnonymousPost : 0;
    $this->EventArguments['AnonymousPost'] = $AnonymousPost;
    $this->FireEvent('BeforeSaveComment'); // Plugin hook

    $Session = Gdn::Session();
    ...

    I may be able to just fire the event and read the form post values from within the handler method of the plugin, but I'm not sure how to read the form values yet or if they are automatically passed with the event. Do you know?

    My before handler method looks like this.

    public function Gdn_DiscussionModel_BeforeSaveDiscussion_Handler(&$CommentModel, $Event)
    {
    $this->beforeSave($CommentModel, $Event);
    }

    public function beforeSave($CommentModel, $Event)
    {
    $postAnonymously = $Event['AnonymousPost'];
    if($postAnonymously == 1)
    {
    // Get session and user data
    $Session = Gdn::Session();
    $UserModel = Gdn::UserModel();
    $User = $UserModel->GetWhere(array('UserID' => $Session->UserID))->FirstRow();

    // Temporarily set the userID for when post is saved
    $AnonymousUser = $UserModel->GetWhere(array('Name' => ($User->Gender == 'm') ? $this->AnonymousMale : $this->AnonymousFemale))->FirstRow();
    $Session->UserID = $AnonymousUser->UserID;
    }
    }

    Thanks for your help.

    Derek.
  • Mark I've scrapped this attempt at reviving the old plugin as it won’t do what I need it to and it’s a bit messy. I’m going to build something else from scratch. Cheers.
Sign In or Register to comment.