Magento – Using Another Controller’s Action Within a Custom Module’s Controller

controllersextensions

I have successfully created an extension that adds all Magento's default contact inquiry submissions into a database. It works great.

I have added an option under the 'Customers' menu that uses Magento's grid to add the contact form submissions into grid.

I am able to mass select the captured data for the purpose of deleting.

I have also added a "Resend" option to the actions and am able to execute:

    public function massResendAction() {
          $post = $this->getRequest()->getPost();
          Zend_Debug::dump($post);
          die('here');
      }

I have added a new action called "Mass Resend":

array(3) {
  ["form_key"] => string(16) "Be8rJzWBona0R2Ij"
  ["enhancedform_id"] => array(2) {
    [0] => string(2) "12"
    [1] => string(2) "13"
  }
  ["massaction_prepare_key"] => string(15) "enhancedform_id"
}

here

What I would like to do is use Magento's built in Contacts form post action:
app/code/core/Mage/Contacts/controllers/IndexController.php

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;
            }

            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')->__('Unable to submit your request. Please, try again later'));
            $this->_redirect('*/*/');
            return;
        }

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

Instead of duplicating this code.

Best Answer

Inchoo did a really nice post about rewriting just about anything (read more here) so we can user that.

In your config.xml

<config>
    <frontend>
        <routers>
            <tag>
                <args>
                    <modules>
                        <[namespace]_[module] before="Mage_Contacts">[Namespace]_[Module]</[namespace]_[module]>
                    </modules>
                </args>
            </tag>
        </routers>
    </frontend>
</config>

Now for your controller controllers/IndexController.php

<?php
require_once(Mage::getModuleDir('controllers','Mage_Contacts').DS.'IndexController.php');


class [Namespace]_[Module]_IndexController extends Mage_Contacts_IndexController
{
    public function postAction()
    {
        $post = $this->getRequest()->getPost();

        // do your magic here

        parent::postAction(); // and now we pass it on to the original controller
    }
}
Related Topic