3

After running through every tutorial on the internet about overriding core controllers I resort to posting here.

Magento: 1.9.1.1 community

Namespace: Tft

Module Name: Customcontact

Files:

/app/code/local/Tft/Customcontact/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Tft_Customcontact>
            <version>1.0.0</version>
        </Tft_Customcontact>
    </modules>
    <frontend>
        <routers>
            <customcontact>
                <args>
                    <modules>
                        <Tft_Customcontact before="Mage_Contacts">Tft_Customcontact</Tft_Customcontact>
                    </modules>
                </args>
            </customcontact>
        </routers>
    </frontend> 
</config>

/app/etc/modules/Tft_Customcontact.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Tft_Customcontact>
            <active>true</active>
            <codepool>local</codepool>
        </Tft_Customcontact>
    </modules>
</config>

And the controller: /app/code/local/Tft/Customcontact/controllers/IndexController.php

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Contacts') . DS . 'IndexController.php';
class Tft_Customcontact_IndexController extends Mage_Contacts_IndexController
{
    public function postAction()
    {
        $post = $this->getRequest()->getPost();
        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;

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

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

                if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                    $error = true;
                }

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

                /**************************************************************/
                $fileName = '';
                if (isset($_FILES['customerimg']['name']) && $_FILES['customerimg']['name'] != '') {
                    try {
                        $fileName       = $_FILES['customerimg']['name'];
                        $fileExt        = strtolower(substr(strrchr($fileName, ".") ,1));
                        $fileNamewoe    = rtrim($fileName, $fileExt);
                        $fileName       = preg_replace('/\s+/', '', $fileNamewoe) . time() . '.' . $fileExt;

                        $uploader       = new Varien_File_Uploader('customerimg');
                        $uploader->setAllowedExtensions(array('jpg', 'png', 'gif')); //add more file types you want to allow
                        $uploader->setAllowRenameFiles(false);
                        $uploader->setFilesDispersion(false);
                        $path = Mage::getBaseDir('media') . DS . 'contacts';
                        if(!is_dir($path)){
                            mkdir($path, 0777, true);
                        }
                        $uploader->save($path . DS, $fileName );

                    } catch (Exception $e) {
                                Mage::getSingleton('customer/session')->addError($e->getMessage());
                        $error = true;
                    }
                }
                /**************************************************************/

                if ($error) {
                    throw new Exception();
                }
                $mailTemplate = Mage::getModel('core/email_template');
                /* @var $mailTemplate Mage_Core_Model_Email_Template */

                /**************************************************************/
                //sending file as attachment
                $attachmentFilePath = Mage::getBaseDir('media'). DS . 'contacts' . DS . $fileName;
                if(file_exists($attachmentFilePath)){
                    $fileContents = file_get_contents($attachmentFilePath);
                    $attachment   = $mailTemplate->getMail()->createAttachment($fileContents);
                    $attachment->filename = $fileName;
                }
                /**************************************************************/

                $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')->__('If you see this at all your custom controller works.'));
                $this->_redirect('*/*/');

                return;
            } catch (Exception $e) {
                $translate->setTranslateInline(true);

                Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
                $this->_redirect('*/*/');
                return;
            }

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

All cache has been flushed dozens of times. The module is very clearly enabled in the admin panel. Yet it only shows the success message from the default system controller. When submitting the contact form I should be getting: "If you see this at all your custom controller works."

If I override the core file directly it works, of course, but obviously overriding core is not ideal.

Anyone see anything out of ordinary here?

1

There issue in

Tft_Customcontact.xml



 <codepool>local</codepool>

should be

 <codePool>local</codePool>

P should Capital word

See how to overrider controller check at here

  • Well there it is. I knew magento was picky about casing but I overlooked that difference between magento's example and inchoo's. Good to know. Thanks! – Kai Qing Jun 14 '15 at 1:04
1

Maybe there is a difference to CE 1.9 but in CE 1.8 the config.xml routers-part must be like this:

<frontend>
    <routers>
        <contacts>
            <args>
                <modules>
                    <Tft_Customcontact before="Mage_Contacts">Tft_Customcontact</Tft_Customcontact>
                </modules>
            </args>
        </contacts>
    </routers>
</frontend> 

Means: "contacts" instead of "customcontact"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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