Ads

Magento how to create a Custom Plugin/ Module / example / tutorial Part II

December 25, 2009
Filed under: Featured, Magento Cart, PHP, Shopping Carts, Zend, eCommerce 

Controller

/app/code/local/<Namespace>/<Module>/controllers/Adminhtml/<Module>Controller.php

NOTE: you need to manually add line 16, which is currently missing in this file. As per suggestion from mkd at page http://www.magentocommerce.com/boards/viewthread/11228/

<?php
 class >Namespace<_<Module>_Adminhtml_<Module>Controller extends
        {
      protected function _initAction()

     {
        $this->loadLayout()
        ->_setActiveMenu('/items')
        ->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager'));
        return $this;
        }
        public function indexAction() {
        $this->_initAction();
        $this->_addContent($this->getLayout()->createBlock('/adminhtml_'));
        $this->renderLayout();
        }

      public function editAction()

     {
        $Id     = $this->getRequest()->getParam('id');
        $Model  =	Mage::getModel('/')->load($Id);
    if ($Model->getId() || $Id ==	0) {
  	  Mage::register('_data', $Model);
  	  $this->loadLayout();
          $this->_setActiveMenu('/items');

	  $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item	Manager'), Mage::helper('adminhtml')->__('Item Manager'));
 	  $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));

      $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);

        $this->_addContent($this->getLayout()->createBlock('/adminhtml__edit'))
        ->_addLeft($this->getLayout()->createBlock('/adminhtml__edit_tabs'));

      $this->renderLayout();

      } else {

      Mage::getSingleton('adminhtml/session')->addError(Mage::helper('')->__('Item does not exist'));

      $this->_redirect('*/*/');

      }

      }

      public function newAction()
        {
        $this->_forward('edit');
        }

      public function saveAction()

      {

      if ( $this->getRequest()->getPost() ) {

      try {

      $postData = $this->getRequest()->getPost();

      $Model = Mage::getModel('/');

      $Model->setId($this->getRequest()->getParam('id'))

      ->setTitle($postData['title'])

      ->setContent($postData['content'])

      ->setStatus($postData['status'])

      ->save();

      Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved'));

      Mage::getSingleton('adminhtml/session')->setData(false);

      $this->_redirect('*/*/');

      return;

      } catch (Exception $e) {

      Mage::getSingleton('adminhtml/session')->addError($e->getMessage());

      Mage::getSingleton('adminhtml/session')->setData($this->getRequest()->getPost());

      $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));

      return;

      }

      }

      $this->_redirect('*/*/');

      }

      public function deleteAction()

      {

      if( $this->getRequest()->getParam('id') > 0 ) {

      try {

      $Model = Mage::getModel('/');

      $Model->setId($this->getRequest()->getParam('id'))

      ->delete();

      Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted'));

      $this->_redirect('*/*/');

      } catch (Exception $e) {

      Mage::getSingleton('adminhtml/session')->addError($e->getMessage());

      $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));

      }

      }

      $this->_redirect('*/*/');

      }

      }

XML Configuration Changes

/app/code/local/<Namespace>/<Module>/etc/config.xml




<?xml version="1.0"?>

<config>

<modules>

<[Namespace]_[Module]>

<version>0.1.0</version>

</[Namespace]_[Module]>

</modules>

<frontend>

<routers>

<[module]>

<use>standard</use>

<args>

<module>[Namespace]_[Module]</module>

<frontName>[module]</frontName>

</args>

</[module]>
</routers>

<layout>

<updates>

<[module]>
<file>[module].xml</file>

</[module]>

</updates>

</layout>

</frontend>

<admin>

<routers>

<[module]>

<use>admin</use>

<args>

<module>[Namespace]_[Module]</module>

<frontName>[module]</frontName>

</args>

</[module]>

</routers>

</admin>

<adminhtml>

<menu>

<[module] module="[module]">

<title>[Module]</title>

<sort_order>71</sort_order>

<children>

<action>[module]/adminhtml_[module]</action>

</items>

</children>

</[module]>

</menu>

<acl>

<resources>

<all>

<title>Allow Everything</title>

</all>

<admin>

<children>
<[module]>
<title>[Module] Module</title>

<sort_order>200</sort_order>

</[module]>

</children>

</admin>

</resources>

</acl>

<layout>

<updates>

<[module]>

<file>[module].xml</file>

</[module]>

</updates>

</layout>

</adminhtml>

<global>

<models>

<[module]>

<class>[Namespace]_[Module]_Model</class>

<resourceModel>[module]_mysql4</resourceModel>

</[module]>

<[module]_mysql4>

<class>[Namespace]_[Module]_Model_Mysql4</class>

<entities>

<[module]>

<table>[module]</table>

</[module]>

</entities>

</[module]_mysql4>

</models>

<resources>

<[module]_setup>

<setup>

<module>[Namespace]_[Module]</module>

</setup>

<connection>

<use>core_setup</use>

</connection>

</[module]_setup>

<[module]_write>

<connection>

<use>core_write</use>

</connection>

</[module]_write>

<[module]_read>

<connection>

<use>core_read</use>

</connection>

</[module]_read>

</resources>

<blocks>

<[module]>

<class>[Namespace]_[Module]_Block</class>

</[module]>

</blocks>

<helpers>

<[module]>

<class>[Namespace]_[Module]_Helper</class>

</[module]>

</helpers>

</global>

</config>

XML Layout

/app/design/adminhtml/<interface>/<theme>/layout/<module>.xml

<?xml version="1.0"?>

<layout version="0.1.0">

<[module]_adminhtml_[module]_index>

<reference name="content">
Source : http://www.magentocommerce.com/wiki/custom_module_with_custom_database_table

Comments

7 Comments on Magento how to create a Custom Plugin/ Module / example / tutorial Part II

    [...] Webhost Ranking | web hosting Magento how to create a Custom Plugin/ Module / example / tutorial Part II Dec [...]

  1. John | Magento Developer on Wed, 17th Feb 2010 6:34 pm
  2. Wow! this is one great tutorial for magento. Thanks for sharing this. Would really help future magento extension developers.

  3. Magento Development India on Tue, 28th Dec 2010 3:01 am
  4. Nice post you have shared.Thanks for sharing.

  5. Syntax3rror on Tue, 19th Jul 2011 3:08 am
  6. really helpfull post

  7. Magento One Page Checkout on Fri, 2nd Sep 2011 9:11 am
  8. Great example…….Thank you so much you just saved me some money and gave me knowledge

  9. Pierre FAY on Wed, 16th Nov 2011 5:55 am
  10. good tutorial, you can see more informations and how to go further here :

    http://www.about-magento.com/magento-developper-guide-howto-tutorial-5

    i hope it can help.

    Pierre.

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!





*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Subscribe without commenting