View source
<?php
namespace Drupal\node_accessibility;
use Drupal\quail_api\QuailApiSettings;
class TypeSettingsStorage {
const DEFAULT_ENABLED = 'disabled';
const DEFAULT_METHOD = 'quail_api_method_immediate';
private static $title_block_options = [
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'div',
'span',
'p',
];
protected $nodeType;
protected $enabled;
protected $method;
protected $formatContent;
protected $formatResults;
protected $formatTitleResults;
protected $titleBlock;
protected $standards;
public function __construct($nodeType = NULL, $enabled = NULL, $method = NULL, $formatContent = NULL, $formatResults = NULL, $formatTitleResults = NULL, $titleBlock = NULL, array $standards = []) {
$this->nodeType = is_null($nodeType) || is_string($nodeType) ? $nodeType : NULL;
$this->enabled = is_null($enabled) || is_string($enabled) ? $enabled : static::DEFAULT_ENABLED;
$this->method = is_null($method) || is_string($method) ? $method : static::DEFAULT_METHOD;
$this->formatContent = is_null($formatContent) || is_string($formatContent) ? $formatContent : NULL;
$this->formatResults = is_null($formatResults) || is_string($formatResults) ? $formatResults : NULL;
$this->formatTitleResults = is_null($formatTitleResults) || is_string($formatTitleResults) ? $formatTitleResults : NULL;
$this->titleBlock = is_null($titleBlock) || is_string($titleBlock) ? $titleBlock : NULL;
$this->standards = $standards;
}
public function __destruct() {
$this->nodeType = NULL;
$this->enabled = NULL;
$this->method = NULL;
$this->formatContent = NULL;
$this->formatResults = NULL;
$this->formatTitleResults = NULL;
$this->titleBlock = NULL;
$this->standards = [];
}
public static function load($nodeType) {
if (!is_string($nodeType) || empty($nodeType)) {
return $results;
}
$result = new static();
try {
$query = \Drupal::database()
->select('node_accessibility_type_settings', 'nats');
$query
->fields('nats');
$query
->condition('node_type', $nodeType);
$existing = $query
->execute()
->fetchObject();
if ($existing) {
if (!empty($existing->node_type)) {
$result
->setNodeType($existing->node_type);
}
if (!empty($existing->enabled)) {
$result
->setEnabled($existing->enabled);
}
else {
$result
->setEnabled(static::DEFAULT_ENABLED);
}
if (!empty($existing->method)) {
$result
->setMethod($existing->method);
}
else {
$result
->setMethod(static::DEFAULT_METHOD);
}
if (!empty($existing->format_content)) {
$result
->setFormatContent($existing->format_content);
}
if (!empty($existing->format_results)) {
$result
->setFormatResults($existing->format_results);
}
if (!empty($existing->title_block)) {
$result
->setTitleBlock($existing->title_block);
}
if (!empty($existing->standards)) {
$result
->setStandards(json_decode($existing->standards, TRUE));
}
}
} catch (Exception $e) {
\Drupal::logger('node_accessibility')
->error("Failed to select from {node_accessibility_type_settings} table, exception: @exception.", [
'@exception' => $e
->getMessage(),
]);
} catch (Error $e) {
\Drupal::logger('node_accessibility')
->error("Failed to select from {node_accessibility_type_settings} table, exception: @exception.", [
'@exception' => $e
->getMessage(),
]);
}
return $result;
}
public static function loadAsArray($nodeType) {
$result = static::load($nodeType);
return $result
->toArray();
}
public static function loadByNode($nid) {
$node_type = NULL;
try {
$query = \Drupal::database()
->select('node', 'n');
$query
->addField('n', 'type', 'node_type');
$query
->condition('n.nid', $nid);
$existing = $query
->execute()
->fetchObject();
if ($existing) {
$node_type = $existing->node_type;
}
} catch (Exception $e) {
\Drupal::logger('node_accessibility')
->error("Failed to select from {node} table, exception: @exception.", [
'@exception' => $e
->getMessage(),
]);
} catch (Error $e) {
\Drupal::logger('node_accessibility')
->error("Failed to select from {node} table, exception: @exception.", [
'@exception' => $e
->getMessage(),
]);
}
return static::load($node_type);
}
public static function loadByNodeAsArray($nid) {
$node_type = NULL;
try {
$query = \Drupal::database()
->select('node', 'n');
$query
->addField('n', 'type', 'node_type');
$query
->condition('n.nid', $nid);
$existing = $query
->execute()
->fetchObject();
if ($existing) {
$node_type = $existing->node_type;
}
} catch (Exception $e) {
\Drupal::logger('node_accessibility')
->error("Failed to select from {node} table, exception: @exception.", [
'@exception' => $e
->getMessage(),
]);
} catch (Error $e) {
\Drupal::logger('node_accessibility')
->error("Failed to select from {node} table, exception: @exception.", [
'@exception' => $e
->getMessage(),
]);
}
return static::loadAsArray($node_type);
}
public static function merge($fields) {
if (is_array($fields)) {
$sanitized = static::sanitizeFields($fields);
}
elseif ($fields instanceof TypeSettingsStorage) {
$fields_array = $fields
->toArray();
$sanitized = static::sanitizeFields($fields_array);
}
else {
return FALSE;
}
if (is_null($sanitized['node_type'])) {
return FALSE;
}
$existing = static::load($sanitized['node_type']);
try {
if (empty($existing
->getNodeType())) {
\Drupal::database()
->insert('node_accessibility_type_settings')
->fields($sanitized)
->execute();
}
else {
\Drupal::database()
->update('node_accessibility_type_settings')
->condition('node_type', $sanitized['node_type'])
->fields($sanitized)
->execute();
}
} catch (Exception $e) {
\Drupal::logger('node_accessibility')
->error("Failed to insert or update on {node_accessibility_type_settings} table, exception: @exception.", [
'@exception' => $e
->getMessage(),
]);
return FALSE;
} catch (Error $e) {
\Drupal::logger('node_accessibility')
->error("Failed to insert or update on {node_accessibility_type_settings} table, exception: @exception.", [
'@exception' => $e
->getMessage(),
]);
return FALSE;
}
return TRUE;
}
public static function delete(string $nodeType) {
if (empty($nodeType)) {
return FALSE;
}
try {
\Drupal::database()
->delete('node_accessibility_type_settings')
->condition('node_type', $nodeType)
->execute();
} catch (Exception $e) {
\Drupal::logger('node_accessibility')
->error("Failed to delete on {node_accessibility_type_settings} table, exception: @exception.", [
'@exception' => $e
->getMessage(),
]);
return FALSE;
} catch (Error $e) {
\Drupal::logger('node_accessibility')
->error("Failed to delete on {node_accessibility_type_settings} table, exception: @exception.", [
'@exception' => $e
->getMessage(),
]);
return FALSE;
}
return TRUE;
}
public function getNodeType() {
return $this->nodeType;
}
public function getEnabled() {
return $this->enabled;
}
public function getMethod() {
return $this->method;
}
public function getFormatContent() {
return $this->formatContent;
}
public function getFormatResults() {
return $this->formatResults;
}
public function getFormatTitleResults() {
return $this->formatTitleResults;
}
public function getTitleBlock() {
return $this->titleBlock;
}
public function getStandards() {
return $this->standards;
}
public function toArray() {
$enabled = $this
->getEnabled();
if (is_null($enabled)) {
$enabled = 'disbled';
}
$method = $this
->getMethod();
if (is_null($method)) {
$method = 'quail_api_method_immediate';
}
return [
'node_type' => $this
->getNodeType(),
'enabled' => $enabled,
'method' => $method,
'format_content' => $this
->getFormatContent(),
'format_results' => $this
->getFormatResults(),
'title_block' => $this
->getTitleBlock(),
'standards' => $this
->getStandards(),
];
}
public function setNodeType($nodeType) {
if (is_null($nodeType) || is_string($nodeType)) {
$this->nodeType = $nodeType;
}
}
public function setEnabled($enabled) {
if (is_null($enabled) || is_string($enabled)) {
$this->enabled = $enabled;
}
}
public function setMethod($method) {
if (is_null($method) || is_string($method)) {
$this->method = $method;
}
}
public function setFormatContent($formatContent) {
if (is_null($formatContent) || is_string($formatContent)) {
$this->formatContent = $formatContent;
}
}
public function setFormatResults($formatResults) {
if (is_null($formatResults) || is_string($formatResults)) {
$this->formatResults = $formatResults;
}
}
public function setFormatTitleResults($formatTitleResults) {
if (is_null($formatTitleResults) || is_string($formatTitleResults)) {
$this->formatTitleResults = $formatTitleResults;
}
}
public function setTitleBlock($titleBlock) {
if (is_null($titleBlock) || is_string($titleBlock)) {
$this->titleBlock = $titleBlock;
}
}
public function setStandards(array $standards) {
$this->standards = $standards;
}
private static function sanitizeFields(array $fields) {
$sanitized = [];
$sanitized['node_type'] = NULL;
$sanitized['enabled'] = static::DEFAULT_ENABLED;
$sanitized['method'] = static::DEFAULT_METHOD;
$sanitized['format_content'] = '';
$sanitized['format_results'] = '';
$sanitized['title_block'] = '';
$sanitized['standards'] = json_encode([]);
if (!empty($fields['node_type']) && is_string($fields['node_type'])) {
$sanitized['node_type'] = $fields['node_type'];
}
if (!empty($fields['method']) && is_string($fields['method'])) {
$methods = QuailApiSettings::get_validation_methods_list();
if (array_key_exists($fields['method'], $methods)) {
$sanitized['method'] = $fields['method'];
}
}
if (!empty($fields['format_content']) && is_string($fields['format_content'])) {
$sanitized['format_content'] = $fields['format_content'];
}
if (!empty($fields['format_results']) && is_string($fields['format_results'])) {
$sanitized['format_results'] = $fields['format_results'];
}
if (!empty($fields['title_block']) && is_string($fields['title_block'])) {
if (in_array($fields['title_block'], self::$title_block_options)) {
$sanitized['title_block'] = $fields['title_block'];
}
}
if (!empty($fields['enabled']) && is_string($fields['enabled'])) {
if ($fields['enabled'] == 'disabled' || $fields['enabled'] == 'optional' || $fields['enabled'] == 'required') {
$sanitized['enabled'] = $fields['enabled'];
}
}
if (!empty($fields['standards']) && is_array($fields['standards'])) {
$standards = QuailApiSettings::get_standards_list();
$standards_array = array();
foreach ($fields['standards'] as $standard_name => $standard_value) {
if (is_numeric($standard_value)) {
continue;
}
if (array_key_exists($standard_name, $standards)) {
$standards_array[$standard_name] = $standard_name;
}
}
$sanitized['standards'] = json_encode($standards_array);
}
return $sanitized;
}
}