2

I've implemented a grid that pulls a custom model and displays it on a new tab within product new and edit pages. I've added a checkbox column, and would like the save button to save the current checkbox selections within the grid. No more functionality is needed within the grid (no edit buttons, or anything), but I haven't been able to figure out how to get the selections to post so I can save them.

In looking a little further, it sounds like I need the grid serializer to keep track of my checkboxes when flipping through pagination / filtering the grid. Unfortunately I haven't been able to get this working from any of the tutorials I've tried following. Here's my code right now:

app/code/local/Company/Module/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Company_Module>
            <version>0.1.0</version>
        </Company_Module>
    </modules>
    <global>


        <resources>
            <mapping_setup>
                <setup>
                    <module>Company_Module</module>
                    <class>Mage_Core_Model_Resource_Setup</class>
                </setup>
                <connection>
                    <use>module_db</use>
                </connection>
            </mapping_setup>

            <module_setup>
                <setup>
                    <module>Company_Module</module>
                    <class>Company_Module_Model_Resource_Module_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>default_setup</use>
                </connection>
            </module_setup>



        </resources>


        <models>

            <!-- This is for our mapping posts -->
            <module>
                <class>Company_Module_Model_Module</class>
                <resourceModel>module_resource</resourceModel>
            </module>

            <!-- Our mysql setup section -->
            <module_resource>
                <class>Company_Module_Model_Resource_Module</class>
                <entities>
                    <project>
                        <table>company_module_project</table>
                    </project>
                </entities>
            </module_resource>



            <!-- This is for our mapping table for projects onto products -->
            <mapping>
                <class>Company_Module_Model_Mapping</class>
                <resourceModel>mapping_resource</resourceModel>
            </mapping>

            <!-- Our mysql setup section -->
            <mapping_resource>
                <class>Company_Module_Model_Resource_Mapping</class>
                <entities>
                    <post>
                        <table>wp_posts</table>
                    </post>
                </entities>
            </mapping_resource>



        </models>

        <blocks>
            <company_module>
                <class>Company_Module_Block</class>
            </company_module>
        </blocks>
        <helpers>
            <company_module>
                <class>Company_Module_Helper</class>
            </company_module>
        </helpers>
    </global>
    <adminhtml>
        <layout>
            <updates>
                <company_module>
                    <file>producttabs.xml</file>
                </company_module>
            </updates>
        </layout>
    </adminhtml>


</config>

app/design/adminhtml/default/default/layout/producttabs.xml:

<?xml version="1.0"?>
<layout>
    <adminhtml_catalog_product_edit>
        <reference name="product_tabs">
            <action method="addTabAfter">
                <name>Related Projects</name>
                <block>company_module/adminhtml_catalog_product_tab</block>
                <after>related</after>
            </action>
        </reference>
    </adminhtml_catalog_product_edit>
    <adminhtml_catalog_product_new>
        <reference name="product_tabs">
            <action method="addTabAfter">
                <name>Related Projects</name>
                <block>company_module/adminhtml_catalog_product_tab</block>
                <after>related</after>
            </action>
        </reference>
    </adminhtml_catalog_product_new>
    <adminhtml_catalog_product_tab>
        <block type="core/text_list" name="root">
            <block type="adminhtml/catalog_product_tab" name="catalog.product.tab"/>
            <block type="adminhtml/widget_grid_serializer" name="tab_grid_serializer">
                <reference name="upsell_grid_serializer">
                    <action method="initSerializerBlock">
                        <grid_block_name>catalog.product.tab</grid_block_name>
                        <data_callback>getProjects</data_callback>
                        <hidden_input_name>map</hidden_input_name>
                        <reload_param_name>projects</reload_param_name>
                    </action>
                    <action method="addColumnInputName">
                        <input_name>position</input_name>
                    </action>
                </reference>
            </block>
        </block>
    </adminhtml_catalog_product_tab>
</layout>

app/code/local/Company/Module/Block/Adminhtml/Catalog/Product/Tab.php:

<?php

class Company_Module_Block_Adminhtml_Catalog_Product_Tab
    extends Mage_Adminhtml_Block_Widget_Grid
    implements Mage_Adminhtml_Block_Widget_Tab_Interface
{

    public function __construct()
    {
        parent::__construct();
        $this->setId('company_module_grid');
        $this->setDefaultSort('ID');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
    }

    protected function _prepareCollection()
    {
        $collection = Mage::getModel('mapping/post')->getCollection();

        $this->setCollection($collection);
        parent::_prepareCollection();
        return $this;
    }

    protected function _prepareColumns()
    {

        // Save the ID of the product we're editing
        $productId = Mage::app()->getRequest()->getParam('id');
        $map = Mage::getModel('module/project')->load($productId);

        $helper = Mage::helper('company_module');
        $currency = (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);

        $this->addColumn('itemCheckbox', array(
            'index'      => 'ID',
            'type'       => 'checkbox',
            'width'      => 20,
            'sortable'   => false,
            'field_name' => 'map',
            'values'     => $map->getProjects($productId)
        ));

        $this->addColumn('post_id', array(
            'header' => $helper->__('ID'),
            'index'  => 'ID'
        ));

        $this->addColumn('post_author', array(
            'header' => $helper->__('Author'),
            'index'  => 'post_author'
        ));

        $this->addColumn('post_date', array(
            'header' => $helper->__('Date'),
            'type'   => 'datetime',
            'index'  => 'post_date'
        ));

        $this->addColumn('post_title', array(
            'header' => $helper->__('Title'),
            'index'  => 'post_title'
        ));


        $this->addExportType('*/*/exportRelatedCsv', $helper->__('CSV'));
        $this->addExportType('*/*/exportRelatedExcel', $helper->__('Excel XML'));

        return parent::_prepareColumns();
    }



    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current'=>true));
    }



    /**
     * Retrieve the label used for the tab relating to this block
     *
     * @return string
     */
    public function getTabLabel()
    {
        return $this->__('Projects');
    }

    /**
     * Retrieve the title used by this tab
     *
     * @return string
     */
    public function getTabTitle()
    {
        return $this->__('Click here to view your custom tab content');
    }

    /**
     * Determines whether to display the tab
     * In our case, we show it on the product edit and new product pages
     *
     * @return bool
     */
    public function canShowTab()
    {

        // Pull in our product
        $product = Mage::registry('current_product');

        // The request
        $request = Mage::app()->getRequest();

        // Show if we have a product already (edit product page)
        if ($product->getId()) {
            return true;
        }

        // Do not show if we don't have any attribute set
        if (!$product->getAttributeSetId()) {
            return false;
        }

        // If we do have an attribute set, it's okay to show (add new product page)
        if ($request->getParam('set')) {
                return true;
        }

        return false;

    }

    /**
     * Stops the tab being hidden
     *
     * @return bool
     */
    public function isHidden()
    {
        return false;
    }



}

app/code/local/Company/Module/controllers/Adminhtml/AjaxGridController.php

<?php

class Company_Module_Adminhtml_AjaxGridController extends Mage_Adminhtml_Catalog_ProductController
{

    public function massactionSaveProjects() {

    }

    public function editAction()
    {
        $this->loadLayout()
            ->_title($this->__('Related Projects Grid'))
            ->renderLayout();
    }
    public function projectsTabAction()
    {
        // used for selecting customers on tab load
        $saved_customer_ids = array(); // your load logic here

        $this->loadLayout()
            ->getLayout()
            ->getBlock('company_module/adminhtml_catalog_product_tab');

        $this->renderLayout();
    }
    public function customersGridAction()
    {
        $this->loadLayout()
            ->getLayout()
            ->getBlock('company_module/adminhtml_catalog_product_tab');

        $this->renderLayout();
    }
    public function saveAction()
    {
        if ($data = $this->getRequest()->getParam('map')) {
            echo 'massactionSaveProjects';
            echo '<pre>'; var_dump($data); echo '</pre>'; die();
        } else {
            echo 'no data'; die();
        }

    }

}

app/code/local/Company/Module/Model/Resource/Mapping/Post/Collection.php:

<?php
class Company_Module_Model_Resource_Mapping_Post_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract{

    public function _construct() {
        parent::_construct();
        $this->_init('mapping/post');
    }

}

app/code/local/Company/Module/Model/Resource/Mapping/Post.php:

<?php
class Company_Module_Model_Resource_Mapping_Post extends Mage_Core_Model_Resource_Db_Abstract {

    public function _construct() {
        $this->_init('mapping/post', 'ID');
    }

}

app/code/local/Company/Module/Model/Resource/Module/Mysql4/Setup.php:

<?php
class Company_Module_Model_Resource_Module_Mysql4_Setup extends Mage_Core_Model_Resource_Setup {

}

app/code/local/Company/Module/Model/Resource/Module/Project/Collection.php:

<?php
class Company_Module_Model_Resource_Module_Project_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract{

    public function _construct() {
        parent::_construct();
        $this->_init('module/project');
    }

}

app/code/local/Company/Module/Model/Resource/Module/Project.php:

<?php
class Company_Module_Model_Resource_Module_Project extends Mage_Core_Model_Resource_Db_Abstract {

    public function _construct() {
        $this->_init('module/project', 'id');
    }

}

app/code/local/Company/Module/Model/Mapping/Post.php:

<?php
class Company_Module_Model_Mapping_Post extends Mage_Core_Model_Abstract {

    public function _construct() {
        $this->_init('mapping/post', 'ID');
    }

}

app/code/local/Company/Module/Model/Module/Project.php:

<?php
class Company_Module_Model_Module_Project extends Mage_Core_Model_Abstract {

    public function _construct() {
        $this->_init('module/project', 'ID');
    }


    /**
     * @param $productId
     * @return array - The projects that should be checked
     *
     * Takes a product ID, and returns an array of all project IDs that are mapped to this
     */
    public function getProjects($productId) {

        // Code omitted here for brevity. I assure you, this function returns an array of the project IDs

        return $checked;

    }

}
3
  • Hi, did you figure it out? I am facing the exact problem. Will appreciate any help.
    – Hashid
    Mar 30 '16 at 13:18
  • 1
    Sorry, never solved it. I ended up quitting my Magento job and leaving it behind for frameworks that are less frustrating. Good luck!
    – carbide20
    Mar 31 '16 at 21:48
  • LOL Unfortunately I don't have the choice to quit. Let me try very hard :-)
    – Hashid
    Apr 1 '16 at 6:26

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.