RulesWebformEventBase.php in RULES WEBFORM 3.x
File
src/Event/RulesWebformEventBase.php
View source
<?php
namespace Drupal\rules_webform\Event;
use Drupal\Component\EventDispatcher\Event;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
class RulesWebformEventBase extends Event {
const EVENT_NAME = 'webform_submit';
public $webform_fields;
public $webform_fields_unchanged;
public $webform_info;
public function __construct(WebformSubmission $submission) {
\Drupal::state()
->set('rules_webform.submission', $submission);
$webform = Webform::load($submission
->getWebform()
->id());
$elements = $webform
->getElementsInitializedAndFlattened();
$fields_definitions = [];
$this
->extractcompositeElements($elements, $fields_definitions);
\Drupal::state()
->set('rules_webform.fields_definitions', $fields_definitions);
$this
->initializeWebformInfo($submission);
$this->webform_fields = $this
->extractcompositeSubmissionData($submission
->getData());
$this->webform_fields_unchanged = $this
->extractcompositeSubmissionData($submission
->getOriginalData());
}
private function extractcompositeElements(array $elements, &$fields_definitions) {
foreach ($elements as $name => $options) {
if (isset($options['#webform_composite_elements'])) {
$this
->extractcompositeElements($options['#webform_composite_elements'], $fields_definitions);
}
else {
if ($options['#type'] != 'webform_wizard_page' && $options['#type'] != "webform_actions") {
$fields_definitions[$name] = (string) isset($options['#title']) ? $options['#title'] : '';
$this->webform_fields[$name] = '';
$this->webform_fields_unchanged[$name] = '';
}
}
}
}
private function extractcompositeSubmissionData(array $submissionData) {
$webform_fields = [];
foreach ($submissionData as $key => $value) {
if (is_array($value)) {
$this
->extractcompositeSubmissionData($value);
}
else {
if (isset($value)) {
$webform_fields[$key] = $value;
}
}
}
return $webform_fields;
}
private function initializeWebformInfo(WebformSubmission $submission) {
$this->webform_info['submission'] = $submission;
$this->webform_info['id'] = $submission
->getWebform()
->id();
$this->webform_info['title'] = $submission
->getWebform()
->get('title');
$this->webform_info['submitter_id'] = $submission
->getOwnerId();
$this->webform_info['submitter_name'] = $submission
->getOwner()
->getDisplayName();
$this->webform_info['submitter_email'] = $submission
->getOwner()
->getEmail();
$timestamp = $submission
->getcreatedTime();
$this->webform_info['created']['timestamp'] = $timestamp;
$this->webform_info['created']['date_short'] = \Drupal::service('date.formatter')
->format($timestamp, 'date_short');
$this->webform_info['created']['date_medium'] = \Drupal::service('date.formatter')
->format($timestamp, 'date_medium');
$this->webform_info['created']['date_long'] = \Drupal::service('date.formatter')
->format($timestamp, 'date_long');
$this->webform_info['created']['html_datetime'] = \Drupal::service('date.formatter')
->format($timestamp, 'html_datetime');
$this->webform_info['created']['html_date'] = \Drupal::service('date.formatter')
->format($timestamp, 'html_date');
$this->webform_info['created']['html_time'] = \Drupal::service('date.formatter')
->format($timestamp, 'html_time');
$timestamp = $submission
->getchangedTime();
$this->webform_info['changed']['timestamp'] = $timestamp;
$this->webform_info['changed']['date_short'] = \Drupal::service('date.formatter')
->format($timestamp, 'date_short');
$this->webform_info['changed']['date_medium'] = \Drupal::service('date.formatter')
->format($timestamp, 'date_medium');
$this->webform_info['changed']['date_long'] = \Drupal::service('date.formatter')
->format($timestamp, 'date_long');
$this->webform_info['changed']['html_datetime'] = \Drupal::service('date.formatter')
->format($timestamp, 'html_datetime');
$this->webform_info['changed']['html_date'] = \Drupal::service('date.formatter')
->format($timestamp, 'html_date');
$this->webform_info['changed']['html_time'] = \Drupal::service('date.formatter')
->format($timestamp, 'html_time');
if ($submission
->isDraft()) {
$this->webform_info['completed']['timestamp'] = '';
$this->webform_info['completed']['date_short'] = '';
$this->webform_info['completed']['date_medium'] = '';
$this->webform_info['completed']['date_long'] = '';
$this->webform_info['completed']['html_datetime'] = '';
$this->webform_info['completed']['html_date'] = '';
$this->webform_info['completed']['html_time'] = '';
}
else {
$timestamp = $submission
->getcompletedTime();
$this->webform_info['completed']['timestamp'] = $timestamp;
$this->webform_info['completed']['date_short'] = \Drupal::service('date.formatter')
->format($timestamp, 'date_short');
$this->webform_info['completed']['date_medium'] = \Drupal::service('date.formatter')
->format($timestamp, 'date_medium');
$this->webform_info['completed']['date_long'] = \Drupal::service('date.formatter')
->format($timestamp, 'date_long');
$this->webform_info['completed']['html_datetime'] = \Drupal::service('date.formatter')
->format($timestamp, 'html_datetime');
$this->webform_info['completed']['html_date'] = \Drupal::service('date.formatter')
->format($timestamp, 'html_date');
$this->webform_info['completed']['html_time'] = \Drupal::service('date.formatter')
->format($timestamp, 'html_time');
}
$this->webform_info['number'] = $submission
->serial();
$this->webform_info['id_submission'] = $submission
->id();
$this->webform_info['uuid'] = $submission
->uuid();
$this->webform_info['uri'] = $submission
->get('uri')->value;
$this->webform_info['ip'] = $submission
->getRemoteAddr();
$this->webform_info['language'] = $submission
->language()
->getName();
$this->webform_info['is_draft'] = $submission
->isDraft();
$this->webform_info['current_page'] = $submission
->getcurrentPage();
}
}