You are here

class LegalLanguageSettings in Legal 2.0.x

Same name and namespace in other branches
  1. 8 src/Form/LegalLanguageSettings.php \Drupal\legal\Form\LegalLanguageSettings

Class LegalLanguageSettings.

@package Drupal\legal\Form

Hierarchy

Expanded class hierarchy of LegalLanguageSettings

1 string reference to 'LegalLanguageSettings'
legal.routing.yml in ./legal.routing.yml
legal.routing.yml

File

src/Form/LegalLanguageSettings.php, line 18

Namespace

Drupal\legal\Form
View source
class LegalLanguageSettings extends FormBase {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * {@inheritdoc}
   */
  public function __construct(Connection $database) {
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('database'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'legal_language_settings';
  }

  /**
   * Languages administration form.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * Get latest version for each language.
   */
  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();

    // Get latest version for each language.
    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 {

      // Get latest version for specific language.
      $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;
  }

  /**
   * Get data from T&C object.
   *
   * @param object $data
   *   T&C object.
   *
   * @return array
   *   T&C data as an array.
   */
  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;
  }

  /**
   * Access control callback.
   *
   * Check that Locale module is enabled and user has access permission.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   Run access checks for this account.
   */
  public function access(AccountInterface $account) {

    // Check permissions and combine with any custom access checking needed.
    // Pass forward parameters from the route and/or request as needed.
    if (!\Drupal::moduleHandler()
      ->moduleExists('locale')) {
      return AccessResult::forbidden();
    }
    return AccessResult::allowed();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
LegalLanguageSettings::$database protected property The database connection.
LegalLanguageSettings::access public function Access control callback.
LegalLanguageSettings::buildForm public function Languages administration form. Overrides FormInterface::buildForm
LegalLanguageSettings::create public static function Instantiates a new instance of this class. Overrides FormBase::create
LegalLanguageSettings::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
LegalLanguageSettings::legalVersionsLatestGet public function Get latest version for each language.
LegalLanguageSettings::legalVersionsLatestGetData public function Get data from T&C object.
LegalLanguageSettings::submitForm public function Form submission handler. Overrides FormInterface::submitForm
LegalLanguageSettings::__construct public function
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.