View source
<?php
namespace Drupal\webform_content_creator\Entity;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\node\Entity\Node;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\webform_content_creator\WebformContentCreatorInterface;
use Drupal\webform_content_creator\WebformContentCreatorUtilities;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\Component\Utility\Html;
class WebformContentCreatorEntity extends ConfigEntityBase implements WebformContentCreatorInterface {
use StringTranslationTrait, MessengerTrait;
protected $id;
protected $title;
protected $field_title;
protected $webform;
protected $content_type;
protected $elements;
protected $use_encrypt;
protected $encryption_profile;
public function getTitle() {
return $this
->get('title');
}
public function setTitle($title) {
$this
->set('title', $title);
return $this;
}
public function getContentType() {
return $this
->get('content_type');
}
public function setContentType($content_type) {
$this
->set('content_type', $content_type);
return $this;
}
public function getWebform() {
return $this
->get('webform');
}
public function setWebform($webform) {
$this
->set('webform', $webform);
return $this;
}
public function getAttributes() {
return $this
->get(WebformContentCreatorInterface::ELEMENTS);
}
public function getSyncEditContentCheck() {
return $this
->get(WebformContentCreatorInterface::SYNC_CONTENT);
}
public function getSyncDeleteContentCheck() {
return $this
->get(WebformContentCreatorInterface::SYNC_CONTENT_DELETE);
}
public function getSyncContentField() {
return $this
->get(WebformContentCreatorInterface::SYNC_CONTENT_FIELD);
}
public function getEncryptionCheck() {
return $this
->get(WebformContentCreatorInterface::USE_ENCRYPT);
}
public function getEncryptionProfile() {
return $this
->get(WebformContentCreatorInterface::ENCRYPTION_PROFILE);
}
private function getNodeTitle() {
if ($this
->get(WebformContentCreatorInterface::FIELD_TITLE) !== NULL && $this
->get(WebformContentCreatorInterface::FIELD_TITLE) !== '') {
$title = $this
->get(WebformContentCreatorInterface::FIELD_TITLE);
}
else {
$title = \Drupal::entityTypeManager()
->getStorage(WebformContentCreatorInterface::WEBFORM)
->load($this
->get(WebformContentCreatorInterface::WEBFORM))
->label();
}
return $title;
}
private function getProfileName() {
$encryption_profile = '';
$use_encrypt = $this
->get(WebformContentCreatorInterface::USE_ENCRYPT);
if ($use_encrypt) {
$encryption_profile = \Drupal::service('entity_type.manager')
->getStorage(WebformContentCreatorInterface::ENCRYPTION_PROFILE)
->load($this
->getEncryptionProfile());
}
return $encryption_profile;
}
private function getDecryptionFromProfile($value, $profile = '') {
if ($this
->getEncryptionCheck()) {
$result = WebformContentCreatorUtilities::getDecryptedValue($value, $profile);
}
else {
$result = $value;
}
return $result;
}
private function mapNodeField(ContentEntityInterface $initial_content, WebformSubmissionInterface $webform_submission, array $fields = [], array $webform_fields = [], array $data = [], $encryption_profile = '', $field_id = '', array $mapping = [], array $attributes = []) {
$content = $initial_content;
$webform = $webform_submission
->getWebform();
if (!$content
->hasField($field_id) || !is_array($mapping)) {
return $content;
}
$field_mapping = \Drupal::service('plugin.manager.webform_content_creator.field_mapping')
->getPlugin($attributes[$field_id][WebformContentCreatorInterface::FIELD_MAPPING]);
$component_fields = $field_mapping
->getEntityComponentFields($fields[$field_id]);
$values = [];
$webform_element = [];
if (sizeOf($component_fields) > 0) {
foreach ($component_fields as $component_field) {
$webform_element[$component_field] = $webform
->getElement($mapping[$component_field][WebformContentCreatorInterface::WEBFORM_FIELD], FALSE);
if ($attributes[$field_id][WebformContentCreatorInterface::CUSTOM_CHECK]) {
$field_value = WebformContentCreatorUtilities::getTokenValue($mapping[WebformContentCreatorInterface::CUSTOM_VALUE], $encryption_profile, $webform_submission);
}
else {
if (!$attributes[$field_id][WebformContentCreatorInterface::TYPE]) {
if (!array_key_exists(WebformContentCreatorInterface::WEBFORM_FIELD, $mapping) || !array_key_exists($mapping[WebformContentCreatorInterface::WEBFORM_FIELD], $data)) {
return $content;
}
$field_value = $this
->getDecryptionFromProfile($data[$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]], $encryption_profile);
if ($fields[$field_id]
->getType() === 'entity_reference' && (!is_array($field_value) && intval($field_value) === 0)) {
$content
->set($field_id, []);
return $content;
}
}
else {
$field_object = $webform_submission->{$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]};
if ($field_object instanceof EntityReferenceFieldItemList) {
$field_value = $webform_submission->{$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]}
->getValue()[0]['target_id'];
}
else {
$field_value = $webform_submission->{$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]}->value;
}
}
}
$values[$component_field] = $field_value;
}
}
else {
if ($attributes[$field_id][WebformContentCreatorInterface::CUSTOM_CHECK]) {
$field_value = WebformContentCreatorUtilities::getTokenValue($mapping[WebformContentCreatorInterface::CUSTOM_VALUE], $encryption_profile, $webform_submission);
}
else {
if (!$attributes[$field_id][WebformContentCreatorInterface::TYPE]) {
if (!array_key_exists(WebformContentCreatorInterface::WEBFORM_FIELD, $mapping) || !array_key_exists($mapping[WebformContentCreatorInterface::WEBFORM_FIELD], $data)) {
return $content;
}
$field_value = $this
->getDecryptionFromProfile($data[$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]], $encryption_profile);
if ($fields[$field_id]
->getType() === 'entity_reference' && (!is_array($field_value) && intval($field_value) === 0)) {
$content
->set($field_id, []);
return $content;
}
}
else {
$field_object = $webform_submission->{$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]};
if ($field_object instanceof EntityReferenceFieldItemList) {
$field_value = $webform_submission->{$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]}
->getValue()[0]['target_id'];
}
else {
$field_value = $webform_submission->{$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]}->value;
}
}
}
$values[$field_id] = $field_value;
}
if ($fields[$field_id]
->getType() == 'datetime') {
$field_value = $this
->convertTimestamp($fields, $field_id, $field_value);
}
$field_value = $field_mapping
->mapEntityField($content, $webform_element, $values, $fields[$field_id]);
return $content;
}
public function createNode(WebformSubmissionInterface $webform_submission) {
$node_title = $this
->getNodeTitle();
$data = $webform_submission
->getData();
if (empty($data)) {
return 0;
}
$encryption_profile = $this
->getProfileName();
$title = WebformContentCreatorUtilities::getTokenValue($node_title, $encryption_profile, $webform_submission);
$decoded_title = Html::decodeEntities($title);
$content = Node::create([
WebformContentCreatorInterface::TYPE => $this
->getContentType(),
'title' => $decoded_title,
]);
$attributes = $this
->get(WebformContentCreatorInterface::ELEMENTS);
$content_type = \Drupal::entityTypeManager()
->getStorage('node_type')
->load($this
->getContentType());
if (empty($content_type)) {
return 0;
}
$webform = $webform_submission
->getWebform();
$webform_fields = [];
if ($webform) {
$webform_fields = $webform
->getElementsDecodedAndFlattened();
}
$fields = WebformContentCreatorUtilities::contentTypeFields($content_type);
if (empty($fields)) {
return 0;
}
foreach ($attributes as $k2 => $v2) {
$content = $this
->mapNodeField($content, $webform_submission, $fields, $webform_fields, $data, $encryption_profile, $k2, $v2, $attributes);
}
$result = 0;
try {
$result = $content
->save();
} catch (\Exception $e) {
\Drupal::logger(WebformContentCreatorInterface::WEBFORM_CONTENT_CREATOR)
->error($this
->t('A problem occurred when creating a new node.'));
\Drupal::logger(WebformContentCreatorInterface::WEBFORM_CONTENT_CREATOR)
->error($e
->getMessage());
}
return $result;
}
public function updateNode(WebformSubmissionInterface $webform_submission, $op = 'edit') {
if (empty($this
->getSyncContentField())) {
return 0;
}
$content_type = \Drupal::entityTypeManager()
->getStorage('node_type')
->load($this
->getContentType());
if (empty($content_type)) {
return 0;
}
$webform = $webform_submission
->getWebform();
$webform_fields = [];
if ($webform) {
$webform_fields = $webform
->getElementsDecodedAndFlattened();
}
$fields = WebformContentCreatorUtilities::contentTypeFields($content_type);
if (empty($fields)) {
return 0;
}
if (!array_key_exists($this
->getSyncContentField(), $fields)) {
return 0;
}
$node_title = $this
->getNodeTitle();
$data = $webform_submission
->getData();
if (empty($data)) {
return 0;
}
$encryption_profile = $this
->getProfileName();
$title = WebformContentCreatorUtilities::getTokenValue($node_title, $encryption_profile, $webform_submission);
$decoded_title = Html::decodeEntities($title);
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties([
$this
->getSyncContentField() => $webform_submission
->id(),
]);
if (!($content = reset($nodes))) {
return 0;
}
if ($op === 'delete' && !empty($this
->getSyncDeleteContentCheck())) {
$result = $content
->delete();
return $result;
}
if (empty($this
->getSyncEditContentCheck())) {
return 0;
}
$content
->setTitle($decoded_title);
$attributes = $this
->get(WebformContentCreatorInterface::ELEMENTS);
foreach ($attributes as $k2 => $v2) {
$content = $this
->mapNodeField($content, $webform_submission, $fields, $webform_fields, $data, $encryption_profile, $k2, $v2, $attributes);
}
$result = 0;
try {
$result = $content
->save();
} catch (\Exception $e) {
\Drupal::logger(WebformContentCreatorInterface::WEBFORM_CONTENT_CREATOR)
->error($this
->t('A problem occurred while updating node.'));
\Drupal::logger(WebformContentCreatorInterface::WEBFORM_CONTENT_CREATOR)
->error($e
->getMessage());
}
return $result;
}
public function existsContentType() {
$content_type_id = $this
->getContentType();
$content_type_entity = \Drupal::entityTypeManager()
->getStorage('node_type')
->load($content_type_id);
return !empty($content_type_entity);
}
public function equalsContentType($ct) {
return $ct === $this
->getContentType();
}
public function equalsWebform($webform) {
return $webform === $this
->getWebform();
}
public function statusMessage($status) {
if ($status) {
$this
->messenger()
->addMessage($this
->t('Saved the %label entity.', [
'%label' => $this
->getTitle(),
]));
}
else {
$this
->messenger()
->addMessage($this
->t('The %label entity was not saved.', [
'%label' => $this
->getTitle(),
]));
}
}
public function convertTimestamp(array $fields, $field_id, $field_value) {
$date_time = new DrupalDateTime($field_value, 'UTC');
$date_type = $fields[$field_id]
->getSettings()['datetime_type'];
if ($date_type === 'datetime') {
$result = \Drupal::service('date.formatter')
->format($date_time
->getTimestamp(), 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, 'UTC');
}
else {
$result = \Drupal::service('date.formatter')
->format($date_time
->getTimestamp(), 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, 'UTC');
}
return $result;
}
}