<?php

class PageLoadNames extends Page
{
  function ReplaceTranslated($orig, $tran, $Text, $ID, $Group)
  {
    $tran_replace = str_replace(' ','&nbsp;',htmlspecialchars($tran));
    $orig_replace = str_replace(' ','&nbsp;',htmlspecialchars($orig));

    //  if (strlen($orig) < strlen($Text)-1) {
    if ($Group <> '')
    {
      $Text = str_replace(' '.$orig,' <span Title="Přelož jako:&nbsp;'.$tran_replace.
        '" class="edit"><a class="edit" target="_NEW2" href="'.$this->System->Link('/form.php?group='.$Group.'&amp;ID='.$ID).'">'.$orig_replace.'</a></span>',$Text);
    } else {
      $Text = str_replace(' '.$orig,' <span Title="Přelož jako:&nbsp;'.$tran_replace.
        '" class="edit"><a class="edit" target="_NEW2" href="'.$this->System->Link('/'.T('dictionary', 'URL').'/?search='.$tran).'">'.$orig_replace.'</a></span>',$Text);
    }
  //   }
    return $Text;
  }

  function ReplaceNotTranslated($orig, $tran, $Text, $ID, $Group)
  {
    $orig_replace = str_replace(' ','&nbsp;',htmlspecialchars($orig));

    $Text = str_replace(' '.$orig,' <span title="Kliknutím přeložíš" class="needtran"><a class="needtran" target="_NEW2" href="'.
      $this->System->Link('/form.php?group='.$Group.'&amp;ID='.$ID).'">'.$orig_replace.'</a></span>',$Text);
    return $Text;
  }

  function ColorNames($Text, $names)
  {
    $Text = ' '.$Text;
    $Text = str_replace('$B$B','$B$B ',$Text);

    $Text = str_replace('$B','<span Title="Znamená odřádkování, překladu zachovej na stejné pozici." class="edit">$B</span>',$Text);
    $Text = str_replace('$N','<span Title="Znamená jméno hráče, překladu zachovej na stejné pozici." class="edit">$N</span>',$Text);
    $Text = str_replace('$R','<span Title="Znamená rasu hráče, překladu zachovej na stejné pozici." class="edit">$R</span>',$Text);
    $Text = str_replace('$C','<span Title="Znamená povolání hráče, překladu zachovej na stejné pozici." class="edit">$C</span>',$Text);
    $Text = str_replace('$G','<span Title="Vybere oslovení podle pohlaví hráče, překladu zachovej na stejné pozici." class="edit">$G</span>',$Text);
    $Text = str_replace('$b','<span Title="Znamená odřádkování, překladu zachovej na stejné pozici." class="edit">$B</span>',$Text);
    $Text = str_replace('$n','<span Title="Znamená jméno hráče, překladu zachovej na stejné pozici." class="edit">$N</span>',$Text);
    $Text = str_replace('$c','<span Title="Znamená povolání hráče, překladu zachovej na stejné pozici." class="edit">$c</span>',$Text);
    $Text = str_replace('$r','<span Title="Znamená rasu hráče, překladu zachovej na stejné pozici." class="edit">$R</span>',$Text);
    $Text = str_replace('$g','<span Title="Vybere oslovení podle pohlaví hráče, překladu zachovej na stejné pozici." class="edit">$G</span>',$Text);
    foreach ($names as $Line) 
    {
      if (($_GET['ID'] <> $Line[0]) or ($Line[1] <> $_GET['GroupId']))
      {
        if ($Line[3] <> '')  
        {
          $Text = $this->ReplaceTranslated($Line[2],$Line[3],$Text,$Line[0],$Line[1]);      
        } else 
        {
          $Text = $this->ReplaceNotTranslated($Line[2],$Line[3],$Text,$Line[0],$Line[1]);
        }
      }
    }
    return $Text;
  }

  function LoadNames()
  {
    $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
    $TranslationTree = $this->System->ModuleManager->Modules['Translation']->GetTranslationTree();

    $Output = '';
    if (array_key_exists('GroupId', $_GET)) $GroupId = $_GET['GroupId'];
      else return ShowMessage(T('GroupId not specified'), MESSAGE_CRITICAL);
    if (!array_key_exists('ID', $_GET)) return ShowMessage(T('ID not specified'), MESSAGE_CRITICAL);
    if (!array_key_exists('Column', $_GET)) return ShowMessage(T('Column not specified'), MESSAGE_CRITICAL);
    $Table = $TranslationTree[$GroupId]['TablePrefix'];
    $DbResult = $this->System->Database->query('SELECT * FROM `'.$Table.'` WHERE `ID` = '.$_GET['ID'].' LIMIT 1');
    $LineAJ = $DbResult->fetch_assoc();
    $Column = $_GET['Column'];
    //$Text = $LineAJ[$TranslationTree[$GroupId]['Items'][$_GET['Item']]];
          $Text = $LineAJ[$Column];
          $names = array();
          if ($User->Licence(LICENCE_USER))
          if (($GroupId < 4) or ($GroupId == 10) or ($GroupId == 11))
          {
            //<span class="edit">barvou.</span>
            $names = GetTranslatNames($Text, 0, GetTranslatNamesArray());
          } else 
          {
            if (($GroupId == 13)) 
            {
              $names = GetTranslatNames($Text, 0, array('Dictionary' => 'Text',
                'TextGlobalString' => 'Text', 'TextArea' => 'Name',
                'TextItemSubClass' => 'Name', 'TextCharacterRace' => 'Name1',), false);
            } else 
            {
              $names = GetTranslatNames($Text, 0, GetTranslatNamesArray());
            }
          }
          //$LineAJ[$Column]
          return $this->ColorNames(htmlspecialchars($Text), $names);
    }

  function Show(): string
  {
    $this->RawPage = true;
    return str_replace("\n", '<br/>', $this->LoadNames());
  }
}
