Created
January 23, 2012 09:21
-
-
Save pascalalich/1662012 to your computer and use it in GitHub Desktop.
Taming TYPO3 4.6's forms: Implementing a Webhook PostProcessor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $this->typoScript[$key] is either 'mail' or 'webhook' in my example | |
$className = 'tx_form_System_Postprocessor_' . ucfirst(strtolower($this->typoScript[$key])); | |
if (class_exists($className, TRUE)) { | |
$processor = t3lib_div::makeInstance($className, $this->form, $processorArguments); | |
$html .= $processor->process(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The webhook post processor | |
* | |
* @author Pascal alich <pascal@alichs.de> | |
* @package TYPO3 | |
* @subpackage form | |
*/ | |
class tx_form_System_Postprocessor_Webhook { | |
/** | |
* @var tx_form_Domain_Model_Form | |
*/ | |
protected $form; | |
/** | |
* @var array | |
*/ | |
protected $typoScript; | |
/** | |
* Constructor | |
* | |
* @param $form tx_form_Domain_Model_Form Form domain model | |
* @param $typoscript array Post processor TypoScript settings | |
* @return void | |
*/ | |
public function __construct(tx_form_Domain_Model_Form $form, array $typoScript) { | |
$this->form = $form; | |
$this->typoScript = $typoScript; | |
} | |
/** | |
* The main method called by the post processor | |
* | |
* Configures the mail message | |
* @todo This current version is properly violating most Extbase rules. | |
* @return string HTML message from this processor | |
*/ | |
public function process() { | |
$params = array(); | |
$this->fillParams($params, $this->form->getElements()); | |
$ch = curl_init($this->typoScript['url']); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_USERAGENT, "TYPO3FormWebhook/1.0"); | |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); | |
$return = curl_exec($ch); | |
curl_close($ch); | |
return $return; | |
} | |
private function fillParams(&$params, &$elements) { | |
foreach ($elements as $element) { | |
if (is_a($element, 'tx_form_Domain_Model_Element_Textline')) { | |
$params[$element->getName()] = $element->getAttributeValue('value'); | |
} else if (is_a($element, 'tx_form_Domain_Model_Element_Select')) { | |
$this->fillParamsFromSelect($params, $element); | |
} else if (is_a($element, 'tx_form_Domain_Model_Element_Textarea')) { | |
$params[$element->getName()] = $element->getData(); | |
} else if (is_a($element, 'tx_form_Domain_Model_Element_Container')) { | |
$this->fillParams($params, $element->getElements()); | |
} | |
} | |
} | |
private function fillParamsFromSelect(&$params, &$select) { | |
foreach ($select->getElements() as $option) { | |
if ($option->hasAttribute('selected') && $option->getAttributeValue('selected')=='selected') { | |
$params[$select->getName()] = $option->getData(); | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment