Difference between revisions of "API/Samples"

From aMember Pro Documentation
Jump to: navigation, search
(Created page with "Create a simple plugin and run a sample query (note the plugin name matches filename) <i>application/default/plugins/misc/<b>test</b>.php</i> <source lang="php"> class Am_Plugin...")
 
Line 50: Line 50:
 
class AdminPluginTestController extends Am_Controller // a Zend_Controller_Action successor
 
class AdminPluginTestController extends Am_Controller // a Zend_Controller_Action successor
 
{
 
{
 +
    // validate if given admin have access to this page, return false if it does not
 +
    public function checkAdminPermissions(Admin $admin)
 +
    {
 +
        return $admin->isSuper();
 +
    }
 
       function indexAction()
 
       function indexAction()
 
       {
 
       {

Revision as of 07:22, 28 February 2012

Create a simple plugin and run a sample query (note the plugin name matches filename)

application/default/plugins/misc/test.php

class Am_Plugin_Test extends Am_Plugin
{
    function init()
    {
          $this->getDi()->db->selectRow("SELECT * FROM ?_user LIMIT 1"); // look at DbSimple docs
     }
    function onInvoiceStarted(Am_Event $event) // catches Am_Event::INVOICE_STARTED
    {
         $invoice = $event->getInvoice(); // Invoice object
         $user = $event->getUser(); // User object
    }
}

Create a simple plugin that provides admin backend application/default/plugins/misc/test.php

class Am_Plugin_Test extends Am_Plugin
{
    function init()
    {
        $router = Zend_Controller_Front::getInstance()->getRouter();
        $router->addRoute('plugin-test-route', new Zend_Controller_Router_Route(
          'admin-plugin-test', // your controller will be accessible as http://www.example.com/amember/admin-plugin-test
           array(
              'module'      => 'default',
              'controller'  => 'admin-plugin-test', // class name must be AdminPluginTestController (!)
              'action'      => 'index', // default action
           )
        ));    
    }
    function onAdminMenu(Am_Event $event)
    {
        $menu = $event->getMenu(); // Am_Navigation_Admin object
        $menu->addItem(
                array(
                    'id' => 'plugin-test-menuitem',
                    'controller' => 'admin-plugin-test', 
                    'module' => 'default',
                    'label' => "Test Plugin Admin",
                )
        );       
    }
}
// if controller name has "admin" string inside, admin authentication will be automatically required
class AdminPluginTestController extends Am_Controller // a Zend_Controller_Action successor
{
     // validate if given admin have access to this page, return false if it does not
     public function checkAdminPermissions(Admin $admin)
     {
         return $admin->isSuper(); 
     }
      function indexAction()
      {
           $this->view->title = "Test Admin Plugin";
           $this->view->content = "I AM ALIVE!";
           $this->view->display('admin/layout.phtml');
      }
}