PHP Magento Tips

Create Custom Contact Inquiry Form With Captcha in Magento

Rate this posting:
{[['']]}

In Last post which is "Save Contact Forms Data to Database and Add Default Captcha To Contact Form In Magento" we learn how to modify Magento Contact form.

In this post we will create new contact Inquiry form and add default Mageto Captcha in this Custom Contact Inquiry Form.

Step 1: Create Module initialization file. Create file in app/etc/modules/Mycompany_Mycontact.xml and add below code in it.

<?xml version="1.0"?>
<config>
    <modules>
        <Mycompany_Mycontact>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Core />
             </depends>
        </Mycompany_Mycontact>
    </modules>
</config>

Step 2: Create Module's adminhtml, system and config.xml files.

a:) Create file in app/code/local/Mycompany/Mycontact/etc/adminhtml.xml and add below code in it.

<?xml version="1.0"?>
<config>
    <acl>
        <resources>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <mycompany_mycontact translate="title" module="mycompany_mycontact">
                                        <title>Mycontact</title>
                                    </mycompany_mycontact>
                                </children>
                            </config>
                        </children>
                    </system>
                    <mycompany_mycontact translate="title" module="mycompany_mycontact">
                        <title>Customer Inquiry Details</title>
                        <children>
                            <contactinquiry translate="title" module="mycompany_mycontact">
                                <title>Contact Inquiry Data</title>
                                <sort_order>0</sort_order>
                            </contactinquiry>
                        </children>
                    </mycompany_mycontact>
                </children>
            </admin>
        </resources>
    </acl>
    <menu>
     <customer>
      <children>
          <mycompany_mycontact translate="title" module="mycompany_mycontact">
              <title>Customer Inquiry Details</title>
              <sort_order>100</sort_order>
              <children>
                  <contactinquiry translate="title" module="mycompany_mycontact">
                      <title>Contact Inquiry Data</title>
                      <action>adminhtml/mycontact_contactinquiry</action>
                      <sort_order>100</sort_order>
                  </contactinquiry>
              </children>
          </mycompany_mycontact>
          </children>
     </customer>
    </menu>
</config>

b:) Create file in app/code/local/Mycompany/Mycontact/etc/config.xml and add below code in it.

<?xml version="1.0"?>
<config>
    <modules>
        <Mycompany_Mycontact>
            <version>1.0.0</version>
        </Mycompany_Mycontact>
    </modules>
    <global>
        <resources>
            <mycompany_mycontact_setup>
                <setup>
                    <module>Mycompany_Mycontact</module>
                    <class>Mycompany_Mycontact_Model_Resource_Setup</class>
                </setup>
            </mycompany_mycontact_setup>
        </resources>
        <blocks>
            <mycompany_mycontact>
                <class>Mycompany_Mycontact_Block</class>
            </mycompany_mycontact>
        </blocks>
        <helpers>
            <mycompany_mycontact>
                <class>Mycompany_Mycontact_Helper</class>
            </mycompany_mycontact>
        </helpers>
        <models>
            <mycompany_mycontact>
                <class>Mycompany_Mycontact_Model</class>
                <resourceModel>mycompany_mycontact_resource</resourceModel>
            </mycompany_mycontact>
            <mycompany_mycontact_resource>
                <class>Mycompany_Mycontact_Model_Resource</class>
                <entities>
                    <contactinquiry>
                        <table>mycompany_mycontact_contactinquiry</table>
                    </contactinquiry>
                </entities>
            </mycompany_mycontact_resource>
        </models>
        <events>
         <!-- for url rewrite -->
            <controller_front_init_routers>
                <observers>
                    <mycompany_mycontact>
                        <class>Mycompany_Mycontact_Controller_Router</class>
                        <method>initControllerRouters</method>
                    </mycompany_mycontact>
                </observers>
            </controller_front_init_routers>
            <!-- for url rewrite -->
            <!-- to add default captcha -->
            <controller_action_predispatch_mycontact_contactinquiry_post>
                <observers>
                    <mycompany_mycontact>
                        <class>mycompany_mycontact/observer</class>
                        <method>checkContacts</method>
                    </mycompany_mycontact>
                </observers>
            </controller_action_predispatch_mycontact_contactinquiry_post>
            <!-- to add default captcha -->
        </events>
        <template>
            <email>
                <requestinquiry_email_success_email_template translate="label" module="mycompany_mycontact">
                    <label>Success Inquiry Template</label>
                    <file>mycompany/success_contact.html</file>
                    <type>html</type>
                </requestinquiry_email_success_email_template>
                <requestinquiry_email_confirm_email_template translate="label" module="mycompany_mycontact">
                    <label>Request Confirm Template</label>
                    <file>mycompany/request_contact.html</file>
                    <type>html</type>
                </requestinquiry_email_confirm_email_template>
            </email>
        </template>
    </global>
    <adminhtml>
        <layout>
            <updates>
                <mycompany_mycontact>
                    <file>mycompany_mycontact.xml</file>
                </mycompany_mycontact>
            </updates>
        </layout>
    </adminhtml>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Mycompany_Mycontact before="Mage_Adminhtml">Mycompany_Mycontact_Adminhtml</Mycompany_Mycontact>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <frontend>
        <events>
            <page_block_html_topmenu_gethtml_before>
                <observers>
                    <mycompany_mycontact>
                        <class>mycompany_mycontact/observer</class>
                        <method>addItemsToTopmenuItems</method>
                    </mycompany_mycontact>
                </observers>
            </page_block_html_topmenu_gethtml_before>
        </events>

        <routers>
            <mycompany_mycontact>
                <use>standard</use>
                <args>
                    <module>Mycompany_Mycontact</module>
                    <frontName>contact-inquiry</frontName>
                </args>
            </mycompany_mycontact>
        </routers>
        <layout>
            <updates>
                <mycompany_mycontact>
                    <file>mycompany_mycontact.xml</file>
                </mycompany_mycontact>
            </updates>
        </layout>
    </frontend>
    <default>
        <mycompany_mycontact>
            <contactinquiry>
                <breadcrumbs>1</breadcrumbs>
                <contactinquiry_custom_field_one>0</contactinquiry_custom_field_one>
                <contactinquiry_custom_field_two>0</contactinquiry_custom_field_two>
            </contactinquiry>
        </mycompany_mycontact>
        <contactsubscription>
             <confirm_email_template>1</confirm_email_template>
             <success_email_template>2</success_email_template>
             <success_email_identity>support</success_email_identity>
             <confirm_email_identity>sales</confirm_email_identity>
        </contactsubscription>
        <!-- to add default captcha -->
        <captcha>
            <frontend>
                <areas>
                    <mycompany_mycontact><!-- take frontend router node -->
                        <label>Contacts Inquiry Page</label>
                    </mycompany_mycontact>
                </areas>
            </frontend>
        </captcha>
        <customer>
            <captcha>
                <always_for>
                    <mycompany_mycontact>1</mycompany_mycontact>
                </always_for>
            </captcha>
        </customer>
        <!-- to add default captcha -->
    </default>
</config>

C:) Create file in app/code/local/Mycompany/Mycontact/etc/system.xml and add below code in it.

<?xml version="1.0"?>
<config>
    <tabs>
        <mycompany translate="label" module="mycompany_mycontact">
            <label>Mycompany</label>
            <sort_order>0</sort_order>
        </mycompany>
    </tabs>
    <sections>
        <mycompany_mycontact translate="label" module="mycompany_mycontact">
            <class>separator-top</class>
            <label>Customer Inquiry Details</label>
            <tab>mycompany</tab>
            <frontend_type>text</frontend_type>
            <sort_order>100</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <contactinquiry translate="label" module="mycompany_mycontact">
                    <label>Contact Inquiry Data</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>0</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <breadcrumbs translate="label">
                            <label>Use Breadcrumbs</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <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>
                        </breadcrumbs>
                        <contactinquiry_custom_field_one>
                            <label>Is Pincode Required?</label>
                            <frontend_type>select</frontend_type>
                            <sort_order>1</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_yesno</source_model>
                            <comment>Yes/No</comment>                   
                        </contactinquiry_custom_field_one>
                        <contactinquiry_custom_field_two>
                            <label>Is City Required?</label>
                            <frontend_type>select</frontend_type>
                            <sort_order>2</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_yesno</source_model>
                            <comment>Yes/No</comment>                   
                        </contactinquiry_custom_field_two>
                        <!-- for url rewrite -->
                        <url_rewrite_list translate="label comment">
                            <label>URL key for list page</label>
                            <frontend_type>text</frontend_type>
                            <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>Leave empty to use default URL module/controller/action</comment>
                        </url_rewrite_list>
                        <url_prefix translate="label comment">
                            <label>URL prefix</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>30</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <comment>Leave empty for no prefix</comment>
                        </url_prefix>
                        <url_suffix translate="label comment">
                            <label>Url suffix</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>40</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <comment>What goes after the dot. Leave empty for no suffix.</comment>
                        </url_suffix>
                        <!-- for url rewrite -->
                    </fields>
                </contactinquiry>
                 <contactsubscription translate="label">
                    <label>Inquiry Email Options</label>
                    <frontend_type>text</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>
                    <fields>
                        <confirm_email_template translate="label">
                            <label>Request Email Template (For Admin)</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_email_template</source_model>
                            <sort_order>1</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </confirm_email_template>
                        <success_email_template translate="label">
                            <label>Success Email Template (To Send User)</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_email_template</source_model>
                            <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>
                        </success_email_template>
      <success_email_identity translate="label">
                            <label>Success Email Sender</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_email_identity</source_model>
                            <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>
                        </success_email_identity>
      <confirm_email_identity translate="label">
                            <label>Request Email Reciever</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_email_identity</source_model>
                            <sort_order>30</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </confirm_email_identity>
                    </fields>
                </contactsubscription>
            </groups>
        </mycompany_mycontact>
    </sections>
</config>

Step 3: Create Sql installer file. Create file in app/code/local/Mycompany/Mycontact/sql/mycompany_mycontact_setup/install-1.0.0.php and add below code in it.

<?php

$this->startSetup();
$table = $this->getConnection()
    ->newTable($this->getTable('mycompany_mycontact/contactinquiry'))
    ->addColumn(
        'entity_id',
        Varien_Db_Ddl_Table::TYPE_INTEGER,
        null,
        array(
            'identity'  => true,
            'nullable'  => false,
            'primary'   => true,
        ),
        'Contact Inquiry Data ID'
    )
    ->addColumn(
        'name',
        Varien_Db_Ddl_Table::TYPE_TEXT, 255,
        array(
            'nullable'  => false,
        ),
        'Name'
    )
    ->addColumn(
        'email',
        Varien_Db_Ddl_Table::TYPE_TEXT, 255,
        array(
            'nullable'  => false,
        ),
        'Email'
    )
    ->addColumn(
        'mobile',
        Varien_Db_Ddl_Table::TYPE_TEXT, 255,
        array(
            'nullable'  => false,
        ),
        'Mobile'
    )
    ->addColumn(
        'city',
        Varien_Db_Ddl_Table::TYPE_TEXT, 255,
        array(),
        'City'
    )
    ->addColumn(
        'pincode',
        Varien_Db_Ddl_Table::TYPE_TEXT, 255,
        array(),
        'Pincode'
    )
    ->addColumn(
        'status',
        Varien_Db_Ddl_Table::TYPE_SMALLINT, null,
        array(),
        'Enabled'
    )
    ->addColumn(
        'updated_at',
        Varien_Db_Ddl_Table::TYPE_TIMESTAMP,
        null,
        array(),
        'Contact Inquiry Data Modification Time'
    )
    ->addColumn(
        'created_at',
        Varien_Db_Ddl_Table::TYPE_TIMESTAMP,
        null,
        array(),
        'Contact Inquiry Data Creation Time'
    ) 
    ->setComment('Contact Inquiry Data Table');
$this->getConnection()->createTable($table);
$this->endSetup();

Step 4: Create Helper Files.
a:) Create app/code/local/Mycompany/Mycontact/Helper/Data.php file and add below code in it.

<?php

class Mycompany_Mycontact_Helper_Data extends Mage_Core_Helper_Abstract
{
    /**
     * convert array to options
     *
     * @access public
     * @param $options
     * @return array
     * @author Ultimate Module Creator
     */
    public function convertOptions($options)
    {
        $converted = array();
        foreach ($options as $option) {
            if (isset($option['value']) && !is_array($option['value']) &&
                isset($option['label']) && !is_array($option['label'])) {
                $converted[$option['value']] = $option['label'];
            }
        }
        return $converted;
    }
}

b:) Create app/code/local/Mycompany/Mycontact/Helper/Contactinquiry.php file and add below code in it.

<?php 

class Mycompany_Mycontact_Helper_Contactinquiry extends Mage_Core_Helper_Abstract
{

    /**
     * get the url to the contact inquiry datas list page
     *
     * @access public
     * @return string
     * @author Ultimate Module Creator
     */
    public function getContactinquiriesUrl()
    {
        if ($listKey = Mage::getStoreConfig('mycompany_mycontact/contactinquiry/url_rewrite_list')) {
            return Mage::getUrl('', array('_direct'=>$listKey));
        }
        return Mage::getUrl('mycompany_mycontact/contactinquiry/index');
    }

    /**
     * check if breadcrumbs can be used
     *
     * @access public
     * @return bool
     * @author Ultimate Module Creator
     */
    public function getUseBreadcrumbs()
    {
        return Mage::getStoreConfigFlag('mycompany_mycontact/contactinquiry/breadcrumbs');
    }
}

Step 5: Create Model Files
a:) Create file in app/code/local/Mycompany/Mycontact/Model/Contactinquiry.php and add below code in it.

<?php

class Mycompany_Mycontact_Model_Contactinquiry extends Mage_Core_Model_Abstract
{
    /**
     * Entity code.
     * Can be used as part of method name for entity processing
     */
    const ENTITY    = 'mycompany_mycontact_contactinquiry';
    const CACHE_TAG = 'mycompany_mycontact_contactinquiry';

    /**
     * Prefix of model events names
     *
     * @var string
     */
    protected $_eventPrefix = 'mycompany_mycontact_contactinquiry';

    /**
     * Parameter name in event
     *
     * @var string
     */
    protected $_eventObject = 'contactinquiry';

    /**
     * constructor
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function _construct()
    {
        parent::_construct();
        $this->_init('mycompany_mycontact/contactinquiry');
    }

    /**
     * before save contact inquiry data
     *
     * @access protected
     * @return Mycompany_Mycontact_Model_Contactinquiry
     * @author Ultimate Module Creator
     */
    protected function _beforeSave()
    {
        parent::_beforeSave();
        $now = Mage::getSingleton('core/date')->gmtDate();
        if ($this->isObjectNew()) {
            $this->setCreatedAt($now);
        }
        $this->setUpdatedAt($now);
        return $this;
    }

    /**
     * save contact inquiry data relation
     *
     * @access public
     * @return Mycompany_Mycontact_Model_Contactinquiry
     * @author Ultimate Module Creator
     */
    protected function _afterSave()
    {
        return parent::_afterSave();
    }

    /**
     * get default values
     *
     * @access public
     * @return array
     * @author Ultimate Module Creator
     */
    public function getDefaultValues()
    {
        $values = array();
        $values['status'] = 1;
        return $values;
    }
    
}

b:) Create file in app/code/local/Mycompany/Mycontact/Model/Observer.php and add below code in it.

Here we create checkContacts function for captcha.

<?php

class Mycompany_Mycontact_Model_Observer
{
    /**
     * add items to main menu
     *
     * @access public
     * @param Varien_Event_Observer $observer
     * @return array()
     * @author Ultimate Module Creator
     */
    public function addItemsToTopmenuItems($observer)
    {
        $menu = $observer->getMenu();
        $tree = $menu->getTree();
        $action = Mage::app()->getFrontController()->getAction()->getFullActionName();
        $contactinquiryNodeId = 'contactinquiry';
        $data = array(
            'name' => Mage::helper('mycompany_mycontact')->__('Contact Inquiry'),
            'id' => $contactinquiryNodeId,
            'url' => Mage::helper('mycompany_mycontact/contactinquiry')->getContactinquiriesUrl(),
            'is_active' => ($action == 'mycompany_mycontact_contactinquiry_index' || $action == 'mycompany_mycontact_contactinquiry_view')
        );
        $contactinquiryNode = new Varien_Data_Tree_Node($data, 'id', $tree, $menu);
        $menu->addChild($contactinquiryNode);
        return $this;
    }
    public function checkContacts($observer){
     $formId = 'mycompany_mycontact';
     $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
     if ($captchaModel->isRequired()) {
      $controller = $observer->getControllerAction();
      $word = $this->_getCaptchaString($controller->getRequest(), $formId);
      if (!$captchaModel->isCorrect($word)) {
       Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
       $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
       $url =  Mage::getUrl('mycompany_mycontact');
       $controller->getResponse()->setRedirect($url);
      }
     }
     return $this;
    }
}

C:) Create file in app/code/local/Mycompany/Mycontact/Model/Resource/Contactinquiry.php and add below code in it.

<?php

class Mycompany_Mycontact_Model_Resource_Contactinquiry extends Mage_Core_Model_Resource_Db_Abstract
{

    /**
     * constructor
     *
     * @access public
     * @author Ultimate Module Creator
     */
    public function _construct()
    {
        $this->_init('mycompany_mycontact/contactinquiry', 'entity_id');
    }
}


D:) Create file in app/code/local/Mycompany/Mycontact/Model/Resource/Setup.php and add below code in it.

<?php

class Mycompany_Mycontact_Model_Resource_Setup extends Mage_Core_Model_Resource_Setup
{
}

E:) Create file in app/code/local/Mycompany/Mycontact/Model/Resource/Contactinquiry/Collection.php and add below code in it.

<?php

class Mycompany_Mycontact_Model_Resource_Contactinquiry_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
    protected $_joinedFields = array();

    /**
     * constructor
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    protected function _construct()
    {
        parent::_construct();
        $this->_init('mycompany_mycontact/contactinquiry');
    }

    /**
     * get contact inquiry datas as array
     *
     * @access protected
     * @param string $valueField
     * @param string $labelField
     * @param array $additional
     * @return array
     * @author Ultimate Module Creator
     */
    protected function _toOptionArray($valueField='entity_id', $labelField='name', $additional=array())
    {
        return parent::_toOptionArray($valueField, $labelField, $additional);
    }

    /**
     * get options hash
     *
     * @access protected
     * @param string $valueField
     * @param string $labelField
     * @return array
     * @author Ultimate Module Creator
     */
    protected function _toOptionHash($valueField='entity_id', $labelField='name')
    {
        return parent::_toOptionHash($valueField, $labelField);
    }

    /**
     * Get SQL for get record count.
     * Extra GROUP BY strip added.
     *
     * @access public
     * @return Varien_Db_Select
     * @author Ultimate Module Creator
     */
    public function getSelectCountSql()
    {
        $countSelect = parent::getSelectCountSql();
        $countSelect->reset(Zend_Db_Select::GROUP);
        return $countSelect;
    }
}

Step 6: Create Controller Files and folders.

a:) Create file in app/code/local/Mycompany/Mycontact/Controller/Router.php and add below code in it.

<?php 

class Mycompany_Mycontact_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract
{
    /**
     * init routes
     *
     * @access public
     * @param Varien_Event_Observer $observer
     * @return Mycompany_Mycontact_Controller_Router
     * @author Ultimate Module Creator
     */
    public function initControllerRouters($observer)
    {
        $front = $observer->getEvent()->getFront();
        $front->addRouter('mycompany_mycontact', $this);
        return $this;
    }

    /**
     * Validate and match entities and modify request
     *
     * @access public
     * @param Zend_Controller_Request_Http $request
     * @return bool
     * @author Ultimate Module Creator
     */
    public function match(Zend_Controller_Request_Http $request)
    {
        if (!Mage::isInstalled()) {
            Mage::app()->getFrontController()->getResponse()
                ->setRedirect(Mage::getUrl('install'))
                ->sendResponse();
            exit;
        }
        $urlKey = trim($request->getPathInfo(), '/');
        $check = array();
        $check['contactinquiry'] = new Varien_Object(
            array(
                'prefix'        => Mage::getStoreConfig('mycompany_mycontact/contactinquiry/url_prefix'),
                'suffix'        => Mage::getStoreConfig('mycompany_mycontact/contactinquiry/url_suffix'),
                'list_key'      => Mage::getStoreConfig('mycompany_mycontact/contactinquiry/url_rewrite_list'),
                'list_action'   => 'index',
                'model'         =>'mycompany_mycontact/contactinquiry',
                'controller'    => 'contactinquiry',
                'action'        => 'view',
                'param'         => 'id',
                'check_path'    => 0
            )
        );
        foreach ($check as $key=>$settings) {
            if ($settings->getListKey()) {
                if ($urlKey == $settings->getListKey()) {
                    $request->setModuleName('contact-inquiry')
                        ->setControllerName($settings->getController())
                        ->setActionName($settings->getListAction());
                    $request->setAlias(
                        Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
                        $urlKey
                    );
                    return true;
                }
            }
            if ($settings['prefix']) {
                $parts = explode('/', $urlKey);
                if ($parts[0] != $settings['prefix'] || count($parts) != 2) {
                    continue;
                }
                $urlKey = $parts[1];
            }
            if ($settings['suffix']) {
                $urlKey = substr($urlKey, 0, -strlen($settings['suffix']) - 1);
            }
            $model = Mage::getModel($settings->getModel());
            $id = $model->checkUrlKey($urlKey, Mage::app()->getStore()->getId());
            if ($id) {
                if ($settings->getCheckPath() && !$model->load($id)->getStatusPath()) {
                    continue;
                }
                $request->setModuleName('contact-inquiry')
                    ->setControllerName($settings->getController())
                    ->setActionName($settings->getAction())
                    ->setParam($settings->getParam(), $id);
                $request->setAlias(
                    Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
                    $urlKey
                );
                return true;
            }
        }
        return false;
    }
}

b:) Create file in app/code/local/Mycompany/Mycontact/Controller/Adminhtml/Mycontact.php and add below code in it.

<?php

class Mycompany_Mycontact_Controller_Adminhtml_Mycontact extends Mage_Adminhtml_Controller_Action
{
    /**
     * upload file and get the uploaded name
     *
     * @access public
     * @param string $input
     * @param string $destinationFolder
     * @param array $data
     * @return string
     * @author Ultimate Module Creator
     */
    protected function _uploadAndGetName($input, $destinationFolder, $data)
    {
        try {
            if (isset($data[$input]['delete'])) {
                return '';
            } else {
                $uploader = new Varien_File_Uploader($input);
                $uploader->setAllowRenameFiles(true);
                $uploader->setFilesDispersion(true);
                $uploader->setAllowCreateFolders(true);
                $result = $uploader->save($destinationFolder);
                return $result['file'];
            }
        } catch (Exception $e) {
            if ($e->getCode() != Varien_File_Uploader::TMP_NAME_EMPTY) {
                throw $e;
            } else {
                if (isset($data[$input]['value'])) {
                    return $data[$input]['value'];
                }
            }
        }
        return '';
    }
}

Step 7: Create controllers files and folders:
a:) Create file in app/code/local/Mycompany/Mycontact/controllers/ContactinquiryController.php and add below code in it.

<?php

class Mycompany_Mycontact_ContactinquiryController extends Mage_Core_Controller_Front_Action
{
 const XML_PATH_EMAIL_CONFIRM_TEMPLATE  = 'mycompany_mycontact/contactsubscription/confirm_email_template';
 const XML_PATH_EMAIL_SUCCESS_TEMPLATE  = 'mycompany_mycontact/contactsubscription/success_email_template';
 const XML_PATH_EMAIL_RECIPIENT  = 'mycompany_mycontact/contactsubscription/confirm_email_identity';
 const XML_PATH_EMAIL_SENDER     = 'mycompany_mycontact/contactsubscription/success_email_identity';
    /**
      * default action
      *
      * @access public
      * @return void
      * @author Ultimate Module Creator
      */
    public function indexAction()
    {
        $this->loadLayout();
        $this->getLayout()->getBlock('contactinquiry_list')->setFormAction( Mage::getUrl('*/*/post', array('_secure' => $this->getRequest()->isSecure())) );
        $this->_initLayoutMessages('catalog/session');
        $this->_initLayoutMessages('customer/session');
        $this->_initLayoutMessages('checkout/session');
        if (Mage::helper('mycompany_mycontact/contactinquiry')->getUseBreadcrumbs()) {
            if ($breadcrumbBlock = $this->getLayout()->getBlock('breadcrumbs')) {
                $breadcrumbBlock->addCrumb(
                    'home',
                    array(
                        'label' => Mage::helper('mycompany_mycontact')->__('Home'),
                        'link'  => Mage::getUrl(),
                    )
                );
                $breadcrumbBlock->addCrumb(
                    'contactinquiries',
                    array(
                        'label' => Mage::helper('mycompany_mycontact')->__('Contact Inquiry'),
                        'link'  => '',
                    )
                );
            }
        }
        $headBlock = $this->getLayout()->getBlock('head');
        if ($headBlock) {
            $headBlock->addLinkRel('canonical', Mage::helper('mycompany_mycontact/contactinquiry')->getContactinquiriesUrl());
        }
        $this->renderLayout();
    }
    
    public function postAction()
    {
     $post = $this->getRequest()->getPost();
    
     if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {
      
      $translate = Mage::getSingleton('core/translate');
      /* @var $translate Mage_Core_Model_Translate */
      $translate->setTranslateInline(false);
      
      $session            = Mage::getSingleton('core/session');
      $customerSession    = Mage::getSingleton('customer/session');
      
      $firstname          = $this->getRequest()->getPost('name');
      $email              = (string) $this->getRequest()->getPost('email');
      $telephone          = $this->getRequest()->getPost('mobile');
      $pincode            = $this->getRequest()->getPost('pincode');
      $location           = $this->getRequest()->getPost('city');
    
      try {
       
       $postObject = new Varien_Object();
       $postObject->setData($post);
       
       $error = false;
       
       if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
        $error = true;
       }
       if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
        $error = true;
       }    
       if (!Zend_Validate::is(trim($post['mobile']) , 'NotEmpty')) {
        $error = true;
       }
       if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
        $error = true;
       }
       
       if ($error) {
        throw new Exception();
       }
       //Sending E-Mail to Admin.
       $emailTemplate  = Mage::getModel('core/email_template')->loadDefault('requestinquiry_email_confirm_email_template');   
       /* @var $mailTemplateAdmin Mage_Core_Model_Email_Template */
       $mail = Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT);
       $email_id = Mage::getStoreConfig('trans_email/ident_'.$mail.'/email');
       $emailTemplate->setDesignConfig(array('area' => 'frontend'))
       ->setReplyTo($post['email'])
       ->sendTransactional(
         Mage::getStoreConfig(self::XML_PATH_EMAIL_CONFIRM_TEMPLATE),
         Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
         $email_id,
         null,
         array('data' => $postObject)
       );
    
       //Sending E-Mail to Customers.
    $emailTemplateuser = Mage::getModel('core/email_template')->loadDefault('requestinquiry_email_success_email_template');
    /* @var $mailTemplate Mage_Core_Model_Email_Template */
    $emailTemplateuser->setDesignConfig(array('area' => 'frontend'))
    ->setReplyTo($post['email'])
    ->sendTransactional(
      Mage::getStoreConfig(self::XML_PATH_EMAIL_SUCCESS_TEMPLATE),
      Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
      $post['email'],
      null,
      array('data' => $postObject)
    );
    
       
       if (!$emailTemplate->getSentSuccess() && !$emailTemplateuser->getSentSuccess()) {
        throw new Exception();
       }
        
       $translate->setTranslateInline(true);
       
       $contact = Mage::getModel('mycompany_mycontact/contactinquiry');
       $contact->setData('name', $firstname);
       $contact->setData('email', $email);
       $contact->setData('mobile', $telephone);
       $contact->setData('city', $location);
       $contact->setData('pincode', $pincode);
       $contact->setData('status', 1);
       $contact->setData('updated_at', $dt);
       $contact->setData('created_date', $dt);
       $contact->save();
       
        
       Mage::getSingleton('customer/session')->addSuccess(Mage::helper('mycompany_mycontact')->__('Your Request has been received. One of our sales representatives will contact you shortly.'));
       $this->_redirectReferer();
    
      } catch (Mage_Core_Exception $e) {
       $session->addException($e, $this->__('There was a problem with sending enquiry mail: %s', $e->getMessage()));
       $this->_redirectReferer();
      }
      catch (Exception $e) {
       $session->addException($e, $this->__('There was a problem with sending enquiry mail.'));
       $this->_redirectReferer();
      }
    
     } else {
      $this->_redirect('*/*/');
     }
    }
}

b:) Create file in app/code/local/Mycompany/Mycontact/controllers/Adminhtml/Mycontact/ContactinquiryController.php and add below code in it.

<?php

class Mycompany_Mycontact_Adminhtml_Mycontact_ContactinquiryController extends Mycompany_Mycontact_Controller_Adminhtml_Mycontact
{
    /**
     * init the contact inquiry data
     *
     * @access protected
     * @return Mycompany_Mycontact_Model_Contactinquiry
     */
    protected function _initContactinquiry()
    {
        $contactinquiryId  = (int) $this->getRequest()->getParam('id');
        $contactinquiry    = Mage::getModel('mycompany_mycontact/contactinquiry');
        if ($contactinquiryId) {
            $contactinquiry->load($contactinquiryId);
        }
        Mage::register('current_contactinquiry', $contactinquiry);
        return $contactinquiry;
    }

    /**
     * default action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function indexAction()
    {
        $this->loadLayout();
        $this->_title(Mage::helper('mycompany_mycontact')->__('Customer Inquiry Details'))
             ->_title(Mage::helper('mycompany_mycontact')->__('Contact Inquiry Datas'));
        $this->renderLayout();
    }

    /**
     * grid action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function gridAction()
    {
        $this->loadLayout()->renderLayout();
    }

    /**
     * edit contact inquiry data - action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function editAction()
    {
        $contactinquiryId    = $this->getRequest()->getParam('id');
        $contactinquiry      = $this->_initContactinquiry();
        if ($contactinquiryId && !$contactinquiry->getId()) {
            $this->_getSession()->addError(
                Mage::helper('mycompany_mycontact')->__('This contact inquiry data no longer exists.')
            );
            $this->_redirect('*/*/');
            return;
        }
        $data = Mage::getSingleton('adminhtml/session')->getContactinquiryData(true);
        if (!empty($data)) {
            $contactinquiry->setData($data);
        }
        Mage::register('contactinquiry_data', $contactinquiry);
        $this->loadLayout();
        $this->_title(Mage::helper('mycompany_mycontact')->__('Customer Inquiry Details'))
             ->_title(Mage::helper('mycompany_mycontact')->__('Contact Inquiry Datas'));
        if ($contactinquiry->getId()) {
            $this->_title($contactinquiry->getName());
        } else {
            $this->_title(Mage::helper('mycompany_mycontact')->__('Add contact inquiry data'));
        }
        if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
            $this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
        }
        $this->renderLayout();
    }

    /**
     * new contact inquiry data action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function newAction()
    {
        $this->_forward('edit');
    }

    /**
     * save contact inquiry data - action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function saveAction()
    {
        if ($data = $this->getRequest()->getPost('contactinquiry')) {
            try {
                $contactinquiry = $this->_initContactinquiry();
                $contactinquiry->addData($data);
                $contactinquiry->save();
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('mycompany_mycontact')->__('Contact Inquiry Data was successfully saved')
                );
                Mage::getSingleton('adminhtml/session')->setFormData(false);
                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $contactinquiry->getId()));
                    return;
                }
                $this->_redirect('*/*/');
                return;
            } catch (Mage_Core_Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setContactinquiryData($data);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            } catch (Exception $e) {
                Mage::logException($e);
                Mage::getSingleton('adminhtml/session')->addError(
                    Mage::helper('mycompany_mycontact')->__('There was a problem saving the contact inquiry data.')
                );
                Mage::getSingleton('adminhtml/session')->setContactinquiryData($data);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        Mage::getSingleton('adminhtml/session')->addError(
            Mage::helper('mycompany_mycontact')->__('Unable to find contact inquiry data to save.')
        );
        $this->_redirect('*/*/');
    }

    /**
     * delete contact inquiry data - action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function deleteAction()
    {
        if ( $this->getRequest()->getParam('id') > 0) {
            try {
                $contactinquiry = Mage::getModel('mycompany_mycontact/contactinquiry');
                $contactinquiry->setId($this->getRequest()->getParam('id'))->delete();
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('mycompany_mycontact')->__('Contact Inquiry Data was successfully deleted.')
                );
                $this->_redirect('*/*/');
                return;
            } catch (Mage_Core_Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError(
                    Mage::helper('mycompany_mycontact')->__('There was an error deleting contact inquiry data.')
                );
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                Mage::logException($e);
                return;
            }
        }
        Mage::getSingleton('adminhtml/session')->addError(
            Mage::helper('mycompany_mycontact')->__('Could not find contact inquiry data to delete.')
        );
        $this->_redirect('*/*/');
    }

    /**
     * mass delete contact inquiry data - action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function massDeleteAction()
    {
        $contactinquiryIds = $this->getRequest()->getParam('contactinquiry');
        if (!is_array($contactinquiryIds)) {
            Mage::getSingleton('adminhtml/session')->addError(
                Mage::helper('mycompany_mycontact')->__('Please select contact inquiry datas to delete.')
            );
        } else {
            try {
                foreach ($contactinquiryIds as $contactinquiryId) {
                    $contactinquiry = Mage::getModel('mycompany_mycontact/contactinquiry');
                    $contactinquiry->setId($contactinquiryId)->delete();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('mycompany_mycontact')->__('Total of %d contact inquiry datas were successfully deleted.', count($contactinquiryIds))
                );
            } catch (Mage_Core_Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError(
                    Mage::helper('mycompany_mycontact')->__('There was an error deleting contact inquiry datas.')
                );
                Mage::logException($e);
            }
        }
        $this->_redirect('*/*/index');
    }

    /**
     * mass status change - action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function massStatusAction()
    {
        $contactinquiryIds = $this->getRequest()->getParam('contactinquiry');
        if (!is_array($contactinquiryIds)) {
            Mage::getSingleton('adminhtml/session')->addError(
                Mage::helper('mycompany_mycontact')->__('Please select contact inquiry datas.')
            );
        } else {
            try {
                foreach ($contactinquiryIds as $contactinquiryId) {
                $contactinquiry = Mage::getSingleton('mycompany_mycontact/contactinquiry')->load($contactinquiryId)
                            ->setStatus($this->getRequest()->getParam('status'))
                            ->setIsMassupdate(true)
                            ->save();
                }
                $this->_getSession()->addSuccess(
                    $this->__('Total of %d contact inquiry datas were successfully updated.', count($contactinquiryIds))
                );
            } catch (Mage_Core_Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError(
                    Mage::helper('mycompany_mycontact')->__('There was an error updating contact inquiry datas.')
                );
                Mage::logException($e);
            }
        }
        $this->_redirect('*/*/index');
    }

    /**
     * export as csv - action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function exportCsvAction()
    {
        $fileName   = 'contactinquiry.csv';
        $content    = $this->getLayout()->createBlock('mycompany_mycontact/adminhtml_contactinquiry_grid')
            ->getCsv();
        $this->_prepareDownloadResponse($fileName, $content);
    }

    /**
     * export as MsExcel - action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function exportExcelAction()
    {
        $fileName   = 'contactinquiry.xls';
        $content    = $this->getLayout()->createBlock('mycompany_mycontact/adminhtml_contactinquiry_grid')
            ->getExcelFile();
        $this->_prepareDownloadResponse($fileName, $content);
    }

    /**
     * export as xml - action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function exportXmlAction()
    {
        $fileName   = 'contactinquiry.xml';
        $content    = $this->getLayout()->createBlock('mycompany_mycontact/adminhtml_contactinquiry_grid')
            ->getXml();
        $this->_prepareDownloadResponse($fileName, $content);
    }

    /**
     * Check if admin has permissions to visit related pages
     *
     * @access protected
     * @return boolean
     * @author Ultimate Module Creator
     */
    protected function _isAllowed()
    {
        return Mage::getSingleton('admin/session')->isAllowed('mycompany_mycontact/contactinquiry');
    }
}

Step 8: Create Block Files and folders.

a:) Create file in app/code/local/Mycompany/Mycontact/Block/Contactinquiry/List.php and add below code in it.

<?php

class Mycompany_Mycontact_Block_Contactinquiry_List extends Mage_Core_Block_Template
{
    /**
     * prepare the layout
     *
     * @access protected
     * @return Mycompany_Mycontact_Block_Contactinquiry_List
     * @author Ultimate Module Creator
     */
    protected function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
}

b:) Create file in app/code/local/Mycompany/Mycontact/Block/Adminhtml/Contactinquiry.php and add below code in it.

<?php

class Mycompany_Mycontact_Block_Adminhtml_Contactinquiry extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    /**
     * constructor
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function __construct()
    {
        $this->_controller         = 'adminhtml_contactinquiry';
        $this->_blockGroup         = 'mycompany_mycontact';
        parent::__construct();
        $this->_headerText         = Mage::helper('mycompany_mycontact')->__('Contact Inquiry Data');
        $this->_updateButton('add', 'label', Mage::helper('mycompany_mycontact')->__('Add Contact Inquiry Data'));
        $this->_removeButton('add');
    }
}

C:) Create file in app/code/local/Mycompany/Mycontact/Block/Adminhtml/Contactinquiry/Edit.php and add below code in it.

<?php

class Mycompany_Mycontact_Block_Adminhtml_Contactinquiry_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
    /**
     * constructor
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function __construct()
    {
        parent::__construct();
        $this->_blockGroup = 'mycompany_mycontact';
        $this->_controller = 'adminhtml_contactinquiry';
        $this->_updateButton(
            'save',
            'label',
            Mage::helper('mycompany_mycontact')->__('Save Contact Inquiry Data')
        );
        $this->_updateButton(
            'delete',
            'label',
            Mage::helper('mycompany_mycontact')->__('Delete Contact Inquiry Data')
        );
        $this->_addButton(
            'saveandcontinue',
            array(
                'label'   => Mage::helper('mycompany_mycontact')->__('Save And Continue Edit'),
                'onclick' => 'saveAndContinueEdit()',
                'class'   => 'save',
            ),
            -100
        );
        $this->_formScripts[] = "
            function saveAndContinueEdit() {
                editForm.submit($('edit_form').action+'back/edit/');
            }
        ";
    }

    /**
     * get the edit form header
     *
     * @access public
     * @return string
     * @author Ultimate Module Creator
     */
    public function getHeaderText()
    {
        if (Mage::registry('current_contactinquiry') && Mage::registry('current_contactinquiry')->getId()) {
            return Mage::helper('mycompany_mycontact')->__(
                "Edit Contact Inquiry Data '%s'",
                $this->escapeHtml(Mage::registry('current_contactinquiry')->getName())
            );
        } else {
            return Mage::helper('mycompany_mycontact')->__('Add Contact Inquiry Data');
        }
    }
}

d:) Create file in app/code/local/Mycompany/Mycontact/Block/Adminhtml/Contactinquiry/Grid.php and add below code in it.

<?php

class Mycompany_Mycontact_Block_Adminhtml_Contactinquiry_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    /**
     * constructor
     *
     * @access public
     * @author Ultimate Module Creator
     */
    public function __construct()
    {
        parent::__construct();
        $this->setId('contactinquiryGrid');
        $this->setDefaultSort('entity_id');
        $this->setDefaultDir('ASC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
    }

    /**
     * prepare collection
     *
     * @access protected
     * @return Mycompany_Mycontact_Block_Adminhtml_Contactinquiry_Grid
     * @author Ultimate Module Creator
     */
    protected function _prepareCollection()
    {
        $collection = Mage::getModel('mycompany_mycontact/contactinquiry')
            ->getCollection();
        
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    /**
     * prepare grid collection
     *
     * @access protected
     * @return Mycompany_Mycontact_Block_Adminhtml_Contactinquiry_Grid
     * @author Ultimate Module Creator
     */
    protected function _prepareColumns()
    {
        $this->addColumn(
            'entity_id',
            array(
                'header' => Mage::helper('mycompany_mycontact')->__('Id'),
                'index'  => 'entity_id',
                'type'   => 'number'
            )
        );
        $this->addColumn(
            'name',
            array(
                'header'    => Mage::helper('mycompany_mycontact')->__('Name'),
                'align'     => 'left',
                'index'     => 'name',
            )
        );
        
        $this->addColumn(
            'status',
            array(
                'header'  => Mage::helper('mycompany_mycontact')->__('Status'),
                'index'   => 'status',
                'type'    => 'options',
                'options' => array(
                    '1' => Mage::helper('mycompany_mycontact')->__('Enabled'),
                    '0' => Mage::helper('mycompany_mycontact')->__('Disabled'),
                )
            )
        );
        $this->addColumn(
            'email',
            array(
                'header' => Mage::helper('mycompany_mycontact')->__('Email'),
                'index'  => 'email',
                'type'=> 'text',

            )
        );
        $this->addColumn(
            'mobile',
            array(
                'header' => Mage::helper('mycompany_mycontact')->__('Mobile'),
                'index'  => 'mobile',
                'type'=> 'text',

            )
        );
        $this->addColumn(
            'city',
            array(
                'header' => Mage::helper('mycompany_mycontact')->__('City'),
                'index'  => 'city',
                'type'=> 'text',

            )
        );
        $this->addColumn(
            'pincode',
            array(
                'header' => Mage::helper('mycompany_mycontact')->__('Pincode'),
                'index'  => 'pincode',
                'type'=> 'text',

            )
        );
        $this->addColumn(
            'action',
            array(
                'header'  =>  Mage::helper('mycompany_mycontact')->__('Action'),
                'width'   => '100',
                'type'    => 'action',
                'getter'  => 'getId',
                'actions' => array(
                    array(
                        'caption' => Mage::helper('mycompany_mycontact')->__('Edit'),
                        'url'     => array('base'=> '*/*/edit'),
                        'field'   => 'id'
                    )
                ),
                'filter'    => false,
                'is_system' => true,
                'sortable'  => false,
            )
        );
        $this->addExportType('*/*/exportCsv', Mage::helper('mycompany_mycontact')->__('CSV'));
        $this->addExportType('*/*/exportExcel', Mage::helper('mycompany_mycontact')->__('Excel'));
        $this->addExportType('*/*/exportXml', Mage::helper('mycompany_mycontact')->__('XML'));
        return parent::_prepareColumns();
    }

    /**
     * prepare mass action
     *
     * @access protected
     * @return Mycompany_Mycontact_Block_Adminhtml_Contactinquiry_Grid
     * @author Ultimate Module Creator
     */
    protected function _prepareMassaction()
    {
        $this->setMassactionIdField('entity_id');
        $this->getMassactionBlock()->setFormFieldName('contactinquiry');
        $this->getMassactionBlock()->addItem(
            'delete',
            array(
                'label'=> Mage::helper('mycompany_mycontact')->__('Delete'),
                'url'  => $this->getUrl('*/*/massDelete'),
                'confirm'  => Mage::helper('mycompany_mycontact')->__('Are you sure?')
            )
        );
        $this->getMassactionBlock()->addItem(
            'status',
            array(
                'label'      => Mage::helper('mycompany_mycontact')->__('Change status'),
                'url'        => $this->getUrl('*/*/massStatus', array('_current'=>true)),
                'additional' => array(
                    'status' => array(
                        'name'   => 'status',
                        'type'   => 'select',
                        'class'  => 'required-entry',
                        'label'  => Mage::helper('mycompany_mycontact')->__('Status'),
                        'values' => array(
                            '1' => Mage::helper('mycompany_mycontact')->__('Enabled'),
                            '0' => Mage::helper('mycompany_mycontact')->__('Disabled'),
                        )
                    )
                )
            )
        );
        return $this;
    }

    /**
     * get the row url
     *
     * @access public
     * @param Mycompany_Mycontact_Model_Contactinquiry
     * @return string
     * @author Ultimate Module Creator
     */
    public function getRowUrl($row)
    {
        return $this->getUrl('*/*/edit', array('id' => $row->getId()));
    }

    /**
     * get the grid url
     *
     * @access public
     * @return string
     * @author Ultimate Module Creator
     */
    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current'=>true));
    }

    /**
     * after collection load
     *
     * @access protected
     * @return Mycompany_Mycontact_Block_Adminhtml_Contactinquiry_Grid
     * @author Ultimate Module Creator
     */
    protected function _afterLoadCollection()
    {
        $this->getCollection()->walk('afterLoad');
        parent::_afterLoadCollection();
    }
}

e:) Create file in app/code/local/Mycompany/Mycontact/Block/Adminhtml/Contactinquiry/Edit/Form.php and add below code in it.

<?php

class Mycompany_Mycontact_Block_Adminhtml_Contactinquiry_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    /**
     * prepare form
     *
     * @access protected
     * @return Mycompany_Mycontact_Block_Adminhtml_Contactinquiry_Edit_Form
     * @author Ultimate Module Creator
     */
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form(
            array(
                'id'         => 'edit_form',
                'action'     => $this->getUrl(
                    '*/*/save',
                    array(
                        'id' => $this->getRequest()->getParam('id')
                    )
                ),
                'method'     => 'post',
                'enctype'    => 'multipart/form-data'
            )
        );
        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

f:) Create file in app/code/local/Mycompany/Mycontact/Block/Adminhtml/Contactinquiry/Edit/Tabs.php and add below code in it.

<?php

class Mycompany_Mycontact_Block_Adminhtml_Contactinquiry_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
    /**
     * Initialize Tabs
     *
     * @access public
     * @author Ultimate Module Creator
     */
    public function __construct()
    {
        parent::__construct();
        $this->setId('contactinquiry_tabs');
        $this->setDestElementId('edit_form');
        $this->setTitle(Mage::helper('mycompany_mycontact')->__('Contact Inquiry Data'));
    }

    /**
     * before render html
     *
     * @access protected
     * @return Mycompany_Mycontact_Block_Adminhtml_Contactinquiry_Edit_Tabs
     * @author Ultimate Module Creator
     */
    protected function _beforeToHtml()
    {
        $this->addTab(
            'form_contactinquiry',
            array(
                'label'   => Mage::helper('mycompany_mycontact')->__('Contact Inquiry Data'),
                'title'   => Mage::helper('mycompany_mycontact')->__('Contact Inquiry Data'),
                'content' => $this->getLayout()->createBlock(
                    'mycompany_mycontact/adminhtml_contactinquiry_edit_tab_form'
                )
                ->toHtml(),
            )
        );
        return parent::_beforeToHtml();
    }

    /**
     * Retrieve contact inquiry data entity
     *
     * @access public
     * @return Mycompany_Mycontact_Model_Contactinquiry
     * @author Ultimate Module Creator
     */
    public function getContactinquiry()
    {
        return Mage::registry('current_contactinquiry');
    }
}

g:) Create file in app/code/local/Mycompany/Mycontact/Block/Adminhtml/Contactinquiry/Edit/Tab/Form.php and add below code in it.

<?php

class Mycompany_Mycontact_Block_Adminhtml_Contactinquiry_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    /**
     * prepare the form
     *
     * @access protected
     * @return Mycompany_Mycontact_Block_Adminhtml_Contactinquiry_Edit_Tab_Form
     * @author Ultimate Module Creator
     */
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $form->setHtmlIdPrefix('contactinquiry_');
        $form->setFieldNameSuffix('contactinquiry');
        $this->setForm($form);
        $fieldset = $form->addFieldset(
            'contactinquiry_form',
            array('legend' => Mage::helper('mycompany_mycontact')->__('Contact Inquiry Data'))
        );

        $fieldset->addField(
            'name',
            'text',
            array(
                'label' => Mage::helper('mycompany_mycontact')->__('Name'),
                'name'  => 'name',
                'required'  => true,
                'class' => 'required-entry',

           )
        );

        $fieldset->addField(
            'email',
            'text',
            array(
                'label' => Mage::helper('mycompany_mycontact')->__('Email'),
                'name'  => 'email',
                'required'  => true,
                'class' => 'required-entry',

           )
        );

        $fieldset->addField(
            'mobile',
            'text',
            array(
                'label' => Mage::helper('mycompany_mycontact')->__('Mobile'),
                'name'  => 'mobile',
                'required'  => true,
                'class' => 'required-entry',

           )
        );

        $fieldset->addField(
            'city',
            'text',
            array(
                'label' => Mage::helper('mycompany_mycontact')->__('City'),
                'name'  => 'city',

           )
        );

        $fieldset->addField(
            'pincode',
            'text',
            array(
                'label' => Mage::helper('mycompany_mycontact')->__('Pincode'),
                'name'  => 'pincode',

           )
        );
        $fieldset->addField(
            'status',
            'select',
            array(
                'label'  => Mage::helper('mycompany_mycontact')->__('Status'),
                'name'   => 'status',
                'values' => array(
                    array(
                        'value' => 1,
                        'label' => Mage::helper('mycompany_mycontact')->__('Enabled'),
                    ),
                    array(
                        'value' => 0,
                        'label' => Mage::helper('mycompany_mycontact')->__('Disabled'),
                    ),
                ),
            )
        );
        $formValues = Mage::registry('current_contactinquiry')->getDefaultValues();
        if (!is_array($formValues)) {
            $formValues = array();
        }
        if (Mage::getSingleton('adminhtml/session')->getContactinquiryData()) {
            $formValues = array_merge($formValues, Mage::getSingleton('adminhtml/session')->getContactinquiryData());
            Mage::getSingleton('adminhtml/session')->setContactinquiryData(null);
        } elseif (Mage::registry('current_contactinquiry')) {
            $formValues = array_merge($formValues, Mage::registry('current_contactinquiry')->getData());
        }
        $form->setValues($formValues);
        return parent::_prepareForm();
    }
}

Step 9: Create Design folder and files (layouts and template files) for admin and frontend.

a:) Create file in app/design/adminhtml/default/default/layout/mycompany_mycontact.xml and add below code in it.

<?xml version="1.0"?>

<layout>
    <adminhtml_mycontact_contactinquiry_index>
        <reference name="menu">
            <action method="setActive">
                <menupath>customer/contactinquiry</menupath>
            </action>
        </reference>
        <reference name="content">
            <block type="mycompany_mycontact/adminhtml_contactinquiry" name="contactinquiry" />

        </reference>
    </adminhtml_mycontact_contactinquiry_index>
    <adminhtml_mycontact_contactinquiry_grid>
        <block type="core/text_list" name="root" output="toHtml">
            <block type="mycompany_mycontact/adminhtml_contactinquiry_grid" name="contactinquiry_grid"/>
        </block>
    </adminhtml_mycontact_contactinquiry_grid>
    <!-- Contactinquiry add/edit action -->
    <adminhtml_mycontact_contactinquiry_edit>
        <update handle="editor"/>
        <reference name="menu">
            <action method="setActive">
                <menupath>mycompany_mycontact/contactinquiry</menupath>
            </action>
        </reference>
        <reference name="content">
            <block type="mycompany_mycontact/adminhtml_contactinquiry_edit" name="contactinquiry_edit"></block>
        </reference>
        <reference name="left">
            <block type="mycompany_mycontact/adminhtml_contactinquiry_edit_tabs" name="contactinquiry_tabs"></block>
        </reference>
    </adminhtml_mycontact_contactinquiry_edit>

</layout>

b:) Create file in app/design/frontend/rwd/default/layout/mycompany_mycontact.xml and add below code in it.

<?xml version="1.0"?>

<layout>
    
    <mycompany_mycontact_contactinquiry_index translate="label" module="mycompany_mycontact">
        <label>Contact Inquiry Form</label>
        <reference name="head">
            <action method="setTitle" translate="title" module="mycompany_mycontact"><title>Contact Inquiries</title></action>
        </reference>
        <update handle="page_two_columns_left" />
        <update handle="cms_menu"/>
        <reference name="content">
            <block type="core/template" name="contactinquiry_list" template="mycompany_mycontact/contactinquiry/list.phtml" />
        </reference>
        <!-- to add default captcha -->
        <reference name="contactinquiry_list">
            <action method="setTemplate"><template>mycompany_mycontact/contactinquiry/list.phtml</template></action>
            <block type="core/text_list" name="form.additional.info">
                <block type="captcha/captcha" name="captcha">
                    <reference name="head">
                        <action method="addJs"><file>mage/captcha.js</file></action>
                    </reference>
                    <action method="setFormId"><formId>mycompany_mycontact</formId></action>
                    <action method="setImgWidth"><width>230</width></action>
                    <action method="setImgHeight"><width>50</width></action>
                </block>
            </block>
        </reference>
        <!-- to add default captcha -->
    </mycompany_mycontact_contactinquiry_index>
</layout>

C:) Create file in app/design/frontend/rwd/default/template/mycompany_mycontact/contactinquiry/list.phtml and add below code in it.

<?php
$pincode_required  = Mage::getStoreConfig('mycompany_mycontact/contactinquiry/contactinquiry_custom_field_one',Mage::app()->getStore());
$city_required = Mage::getStoreConfig('mycompany_mycontact/contactinquiry/contactinquiry_custom_field_two',Mage::app()->getStore());

?>
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<div class="newlocation">
<div class="page-title contactinquiry-title">
    <h1><?php echo Mage::helper('mycompany_mycontact')->__('Contact Inquiry') ?></h1>
</div>
<form action="<?php echo $this->getFormAction(); ?>" id="inquiryForm" method="post">
<div class="fieldset" style="width:100%">
 <p class="required"><?php echo Mage::helper('mycompany_mycontact')->__('* Required Fields') ?></p>
    <ul class="form-list" style="list-style: none;margin: 0;padding: 0;">
        <li class="fields">
            <div class="field"> 
            <label for="name" class="required"><em>*</em><?php echo Mage::helper('mycompany_mycontact')->__('Name') ?></label>
            <div class="input-box">
                <input name="name" id="name" title="<?php echo $this->__('Name') ?>" value="" class="input-text inBx required-entry validate-alpha" type="text" />
            </div>
            </div>
            <div class="field">
             <label for="email" class="required"><em>*</em><?php echo Mage::helper('mycompany_mycontact')->__('Email') ?></label>
             <div class="input-box">
                <input name="email" id="email" title="<?php echo $this->__('Email') ?>" value="<?php echo $this->escapeHtml($this->getUserEmail()) ?>" class="input-text required-entry validate-email" type="text" />
             </div>
             </div>
         </li>
         
         <li class="fields">
            <div class="field">
             <label for="telephone" class="required"><em>*</em><?php echo Mage::helper('mycompany_mycontact')->__('Mobile Phone') ?></label>
             <div class="input-box">
                <input name="mobile" id="mobile" title="<?php echo $this->__('Mobile Number') ?>" value="" class="input-text required-entry validate-phoneLax" type="text" />
             </div>
             </div>
            <div class="field"> 
            <label for="city" class="<?php if($city_required==1){ echo "required";}?>"><?php if($city_required==1){ echo "<em>*</em>";}?><?php echo Mage::helper('mycompany_mycontact')->__('City') ?></label>
            <div class="input-box">
                <input name="city" id="city" title="<?php echo $this->__('city') ?>" value="" class="input-text <?php if($city_required==1){ echo 'required-entry';}?>" type="text" />
            </div>
            </div>
            <div class="field">
             <label for="pincode" class="<?php if($pincode_required==1){ echo "required";}?>"><?php if($pincode_required==1){ echo "<em>*</em>";}?><?php echo Mage::helper('mycompany_mycontact')->__('Pincode') ?></label>
             <div class="input-box">
                <input name="pincode" id="pincode" title="<?php echo $this->__('Pincode') ?>" value="" class="input-text <?php if($pincode_required==1){ echo 'required-entry';}?>" type="text" />
             </div>
             </div>
             <?php echo $this->getChildHtml('form.additional.info'); ?>
         </li>         
     </ul>
     
 </div>
  <div class="buttons-set" style="margin-top:20px;"> 
   <input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
   <button type="submit" title="<?php echo $this->__('Submit') ?>" class="button"><span><span><?php echo Mage::helper('mycompany_mycontact')->__('Submit') ?></span></span></button>
  </div>
</form>
<script type="text/javascript">
//<![CDATA[
    var contactForm = new VarienForm('inquiryForm', true);
 (function($) {
   $(".newlocation input").blur();
   window.scrollTo(0,0);
        })(jQuery);
//]]>
</script>
</div>

Step 10: Create Mail template files.

a:) Create file in app/locale/en_US/template/email/mycompany/request_contact.html and add below code in it.

  
<!--@subject Contact Request Confirm Email @-->
  <html>
    <head>
      <title>Contact for Inquiry</title>
    </head>
        <body>
        <table border='0' cellpadding='4' cellspacing='0' width='90%'>
        <tbody>
        <tr>
          <td align='center' colspan='2' height='30' >Contact Inquiry</td>       
        </tr>    
        <tr>
          <td align='right' width='40%'><b>Name:</b></td>
          <td width='55%'>{{var data.name}}</td>
        </tr>       
        <tr>
          <td align='right' width='40%'><b>Email:</b></td>
          <td width='55%'>{{var data.email}}</td>
        </tr>
        <tr>
          <td align='right' width='40%'><b>Mobile Number:</b></td>
          <td width='55%'>{{var data.mobile}}</td>
        </tr>        
        <tr>
          <td align='right' width='40%'><b>City:</b></td>
          <td width='55%'>{{var data.city}}</td>
        </tr>
        <tr>
          <td align='right' width='40%'><b>Pincode:</b></td>  
          <td width='55%'>{{var data.pincode}}</td>
        </tr>
        </tbody>  
        </body>
    </html>
b:) Create file in app/locale/en_US/template/email/mycompany/success_contact.html and add below code in it.

<!--@subject Contact Success Email @-->
  <html>
    <head>
      <title>Contact for Inquiry</title>
    </head>
        <body>
        <table border='0' cellpadding='4' cellspacing='0' width='90%'>
        <tbody>
        <tr>
          <td align='center' colspan='2' height='30' >Contact Inquiry</td>       
        </tr>    
        <tr>
          <td align='right' width='40%'><b>Name:</b></td>
          <td width='55%'>{{var data.name}}</td>
        </tr>       
        <tr>
          <td align='right' width='40%'><b>Email:</b></td>
          <td width='55%'>{{var data.email}}</td>
        </tr>
        <tr>
          <td align='right' width='40%'><b>Mobile Number:</b></td>
          <td width='55%'>{{var data.mobile}}</td>
        </tr>        
        <tr>
          <td align='right' width='40%'><b>City:</b></td>
          <td width='55%'>{{var data.city}}</td>
        </tr>
        <tr>
          <td align='right' width='40%'><b>Pincode:</b></td>  
          <td width='55%'>{{var data.pincode}}</td>
        </tr>
        </tbody>  
        </body>
    </html>

  • Now go to admin and load your mail templates from System >> Transactional Emails.
  • And from admin >> system >> Configuration. Set url key, mail templates and other settings.
  • From admin >> system >> Configuration >> Customers >> Customer Configuration select Contacts Inquiry Page to display captcha on that page. 
You will find your custom contact page as per below screenshot.

Download Whole code from Github: https://github.com/vijayrami/Custom_Contact_Inquiry_Form_With_Captcha
Tag : Magento
0 Comments On "Create Custom Contact Inquiry Form With Captcha in Magento"

Back To Top