Categories
CMS Magento Mastering Development

Custom module admin grid show blank page in magento 1.9

I have created one simple form on frontend that will submitted by customer and create one menu in admin panel to show only the grid for same, no add-edit form.

When I click on the menu it will redirected to a page where header and footer are present but the content area is blank, No grid shown there.

Here is the exact code for my module.

app/etc/modules/Test_Formsubscribe.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Test_Formsubscribe>
      <active>true</active>
      <codePool>local</codePool>
      <version>1.0.0</version>
    </Test_Formsubscribe>
  </modules>
</config>

app/code/local/Test/Formsubscribe/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Test_Formsubscribe>
            <version>1.0.0</version>
        </Test_Formsubscribe>
    </modules>

  <frontend>
    <routers>
      <formsubscribe>
        <use>standard</use>
        <args>
          <module>Test_Formsubscribe</module>
          <frontName>formsubscribe</frontName>
        </args>
      </formsubscribe>
    </routers>

    <!-- New lines -->
        <layout>
            <updates>
                <formsubscribe>
                    <file>formsubscribe.xml</file>
                </formsubscribe>
            </updates>
        </layout>
    <!-- End of New Lines -->
  </frontend>
  <global>
        <models>
            <formsubscribe>
                <class>Test_Formsubscribe_Model</class>
                <resourceModel>formsubscribe_resource</resourceModel>
            </formsubscribe>
            <formsubscribe_resource>
                <class>Test_Formsubscribe_Model_Resource</class>
                <entities>
                    <formsubscriber>
                        <table>formsubscriber</table>
                    </formsubscriber>
                </entities>
            </formsubscribe_resource>
        </models>
    <resources>
            <formsubscribe_setup>
                <setup>
                    <module>Test_Formsubscribe</module>
                </setup>
            </formsubscribe_setup>
        </resources>
        <blocks>
            <formsubscribe>
                <class>Test_Formsubscribe_Block</class>
            </formsubscribe>
        </blocks>
        <helpers>
            <formsubscribe>
                <class>Test_Formsubscribe_Helper</class>
            </formsubscribe>
        </helpers>

      <template>
          <email>
              <formsubscribe_email_email_template translate="label" module="formsubscribe">
                  <label>Form Subscription Email</label>
                  <file>form_subscription_email_template.html</file>
                  <type>html</type>
              </formsubscribe_email_email_template>
          </email>
      </template>
    </global>

    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Test_Formsubscribe before="Mage_Adminhtml">Test_Formsubscribe_Adminhtml</Test_Formsubscribe>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>


    <adminhtml>
            <layout>
                <updates>
                    <formsubscribe>
                        <file>formsubscribe.xml</file>
                    </formsubscribe>
                </updates>
            </layout>

        <acl>
            <resources>
                <admin>
                    <children>
                        <system>
                            <children>
                                <config>
                                    <children>
                                        <subscribe_form translate="title" module="formsubscribe">
                                            <title>Form Subscription</title>
                                        </subscribe_form>
                                    </children>
                                </config>
                            </children>
                        </system>
                        <test_formsubscriber translate="title" module="formsubscribe">
                            <title>Form Subscribers</title>
                            <children>
                                <subscribers translate="title" module="formsubscribe">
                                    <title>Manage Subscribers</title>
                                    <sort_order>0</sort_order>
                                </subscribers>
                            </children>
                        </test_formsubscriber>
                    </children>
                </admin>
            </resources>
        </acl>

        <menu>
            <test_formsubscriber translate="title" module="formsubscribe">
                <title>Subscribers</title>
                <sort_order>90</sort_order>
                <children>
                    <subscribers translate="title" module="formsubscribe">
                        <title>Manage Subscribers</title>
                        <action>adminhtml/formsubscribe/index</action>
                        <!-- adminroute/admincontroller/action -->
                        <sort_order>0</sort_order>
                    </subscribers>
                </children>
            </test_formsubscriber>
        </menu>
    </adminhtml>
</config>

app/code/local/Test/Formsubscribe/etc/system.xml

<?xml version="1.0" encoding="utf-8"?>
<config>
    <tabs>
        <form_sub translate="label" module="formsubscribe">
            <label>Subscription</label>
            <sort_order>80</sort_order>
        </form_sub>
    </tabs>
    <sections>
        <subscribe_test translate="label" module="formsubscribe">
            <label>Form Subscription</label>
            <tab>form_sub</tab>
            <sort_order>10</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <subscription_config translate="label">
                    <label>Configuration</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>15</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <subscribe_select translate="label">
                            <label>Enable Module</label>
                            <frontend_type>select</frontend_type>
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <source_model>adminhtml/system_config_source_enabledisable</source_model>
                        </subscribe_select>
                        <email translate="label">
                            <label>Admin Email</label>
                            <frontend_type>text</frontend_type>
                            <validate>required-entry</validate>
                            <depends>
                                <subscribe_select>1</subscribe_select>
                            </depends>
                            <sort_order>20</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <comment>Enter admin's email address like john@example.com</comment>
                        </email>
                        <template translate="label">
                            <label>Email Template</label>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <depends>
                                <subscribe_select>1</subscribe_select>
                            </depends>
                            <sort_order>30</sort_order>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_email_template</source_model>
                        </template>
                    </fields>
                </subscription_config>
            </groups>
        </subscribe_test>
    </sections>
</config>

app/code/local/Test/Formsubscribe/Helper/Data.php

<?php
class Test_Formsubscribe_Helper_Data extends Mage_Core_Helper_Abstract {

}

app/code/local/Test/Formsubscribe/Model/Formsubscriber.php

<?php
class Test_Formsubscribe_Model_Formsubscriber extends Mage_Core_Model_Abstract
{
    protected function _construct()
    {
        $this->_init('formsubscribe/formsubscriber');
    }
}

app/code/local/Test/Formsubscribe/Model/Resource/Formsubscriber.php

<?php
class Test_Formsubscribe_Model_Resource_Test_Formsubscriber extends Mage_Core_Model_Resource_Db_Abstract
{
    protected function _construct()
    {
        $this->_init('formsubscribe/formsubscriber', 'subscribe_id');
    }
}

app/code/local/Test/Formsubscribe/Model/Resource/Formsubscriber/Collection.php

<?php
class Test_Formsubscribe_Model_Resource_Formsubscriber_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
    public function _construct()
    {
        $this->_init('formsubscribe/formsubscriber');
    }
}

app/code/local/Test/Formsubscribe/Block/Index.php

<?php
class Test_Formsubscribe_Block_Index extends Mage_Core_Block_Template
{
    public function getFormAction(){
        return $this->getUrl('formsubscribe/index/post');
    }
}

app/code/local/Test/Formsubscribe/Block/Adminhtml/Formsubscribe.php

<?php

class Test_Formsubscribe_Block_Adminhtml_Formsubscribe extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct()
    {
        $this->_blockGroup = 'test_formsubscribe';
        $this->_controller = 'adminhtml_formsubscribe';
        $this->_headerText = Mage::helper('formsubscribe')->__('Form Subscriber');
        parent::__construct();
        $this->_removeButton('add');
    }
}

app/code/local/Test/Formsubscribe/Block/Adminhtml/Formsubscribe/Grid.php

class Test_Formsubscribe_Block_Adminhtml_Formsubscribe_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {

        parent::__construct();
        $this->setId('formsubscribe_grid');
        $this->setDefaultSort('subscribe_id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
    }

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

    protected function _prepareColumns()
    {
        $helper = Mage::helper('formsubscribe');

        $this->addColumn('subscribe_id', array(
            'header' => $helper->__('Subscriber #'),
            'index'  => 'subscribe_id'
        ));

        $this->addColumn(
            'email',
            array(
                'header' => Mage::helper('formsubscribe')->__('Email'),
                'index'  => 'email'
            )
        );

        $this->addColumn('contact_no', array(
            'header' => $helper->__('Contact Number'),
            'index'     => 'contact_no',
        ));

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

        $this->addExportType('*/*/exportCsv', $helper->__('CSV'));
        $this->addExportType('*/*/exportExcel', $helper->__('Excel'));

        return parent::_prepareColumns();
    }

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

app/code/local/Test/Formsubscribe/Adminhtml/FormsubscribeController.php

class Test_Formsubscribe_Adminhtml_FormsubscribeController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }

    public function gridAction()
    {
       $this->loadLayout()->renderLayout();
    }

    public function exportCsvAction()
    {
        $fileName = 'formsubscribe.csv';
        $grid = $this->getLayout()->createBlock('formsubscribe/adminhtml_formsubscribe_grid');
        $this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
    }

    public function exportExcelAction()
    {
        $fileName = 'formsubscribe.xml';
        $grid = $this->getLayout()->createBlock('formsubscribe/adminhtml_formsubscribe_grid');
        $this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
    }

    protected function _isAllowed()
    {
        return Mage::getSingleton('admin/session')->isAllowed('test_formsubscriber/formsubscribes');
    }
}

app/code/local/Test/Formsubscribe/IndexController.php

<?php
class Test_Formsubscribe_IndexController extends Mage_Core_Controller_Front_Action
{
    const XML_MODULE_ENABLE = 'subscribe_form/subscription_config/subscribe_select';
    const XML_PATH_EMAIL_RECIPIENT = 'subscribe_form/subscription_config/email';
    const XML_PATH_EMAIL_TEMPLATE = 'subscribe_form/subscription_config/template';
    const XML_PATH_NAME_RECIPIENT = 'Admin';

    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }

    public function postAction()
    {

    if(Mage::getStoreConfig(self::XML_MODULE_ENABLE) == 0)
        {
            return;
        }
        $post = $this->getRequest()->getPost();

        if ($post){
            try {
                $subscriber = Mage::getModel('formsubscribe/formsubscribe');
                $fullname = strtolower(trim($post['f_name'])).' '.strtolower(trim($post['l_name']));
                $subscriber->setData('name', $fullname);
                $subscriber->setData('email', trim($post['email']));
                $subscriber->setData('contact_no', $post['contact_no']);
                if(array_key_exists('message',$post) && trim($post['message']) != ''){$subscriber->setData('message', trim($post['message']));}

        $subscriber->save();

                $name = ucwords(trim($post["f_name"]." ".$post["l_name"]));
                $email = trim($post["email"]);
                $postcode = trim($post["contact_no"]); 
                $message = (array_key_exists('message',$post) && trim($post['message']) != '')?trim($post["message"]):''; 
                $to = Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT); //Admin Email Address
                $subject = "Form Subcription Request";
                $body .= "<p>Dear Admin,<br/> I am requesting for you test subscription, below are my details: </p>";
                $body .= "<p>Name : " . $name . "<p>";
                $body .= "<p>Email  : " . $email . "<p>";
                $body .= "<p>Contact Number : " . $contact_no . "<p>";
                $body .= "<p>Message : " . $message . "<p>";
                $body .= "<br/><p>Regards,<br/>".$name."</p>";
                $from = $email;
                $mail = Mage::getModel('core/email');
                $mail->setToName(self::XML_PATH_NAME_RECIPIENT);
                $mail->setToEmail($to);
                $mail->setBody($body);
                $mail->setSubject($subject);
                $mail->setFromEmail($from);
                $mail->setType('html');// YOu can use Html or text as Mail format
                $mail->setBodyHTML($body);  // your content or message
                $mail->send();

                echo Mage::getSingleton('core/session')->addSuccess('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.');
                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                echo Mage::getSingleton('core/session')->addError('Unable to submit your request. Please, try again later');*/
               this->_redirect('*/*/');
               return;
            }
        } else {
            $this->_redirect('*/*/');
        }
    }
}

app/code/local/Test/Formsubscribe/sql/formsubscribe_setup/install-1.0.0.php

<?php

$installer = $this;

$installer->startSetup();

$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */

$installer->startSetup();

$installer->run("
DROP TABLE IF EXISTS `{$this->getTable('formsubscribe/formsubscriber')}`;
CREATE TABLE `{$this->getTable('formsubscribe/formsubscriber')}` (
  `subscribe_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `email` varchar(80) NOT NULL,
  `contact_no` varchar(255) NOT NULL,
  `message` text NULL,
  PRIMARY KEY (`subscribe_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Subscribers';
");

$installer->endSetup();

Admin Layout
app/design/adminhtml/default/default/layout/formsubscribe.xml
(I have doubt on this file)

<?xml version="1.0"?>
<layout>
    <adminhtml_formsubscribe>
        <reference name="menu">
            <action method="setActive">
                <menupath>test_formsubscriber/subscribers</menupath>
            </action>
        </reference>
        <reference name="content">
            <block type="test_auctionsubscribe/adminhtml_formsubscribe" name="formsubscribe" />
        </reference>
    </adminhtml_formsubscribe>
    <adminhtml_formsubscribe_grid>
        <block type="core/text_list" name="root" output="toHtml">
            <block type="test_formsubscribe/adminhtml_formsubscribe_grid" name="formsubscribe_grid"/>
        </block>
    </adminhtml_formsubscribe_grid>
</layout>

Frontend layout
app/design/frontend/base/default/layout/formsubscribe.xml

<layout version="0.1.0">
    <formsubscribe_index_index>
        <reference name="root">
            <action method="setTemplate"><template>page/1column.phtml</template></action>
        </reference>
        <reference name="content">
            <block type="formsubscribe/index" name="formsubscribe" template="formsubscribe/form_subscribe.phtml" />
        </reference>
    </formsubscribe_index_index>
</layout>

Frontend template
app/design/frontend/sterling/default/template/formsubscribe/form_subscribe.phtml

 <div class="page-title">
 <h2><?php echo $this->__('Subscribe Form') ?></h2>
 </div>

 <form id="subscribe-form-validate" method="post" action="<?php echo $this->getFormAction()?>">
     <div class="fieldset">
        <h2 class="legend"><?php echo $this->__('Information')?></h2>
        <ul class="form-list">
            <li><p class="required">* <?php echo $this->__('Required Fields')?></p></li>
            <li class="fields">
                <div class="field name-firstname">
                    <label class="required" for="fname"><em>*</em><?php echo $this->__('First Name')?></label>
                    <div class="input-box">
                        <input type="text" class="input-text validate-alpha required-entry" maxlength="255" title="Name" value="" name="f_name" id="f_name">
                    </div>
                </div>
            </li>
            <li class="fields">
                <div class="field name-lastname">
                    <label class="required" for="lname"><em>*</em><?php echo $this->__('Last Name')?></label>
                    <div class="input-box">
                        <input type="text" class="input-text validate-alpha required-entry" maxlength="255" title="Last Name" value="" name="l_name" id="l_name">
                    </div>
                </div>
            </li>
            <li class="fields">
                <div class="field email">
                    <label class="required" for="email"><em>*</em><?php echo $this->__('Email')?></label>
                    <div class="input-box">
                        <input type="text" class="input-text validate-email required-entry" maxlength="255" title="Email" value="" placeholder="john@example.com" name="email" id="email">
                    </div>
                </div>
            </li>
            <li class="fields">
                <div class="field contact_no">
                    <label class="required" for="contact_no"><em>*</em><?php echo $this->__('Contact No.')?></label>
                    <div class="input-box">
                        <input type="tel" class="input-text required-entry" title="Contact Number" value="" name="contact_no" id="contact_no">
                    </div>
                </div>
            </li>
            <li class="fields">
                <div class="field message">
                    <label for="message"><?php echo $this->__('Message')?></label>
                    <div class="input-box">
                        <textarea title="Message" name="message" id="address" class="input-text"></textarea> 
                    </div>
                </div>
            </li>
        </ul>
     </div>
     <div class="buttons-set">
     <button class="button" title="Submit" type="submit"><span><span><?php echo strtoupper($this->__('Subscribe'))?></span></span></button>
     </div>
 </form>

 <script type="text/javascript">
//<![CDATA[
var post = new VarienForm('subscribe-form-validate', true);
//]]>
</script>

Leave a Reply

Your email address will not be published. Required fields are marked *