LegalLanguageSettings.php in Legal 8
File
src/Form/LegalLanguageSettings.php
View source
<?php
namespace Drupal\legal\Form;
use Drupal\Component\Render\HtmlEscapedText;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LegalLanguageSettings extends FormBase {
protected $database;
public function __construct(Connection $database) {
$this->database = $database;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('database'));
}
public function getFormId() {
return 'legal_language_settings';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$latest_header = [
$this
->t('Language'),
$this
->t('Version'),
$this
->t('Revision'),
];
$latest_rows = $this
->legalVersionsLatestGet();
$rows = [];
foreach ($latest_rows as $language_name => $language) {
$row = [];
$row[] = new HtmlEscapedText($language_name);
$row[] = empty($language['version']) ? '-' : $language['version'];
$row[] = empty($language['revision']) ? '-' : $language['revision'];
$rows[] = $row;
}
$form['latest'] = [
'#type' => 'details',
'#title' => $this
->t('Latest Version'),
];
$form['latest']['#value'] = [
'#type' => 'table',
'#header' => $latest_header,
'#rows' => $rows,
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
public function legalVersionsLatestGet($language = NULL) {
$conditions = [];
$current_version = $this->database
->select('legal_conditions', 'lc')
->fields('lc', [
'version',
])
->orderBy('version', 'DESC')
->range(0, 1)
->execute()
->fetchField();
if (empty($language)) {
$languages = \Drupal::languageManager()
->getLanguages();
foreach ($languages as $language_id => $language) {
$result = $this->database
->select('legal_conditions', 'lc')
->fields('lc')
->condition('version', $current_version)
->condition('language', $language_id)
->orderBy('revision', 'DESC')
->range(0, 1)
->execute()
->fetchAllAssoc('tc_id');
$row = count($result) ? (object) array_shift($result) : FALSE;
$conditions[$language
->getId()] = $this
->legalVersionsLatestGetData($row);
}
}
else {
$result = $this->database
->select('legal_conditions', 'lc')
->fields('lc')
->condition('language', $language)
->groupBy('language')
->orderBy('version', 'DESC')
->range(0, 1)
->execute()
->fetchAllAssoc('tc_id');
$row = count($result) ? (object) array_shift($result) : FALSE;
$conditions[$language] = $this
->legalVersionsLatestGetData($row);
}
return $conditions;
}
public function legalVersionsLatestGetData($data) {
$row['revision'] = isset($data->revision) ? $data->revision : '';
$row['language'] = isset($data->language) ? $data->language : '';
$row['conditions'] = isset($data->conditions) ? $data->conditions : '';
$row['date'] = isset($data->date) ? $data->date : '';
$row['extras'] = isset($data->extras) ? $data->extras : '';
$row['changes'] = isset($data->changes) ? $data->changes : '';
return $row;
}
public function access(AccountInterface $account) {
if (!\Drupal::moduleHandler()
->moduleExists('locale')) {
return AccessResult::forbidden();
}
return AccessResult::allowed();
}
}