<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

namespace Kufer\Web\KuferTools\Utils\WssField\Basis;

use Kufer\Web\KuferTools\Basis\UseCaseBuilder\ToolSection;


class WssFieldBasisWebLink extends \Kufer\Web\KuferTools\Utils\WssField\WssField
{
  protected $linkTarget;

  
  public function __construct(ToolSection $toolSection, \ToolSettings $toolSettings, $pDatamodulColumn, $linkTarget = '')
  {
    parent::__construct($toolSection, $toolSettings, $pDatamodulColumn);
    $this->linkTarget = $linkTarget;
  }

  protected function getFieldNichtBearbeitbar($datamodulWert, $isFormular, string $requestPrefix = '')
  {
    $result = \CleanOperations::cleanStringForHtml($this->getValue($datamodulWert, $isFormular));
    $matches = array();
    if (\preg_match('&^([a-z]+)://&', $result, $matches) === 1) {
      $target = $this->linkTarget === '' ? '' : ('target="' . \CleanOperations::cleanStringForHtml($this->linkTarget) . '"');
      $protocol = $matches[1] ?? '';
      if ($protocol === 'https' || $protocol === 'http') {
        
        
        $tagClasses = array('external-link'); 
        if ($protocol === 'http') {
          $tagClasses[] = 'external-link-unsecure';
        }

        return '<a class="' . \implode(' ', $tagClasses) . '" href="' . $result . '" rel="noopener noreferrer nofollow" ' . $target . '>'
          . $this->getLinkDescription($result)
          . '</a>';
      }
    }

    return $result;
  }

  protected function getLinkDescription($htmlLink)
  {
    return $htmlLink;
  }
}