Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Utility getCustomUrl, corrected prepareCustomLink #342

Merged
merged 4 commits into from
May 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 51 additions & 11 deletions lib/MForm/Utils/MFormOutputHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,90 @@ public static function isFirstSlice($sliceId): bool

public static function prepareCustomLink(array $item, bool $externBlank = true): array
{
// set url
// Set URL
if (!isset($item['link']) || empty($item['link'])) {
return $item;
}
$item['customlink_text'] = (isset($item['text']) && !isset($item['customlink_text'])) ? $item['text'] : '';
$item['customlink_url'] = $item['link'];
$item['customlink_target'] = '';

// media file?
if (true === file_exists(rex_path::media($item['link']))) {
// Media file?
if (file_exists(rex_path::media($item['link']))) {
$item['customlink_url'] = rex_url::media($item['link']);
$item['customlink_class'] = ' media';
} else {
// no media and no url and is numeric it must be an rex article id
if (false === filter_var($item['link'], FILTER_VALIDATE_URL) && is_numeric($item['link'])) {
$item['customlink_url'] = rex_getUrl($item['link'], rex_clang::getCurrentId());
// Check for rex:// URL
if (str_starts_with($item['link'], 'rex://')) {
$articleId = (int) substr($item['link'], 6);
$item['customlink_url'] = rex_getUrl($articleId, rex_clang::getCurrentId());
$item['customlink_class'] = ' intern';

if (empty($item['customlink_text'])) {
$art = rex_article::get($articleId, rex_clang::getCurrentId());
if ($art) {
$item['customlink_text'] = $art->getName();
}
}
}
// No media and no URL and is numeric, it must be a rex article id
elseif (!filter_var($item['link'], FILTER_VALIDATE_URL) && is_numeric($item['link'])) {
$item['customlink_url'] = rex_getUrl($item['link'], rex_clang::getCurrentId());
$item['customlink_class'] = ' internal';

if (empty($item['customlink_text'])) {
$art = rex_article::get($item['link'], rex_clang::getCurrentId());
if ($art) {
$item['customlink_text'] = $art->getName();
}
}
} else {
$item['customlink_class'] = ' extern';
$item['customlink_class'] = ' external';

if (strpos($item['customlink_url'], 'tel:') === 0) {
if (str_starts_with($item['customlink_url'], 'tel:')) {
$item['customlink_class'] = 'tel';
} elseif (strpos($item['customlink_url'], 'mailto:') === 0) {
} elseif (str_starts_with($item['customlink_url'], 'mailto:')) {
$item['customlink_class'] = 'mail';
}

if ($externBlank) {
$item['customlink_target'] = ' target="_blank"';
$item['customlink_target'] = ' target="_blank" rel="noopener noreferrer"';
}
}
}

// no link text?
// No link text?
if (empty($item['customlink_text'])) {
$item['customlink_text'] = str_replace(['http://', 'https://'], '', $item['customlink_url']);
}

return $item;
}

function getCustomUrl(mixed $value = null, ?string $lang = null): string
{
// Check if the value is null or empty
if (is_null($value) || $value === '') {
return '';
}

// Determine the language to use (current language if none provided)
$lang = $lang ?? rex_clang::getCurrentId();

// Check if the value is a REDAXO article (starts with rex://)
if (str_starts_with($value, 'rex://')) {
$articleId = (int) substr($value, 6); // Remove 'rex://' and convert the rest to an integer
return rex_getUrl($articleId, $lang);
}

// Check if the value is numeric
if (is_numeric($value)) {
$articleId = (int) $value;
return rex_getUrl($articleId, $lang);
}

// If the value is neither a REDAXO URL nor numeric, return the value
return $value;
}

}