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"?>
true
local
1.0.0
app/code/local/Test/Formsubscribe/etc/config.xml
< ?xml version="1.0"?>
1.0.0
Test_Formsubscribe
formsubscribe
formsubscribe.xml
Test_Formsubscribe_Model
formsubscribe_resource
Test_Formsubscribe_Model_Resource
formsubscriber
Test_Formsubscribe
Test_Formsubscribe_Block
Test_Formsubscribe_Helper
form_subscription_email_template.html
html
Test_Formsubscribe_Adminhtml
formsubscribe.xml
Form Subscription
Form Subscribers
Manage Subscribers
0
app/code/local/Test/Formsubscribe/etc/system.xml
< ?xml version="1.0" encoding="utf-8"?>
80
form_sub
10
1
1
1
text
15
1
1
1
select
10
1
1
1
adminhtml/system_config_source_enabledisable
text
required-entry
1
20
1
1
1
Enter admin's email address like john@example.com
1
1
1
1
30
select
adminhtml/system_config_source_email_template
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 .= "Dear Admin,
I am requesting for you test subscription, below are my details:
";
$body .= "Name : " . $name . "
";
$body .= "
Email : " . $email . "
";
$body .= "
Contact Number : " . $contact_no . "
";
$body .= "
Message : " . $message . "
";
$body .= "
Regards,
".$name."
";
$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"?>
test_formsubscriber/subscribers
Frontend layout
app/design/frontend/base/default/layout/formsubscribe.xml
page/1column.phtml
Frontend template
app/design/frontend/sterling/default/template/formsubscribe/form_subscribe.phtml
< ?php echo $this->__('Subscribe Form') ?>