Difference between revisions of "API/Grid"

From aMember Pro Documentation
Jump to: navigation, search
 
Line 3: Line 3:
 
<source>
 
<source>
 
<?php
 
<?php
class AdminSampleBanController extends Am_Controller
+
class AdminSampleBanController extends Am_Mvc_Controller
 
{
 
{
 
     public function checkAdminPermissions(Admin $admin)
 
     public function checkAdminPermissions(Admin $admin)
Line 9: Line 9:
 
         return $admin->isSuper();
 
         return $admin->isSuper();
 
     }
 
     }
 +
 
     function createGrid()
 
     function createGrid()
 
     {
 
     {
Line 23: Line 24:
 
         return $g;
 
         return $g;
 
     }
 
     }
 +
 
     public function createForm(Am_Grid_Editable $grid)
 
     public function createForm(Am_Grid_Editable $grid)
 
     {
 
     {
Line 36: Line 38:
 
         return $form;
 
         return $form;
 
     }
 
     }
 +
 
     public function indexAction()
 
     public function indexAction()
 
     {
 
     {

Latest revision as of 07:58, 7 May 2016

The grid component we developed from scratch provides very easy way to deal with table records - editing and displaying. There is a simple example of usage. You can save it to file AdminSampleBanController.php and copy to am/application/default/controllers/ , then access as http://example.com/am/admin-sample-ban

<?php
class AdminSampleBanController extends Am_Mvc_Controller
{
    public function checkAdminPermissions(Admin $admin)
    {
        return $admin->isSuper();
    }
 
    function createGrid()
    {
        // create query to database table
        $ds = new Am_Query($this->getDi()->banTable); // inside controllers, plugins and tables, Am_Di instance is available with this method call: $this->getDi()
        // create grid with id, title, query, pass request and view
        $g = new Am_Grid_Editable('_ban', "Ban Records", $ds, $this->_request, $this->view);
        // set form creation callback for edit/add forms in grid
        $g->setForm(array($this, 'createForm'));
        // add fields to grid. @see Am_Grid_Field
        $g->addGridField("type", "Type");
        $g->addGridField("value", "Locked Value");
        $g->addGridField("comment", "Comment");
        return $g;
    }
 
    public function createForm(Am_Grid_Editable $grid)
    {
        // create admin-side form
        $form = new Am_Form_Admin;
        // add selection box with 2 options
        $form->addSelect("type")->setLabel(array("Type")->loadOptions(array('ip'=>'IP', 'email'=>'E-Mail',));
        // add text field for "value" field of ban table
        $form->addText("value", array('size' => 40))->setLabel(array("Value", "You can use % as wildcard mask"));
        // add text field for "comment" field of ban table
        $form->addText('comment', array('size' => 40))->setLabel("Comment");
        // buttons will be added by grid
        return $form;
    }
 
    public function indexAction()
    {
        // now just run what we created before, and put grid output to response object of controller
        $this->createGrid()->run($this->getResponse());
    }
}