Posted by: james | August 1, 2009

Magento module for insert database and admin panel display

Module:

Note: When i write here go to any directory and if they are not exist then please create that and never change in existing file of magento..

First of all go to local folder and create two folder
1.Form and inside it 2.Warrenty

then go to app/etc/modules/Form_Warrenty.xml
and in that write…

<?xml version=”1.0″ encoding=”UTF-8″?>
<config>
<modules>
<Form_Warrenty>
<active>true</active>
<codePool>local</codePool>
</Form_Warrenty>
</modules>
</config>

now go to app/code/local/Form/Warrenty/etc/config.xml
and in that write…….

<?xml version=”1.0″?>
<config>
<modules>
<Form_Warrenty>
<version>0.1.0</version>
</Form_Warrenty>
</modules>
<global>
<models>
<warrenty>
<class>Form_Warrenty_Model</class>
<resourceModel>warrenty_mysql4</resourceModel>
</warrenty>
<warrenty_mysql4>
<class>Form_Warrenty_Model_Mysql4</class>
<entities>
<warrenty>
<table>warrenty</table> // it will first of all go to warrenty model in that it will find resource model and in that it will find

entities and in that table name will be there.
</warrenty>
</entities>
</warrenty_mysql4>
</models>

<resources>
<warrenty_setup>
<setup>
<module>Form_Warrenty</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</warrenty_setup>
<warrenty_write>
<connection>
<use>core_write</use>
</connection>
</warrenty_write>
<warrenty_read>
<connection>
<use>core_read</use>
</connection>
</warrenty_read>
</resources>
<blocks>
<warrenty>
<class>Form_Warrenty_Block</class>
</warrenty>
</blocks>

<helpers>
<warrenty>
<class>Form_Warrenty_Helper</class>
</warrenty>
</helpers>
</global>

<adminhtml> // to display in admin panel and if menu is there then use childern tag.
<menu>
<warrenty translate=”title” module=”warrenty”>
<title>Warrenty</title>
<sort_order>200</sort_order>
<action>warrenty/data/new</action>
</warrenty>
</menu>
</adminhtml>

<frontend>
<routers>
<warrenty>
<use>standard</use>
<args>
<module>Form_Warrenty</module>
<frontName>warrenty</frontName> // don’t forget to give this name as you use in previous in this file..
</args>
</warrenty>
</routers>

</frontend>

</config>

After that create table warrenty in magento database.. with this script..
DROP TABLE IF EXISTS `magento_vas`.`warrenty`;
CREATE TABLE  `magento_vas`.`warrenty` (
`id` int(10) unsigned NOT NULL auto_increment,
`title` varchar(45) default NULL,
`fname` varchar(255) default

NULL,
`lname` varchar(255) default NULL,
`address` varchar(255) default NULL,
`town` varchar(255) default NULL,
`pcode` varchar(255) default NULL,

`state` varchar(255) default NULL,
`mail` varchar(255) default NULL,
`daytime` varchar(255) default NULL,
`isyes` varchar(45) default NULL,
`brand`

varchar(45) default NULL,
`series` varchar(45) default NULL,
`size` varchar(45) default NULL,
`day` varchar(45) default NULL,
`month` varchar(45)

default NULL,
`year` varchar(45) default NULL,
`store` varchar(45) default NULL,
PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Now go to app/code/local/Form/Warrenty/Block/Form.php
and in that write..

class Form_Warrenty_Block_Form extends Mage_Core_Block_Template
{
public function __construct()
{
parent::__construct();
$this->setTemplate(‘warrenty/form.phtml’);
Mage::app()->getFrontController()->getAction()->getLayout()->getBlock(‘root’)->setHeaderTitle(Mage::helper(‘warrenty’)->__(‘Warrenty’));
}
}

OK now go to app/design/frontend/default/default/template/warrenty/form.phtml

and create form like this..
//its a one kind of form..
<?php
?>
<h2 align=”center”>Contact Details</h2>
<form id=”warrenty” action=”add” method=”post” name=”warrenty”>
<table width=”100%”>
<tr>
<td>Title:</td>
<td height=”31″>
<select name=”title” id=”title”>
<option>Choose</option>
<option>Mr.</option>
<option>Mrs.</option>
<option>Miss.</option>
<option>Ms.</option>
</select>
</td>
</tr>
<tr></tr>
<tr>
<td height=”31″>First Name:</td>
<td height=”31″><input id=”fname” type=”text” name=”fname” /></td>
<td height=”31″>Last Name:</td>
<td height=”31″><input id=”lname” type=”text” name=”lname” /></td>
</tr>
<tr>
<td height=”31″>Street Address:</td>
<td height=”31″><input  id=”address” name=”address” type=”text”></input></td>
</tr>
<tr>
<td height=”31″>Suburb/Town:</td>
<td height=”31″ width=”200″><input id=”town” type=”text” name=”town” /></td>
<td height=”31″>Post Code:</td>
<td height=”31″><input id=”pcode” type=”text” name=”pcode” /></td>
</tr>
<tr>
<td>State:</td>
<td height=”31″>
<select name=”state” id=”state”>
<option>Choose</option>
<option>Gujarat</option>
<option>Maharashtra</option>
<option>Rajasthan</option>
<option>Chennai</option>
</select>
</td>
</tr>
<tr>
<td height=”31″>Email:</td>
<td height=”31″><input id=”mail” type=”text” name=”mail” /></td>
</tr>
<tr>
<td></td>
<td colspan=”2″>(Please double check your email address)</td>
</tr>
<tr>
<td>Daytime Ph No.:</td>
<td height=”31″><input id=”daytime” type=”text” name=”daytime” /></td></td>
</tr>

<tr>
<td></td>
<td colspan=”4″>Do you wish to receive new product info from Magento Comm. Pvt. Ltd </td>
</tr>
<tr>
<td></td>
<td><input type=”checkbox” name=”isyes” id=”isyes”>&nbsp;&nbsp;&nbsp;Yes</td>
</tr>

<tr>
<td></td>
<td>
<h2 align=”center”>Reel Purchase Details</h2>
</td>
</tr>
<tr>
<td>Brand:</td>
<td height=”31″>
<select name=”brand” id=”brand”>
<option>First</option>
<option>Second</option>
</select></td>
</tr>
<tr>
<td height=”31″>Series Name:</td>
<td><input type=”text” name=”series” id=”series” /></td>
</tr>
<tr>
<td height=”31″> Model Size:</td>
<td><input type=”text” name=”size” id=”size”></td>
</tr>

<tr>
<td>Purchase Date:</td>
<td>
<select name=”day” id=”day”>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select>

<select name=”month” id=”month”>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select>

<select name=”year” id=”year”>
<option>2005</option>
<option>2006</option>
<option>2007</option>
</select>
</td>
</tr>
<tr>
<td>Retail Store Name:</td>
<td><input type=”text” id=”store” name=”store”></td>
</tr>
</form>
<tr>
<td><input type=”submit”  value=”SEND”> </td>
</tr>
</table>

Here your design has been completed and remeber when you create any .phtml file then it will call function from its block file.

Now go to app/code/local/Form/Warrenty/Helper/Data.php
//in that write..

class Form_Warrenty_Helper_Data extends Mage_Core_Helper_Abstract
{

}

Now go to controller and create one controller.
app/code/local/Form/Warrenty/controllers/FormController.php // don’t forget to define routers in config.xml for using controller..
in that create action. and in the block file we have created one line to set action and so form that line now it will come here in default indexAction

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

$this->getLayout()->getBlock(‘content’)->append(
$this->getLayout()->createBlock(‘warrenty/form’)
);
$this->getLayout()->getBlock(‘head’)->setTitle($this->__(‘Warrenty’));

$this->renderLayout();
}
//now here we have just created one block and we have append it to content main block and we have render the layout..

//now in the form we have given one action “add” so from that it will go to this action addAction so create one new action for that.. Always first of

all do some echo statement for testing the action.

public function addAction()
{
$data = $this->getRequest()->getPost(); // get all the data which is posted

$form = Mage::getModel(‘warrenty/warrenty’) // get model
->setData($this->getRequest()->getPost()); // set all the data

//$form->addDataNew($form);

$errors = $form->validate(); // put validate function in the model file.

try
{
if(empty($errors))
{
}
else
{
Mage::getSingleton(‘core/session’)->setWarrentyFormData($this->getRequest()->getPost()); // create object of session
if (is_array($errors)) {
foreach ($errors as $errorMessage) {
Mage::getSingleton(‘core/session’)->addError($errorMessage); // if any error the add error to the $errors
$this->_redirect(‘warrenty/form/’); // redirect to the same page..
}
}
else {
$form->save(); // else call save method..
Mage::getSingleton(‘core/session’)->addSuccess($this->__(‘Your Record has been inserted’)); //display message
$this->_redirect(‘warrenty/form/’); // redirect to the same page..
}
}
}
catch (Mage_Core_Exception $e) {
Mage::getSingleton(‘core/session’)->addError($e->getMessage())
->setWarrentyFormData($this->getRequest()->getPost());
$this->_redirect(‘warrenty/form/’);
}

catch (Exception $e) {
Mage::getSingleton(‘core/session’)->setWarrentyFormData($this->getRequest()->getPost())
->addException($e, $this->__(‘Can\’t save Data’));

$this->_redirect(‘warrenty/form/’);
}
}
}

//AnotherMEthodAnotherMEthodAnotherMEthodAnotherMEthodAnotherMEthodAnotherMEthodAnotherMEthodAnotherMEthodAnotherMEthodAnotherMEthod

public function addAction()
{
//now get object of model class and redirect to that model class and from that redirect it to model resource class and there call insert query..
// in the simle model class means it will come directly in model folder and in that query can’t fire while the resource model class means this file

will come in model/mysql4 folder so its path will be model/mysql4/filename.php and in that query can fire.. so i use this method also for that…

$data = $this->getRequest()->getPost();
$form = Mage::getModel(‘warrenty/warrenty’); //it will go to app/code/local/Form/Warrenty/Model/Warrenty.php
$form->addDataNew($data); // now create one function in model file newData(); and from that call get instance of resource model class and call the

function of that model file and execute the query..
}

Now go to app/code/local/Form/Warrenty/Model/Warrenty.php

class Form_Warrenty_Model_Warrenty extends Mage_Core_Model_Abstract
{
protected function _construct()
{
$this->_init(‘warrenty/warrenty’);
}

public function addDataNew($form)  // function for another method
{

$d = $this->_getResource()->newData($form);// it will go to resource file and execute the function newData();
}

public function validate() //this function is for validation on all the fields..
{
$errors = array();
if($this->getTitle() == ‘Choose’)
{
//if(!Zend_Validate::is(trim($this->getTitle()), ‘NotEmpty’)) {
$errors[] = Mage::helper(‘warrenty’)->__(‘Please Select Title’);
}
if(!Zend_Validate::is( trim($this->getFname()), ‘NotEmpty’)) {
$errors[] = Mage::helper(‘warrenty’)->__(‘Please Enter First Name’);
}
else {
if(!Zend_Validate::is( trim($this->getFname()), ‘Alpha’)) {
$errors[] = Mage::helper(‘warrenty’)->__(‘Please Enter Only Alphabet in FirstName’);
}
}

if(!Zend_Validate::is( trim($this->getLname()), ‘NotEmpty’)) {
$errors[] = Mage::helper(‘warrenty’)->__(‘Please Enter Last Name’);
}
else {
if(!Zend_Validate::is( trim($this->getLname()), ‘Alpha’)) {
$errors[] = Mage::helper(‘warrenty’)->__(‘Please Enter Only Alphabet in lastName’);
}
}

if(!Zend_Validate::is( trim($this->getAddress()), ‘NotEmpty’)) {
$errors[] = Mage::helper(‘warrenty’)->__(‘Please Enter Address’);
}

if(!Zend_Validate::is(trim($this->getTown()), ‘NotEmpty’)) {
$errors[] = Mage::helper(‘warrenty’)->__(‘Please Enter Your Town Name’);
}

if(!Zend_Validate::is(trim($this->getPcode()), ‘NotEmpty’)) {
$errors[] = Mage::helper(‘warrenty’)->__(‘Please Enter Pincode’);
}

if($this->getState() == ‘Choose’)
{
$errors[] = Mage::helper(‘warrenty’)->__(‘Please Select State’);
}

if(!Zend_Validate::is(trim($this->getMail()), ‘NotEmpty’)) {
$errors[] = Mage::helper(‘warrenty’)->__(‘Please Enter Email Address’);
}
else
{
if(!Zend_Validate::is(trim($this->getMail()), ‘EmailAddress’)) {
$errors[] = Mage::helper(‘warretny’)->__(‘Please Enter Valid Email-id’);
}
}

if(!Zend_Validate::is(trim($this->getDaytime()), ‘NotEmpty’)) {
$errors[] = Mage::helper(‘warrenty’)->__(‘Please Enter Your Daytime No.’);
}

/*if(!Zend_Validate::is(trim($this->getIsyes()), ‘NotEmpty’)) {
$errors = Mage::helper(‘warrenty’)->__(‘Please Enter ‘)
}*/

if($this->getBrand() == ‘Choose’)
{
$errors[] = Mage::helper(‘warrenty’)->__(‘Please Select Brand’);
}

if(!Zend_Validate::is(trim($this->getSeries()), ‘NotEmpty’)) {
$errors[] = Mage::helper(‘warrenty’)->__(‘Please Enter Series Number’);
}

if(!Zend_Validate::is(trim($this->getSize()), ‘NotEmpty’)) {
$errors[] = Mage::helper(‘warrenty’)->__(‘Please Enter Size’);
}

/*if(!Zend_Validate::is(trim($this->getDay()), ‘NotEmpty’)) {
$errors[] = Mage::helper(‘warrenty’)->__(‘Please Select Day’);
}*/

if(!Zend_Validate::is(trim($this->getStore()), ‘NotEmpty’)) {
$errors[] = Mage::helper(‘warrenty’)->__(‘Please Enter Store name’);
}
if(empty($errors))
{
return true;
}
return $errors;
}

public function fetchData() // this is used for admin side display data..
{
return $this->_getResource()->getFname();
}
}

//crerate folder Mysql4 and in that Warrenty.php
so app/code/local/Form/Warrenty/Model/Mysql4/Warrenty.php

class Form_Warrenty_Model_Mysql4_Warrenty extends Mage_Core_Model_Mysql4_Abstract
{
protected function _construct() // init function
{
//2 post
$this->_init(‘warrenty/warrenty’, ‘id’); //always pass the primary key of table if dont’t pass it will take default id as key
}

public function newData($form) // function for another method..
{
// here you have two methods one is execute insert query or another is execute default method save..

// first one

$this->_getWriteAdapter()->insert($this->getTable(‘warrenty’), $data);

//ororororororororororororororororororororororororororororororororororororororororororor

$form->save();

echo “Your record has been inserted”;
}

public function getFname()
{
$select = $this->_getReadAdapter()->select() // create adapter for read and select..
->from($this->getTable(‘warrenty’)); // get table and you can use also config file syntax for that please see customer module..
$all = $this->_getReadAdapter()->fetchAll($select);
return $all;
}

//now for admin panel to display it in menu you have to create cnofig.xml as i have created and in that action specify module name ,controller name

,action name so it will go directly there.. in our example i have given..warrenty/data/new

//and you have to create one another file in block also b’cos .phtml file will  so go to

app/code/local/Form/Warrenty/Block/Data.php
//in that write…

class Form_Warrenty_Block_Data extends Mage_Adminhtml_Block_Template
{
public function __construct()
{
parent::__construct();
$this->setTemplate(‘warrenty/new.phtml’);
}

public function getFirstName()
{
$form = Mage::getModel(‘warrenty/warrenty’);
$d = $form->fetchData();
return $d;
}
}

now go to controller and ..so i have to create new controller called DataController.php and in that newAction so ..

app/code/local/Form/Warrenty/controllers/DataController.php
//in that write..
class Form_Warrenty_DataController extends Mage_Adminhtml_Controller_Action
{

/*public function __construct()
{
parent::_construct;
$this->setTemplate(‘warrenty/new.phtml’);
}*/

public function newAction()
{
//$this->setTemplate(‘warrenty/new.phtml’);
$this->loadLayout();
$this->getLayout()->getBlock(‘content’)->append(
$this->getLayout()->createBlock(‘warrenty/data’)
);
$this->_setActiveMenu(‘warrenty’);
$this->_addBreadcrumb(Mage::helper(‘adminhtml’)->__(‘Warrenty’), Mage::helper(‘adminhtml’)->__(‘Warrenty’));
$this->renderLayout();

$form = Mage::getModel(‘warrenty/warrenty’); // so now it will go to model class file and in that find fetch data. please see our previous model file

$form->fetchData();                    in that this function will be there and from that it will go to resource model and in that our select

query will execute..

}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: