Skip to content
Snippets Groups Projects
Commit 6fe04cec authored by jensschuppe's avatar jensschuppe Committed by Jaap Jansma
Browse files

Issue #3339639: Add configuration option for when to execute the handler

parent 674afa9c
No related branches found
No related tags found
1 merge request!2Issue #3339639: Add configuration option for when to execute the handler
......@@ -39,6 +39,11 @@ class FormProcessorWebformHandler extends WebformHandlerBase {
*/
protected $tokenManager;
/**
* The CiviMRF Core.
*
* @var \CMRF\Core\Core
*/
protected $core;
......@@ -77,6 +82,7 @@ class FormProcessorWebformHandler extends WebformHandlerBase {
public function defaultConfiguration() {
return [
'connection' => null,
'states' => [WebformSubmissionInterface::STATE_COMPLETED],
'form_processor' => null,
'form_processor_fields' => [],
'form_processor_params' => [],
......@@ -133,6 +139,25 @@ class FormProcessorWebformHandler extends WebformHandlerBase {
[ '#type' => 'fieldset',
'#title' => $this->t('Form Processor'),
];
// Settings: States.
$results_disabled = $this->getWebform()->getSetting('results_disabled');
$form['additional']['states'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Send to CiviCRM'),
'#options' => [
WebformSubmissionInterface::STATE_DRAFT_CREATED => $this->t('…when <b>draft is created</b>.'),
WebformSubmissionInterface::STATE_DRAFT_UPDATED => $this->t('…when <b>draft is updated</b>.'),
WebformSubmissionInterface::STATE_CONVERTED => $this->t('…when anonymous <b>submission is converted</b> to authenticated.'),
WebformSubmissionInterface::STATE_COMPLETED => $this->t('…when <b>submission is completed</b>.'),
WebformSubmissionInterface::STATE_UPDATED => $this->t('…when <b>submission is updated</b>.'),
WebformSubmissionInterface::STATE_DELETED => $this->t('…when <b>submission is deleted</b>.'),
WebformSubmissionInterface::STATE_LOCKED => $this->t('…when <b>submission is locked</b>.'),
],
'#access' => $results_disabled ? FALSE : TRUE,
'#default_value' => $results_disabled ? [WebformSubmissionInterface::STATE_COMPLETED] : $this->configuration['states'],
];
if ($selected_formprocessor) {
$form['additional']['fields'] =
[
......@@ -215,7 +240,12 @@ class FormProcessorWebformHandler extends WebformHandlerBase {
parent::submitConfigurationForm($form, $form_state);
$this->applyFormStateToConfiguration($form_state);
$values = $form_state->getValues();
// Cleanup states.
$values['states'] = array_values(array_filter($values['states']));
$this->configuration['connection'] = $values['connection'];
$this->configuration['states'] = $values['states'];
$this->configuration['form_processor'] = $values['form_processor'];
$this->configuration['form_processor_fields'] = $values['additional']['fields'];
$this->configuration['form_processor_params'] = [];
......@@ -296,27 +326,58 @@ class FormProcessorWebformHandler extends WebformHandlerBase {
* {@inheritdoc}
*/
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE, $params = []) {
$state = $webform_submission->getWebform()->getSetting('results_disabled') ? WebformSubmissionInterface::STATE_COMPLETED : $webform_submission->getState();
if ($this->configuration['states'] && in_array($state, $this->configuration['states'])) {
$this->sendToCiviCRM($webform_submission);
}
}
/**
* {@inheritDoc}
*/
public function postDelete(WebformSubmissionInterface $webform_submission) {
if (in_array(WebformSubmissionInterface::STATE_DELETED, $this->configuration['states'])) {
$this->sendToCiviCRM($webform_submission);
}
}
/**
* @param \Drupal\webform\WebformSubmissionInterface $webform_submission
*/
private function sendToCiviCRM(WebformSubmissionInterface $webform_submission) {
$data = $webform_submission->getData();
$fields = $this->configuration['form_processor_fields'];
foreach($fields as $key=>$field){
if(key_exists($key,$data)){
if(isset($data[$key]) && in_array($this->getWebform()->getElement($key)['#type'],['webform_document_file','managed_file','webform_image_file'])){
$file = File::load($data[$key]);
$fileData = [
foreach ($fields as $key => $field) {
if (key_exists($key, $data)) {
if (isset($data[$key]) && in_array($this->getWebform()
->getElement($key)['#type'], [
'webform_document_file',
'managed_file',
'webform_image_file',
])) {
$file = File::load($data[$key]);
$fileData = [
'name' => $file->getFilename(),
'mime_type' => $file->getMimeType(),
'content'=> base64_encode(file_get_contents($file->getFileUri()))
'content' => base64_encode(file_get_contents($file->getFileUri())),
];
$params[$key] = $fileData;
} else {
}
else {
$params[$key] = $data[$key];
}
}
}
if( $this->configuration['form_processor_current_contact']){
$params[$this->configuration['form_processor_current_contact']]=$this->getContactId();
if ($this->configuration['form_processor_current_contact']) {
$params[$this->configuration['form_processor_current_contact']] = $this->getContactId();
}
$call = $this->core->createCall($this->configuration['connection'],'FormProcessor',$this->configuration['form_processor'],$params,[]);
$call = $this->core->createCall(
$this->configuration['connection'],
'FormProcessor',
$this->configuration['form_processor'],
$params,
[]
);
$this->core->executeCall($call);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment