Question

I am creating a simple module with grid with an add button and form. This is my structure so far (based on the tried & tested structure I've obtained using ModuleCreator script):

/Integration/
--/controllers/
----ManualordersyncController.php
--/Block/
----Manualordersync.php
------/Adminhtml/
--------Manualordersync.php
----------/Manualordersync/
------------Grid.php
------------Edit.php
--------------/Edit/
----------------Form.php
----------------Tabs.php
------------------/Tab/
--------------------Form.php

Which looks like this on my editor:

enter image description here

As you can see, I've already have other similar grid with their own edit screen, tabs, forms etc... which all works fine.

However, on this new grid I am working on Manualordersync, it displays the grid correctly: I added some data manually using phpMyAdmin

enter image description here

However when I click the Add Order button I get a 404 page and I am not sure where this is coming from. Been looking at this for hours.

Can someone help? This is my code so far:


/Integration/
  /controllers/
    ManualordersyncController.php

class Companyname_Integration_Adminhtml_ManualordersyncController extends Mage_Adminhtml_Controller_action
{
    protected function _initAction()
    {
        // Init
        $this->loadLayout()
            ->_setActiveMenu('integration/manualordersync')
            ->_addBreadcrumb(Mage::helper('adminhtml')->__('Manual Order Sync'));
        return $this;
    }   

    public function indexAction()
    {
        // Render Page
        $this->_initAction()
             ->renderLayout();
    }

    public function editAction()
    {
        // Lad Data
        $id     = $this->getRequest()->getParam('id');
        $model  = Mage::getModel('ibi/manualordersync')->load($id);

        // Proceed If Data Is Loaded
        if ($model->getId() || $id == 0)
        {
            // Load Session
            $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
            if (!empty($data)) {
                $model->setData($data);
            }

            // Register Model
            Mage::register('manualordersync_data', $model);

            // Load Layout
            $this->loadLayout();
            $this->_setActiveMenu('integration/manualordersync');
            $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Manual Order Sync'));
            $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
            $this->_addContent($this->getLayout()->createBlock('integration/adminhtml_manualordersync_edit'))
                 ->_addLeft($this->getLayout()->createBlock('integration/adminhtml_manualordersync_edit_tabs'));

            // Render Page
            $this->renderLayout();
        }
        else
        {
            // Error
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('integration')->__('Order does not exist'));
            $this->_redirect('*/*/');
        }
    }

    public function saveAction()
    { /*snipped*/ }

    public function exportCsvAction()
    { /*snipped*/ }

    public function exportXmlAction()
    { /*snipped*/ }

    protected function _sendExportResponse($fileName, $content, $contentType = 'application/octet-stream')
    { /*snipped*/ }
}

/Integration/
  /Block/
    Manualordersync.php

class Companyname_Integration_Block_Manualordersync extends Mage_Core_Block_Template
{
    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }

     public function getManualordersync()     
     { 
        if (!$this->hasData('manualordersync')) {
            $this->setData('manualordersync', Mage::registry('manualordersync'));
        }
        return $this->getData('manualordersync');
    }
}

/Integration/
  /Block/
      /Adminhtml/
        Manualordersync.php

class Companyname_Integration_Block_Adminhtml_Manualordersync extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct() {
        $this->_controller = 'adminhtml_manualordersync';
        $this->_blockGroup = 'integration';
        $this->_headerText = Mage::helper('integration')->__('Manual Order Sync');
        $this->_addButtonLabel = Mage::helper('integration')->__('Add Order');
        parent::__construct();
    }
}

/Integration/
  /Block/
      /Adminhtml/
          /Manualordersync/
            Grid.php

class Companyname_Integration_Block_Adminhtml_Manualordersync_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('manualordersyncGrid');
        $this->setDefaultSort('manualordersync_id');
        $this->setDefaultDir('ASC');
        $this->setSaveParametersInSession(true);
    }

    protected function _prepareCollection()
    {
        $collection = Mage::getModel('ibi/manualordersync')->getCollection();
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
        $this->addColumn('manualordersync_id', array(
            'header' => Mage::helper('integration')->__('ID'),
            'align' => 'left',
            'index' => 'manualordersync_id'
        ));

        $this->addColumn('order_number', array(
            'header' => Mage::helper('integration')->__('Order Number'),
            'align' => 'left',
            'index' => 'order_number'
        ));

        $this->addColumn('order_number', array(
            'header' => Mage::helper('integration')->__('Order Number'),
            'align' => 'left',
            'index' => 'order_number'
        ));

        $this->addColumn('created_at', array(
            'header' => Mage::helper('integration')->__('Created At'),
            'align' => 'left',
            'index' => 'created_at'
        ));

        $this->addColumn('created_by', array(
            'header' => Mage::helper('integration')->__('Created By'),
            'align' => 'left',
            'index' => 'created_by'
        ));

        $this->addColumn('action', array(
            'header' => Mage::helper('integration')->__('Action'),
            'width' => '100',
            'type' => 'action',
            'getter' => 'getId',
            'actions' => array(
                array(
                    'caption' => Mage::helper('integration')->__('Edit'),
                    'url' => array(
                        'base' => '*/*/edit'
                    ),
                    'field' => 'id'
                )
            ),
            'filter' => false,
            'sortable' => false,
            'index' => 'stores',
            'is_system' => true
        ));

        $this->addExportType('*/*/exportCsv', Mage::helper('integration')->__('CSV'));
        $this->addExportType('*/*/exportXml', Mage::helper('integration')->__('XML'));

        return parent::_prepareColumns();
    }

    public function getRowUrl($row)
    {
        return $this->getUrl('*/*/edit', array(
            'id' => $row->getId()
        ));
    }
}

/Integration/
  /Block/
      /Adminhtml/
          /Manualordersync/
            Edit.php

class Companyname_Integration_Block_Adminhtml_Manualordersync_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
    public function __construct()
    {
        parent::__construct();

        $this->_objectId = 'id';
        $this->_blockGroup = 'integration';
        $this->_controller = 'adminhtml_manualordersync';

        $this->_updateButton('save', 'label', Mage::helper('integration')->__('Save'));
        $this->_updateButton('delete', 'label', Mage::helper('integration')->__('Delete'));

        $this->_addButton('saveandcontinue', array(
            'label'     => Mage::helper('adminhtml')->__('Save And Continue Edit'),
            'onclick'   => 'saveAndContinueEdit()',
            'class'     => 'save',
        ), -100);

        $this->_formScripts[] = "
            function saveAndContinueEdit(){
                editForm.submit($('edit_form').action+'back/edit/');
            }
        ";
    }

    public function getHeaderText()
    {
        if (Mage::registry('manualordersync_data') && Mage::registry('manualordersync_data')->getId()) {
            return Mage::helper('integration')->__("Edit Manual Sync for '%s'", $this->htmlEscape(Mage::registry('manualordersync_data')->getOrderNumber()));
        } else {
            return Mage::helper('integration')->__('Add Order');
        }
    }
}

/Integration/
  /Block/
      /Adminhtml/
          /Manualordersync/
              /Edit/
                Form.php

class Companyname_Integration_Block_Adminhtml_Manualordersync_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form(array(
                                        'id' => 'edit_form',
                                        'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
                                        'method' => 'post'
                                     )
        );

        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

/Integration/
  /Block/
      /Adminhtml/
          /Manualordersync/
              /Edit/
                Tabs.php

class Companyname_Integration_Block_Adminhtml_Manualordersync_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('integration_tabs');
        $this->setDestElementId('edit_form');
        $this->setTitle(Mage::helper('integration')->__('Order Information'));
    }

    protected function _beforeToHtml()
    {
        $this->addTab('form_section', array(
            'label'     => Mage::helper('integration')->__('Order Information'),
            'title'     => Mage::helper('integration')->__('Order Information'),
            'content'   => $this->getLayout()->createBlock('integration/adminhtml_manualordersync_edit_tab_form')->toHtml(),
        ));

        return parent::_beforeToHtml();
    }
}

/Integration/
  /Block/
      /Adminhtml/
          /Manualordersync/
              /Edit/
                  /Tab/
                    Form.php

class Companyname_Integration_Block_Adminhtml_Manualordersync_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $this->setForm($form);

        $fieldset = $form->addFieldset('integration_form', array(
            'legend' => Mage::helper('integration')->__('Order Information')
        ));

        $resource = Mage::getSingleton('core/resource');
        $readConnection = $resource->getConnection('core_read');
        $tableName = $resource->getTableName('sales/order');
        $results = $readConnection->fetchAll("SELECT `increment_id` FROM $tableName");
        $order_numbers = array();
        foreach ($results as $order)
            $order_numbers[$order['increment_id']] = $order['increment_id'];
        $fieldset->addField('order_number', 'select', array(
            'label' => Mage::helper('integration')->__('Order Number'),
            'class' => 'required-entry',
            'required' => true,
            'name' => 'order_number',
            'values' => $order_numbers
        ));
        $resource = $readConnection = $tableName = $results = null;

        if (Mage::getSingleton('adminhtml/session')->getManualordersyncData()) {
            $form->setValues(Mage::getSingleton('adminhtml/session')->getManualordersyncData());
            Mage::getSingleton('adminhtml/session')->setManualordersyncData(null);
        } elseif (Mage::registry('manualordersync_data')) {
            $form->setValues(Mage::registry('manualordersync_data')->getData());
        }
        return parent::_prepareForm();
    }
}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top