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

Help with new 'DiscussionWordMention' custom rule

2»

Comments

  • Options
    peregrineperegrine MVP
    edited December 2014

    I just want to note that you shouldn't have to include the file directly. Assuming you know the class name, and it's file matches the auto loader requirements, you can just instantiate it in that class since the plugin folder is automatically added to the search path.

    examples always help with theory :wink:

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options
    hgtonighthgtonight ∞ · New Moderator

    @peregrine said:
    examples always help with theory :wink:

    public function Yaga_AfterGetRules_Handler($Sender) {
      $Rule = new MySweetRule();
      $Sender->EventArguments['Rules'][get_class($Rule)] = $Rule->Name();
    }
    

    Or even just

    public function Yaga_AfterGetRules_Handler($Sender) {
      $Sender->EventArguments['Rules']['MySweetRule'] = 'My Awesome Rule Name';
    }
    

    If you don't mind updating it outside of the class.

    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
    peregrineperegrine MVP
    edited December 2014

    still guessing. haven't tried it

    do you mean like this.

    • option 1

    and you just put your class rule file in the rules folder of your plugin?

    A - e.g. just copy the file of class.discussionwordmention.php

     to
    
    
     plugins/YagaRuleAddition/rules/class.discussionwordmention.php
    

    in

    B - so plugins/YagaRuleAddition/default.php would be

    <?php if (!defined('APPLICATION')) exit();
    
    
    // Define the plugin:
    $PluginInfo['Yaga Rule Addition'] = array(
       'Name' => 'Yaga Rule Addition',
       'Description' => 'Add rules to Yaga Application',
       'Version' => '1.0',
       'Author' => "Yaga",
       'MobileFriendly' => TRUE,
    );
    
    
    class YagaRuleAdditionPlugin extends Gdn_Plugin {
    
    
        public function Yaga_AfterGetRules_Handler($Sender) {
        $Rule = new DiscussionWordMention()
        $Sender->EventArguments['Rules'][get_class($Rule)] = $Rule->Name();
        }
    }
    

    or could you also do

    • option 2

      <?php if (!defined('APPLICATION')) exit();
      
      
      // Define the plugin:
      $PluginInfo['Yaga Rule Addition'] = array(
         'Name' => 'Yaga Rule Addition',
         'Description' => 'Add rules to Yaga Application',
         'Version' => '1.0',
         'Author' => "Yaga",
         'MobileFriendly' => TRUE,
      );
      
      
      class YagaRuleAdditionPlugin extends Gdn_Plugin {
      
      
          public function Yaga_AfterGetRules_Handler($Sender) {
          $Rule = new DiscussionWordMention()
          $Sender->EventArguments['Rules'][get_class($Rule)] = $Rule->Name();
          }
      }
      
      
          class DiscussionWordMention implements YagaRule{
          public function Award($Sender, $User, $Criteria) {
          $PostInfo = $Sender->EventArguments['FormPostValues'];
          $PBodyText = val('Body',$PostInfo);
          $PUserID = val('InsertUserID',$PostInfo);
          if (strpos(strtolower($PBodyText), strtolower($Criteria->WordToMatch)) !== FALSE) {
          return $PUserID;
          }
          return FALSE;
          }
          public function Form($Form) {
          $String = $Form->Label('Yaga.Rules.DiscussionWordMention.Criteria.Head', 'DiscussionWordMention');
          $String .= $Form->Textbox('WordToMatch', array('class' => 'WideInput'));
          return $String;
          }
          public function Validate($Criteria, $Form) {
          $Validation = new Gdn_Validation();
          $Validation->ApplyRules(array(
          array(
          'Name' => 'WordToMatch', 'Validation' => array('Required')
          )
          ));
          $Validation->Validate($Criteria);
          $Form->SetValidationResults($Validation->Results());
          }
          public function Hooks() {
          return array('CommentModel_AfterSaveComment', 'DiscussionModel_AfterSaveDiscussion');
          }
          public function Description() {
          $Description = sprintf(T('Yaga.Rules.DiscussionWordMention.Desc'), C('Vanilla.Comment.MaxLength'));
          return Wrap($Description, 'div', array('class' => 'InfoMessage'));
          }
          public function Name() {
          return T('Yaga.Rules.DiscussionWordMention');
          }
          public function Interacts() {
          return FALSE;
          }
          }
      

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options
    hgtonighthgtonight ∞ · New Moderator

    I was referring to option 1. You can put the rule files in any folder starting at the root of the plugin and the auto loader will pick it up.

    I definitely suggest keeping your folders tidy as you did in your example :smile:

    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

    Hi gents ! Since 2.2, "discussionwordmention" makes yaga crash when creating a new badge and existing rules don't work anymore :/ ...

    I'll try to fix it, but maybe you'll find a fix faster than me !

Sign In or Register to comment.