Add Custom Functionality to Submission

Many of you may have experience with Magento event system, so this help page will be easy to get into.

Lets create observer function for WebForms that dumps submitted result into XML file.

We will be creating new extension called WebFormsProcessResult.

WebFormsProcessResult file structure

It contains 4 files in total.

app
├── code
│ └── community
│ └── VladimirPopov
│ └── WebFormsProcessResult
│ ├── etc
│ │ ├── config.xml
│ │ └── system.xml
│ └── Model
│ └── Observer.php
└── etc
└── modules
└── VladimirPopov_WebFormsProcessResult.xml

app / etc / modules / VladimirPopov_WebFormsProcessResult.xml

This file registeres and enables extension in Magento

<?xml version="1.0"?>
<config>
<modules>
<VladimirPopov_WebFormsProcessResult>
<active>true</active>
<codePool>community</codePool>
</VladimirPopov_WebFormsProcessResult>
</modules>
</config>

app / code / community / VladimirPopov / WebFormsProcessResult

This folder contains main extension files.

etc / config.xml

In this file we are registering model for our extension and hook it to WebForms event "webforms_result_submit".

<?xml version="1.0"?>
<config>
<modules>
<VladimirPopov_WebFormsProcessResult>
<version>1.0.0</version>
</VladimirPopov_WebFormsProcessResult>
</modules>
<adminhtml>
<acl>
<resources>
<all>
<title>Allow Everything</title>
</all>
</resources>
</acl>
</adminhtml>
<global>
<models>
<webformsprocessresult>
<class>VladimirPopov_WebFormsProcessResult_Model</class>
</webformsprocessresult>
</models>
<events>
<webforms_result_submit>
<observers>
<webforms_result_submit_observer>
<type>singleton</type>
<class>VladimirPopov_WebFormsProcessResult_Model_Observer</class>
<method>exportXML</method>
</webforms_result_submit_observer>
</observers>
</webforms_result_submit>
</events>
</global>
</config>

etc / system.xml

This file holds configuration options for our extension. WebFormsProcessResult has turn on/off switch.

<?xml version="1.0"?>
<config>
<sections>
<webforms>
<groups>
<processresult translate="label comment">
<comment></comment>
<label>Results Post Processing</label>
<frontend_type>text</frontend_type>
<sort_order>1000</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<enable translate="label comment">
<comment>
<![CDATA[Results will be additionally processed by custom function]]>
</comment>
<label>Enable results post processing</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>
</enable>
</fields>
</processresult>
</groups>
</webforms>
</sections>
</config>

You`ll notice new block on Web-forms > Forms Settings page.

configuration

Model / Observer.php

Here we define model that has method of exporting submited result to xml file.

<?php
class VladimirPopov_WebFormsProcessResult_Model_Observer{
public function exportXML($observer){
if(!Mage::getStoreConfig('webforms/processresult/enable')) return;
$webform = $observer->getWebform();
/*
* Check web-form code
* if($webform->getCode() != 'myform') return;
*/
$result = Mage::getModel('webforms/results')->load($observer->getResult()->getId());
$xmlObject = new Varien_Simplexml_Config($result->toXml());
// generate unique filename
$destinationFolder = Mage::getBaseDir('media') . DS . 'webforms' . DS . 'xml';
$filename = $destinationFolder . DS . $result->getId().'.xml';
// create folder
if (!(@is_dir($destinationFolder) || @mkdir($destinationFolder, 0777, true))) {
throw new Exception("Unable to create directory '{$destinationFolder}'.");
}
// export to file
$xmlObject->getNode()->asNiceXml($filename);
}
}
?>

$destinationFolder - variable that sets folder where result is stored. We will be creating files in media / webforms / xml folder.

$filename - is complete path to the file. For example media / webforms / xml / 123.xml.

Make sure media folder is writable!

Thats it! Feel free to modify this extension, experiment and add required post processing functionality to WebForms.

You can download sources here.