Skip to content

Commit

Permalink
dev: Replace some view variables by helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
marienfressinaud committed Dec 1, 2024
1 parent 263bb22 commit 0efb5bd
Show file tree
Hide file tree
Showing 41 changed files with 114 additions and 104 deletions.
7 changes: 0 additions & 7 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,17 @@ public function run(\Minz\Request $request): mixed

$app_conf = \App\Configuration::$application;
\Minz\Output\View::declareDefaultVariables([
'environment' => \App\Configuration::$environment,
'brand' => $app_conf['brand'],
'csrf_token' => \Minz\Csrf::generate(),
'errors' => $errors,
'error' => $error,
'status' => $status,
'available_locales' => utils\Locale::availableLocales(),
'current_locale' => $locale,
'current_user' => $current_user,
'beta_enabled' => $beta_enabled,
'autoload_modal_url' => $autoload_modal_url,
'now' => \Minz\Time::now(),
'javascript_configuration' => json_encode(include('utils/javascript_configuration.php')),
'modal_requested' => $request->header('HTTP_TURBO_FRAME') === 'modal-content',
'demo' => $app_conf['demo'],
'registrations_opened' => $app_conf['registrations_opened'],
'plausible_url' => $app_conf['plausible_url'],
'current_host' => \App\Configuration::$url_options['host'],
]);

$response = \Minz\Engine::run($request);
Expand Down
20 changes: 20 additions & 0 deletions src/utils/view_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ function format_number(int|float $number): string
return $formatted_number;
}

function is_environment(string $environment): bool
{
return \App\Configuration::$environment === $environment;
}

function get_app_configuration(string $key): mixed
{
return \App\Configuration::$application[$key] ?? '';
}

function get_current_host(): string
{
return \App\Configuration::$url_options['host'];
}

function get_current_locale(): string
{
return \App\utils\Locale::currentLocale();
}

/**
* Transform a locale to BCP47 format
*
Expand Down
1 change: 0 additions & 1 deletion src/views/_layouts/base.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
// The title can be considered as safe since it's already sanitized in
// the calling view.
'title' => $this->safe('title'),
'current_locale' => $current_locale,
'current_tab' => $current_tab ?? null,
'current_page' => $current_page ?? null,
'canonical' => $this->safe('canonical'),
Expand Down
16 changes: 8 additions & 8 deletions src/views/_layouts/connected.phtml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<!DOCTYPE html>
<html lang="<?= locale_to_bcp_47($current_locale) ?>">
<html lang="<?= locale_to_bcp_47(get_current_locale()) ?>">
<head>
<meta charset="UTF-8" />
<title><?= $environment === 'development' ? '[dev] ' : '' ?><?= $has_errors ? _('Error: ') : '' ?><?= $title ?> · <?= $brand ?></title>
<title><?= is_environment('development') ? '[dev] ' : '' ?><?= $has_errors ? _('Error: ') : '' ?><?= $title ?> · <?= get_app_configuration('brand') ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="canonical" href="<?= $this->safe('canonical') ?>">

<?php foreach ($alternates as $alternate): ?>
<link rel="alternate" type="<?= $alternate['type'] ?>" title="<?= protect($alternate['title']) ?>" href="<?= $alternate['url'] ?>" />
<?php endforeach; ?>

<meta name="application-name" content="<?= $brand ?>">
<meta name="application-name" content="<?= get_app_configuration('brand') ?>">
<meta name="theme-color" content="#00d0ad">
<link rel="manifest" href="<?= url('webmanifest') ?>">
<link rel="icon" type="image/png" sizes="32x32" href="<?= url_static('icons/icon-32.png') ?>">
Expand All @@ -27,8 +27,8 @@
</script>
<script src="<?= url_asset('javascripts/application.js') ?>" data-turbo-track="reload" defer></script>

<?php if ($plausible_url): ?>
<script defer data-domain="<?= $current_host ?>" src="<?= $plausible_url ?>/js/script.js"></script>
<?php if (get_app_configuration('plausible_url')): ?>
<script defer data-domain="<?= get_current_host() ?>" src="<?= get_app_configuration('plausible_url') ?>/js/script.js"></script>
<?php endif; ?>

<meta name="csrf-param" content="csrf" />
Expand All @@ -49,15 +49,15 @@
<noscript>
<div class="layout__banner layout__banner--alert">
<div class="banner__container">
<?= _f('You need to activate JavaScript in order to use %s.', $brand) ?>
<?= _f('You need to activate JavaScript in order to use %s.', get_app_configuration('brand')) ?>
</div>
</div>
</noscript>

<?php if ($demo): ?>
<?php if (get_app_configuration('demo')): ?>
<div class="layout__banner">
<div class="banner__container">
<?= _f('You’re using a demo version of %s, the data are reset every night.', $brand) ?>
<?= _f('You’re using a demo version of %s, the data are reset every night.', get_app_configuration('brand')) ?>
</div>
</div>
<?php endif; ?>
Expand Down
16 changes: 8 additions & 8 deletions src/views/_layouts/connected_blocked.phtml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<!DOCTYPE html>
<html lang="<?= locale_to_bcp_47($current_locale) ?>">
<html lang="<?= locale_to_bcp_47(get_current_locale()) ?>">
<head>
<meta charset="UTF-8" />
<title><?= $environment === 'development' ? '[dev] ' : '' ?><?= $has_errors ? _('Error: ') : '' ?><?= $title ?> · <?= $brand ?></title>
<title><?= is_environment('development') ? '[dev] ' : '' ?><?= $has_errors ? _('Error: ') : '' ?><?= $title ?> · <?= get_app_configuration('brand') ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="canonical" href="<?= $this->safe('canonical') ?>">

<meta name="application-name" content="<?= $brand ?>">
<meta name="application-name" content="<?= get_app_configuration('brand') ?>">
<meta name="theme-color" content="#00d0ad">
<link rel="manifest" href="<?= url('webmanifest') ?>">
<link rel="icon" type="image/png" sizes="32x32" href="<?= url_static('icons/icon-32.png') ?>">
Expand All @@ -23,8 +23,8 @@
</script>
<script src="<?= url_asset('javascripts/application.js') ?>" data-turbo-track="reload" defer></script>

<?php if ($plausible_url): ?>
<script defer data-domain="<?= $current_host ?>" src="<?= $plausible_url ?>/js/script.js"></script>
<?php if (get_app_configuration('plausible_url')): ?>
<script defer data-domain="<?= get_current_host() ?>" src="<?= get_app_configuration('plausible_url') ?>/js/script.js"></script>
<?php endif; ?>

<meta name="csrf-param" content="csrf" />
Expand All @@ -45,15 +45,15 @@
<noscript>
<div class="layout__banner layout__banner--alert">
<div class="banner__container">
<?= _f('You need to activate JavaScript in order to use %s.', $brand) ?>
<?= _f('You need to activate JavaScript in order to use %s.', get_app_configuration('brand')) ?>
</div>
</div>
</noscript>

<?php if ($demo): ?>
<?php if (get_app_configuration('demo')): ?>
<div class="layout__banner">
<div class="banner__container">
<?= _f('You’re using a demo version of %s, the data are reset every night.', $brand) ?>
<?= _f('You’re using a demo version of %s, the data are reset every night.', get_app_configuration('brand')) ?>
</div>
</div>
<?php endif; ?>
Expand Down
10 changes: 5 additions & 5 deletions src/views/_layouts/error.phtml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<!DOCTYPE html>
<html lang="<?= locale_to_bcp_47($current_locale) ?>">
<html lang="<?= locale_to_bcp_47(get_current_locale()) ?>">
<head>
<meta charset="UTF-8" />
<title><?= $environment === 'development' ? '[dev] ' : '' ?><?= $title ?> · <?= $brand ?></title>
<title><?= is_environment('development') ? '[dev] ' : '' ?><?= $title ?> · <?= get_app_configuration('brand') ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="<?= url_static('favicon-32px.png') ?>">
<meta name="robots" content="noindex">

<link rel="stylesheet" href="<?= url_asset('stylesheets/application.css') ?>" data-turbo-track="reload">

<?php if ($plausible_url): ?>
<script defer data-domain="<?= $current_host ?>" src="<?= $plausible_url ?>/js/script.js"></script>
<?php if (get_app_configuration('plausible_url')): ?>
<script defer data-domain="<?= get_current_host() ?>" src="<?= get_app_configuration('plausible_url') ?>/js/script.js"></script>
<?php endif; ?>
</head>

Expand All @@ -34,7 +34,7 @@
height="250"
/>

<?php if ($environment !== 'production' && $error): ?>
<?php if (!is_environment('production') && $error): ?>
<pre class="layout--error__box"><?= (string)$error ?></pre>
<?php endif; ?>
</main>
Expand Down
22 changes: 11 additions & 11 deletions src/views/_layouts/not_connected.phtml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!DOCTYPE html>
<html lang="<?= locale_to_bcp_47($current_locale) ?>">
<html lang="<?= locale_to_bcp_47(get_current_locale()) ?>">
<head>
<meta charset="UTF-8" />
<title><?= $environment === 'development' ? '[dev] ' : '' ?><?= $has_errors ? _('Error: ') : '' ?><?= $title ?> · <?= $brand ?></title>
<title><?= is_environment('development') ? '[dev] ' : '' ?><?= $has_errors ? _('Error: ') : '' ?><?= $title ?> · <?= get_app_configuration('brand') ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="canonical" href="<?= $this->safe('canonical') ?>">

Expand All @@ -24,7 +24,7 @@
<?php endif; ?>
<?php endif; ?>

<meta name="application-name" content="<?= $brand ?>">
<meta name="application-name" content="<?= get_app_configuration('brand') ?>">
<meta name="theme-color" content="#00d0ad">
<link rel="manifest" href="<?= url('webmanifest') ?>">
<link rel="icon" type="image/png" sizes="32x32" href="<?= url_static('icons/icon-32.png') ?>">
Expand All @@ -41,8 +41,8 @@
</script>
<script src="<?= url_asset('javascripts/application.js') ?>" data-turbo-track="reload" defer></script>

<?php if ($plausible_url): ?>
<script defer data-domain="<?= $current_host ?>" src="<?= $plausible_url ?>/js/script.js"></script>
<?php if (get_app_configuration('plausible_url')): ?>
<script defer data-domain="<?= get_current_host() ?>" src="<?= get_app_configuration('plausible_url') ?>/js/script.js"></script>
<?php endif; ?>

<meta name="csrf-param" content="csrf" />
Expand All @@ -63,15 +63,15 @@
<noscript>
<div class="layout__banner layout__banner--alert">
<div class="banner__container">
<?= _f('You need to activate JavaScript in order to use %s.', $brand) ?>
<?= _f('You need to activate JavaScript in order to use %s.', get_app_configuration('brand')) ?>
</div>
</div>
</noscript>

<?php if ($demo): ?>
<?php if (get_app_configuration('demo')): ?>
<div class="layout__banner">
<div class="banner__container">
<?= _f('You’re using a demo version of %s, the data are reset every night.', $brand) ?>
<?= _f('You’re using a demo version of %s, the data are reset every night.', get_app_configuration('brand')) ?>
</div>
</div>
<?php endif; ?>
Expand All @@ -89,7 +89,7 @@

<div class="header__separator"></div>

<?php if ($registrations_opened && $current_page !== 'registration'): ?>
<?php if (get_app_configuration('registrations_opened') && $current_page !== 'registration'): ?>
<a class="header__link no-mobile" href="<?= url('registration') ?>">
<?= _('Sign up') ?>
</a>
Expand Down Expand Up @@ -120,7 +120,7 @@
<?php foreach ($available_locales as $locale_value => $locale_name): ?>
<option
value="<?= $locale_value ?>"
<?= $locale_value === $current_locale ? 'selected' : '' ?>
<?= $locale_value === get_current_locale() ? 'selected' : '' ?>
>
<?= $locale_name ?>
</option>
Expand Down Expand Up @@ -160,7 +160,7 @@
<footer class="layout__footer">
<p>
<a href="<?= url('about') ?>">
<?= _f('About %s', $brand) ?>
<?= _f('About %s', get_app_configuration('brand')) ?>
</a>
</p>
</footer>
Expand Down
12 changes: 6 additions & 6 deletions src/views/_layouts/onboarding.phtml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<!DOCTYPE html>
<html lang="<?= locale_to_bcp_47($current_locale) ?>">
<html lang="<?= locale_to_bcp_47(get_current_locale()) ?>">
<head>
<meta charset="UTF-8" />
<title><?= $environment === 'development' ? '[dev] ' : '' ?><?= $title ?> · <?= $brand ?></title>
<title><?= is_environment('development') ? '[dev] ' : '' ?><?= $title ?> · <?= get_app_configuration('brand') ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="canonical" href="<?= $this->safe('canonical') ?>">

<meta name="application-name" content="<?= $brand ?>">
<meta name="application-name" content="<?= get_app_configuration('brand') ?>">
<meta name="theme-color" content="#00d0ad">
<link rel="manifest" href="<?= url('webmanifest') ?>">
<link rel="icon" type="image/png" sizes="32x32" href="<?= url_static('icons/icon-32.png') ?>">
Expand All @@ -23,8 +23,8 @@
</script>
<script src="<?= url_asset('javascripts/application.js') ?>" data-turbo-track="reload" defer></script>

<?php if ($plausible_url): ?>
<script defer data-domain="<?= $current_host ?>" src="<?= $plausible_url ?>/js/script.js"></script>
<?php if (get_app_configuration('plausible_url')): ?>
<script defer data-domain="<?= get_current_host() ?>" src="<?= get_app_configuration('plausible_url') ?>/js/script.js"></script>
<?php endif; ?>

<meta name="csrf-param" content="csrf" />
Expand All @@ -45,7 +45,7 @@
<noscript>
<div class="layout__banner layout__banner--alert">
<div class="banner__container">
<?= _f('You need to activate JavaScript in order to use %s.', $brand) ?>
<?= _f('You need to activate JavaScript in order to use %s.', get_app_configuration('brand')) ?>
</div>
</div>
</noscript>
Expand Down
2 changes: 1 addition & 1 deletion src/views/collections/followed.opml.xml.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?= '<?xml version="1.0" encoding="UTF-8"?>' . "\n" ?>
<opml version="2.0">
<head>
<title><?= $brand ?></title>
<title><?= get_app_configuration('brand') ?></title>
<dateCreated><?= $now->format('D, d M Y H:i:s') ?></dateCreated>
</head>

Expand Down
4 changes: 2 additions & 2 deletions src/views/collections/show_public.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
if ($collection->type === 'feed') {
$open_graph = [
'title' => $collection->name,
'description' => _f('A feed from %s, on %s.', protect($collection->feedWebsite()), $brand),
'description' => _f('A feed from %s, on %s.', protect($collection->feedWebsite()), get_app_configuration('brand')),
'locale' => $owner->locale,
'url' => url_full('collection', ['id' => $collection->id]),
];
Expand All @@ -17,7 +17,7 @@
} else {
$open_graph = [
'title' => $collection->name,
'description' => _f('A collection created by %s on %s.', $owner->username, $brand),
'description' => _f('A collection created by %s on %s.', $owner->username, get_app_configuration('brand')),
'locale' => $owner->locale,
'url' => url_full('collection', ['id' => $collection->id]),
];
Expand Down
4 changes: 2 additions & 2 deletions src/views/discovery/show.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
'current_tab' => 'feeds',
'open_graph' => [
'title' => _('Discover collections'),
'description' => _f('Discover the public collections on %s.', $brand),
'locale' => $current_locale,
'description' => _f('Discover the public collections on %s.', get_app_configuration('brand')),
'locale' => get_current_locale(),
'url' => url_full('discovery'),
],
'back_options' => [
Expand Down
2 changes: 1 addition & 1 deletion src/views/feeds/feeds.xsl.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<xsl:attribute name="href">
<xsl:value-of select="/atom:feed/atom:link[@rel='alternate']/@href"/>
</xsl:attribute>
<?= _f('Follow on %s', $brand) ?>
<?= _f('Follow on %s', get_app_configuration('brand')) ?>
</a>
</p>
</section>
Expand Down
10 changes: 5 additions & 5 deletions src/views/importations/pocket/show.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</div>

<p class="section__intro">
<?= _f('%s enables you to import your links saved in Pocket.', $brand) ?>
<?= _f('%s enables you to import your links saved in Pocket.', get_app_configuration('brand')) ?>
</p>

<?= $this->include('alerts/_error.phtml', ['message' => $error]) ?>
Expand Down Expand Up @@ -88,11 +88,11 @@

<p class="form-group__caption form-group__caption--alt" id="import-bookmarks-caption" aria-live="polite">
<span data-caption-switcher-target="caption" data-caption-value="on">
<?= _f('%s will import your non-archived Pocket links in your bookmarks.', $brand) ?>
<?= _f('%s will import your non-archived Pocket links in your bookmarks.', get_app_configuration('brand')) ?>
</span>

<span data-caption-switcher-target="caption" data-caption-unchecked>
<?= _f('%s will leave your bookmarks unchanged.', $brand) ?>
<?= _f('%s will leave your bookmarks unchanged.', get_app_configuration('brand')) ?>
</span>
</p>
</div>
Expand All @@ -114,11 +114,11 @@

<p class="form-group__caption form-group__caption--alt" id="import-favorites-caption" aria-live="polite">
<span data-caption-switcher-target="caption" data-caption-value="on">
<?= _f('%s doesn’t provide favorites, but it’ll import them in a collection named “Pocket favorites”.', $brand) ?>
<?= _f('%s doesn’t provide favorites, but it’ll import them in a collection named “Pocket favorites”.', get_app_configuration('brand')) ?>
</span>

<span data-caption-switcher-target="caption" data-caption-unchecked>
<?= _f('%s doesn’t provide favorites, so they’ll be ignored.', $brand) ?>
<?= _f('%s doesn’t provide favorites, so they’ll be ignored.', get_app_configuration('brand')) ?>
</span>
</p>
</div>
Expand Down
1 change: 0 additions & 1 deletion src/views/internal_server_error.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
$this->layout('error.phtml', [
'title' => _('Something bad happened'),
'error' => $error,
'environment' => $environment,
]);
?>

Expand Down
Loading

0 comments on commit 0efb5bd

Please sign in to comment.