1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How do I add an input field in the Admin CP and create table

Discussion in 'XenForo Development Discussions' started by AndyB, Sep 30, 2013.

  1. AndyB

    AndyB Well-Known Member

    I'm creating an add-on and I would like to allow the admin to set the number of smilies allowed per post.

    I assume the steps to do this are:

    1) Create link in the Tools tab (Template Modification) which template?
    2) Create form for the input. Create a template.
    3) Add code to create a table in the database to store the value
    4) Add code read the value in the table created
    5) Modify error phrase so it indicates the maximum smilies allowed
    6) Create uninstall code to remove table
     
  2. Jeremy

    Jeremy XenForo Moderator Staff Member

    1. Links for the Admin Control Panel are added in the Development section.
     
  3. AndyB

    AndyB Well-Known Member

    That was easy. What goes into the Link: field?

    pic001.jpg
     
  4. Jeremy

    Jeremy XenForo Moderator Staff Member

    That's the link you want it to go to (route).
     
  5. AndyB

    AndyB Well-Known Member

  6. AndyB

    AndyB Well-Known Member

    Route Prefix created.

    pic001.jpg
     
    Last edited: Sep 30, 2013
  7. AndyB

    AndyB Well-Known Member

    library/Andy/SmilieCount/Route/Prefix/SmilieCount.php

    I have no idea if the code below is correct.

    PHP:
    <?php

    class Andy_SmilieCount_Route_Prefix_SmilieCount implements XenForo_Route_Interface
    {
       public function 
    match($routePathZend_Controller_Request_Http $requestXenForo_Router $router)
       {
         return 
    $router->getRouteMatch('Andy_SmilieCount_ControllerAdmin_Tools'$routePath);
       }
    }

    ?>
     
    Last edited: Sep 30, 2013
  8. AndyB

    AndyB Well-Known Member

    At this point when I click the Smilie Count link under the Tools tab it properly calls the below file. What is the correct responseRedirect code. What I have there now doesn't show the pretty jquery dropdown.

    library/Andy/ControllerAdmin/Tools.php

    PHP:
    <?php

    class Andy_SmilieCount_ControllerAdmin_Tools extends XenForo_ControllerAdmin_Tools
    {
        public function 
    actionIndex()
        {        
            return 
    $this->responseRedirect(
                
    XenForo_ControllerResponse_Redirect::SUCCESS,
                
    XenForo_Link::buildAdminLink('tools')
            );    
        }
    }

    ?>
     
    Last edited: Sep 30, 2013
  9. AndyB

    AndyB Well-Known Member

    How should I edit the code below so that it brings up the andy_smiliecount template?

    PHP:
    <?php

    class Andy_SmilieCount_ControllerAdmin_Tools extends XenForo_ControllerAdmin_Tools
    {
        public function 
    actionSmiliecount()
        {
        
            
    $post 'test';
            
    // display andy_smiliecount template
            
    $viewParams = array('post' => $post);
            return 
    $this->responseView('Andy_ViewPublic_Post','andy_smiliecount',$viewParams);
        }
        
        public function 
    actionSmiliecountsave()
        {        
            
            return 
    $this->responseRedirect(
                
    XenForo_ControllerResponse_Redirect::SUCCESS,
                
    XenForo_Link::buildAdminLink('tools')
            );
        }
    }

    ?>
     
    Last edited: Sep 30, 2013
  10. Jeremy

    Jeremy XenForo Moderator Staff Member

    actionIndex, not actionSmilieCount.
     
    AndyB likes this.
  11. AndyB

    AndyB Well-Known Member

    Thank you, Jeremy. Now the andy_smiliecount template is working!
     
  12. AndyB

    AndyB Well-Known Member

    My andy_smiliecount template

    Code:
    <xen:title>Smilie Count</xen:title>
    
    <form action="{xen:link 'smiliecount/smiliecount-save', $post}" method="post" class="xenForm AutoValidator" data-redirect="on">
    
    <fieldset>
    
    <dl class="ctrlUnit">
      <dt><label>Enter maximum smilie count per post:</label></dt>
      <dd><input type="text" name="new_smilie_count" size="10" value="{$post.formatted_date}" class="textCtrl titleCtrl"></dd>
    </dl>
    
    <dl class="ctrlUnit submitUnit">
      <dt></dt>
      <dd><input type="submit" value="Submit" class="button primary" accesskey="s" /></dd>
    </dl>
    </fieldset>
    
    <input type="hidden" name="_xfToken" value="{$visitor.csrf_token_page}" />
    </form>
    
     
  13. AndyB

    AndyB Well-Known Member

    When I click the Submit button, I get the following:

    The following error occurred:
    Route smiliecount/smiliecount-save could not be found.
     
  14. Jeremy

    Jeremy XenForo Moderator Staff Member

    I'd recommend changing it from smiliecount-save to be just save and update your action to be actionSave().
     
  15. AndyB

    AndyB Well-Known Member

    Unfortunately that didn't work.

    pic001.jpg
     
  16. AndyB

    AndyB Well-Known Member

    Got it!

    Needed to use xen:adminlink not xen:link in my andy_smiliecount template.
     
  17. AndyB

    AndyB Well-Known Member

    Tools.php

    PHP:
    <?php

    class Andy_SmilieCount_ControllerAdmin_Tools extends XenForo_ControllerAdmin_Tools
    {
       public function 
    actionIndex()
       {
         
         
    $post '5';
         
    // display andy_smiliecount template
         
    $viewParams = array('post' => $post);
         return 
    $this->responseView('Andy_ViewPublic_Post','andy_smiliecount',$viewParams);
       }
         
       public function 
    actionSave()
       {       
         
    // make sure data comes from $_POST
         
    $this->_assertPostOnly();
         
         
    // get new_post_date from overlay     
         
    $newSmilieCount $this->_input->filterSingle('new_smilie_count'XenForo_Input::STRING);
         
         
    //########################################
         // save $newSmilieCount
         
         
         
         
         //########################################
         // response redirect        
           
         
    return $this->responseRedirect(
           
    XenForo_ControllerResponse_Redirect::SUCCESS,
           
    XenForo_Link::buildAdminLink('tools')
         );   
       }
    }

    ?>
     
  18. AndyB

    AndyB Well-Known Member

    Next step is to save the variable $newSmilieCount in a table.
     
    Last edited: Sep 30, 2013
  19. AndyB

    AndyB Well-Known Member

    I see in the main add-on screen there are two fields that deal with installation and uninstallation.

    pic001.jpg
     
  20. AndyB

    AndyB Well-Known Member

    My current directory and files:

    library
    --Andy
    ----SmilieCount
    ------ControllerAdmin
    --------Tools.php
    ------ControllerPublic
    --------Forum
    --------Post
    --------Thread
    ------Listener.php
    ------Model
    --------Smilie.php
    ------Route
    --------Prefix
    ----------SmilieCount
     

Share This Page