You are here

persiantools.module in PersianTools 8

Same filename and directory in other branches
  1. 7 persiantools.module

File

persiantools.module
View source
<?php

use Drupal\Core\Render\Markup;
use Drupal\Core\Database\Database;
use Drupal\persiantools\RTLMaker;

/**
 * Implements hook_preprocess_html().
 */
function persiantools_preprocess_html(&$variables) {
  $lang = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  if ($lang != 'fa' && $lang != 'ar') {
    return;
  }
  $variables['page']['#post_render'][] = 'persiantools_post_render_rtl';
}
function persiantools_post_render_rtl($html, array $elements) {
  $html_page = (string) $html;
  $digit_method = \Drupal::config('persiantools.settings')
    ->get('digit_method');
  $rtlmaker = \Drupal::config('persiantools.settings')
    ->get('rtlmaker');
  $skip_tags = '(<(textarea|script|style)( [^>]*)?>(.*?)<\\/\\3>\\s*)';
  $normal_tags = '(<\\/?[^>]+>\\s*)?([^<]*)(?=<)';
  $chained_tags = '/(?s)(' . $skip_tags . '|' . $normal_tags . ')([^<]*)(?=<)/';
  preg_match_all($chained_tags, $html_page, $matches, PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER);
  $new_html_page = '';
  for ($i = 0; $i < count($matches[0]); $i++) {
    if (strlen($matches[7][$i][0]) + strlen($matches[8][$i][0]) > 0) {
      $matches[7][$i][0] = RTLMaker::convert_sm($matches[7][$i][0] . $matches[8][$i][0], $digit_method, $rtlmaker);
    }
    $new_html_page .= $matches[2][$i][0] . $matches[6][$i][0] . $matches[7][$i][0];
  }
  $last = end($matches[0]);
  $last_pos = strlen($last[0]) + $last[1];
  if ($last_pos < strlen($html_page)) {
    $new_html_page .= substr($html_page, $last_pos);
  }
  return $new_html_page;
}
function persiantools_sort_fix_submit() {
  $dbinfo = Database::getConnectionInfo('default');
  $dbname = $dbinfo['default']['database'];
  db_query('ALTER DATABASE ' . $dbname . ' DEFAULT CHARACTER SET utf8 COLLATE utf8_persian_ci');
  $table_ref = "Tables_in_{$dbname}";
  $result = db_query("SHOW TABLES");
  foreach ($result as $tableinfo) {
    $tablename = $tableinfo->{$table_ref};
    db_query('ALTER TABLE ' . $tablename . ' CONVERT TO CHARACTER SET utf8 COLLATE utf8_persian_ci');
  }
  drupal_set_message(t('Default database collation, and collation for all tables changed to persian (utf8_persian_ci).'), 'status', FALSE);
}

Functions