View source
<?php
namespace Drupal\webform_submission_log;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
class WebformSubmissionLogManager implements WebformSubmissionLogManagerInterface {
use DependencySerializationTrait;
protected $database;
public function __construct(Connection $database) {
$this->database = $database;
}
public function insert(array $fields) {
$fields += [
'webform_id' => '',
'sid' => '',
'handler_id' => '',
'operation' => '',
'uid' => '',
'message' => '',
'variables' => serialize([]),
'data' => serialize([]),
'timestamp' => '',
];
$this->database
->insert(WebformSubmissionLogManagerInterface::TABLE)
->fields($fields)
->execute();
}
public function getQuery(EntityInterface $webform_entity = NULL, EntityInterface $source_entity = NULL, AccountInterface $account = NULL, array $options = []) {
$options += [
'header' => NULL,
'limit' => NULL,
];
$query = $this->database
->select(WebformSubmissionLogManagerInterface::TABLE, 'log');
$query
->fields('log', [
'lid',
'uid',
'webform_id',
'sid',
'handler_id',
'operation',
'message',
'variables',
'timestamp',
'data',
]);
$query
->leftJoin('users_field_data', 'user', 'log.uid = user.uid');
$query
->leftJoin('webform_submission', 'submission', 'log.sid = submission.sid');
$query
->fields('submission', [
'entity_type',
'entity_id',
]);
if ($webform_entity instanceof WebformInterface) {
$query
->condition('log.webform_id', $webform_entity
->id());
}
elseif ($webform_entity instanceof WebformSubmissionInterface) {
$query
->condition('log.webform_id', $webform_entity
->getWebform()
->id());
$query
->condition('log.sid', $webform_entity
->id());
}
if ($source_entity) {
$query
->condition('submission.entity_type', $source_entity
->getEntityTypeId());
$query
->condition('submission.entity_id', $source_entity
->id());
}
if ($account) {
$query
->condition('log.uid', $account
->id());
}
if ($options['header']) {
$query = $query
->extend('\\Drupal\\Core\\Database\\Query\\TableSortExtender')
->orderByHeader($options['header']);
}
if ($options['limit']) {
$query = $query
->extend('\\Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->limit($options['limit']);
}
return $query;
}
public function loadByEntities(EntityInterface $webform_entity = NULL, EntityInterface $source_entity = NULL, AccountInterface $account = NULL, array $options = []) {
$result = $this
->getQuery($webform_entity, $source_entity, $account, $options)
->execute();
$records = [];
while ($record = $result
->fetchObject()) {
$record->variables = unserialize($record->variables);
$record->data = unserialize($record->data);
$records[] = $record;
}
return $records;
}
}