Skip to content

Commit

Permalink
Update Job.php
Browse files Browse the repository at this point in the history
Update URL Construction to fix duplicate URLs by appending a counter if the URL is already in use.

Improve Link Cleaning by removing unnecessary information from the link.
  • Loading branch information
moritz-sauer-13 authored Nov 6, 2024
1 parent cb53ea5 commit 930a663
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions src/DataObjects/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,48 @@ public function onBeforeWrite()

private function constructURLSegment()
{
return $this->cleanLink(strtolower(str_replace(" ", "-", $this->Title)));
$link = $this->cleanLink(strtolower($this->Title));
$count = 0;

// Stelle sicher, dass der Link eindeutig ist
while(Job::get()->filter('URLSegment', $link . ($count > 0 ? "-$count" : ''))->exists()) {
$count++;
}

return $link . ($count > 0 ? "-$count" : '');
}

private function cleanLink($string)
{
$string = str_replace("ä", "ae", $string);
$string = str_replace("ü", "ue", $string);
$string = str_replace("ö", "oe", $string);
$string = str_replace("Ä", "Ae", $string);
$string = str_replace("Ü", "Ue", $string);
$string = str_replace("Ö", "Oe", $string);
$string = str_replace("ß", "ss", $string);
$string = str_replace(["´", ",", ":", ";"], "", $string);
$string = str_replace(["´", ",", ":", ";"], "", $string);
$string = str_replace(["/", "(", ")"], "_", $string);
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string);
// Entferne führende und nachfolgende Leerzeichen
$string = trim($string);

$replacements = [
" " => "-", "ä" => "ae", "ü" => "ue", "ö" => "oe",
"Ä" => "Ae", "Ü" => "Ue", "Ö" => "Oe", "ß" => "ss",
"´" => "", "," => "", ":" => "", ";" => "",
"/" => "", "(" => "", ")" => ""
];

// Entferne typische Geschlechtskennzeichnungen wie (m/w/d)
$string = preg_replace('/\b(m\/w\/d|m\/w|w\/m|d|f|div)\b/i', '', $string);

// Ersetze alle definierten Zeichen
$string = strtr($string, $replacements);

// Entferne alle unzulässigen Zeichen
$string = preg_replace('/[^A-Za-z0-9\-_]/', '', $string);

// Entferne abschließende Bindestriche
$string = rtrim($string, '-');

// Ersetze doppelte Bindestriche oder Unterstriche durch einen einzigen
$string = preg_replace('/-{2,}/', '-', $string);
$string = preg_replace('/_{2,}/', '_', $string);

return $string;
}

public function getCMSFields()
{
$fields = parent::getCMSFields();
Expand Down

0 comments on commit 930a663

Please sign in to comment.