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.
Options

Making a custom error if a condition is met before submitting a thread?

Hi!
I am totally new to Vanilla Forums and im moving from MyBB. I want to make a plugin (Which I have half done in MyBB) which allows you to set a bounty for your question which gets taken out your wallet (which is something else il be developing for it). What I need to do is, make sure the user has enough credits to set a bounty. So before the thread is submitted, I need to do a check to see if the user has enough credits to set that much bounty, if so, continue. If not, send the user back and tell them to set a lower bounty.

Im capible of doing all of that in PHP, I just need to know what I would need to do to stop a thread from being submitted.
Any ideas on what hook id need to use and how id stop the thread submission?

Comments

  • Options
    hgtonighthgtonight ∞ · New MVP

    Welcome to the community!

    You need to hook into the discussion model and run your validation code before discussion save.

    Something like this will get you started:

    public function DiscussionModel_BeforeSaveDiscussion_Handler($Sender) {
      // Validation code here
    
      // If validation fails, die with an error message
      die('Error message here');
    }
    

    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.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    If you have code that would make up a plugin for MyBB, you do not benefit from Garden, the framework that Vanilla is based on. I do not know how you are storing the credit info but if you have created a simple table UserID|CreditAmount you could benefit from the framework a lot.
    Simply add something like that to your code and you have the credit amount always at hand, because that info will be automagically available with the user object as $User->Credit:

    public function Setup() {
          $Structure->Table('User')
             ->Column('Credit', 'int', 0)
             ->Set(FALSE, FALSE);
    

    But maybe you already did it that way or your plugin needs a one-to-many relation between user and credit, then simply forget about it :)

    Nevertheless I would advice you to look at plugins that try to achieve similar things you want to achieve and learn from them. Maybe Karma is a good plugin to look at

  • Options
    x00x00 MVP
    edited February 2014

    @hgtonight said:
    Welcome to the community!

    You need to hook into the discussion model and run your validation code before discussion save.

    Something like this will get you started:

    public function DiscussionModel_BeforeSaveDiscussion_Handler($Sender) {
      // Validation code here
    
      // If validation fails, die with an error message
      die('Error message here');
    }
    

    Or

    public function DiscussionModel_BeforeSaveDiscussion_Handler($Sender, $Args) {
    
        $Bounty = GetValueR('FormPostValues.Bounty',$Args);
    
       // Format and validate Bounty
    
       // Check available funds
       if($this->AvailableCredits<$Bounty)
           $Sender->Validation->AddValidationResult('Bounty',T('YourPlugin.LackAvailableFunds','You don\'t have enough available funds to set this bounty '));
    }
    

    @Chizbang I take you are going to escrow the funds on save, otherwise checking whether there is funds coun be moot. Especially if someone tries to break it.

    You need a transactional system strictly speaking. So a bounty is held until that one discussion is posted, and no other discussion can be posted until either the transaction is canceled, or it is placed and the funds secure.

    Definitely worth reading up on transactional systems.

    Another way is to 'purchase' the bounty before hand. That is what I have done with purchase anonymous posts. It is a type of discussion that either available or not.

    grep is your friend.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    @x00 said:
    That is what I have done with purchase anonymous posts.

    Sounds like a funny feature! Which plugin offers something like that?

  • Options

    Thanks both! That should work swell.
    Il update you on how I get along.

  • Options
    hgtonighthgtonight ∞ · New MVP

    @x00 good call on $Sender->Validation->AddValidationResult();

    Looks like DiscussionPolls needs some polishing.

    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.

Sign In or Register to comment.