0

I am trying to run a SQL INSERT query from the Mage_contacts_indexcontroller that I have extended to my own module but the query never runs. I have tried creating a separate function and running the function within the postAction but no luck. The function runs in other modules just not the contacts module. I was also thinking that creating an observer for this might be the way to go but I don't think the contacts form fires an event that Magento tracks. Any help or ideas is appreciated.

edited my post to add my code:

class Mage_Contacts_IndexController extends Mage_Core_Controller_Front_Action
{

    const XML_PATH_EMAIL_RECIPIENT  = 'contacts/email/recipient_email';
    const XML_PATH_EMAIL_SENDER     = 'contacts/email/sender_email_identity';
    const XML_PATH_EMAIL_TEMPLATE   = 'contacts/email/email_template';
    const XML_PATH_ENABLED          = 'contacts/contacts/enabled';

    public function preDispatch()
    {
        parent::preDispatch();

        if( !Mage::getStoreConfigFlag(self::XML_PATH_ENABLED) ) {
            $this->norouteAction();
        }

    }

    public function indexAction()
    {


        $this->loadLayout();
        $this->getLayout()->getBlock('contactForm')
            ->setFormAction( Mage::getUrl('*/*/post') );

        $this->_initLayoutMessages('customer/session');
        $this->_initLayoutMessages('catalog/session');
        $this->renderLayout();

    }
    public function insert(){
        $post = $this->getRequest()->getPost();

        $resource   = Mage::getSingleton('core/resource');
        $write      = Mage::getSingleton('core/resource')->getConnection('core_write');

        $table      = $resource->getTableName('contact_forms');
        $subject    = $post['subject'];
        $name       = $post['name'];
        $email      = $post['email'];
        $telephone  = $post['telephone'];
        $comments   = $post['comments'];

        $query      =  "Insert Into {$table} (subject,name,email,telephone,comment,modified_at) values (:subject,:name,:email,:telephone,:comments, NOW())";


        $binds      =  array(
            'subject'   => $subject,
            'name'      => $name,
            'email'     => $email,
            'telephone' => $telephone,
            'comments'  => $comments
        );

        $write->query($query,$binds);

    }
    public function postAction()
    {
        $post = $this->getRequest()->getPost();

        insert();

        if ( $post ) {
            $translate = Mage::getSingleton('core/translate');
            /* @var $translate Mage_Core_Model_Translate */
            $translate->setTranslateInline(false);
            try {
                $postObject = new Varien_Object();
                $postObject->setData($post);

                $error = false;
                $error2 = '';

                if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
                    $error = true;
                    $error2 = 'Please add a contact name for this inquiry.';
                }

                if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                    $error = true;
                    $error2 = 'Please add a valid email address and resubmit the contact form.';
                }

                if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                    $error = true;
                }

                if ($error) {
                    throw new Exception();
                }
                $mailTemplate = Mage::getModel('core/email_template');
                /* @var $mailTemplate Mage_Core_Model_Email_Template */
                $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                    ->setReplyTo($post['email'])
                    ->sendTransactional(
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                        null,
                        array('data' => $postObject)
                    );

                if (!$mailTemplate->getSentSuccess()) {
                    throw new Exception();
                }

                $translate->setTranslateInline(true);

                Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
                $this->_redirect('*/*/');

                return;
            } catch (Exception $e) {

                $translate->setTranslateInline(true);


                Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__( $error2 ));
                $this->_redirect('*');
                return;
            }

        } else {
            $this->_redirect('*/*/');
        }
    }




}
1
  • please share your code.what you have tried Jul 9, 2014 at 17:48

1 Answer 1

2

It looks as if you're incorrectly defining the controller rewrite. Merely creating the controller in your own local module will not create the rewrite. To define the rewrite you must:

  • Create your own module
  • Create a controller rewrite
  • Author the rewrite so that it extends the class you're targeting to rewrite. This class must be a class within your own module's namespace.

Some example code:

1. Add this to your module's config.xml:

File: app/code/local/YourCompany/Contacts/etc/config.xml

<config>
    <frontend>
        <routers>
            <contacts>
                <args>
                    <modules>
                        <yourcompany_contacts before="Mage_Contacts">YourCompany_Contacts</yourcompany_contacts>
                    </modules>
                </args>
            </contacts>
        </routers>
    </frontend>
</config>

2. Create your own rewritten controller:

File: app/code/local/YourCompany/Contacts/controllers/IndexController.php

<?php 
require_once(Mage::getModuleDir('controllers','Mage_Contacts').DS.'IndexController.php');
class YourCompany_Contacts_IndexController extends Mage_Contacts_IndexController
{
    //your targeted controller action rewrite here
}
1
  • Yeah I didn't realize I had not extended it properly. Thanks for pointing that out. Yeah I dropped my function into the core file to test it and sure enough it worked. Thanks for the help.
    – Egregory
    Jul 10, 2014 at 14:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.