2

I'm currently trying to add a Grid inside a Tab of its parent Grid(?). I already have a working Grid and read through a lot of tutorials about the Grid serializer but I cant get it to work. Here is what I got so far:

code/local/MP/Berater/controllers/Adminhtml/IndexController.php

<?php


class MP_Berater_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action {

    public function indexAction() {
        $this->loadLayout();
        $myblock = $this->getLayout()->createBlock('berater/adminhtml_berater');        
        $this->_addContent($myblock);

        $this->_title($this->__('System'))->_title($this->__('Manage Form Elements'));      
        $this->_initLayoutMessages('adminhtml/session');
        $this->_setActiveMenu('Ics');

        $this->renderLayout();
    }

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

    public function editAction() {
        $beraterId= $this->getRequest()->getParam('form_sid');
        $beraterModel = Mage::getModel('berater/form')->load($beraterId);

        if ($beraterModel ->getId() || $beraterId== 0)
        {
            Mage::register('berater_data', $beraterModel );
            $this->loadLayout();
            $this->_setActiveMenu('berater/set_time');
            $this->_addBreadcrumb('Form Manager', 'Form Manager');
            $this->_addBreadcrumb('Form Description', 'Form Description');
            $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
            $this->_addContent($this->getLayout()
                                    ->createBlock('berater/adminhtml_berater_edit'))
                 ->_addLeft($this->getLayout()->createBlock('berater/adminhtml_berater_edit_tabs')
                 );
            $this->renderLayout();
        }
        else
        {
            Mage::getSingleton('adminhtml/session')->addError('Form does not exist');
            $this->_redirect('*/*/');
        }
    }

    public function saveAction() {
        if ($this->getRequest()->getPost())
        {
            try {

                Mage::dispatchEvent('adminhtml_berater_save_after');

                $postData = $this->getRequest()->getPost();
                $testModel = Mage::getModel('berater/form');
                if( $this->getRequest()->getParam('id') <= 0 )
                    $testModel->setCreatedTime(
                        Mage::getSingleton('core/date')
                            ->gmtDate()
                    );
                $testModel
                    ->addData($postData)
                    ->setUpdateTime(
                        Mage::getSingleton('core/date')
                            ->gmtDate())
                    ->setId($this->getRequest()->getParam('id'))
                    ->save();
                Mage::getSingleton('adminhtml/session')
                    ->addSuccess('successfully saved');
                Mage::getSingleton('adminhtml/session')
                    ->settestData(false);
                $this->_redirect('*/*/');
                return;
            } catch (Exception $e){
                Mage::getSingleton('adminhtml/session')
                    ->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')
                    ->settestData($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
            {
                $testModel = Mage::getModel('berater/form');
                $testModel->setId($this->getRequest()->getParam('id'))->delete();

                Mage::getSingleton('adminhtml/session')
                    ->addSuccess('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('*/*/');
    }

    public function massDeleteAction() {
        $webIds = $this->getRequest()->getParam('developers');
        if(!is_array($webIds)) {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select item(s)'));
        } else {
            try {
                foreach ($webIds as $webId) {
                    $web = Mage::getModel('berater/form')->load($webId);
                    $web->delete();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('adminhtml')->__(
                        'Total of %d record(s) were successfully deleted', count($webIds)
                    )
                );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/index');
    }
}

code/local/MP/Berater/Block/Adminhtml/Berater.php

<?php

class MP_Berater_Block_Adminhtml_Berater extends Mage_Adminhtml_Block_Widget_Grid_Container
{

    public function __construct()
    {
        parent::__construct();
        $this->_controller = 'adminhtml_berater';
        $this->_blockGroup = 'berater';
        $this->_headerText = Mage::helper('berater')->__('Form Elements');
        $this->_addButtonLabel = Mage::helper('berater')->__('my button text');

    }

    protected function _prepareLayout()
    {
        $this->setChild( 'grid',
            $this->getLayout()->createBlock( $this->_blockGroup.'/' . $this->_controller . '_grid',
            $this->_controller . '.grid')->setSaveParametersInSession(true) );

        return parent::_prepareLayout();
    }
}

code/local/MP/Berater/Block/Adminhtml/Berater/Grid.php

<?php

class MP_Berater_Block_Adminhtml_Berater_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

    public function __construct()
    {
        parent::__construct();
        $this->setUseAjax(true);
        $this->setId('beraterGrid');
        $this->setDefaultSort('id');
        $this->setDefaultDir('ASC');
        $this->setSaveParametersInSession(true);
    }

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

    protected function _prepareColumns()
    {
        $this->addColumn('id', array(
          'header'    => Mage::helper('berater')->__('ID'),
          'align'     =>'right',
          'width'     => '10px',
          'index'     => 'id',
        ));

        $this->addColumn('title', array(
          'header'    => Mage::helper('berater')->__('Title'),
          'align'     =>'left',
          'width'     => '500px',
          'index'     => 'title',
        ));


        $this->addColumn('content', array(
            'header'    => Mage::helper('berater')->__('Description'),
            'index'     => 'description',
        ));
        return parent::_prepareColumns();
    }

    protected function _prepareMassaction() {
        $this->setMassactionIdField('id');
        $this->getMassactionBlock()->setFormFieldName('berater');

        $this->getMassactionBlock()->addItem('delete', array(
            'label'    => Mage::helper('berater')->__('Delete'),
            'url'      => $this->getUrl('*/*/massDelete'),
            'confirm'  => Mage::helper('berater')->__('Are you sure?')
        ));
        return $this;
    }
}

code/local/MP/Berater/Block/Adminhtml/Berater/edit.php

<?php


class MP_Berater_Block_Adminhtml_Berater_Edit extends Mage_Adminhtml_Block_Widget_Form_Container {

    public function __construct() {
        parent::__construct();
        $this->_objectId = 'id';
        //we assign the same blockGroup as the Grid Container
        $this->_blockGroup = 'berater';
        //and the same controller
        $this->_controller = 'adminhtml_berater';
        //define the label for the save and delete button
        $this->_updateButton('save', 'label','Save Form Element');
        $this->_updateButton('delete', 'label', 'Delete Form Element');

    }
    /* Here, we're looking if we have transmitted a form object,
       to update the good text in the header of the page (edit or add) */
    public function getHeaderText()
    {
        if( Mage::registry('berater_data') && Mage::registry('berater_data')->getId())
        {
            return 'Editer'.$this->htmlEscape(
                Mage::registry('berater_data')->getTitle()).'<br />';
        }
        else
        {
            return 'Add Developers';
        }
    }
}

code/local/MP/Berater/Block/Adminhtml/Berater/Edit/Tabs.php

<?php


class MP_Berater_Block_Adminhtml_Berater_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
    public function __construct()    {
        parent::__construct();
        $this->setId('berater_tabs');
        $this->setDestElementId('edit_form');
        $this->setTitle('Form Information');
    }

    protected function _beforeToHtml()    {
        $this->addTab('form_section', array(
            'label'     => 'Label Form Information',
            'title'     => 'Title Form Information',
            'content'   => $this->getLayout()->createBlock('berater/adminhtml_berater_edit_tab_form')->toHtml()
        ));

        $this->addTab('options', array(
            'label'     => Mage::helper('berater')->__('Options'),
            'content'   => $this->getLayout()->createBlock('berater/adminhtml_berater_edit_tab_options')->toHtml()
        ));

        return parent::_beforeToHtml();
    }
}

code/local/MP/Berater/Block/Adminhtml/Berater/Edit/Form.php

<?php


class MP_Berater_Block_Adminhtml_Berater_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();
    }
}

code/local/MP/Berater/Block/Adminhtml/Berater/Edit/Tab/form.php

<?php


class MP_Berater_Block_Adminhtml_Berater_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form {

    protected function _prepareForm()
    {   
        $attributeCollection = Mage::getSingleton('eav/config')
                                ->getEntityType(Mage_Catalog_Model_Product::ENTITY)
                                ->getAttributeCollection();

        $attributes = array();
        foreach($attributeCollection as $attribute) {
            array_push($attributes, $attribute->getName());
        }


        $form = new Varien_Data_Form();
        $this->setForm($form);
        $fieldset = $form->addFieldset('berater_form',
            array('legend'=>'Legend Form Information'));

        $fieldset->addField('title', 'text',
            array(
                'label' => 'Title',
                'class' => 'required-entry',
                'required' => true,
                'name' => 'title'
            )
        );

        $fieldset->addField('description', 'textarea',
            array(
                'name'      => 'Description',
                'label'     => Mage::helper('berater')->__('Label Description'),
                'title'     => Mage::helper('berater')->__('Title Description')
            )
        );

        $fieldset->addField('type', 'select',
            array(
                  'label'     => 'Form Element Type',
                  'class'     => 'required-entry',
                  'required'  => true,
                  'name'      => 'type',
                  'onclick' => "",
                  'onchange' => "",
                  'value'  => '1',
                  'values' => array('1' => 'Select'),
                  'disabled' => false,
                  'readonly' => false,
                  'tabindex' => 1
            )
        );

        $fieldset->addField('action', 'select',
            array(
                  'label'     => 'Action',
                  'class'     => 'required-entry',
                  'required'  => true,
                  'name'      => 'action',
                  'onclick' => "",
                  'onchange' => "",
                  'value'  => '1',
                  'values' => array('1' => 'Set Attribute', '2' => 'Modify Attribute'),
                  'disabled' => false,
                  'readonly' => false,
                  'tabindex' => 1
            )
        );

        $fieldset->addField('attribute', 'select',
            array(
                  'label'     => 'Attribute',
                  'class'     => 'required-entry',
                  'required'  => true,
                  'name'      => 'type',
                  'onclick' => "",
                  'onchange' => "",
                  'value'  => '1',
                  'values' => $attributes,
                  'disabled' => false,
                  'readonly' => false,
                  'tabindex' => 1
            )
        );


        if ( Mage::registry('berater_data') )
        {
            $form->setValues(Mage::registry('berater_data')->getData());
        }
        return parent::_prepareForm();
    }
}

Ideally I would like to create another tab (options.php) inside code/local/MP/Berater/Block/Adminhtml/Berater/Edit/Tab/ which contains a Grid or something with similiar functionality. I'll worry about the model when I got the block done. Of course I tried to get this done by myself (read many tutorials and articles) but I never got it working. I'm actually pretty desperate at the moment since I really want to finish this module and I'm running out of time. Hopfully someone can help me here and lead me in the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.