API/Samples

From aMember Pro Documentation
Revision as of 07:20, 28 February 2012 by Alex-scott (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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
{
      function indexAction()
      {
           $this->view->title = "Test Admin Plugin";
           $this->view->content = "I AM ALIVE!";
           $this->view->display('admin/layout.phtml');
      }
}