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

Modules and AJAX

I like to update a modules content with AJAX. So I started by creating the module and gave some arguments to the modules ToString() function. Let me give you an easy example: say I just want to have a colored PanelBox and the color should be variable.

public function ToString($Color) {
    echo ' < div id="ColorExampleModule" class="Box ColorExampleBox '.$Color.'">';
        echo '< a href"#" class="Button HelloKittyPink">< /a>';
        echo '< a href"#" class="Button SpongeBobYellow">< /a>';
    echo '< /div>';
    ...
}

1.) How would my plugin have to call the module so that it displays a given color (maybe 'DepressiveViolett')

$ColorExampleModule = new ColorExampleModule ($Sender);
$Sender->AddModule($ColorExampleModule); //no place for module arguments here :-/

Do I need a constructor for that? Like new ColorExampleModule ($Sender, $Color) and then public function __construct($Sender, $Color) {$this->Color = $Color}?

2.) How could that be called from JavaScript? Would I have to do something like the below to get my ColorExampleModule::ToString($Solor) output from /myforum/plugin/colorexample/hellokittypink or is there an easier/more direct way?

public function PluginController_ColorExample_Create($Sender, $Args) {
    $Color = $Args[0];
    $ColorExampleModule = new ColorExampleModule ($Sender);

    return $ColorExampleModule->ToString($Color);
}

Best Answers

  • Options
    hgtonighthgtonight ∞ · New Moderator
    Answer ✓

    Create public members/methods on your module that sets whatever params you want.

    Create a controller that accepts the params you want and creates the module and returns the rendered module.

    Request the controller method as appropriate via JS.

    Sample module code:

    private $Color;
    public $Number;
    
    public function SetData($col) {
      $this->Color = $col;
    }
    
    public function ToString() {
      echo ' < div id="ColorExampleModule" class="Box ColorExampleBox '.$this->Color.'">';
      echo '< a href"#" class="Button HelloKittyPink">< /a>';
      echo '< a href"#" class="Button SpongeBobYellow">< /a>';
      echo $this->Number;
      echo '< /div>';
    }
    

    Sample hook:

    public function PluginController_ColorExample_Create($Sender, $Args) {
      $Color = $Args[0];
      $ColorExampleModule = new ColorExampleModule ($Sender);
    
      // Use a public method to set the data
      $ColorExampleModule->SetData($Color);
    
      // Set a public member
      $ColorExampleModule->Number = 12;
    
      return $ColorExampleModule->ToString();
    }
    

    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.

Answers

  • Options
    hgtonighthgtonight ∞ · New Moderator
    Answer ✓

    Create public members/methods on your module that sets whatever params you want.

    Create a controller that accepts the params you want and creates the module and returns the rendered module.

    Request the controller method as appropriate via JS.

    Sample module code:

    private $Color;
    public $Number;
    
    public function SetData($col) {
      $this->Color = $col;
    }
    
    public function ToString() {
      echo ' < div id="ColorExampleModule" class="Box ColorExampleBox '.$this->Color.'">';
      echo '< a href"#" class="Button HelloKittyPink">< /a>';
      echo '< a href"#" class="Button SpongeBobYellow">< /a>';
      echo $this->Number;
      echo '< /div>';
    }
    

    Sample hook:

    public function PluginController_ColorExample_Create($Sender, $Args) {
      $Color = $Args[0];
      $ColorExampleModule = new ColorExampleModule ($Sender);
    
      // Use a public method to set the data
      $ColorExampleModule->SetData($Color);
    
      // Set a public member
      $ColorExampleModule->Number = 12;
    
      return $ColorExampleModule->ToString();
    }
    

    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

    Before I've posted my question I had taken a look at your LatestPostList but didn't quite understood what you have done. Now I do :)

  • Options
    hgtonighthgtonight ∞ · New Moderator

    Ah... LatestPostList, the plugin that started my love affair with Vanilla in the Garden.

    Sweet, sweet memories.

    P.S. It goes without saying that you should validate the user input before accepting it willy-nilly like I did in the example.

    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

    My real function only takes integers and boolean, so sanitizing the user input is really easy :)

    I would prefere using __construct($Sender, $Args = array('Number' => 13)) {$this->Number = (int)$Args['Number']}. After creating my module with $ColorExampleModule = new ColorExampleModule ($Sender, array('Number' => 5));I should be able to access my $ColorExampleModule->Number anywhere I want to.

    Do you see any disadvantages to your solution?

  • Options
    businessdadbusinessdad Stealth contributor MVP

    @hgtonight said:
    Ah... LatestPostList, the plugin that started my love affair with Vanilla in the Garden.

    And the source of inspiration for Hot Threads too! Another point for Griffin-hgtonight!

Sign In or Register to comment.