Skip to content

Commit

Permalink
Merge pull request #574 from leepeuker/569-req-about-languages-and-ti…
Browse files Browse the repository at this point in the history
…me-zone

Add timezone selector to server settings and display corret timezone …
  • Loading branch information
leepeuker authored Jan 24, 2024
2 parents 813ae89 + 4e26f4f commit 41e67a1
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 24 deletions.
24 changes: 12 additions & 12 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ The `Web UI` column is set to yes if an environment variable can alternatively b

### General

| NAME | DEFAULT VALUE | INFO | Web UI |
|:--------------------------------------------|:---------------:|:------------------------------------------------------------------------|:------:|
| `TMDB_API_KEY` | - | **Required** (get key [here](https://www.themoviedb.org/settings/api)) | yes |
| `APPLICATION_URL` | - | Public base url of the application (e.g. `htttp://localhost`) | yes |
| `APPLICATION_NAME` | `Movary` | Application name, displayed e.g. as brand name in the navbar | yes |
| `TMDB_ENABLE_IMAGE_CACHING` | `0` | More info [here](features/tmdb-data.md#image-cache) | |
| `ENABLE_REGISTRATION` | `0` | Enables public user registration | |
| `MIN_RUNTIME_IN_SECONDS_FOR_JOB_PROCESSING` | `15` | Minimum time between background jobs processing | |
| `TIMEZONE` | `Europe/Berlin` | Supported timezones [here](https://www.php.net/manual/en/timezones.php) | |
| `DEFAULT_LOGIN_EMAIL` | - | Email address to always autofill on login page | |
| `DEFAULT_LOGIN_PASSWORD` | - | Password to always autofill on login page | |
| `TOTP_ISSUER` | `Movary` | The issuer used when setting up two factor authentication | |
| NAME | DEFAULT VALUE | INFO | Web UI |
|:--------------------------------------------|:-------------:|:------------------------------------------------------------------------|:------:|
| `TMDB_API_KEY` | - | **Required** (get key [here](https://www.themoviedb.org/settings/api)) | yes |
| `APPLICATION_URL` | - | Public base url of the application (e.g. `htttp://localhost`) | yes |
| `APPLICATION_NAME` | `Movary` | Application name, displayed e.g. as brand name in the navbar | yes |
| `TMDB_ENABLE_IMAGE_CACHING` | `0` | More info [here](features/tmdb-data.md#image-cache) | |
| `ENABLE_REGISTRATION` | `0` | Enables public user registration | |
| `MIN_RUNTIME_IN_SECONDS_FOR_JOB_PROCESSING` | `15` | Minimum time between background jobs processing | |
| `TIMEZONE` | `UTC` | Supported timezones [here](https://www.php.net/manual/en/timezones.php) | yes |
| `DEFAULT_LOGIN_EMAIL` | - | Email address to always autofill on login page | |
| `DEFAULT_LOGIN_PASSWORD` | - | Password to always autofill on login page | |
| `TOTP_ISSUER` | `Movary` | The issuer used when setting up two factor authentication | |

### Database

Expand Down
13 changes: 6 additions & 7 deletions public/js/settings-server-general.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const tmdbApiKeyInput = document.getElementById('tmdbApiKeyInput');
const applicationUrlInput = document.getElementById('applicationUrlInput');
const applicationNameInput = document.getElementById('applicationNameInput');
const applicationTimezoneSelect = document.getElementById('applicationTimezoneSelect');

document.getElementById('generalServerUpdateButton').addEventListener('click', async () => {
tmdbApiKeyInput.classList.remove('invalid-input');
applicationUrlInput.classList.remove('invalid-input');
applicationNameInput.classList.remove('invalid-input');
applicationTimezoneSelect.classList.remove('invalid-input');

let tmdbApiKeyInputValue = null;

Expand All @@ -29,15 +31,14 @@ document.getElementById('generalServerUpdateButton').addEventListener('click', a
}

if (applicationNameInput.value !== '') {
console.log(applicationNameInput)
if (isValidName(applicationNameInput.value) === false) {
addAlert('alertGeneralServerDiv', 'Application name not valid. Must only contain letters, numbers, spaces or \'-\' and have max 15 characters', 'danger');
applicationNameInput.classList.add('invalid-input');
return;
}
}

const response = await updateGeneral(tmdbApiKeyInputValue, applicationUrlInput.value, applicationNameInput.value);
const response = await updateGeneral(tmdbApiKeyInputValue, applicationUrlInput.value, applicationNameInput.value, applicationTimezoneSelect.value);

switch (response.status) {
case 200:
Expand All @@ -63,14 +64,15 @@ document.getElementById('generalServerUpdateButton').addEventListener('click', a
}
});

function updateGeneral(tmdbApiKey, applicationUrl, applicationName) {
function updateGeneral(tmdbApiKey, applicationUrl, applicationName, applicationTimezone) {
return fetch('/settings/server/general', {
method: 'POST', headers: {
'Content-Type': 'application/json'
}, body: JSON.stringify({
'tmdbApiKey': tmdbApiKey,
'applicationUrl': applicationUrl,
'applicationName': applicationName
'applicationName': applicationName,
'applicationTimezone': applicationTimezone,
})
});
}
Expand All @@ -86,9 +88,6 @@ function isValidUrl(urlString) {
function isValidName(nameString) {
const alphanumericRegex = /^[a-zA-Z0-9\s]+$/;

console.log(nameString)
console.log(alphanumericRegex.test(nameString))

if (alphanumericRegex.test(nameString) === false) {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Movary\Util\SessionWrapper;
use Movary\ValueObject\Config;
use Movary\ValueObject\DateFormat;
use Movary\ValueObject\DateTime;
use Movary\ValueObject\Http\Request;
use Phinx\Console\PhinxApplication;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -298,6 +299,7 @@ public static function createTwigEnvironment(ContainerInterface $container) : Tw
}

$twig->addGlobal('applicationName', $container->get(ServerSettings::class)->getApplicationName() ?? 'Movary');
$twig->addGlobal('applicationTimezone', $container->get(ServerSettings::class)->getApplicationTimezone() ?? DateTime::DEFAULT_TIME_ZONE);
$twig->addGlobal('currentUserName', $user?->getName());
$twig->addGlobal('currentUserIsAdmin', $user?->isAdmin());
$twig->addGlobal('currentUserCountry', $user?->getCountry());
Expand Down
10 changes: 9 additions & 1 deletion src/HttpController/Web/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Movary\Util\Json;
use Movary\Util\SessionWrapper;
use Movary\ValueObject\DateFormat;
use Movary\ValueObject\DateTime;
use Movary\ValueObject\Http\Header;
use Movary\ValueObject\Http\Request;
use Movary\ValueObject\Http\Response;
Expand Down Expand Up @@ -153,7 +154,6 @@ public function renderAppPage() : Response
$this->twig->render('page/settings-app.html.twig', [
'currentApplicationVersion' => $this->serverSettings->getApplicationVersion(),
'latestRelease' => $this->githubApi->fetchLatestMovaryRelease(),
'timeZone' => date_default_timezone_get(),
]),
);
}
Expand Down Expand Up @@ -442,10 +442,14 @@ public function renderServerGeneralPage() : Response
$this->twig->render('page/settings-server-general.html.twig', [
'applicationUrl' => $this->serverSettings->getApplicationUrl(),
'applicationName' => $this->serverSettings->getApplicationName(),
'applicationTimezone' => $this->serverSettings->getApplicationTimezone(),
'applicationTimezoneDefault' => DateTime::DEFAULT_TIME_ZONE,
'applicationTimezonesAvailable' => timezone_identifiers_list(),
'tmdbApiKey' => $this->serverSettings->getTmdbApiKey(),
'tmdbApiKeySetInEnv' => $this->serverSettings->isTmdbApiKeySetInEnvironment(),
'applicationUrlSetInEnv' => $this->serverSettings->isApplicationUrlSetInEnvironment(),
'applicationNameSetInEnv' => $this->serverSettings->isApplicationNameSetInEnvironment(),
'applicationTimezoneSetInEnv' => $this->serverSettings->isApplicationTimezoneSetInEnvironment(),
]),
);
}
Expand Down Expand Up @@ -711,6 +715,7 @@ public function updateServerGeneral(Request $request) : Response
$tmdbApiKey = isset($requestData['tmdbApiKey']) === false ? null : $requestData['tmdbApiKey'];
$applicationUrl = isset($requestData['applicationUrl']) === false ? null : $requestData['applicationUrl'];
$applicationName = isset($requestData['applicationName']) === false ? null : $requestData['applicationName'];
$applicationTimezone = isset($requestData['applicationTimezone']) === false ? null : $requestData['applicationTimezone'];

if ($tmdbApiKey !== null) {
$this->serverSettings->setTmdbApiKey($tmdbApiKey);
Expand All @@ -721,6 +726,9 @@ public function updateServerGeneral(Request $request) : Response
if ($applicationName !== null) {
$this->serverSettings->setApplicationName($applicationName);
}
if ($applicationTimezone !== null) {
$this->serverSettings->setApplicationTimezone($applicationTimezone);
}

return Response::createOk();
}
Expand Down
17 changes: 17 additions & 0 deletions src/Service/ServerSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class ServerSettings
{
private const APPLICATION_TIMEZONE = 'TIMEZONE';

private const TOTP_ISSUER = 'TOTP_ISSUER';

private const JELLYFIN_DEVICE_ID = 'JELLYFIN_DEVICE_ID';
Expand Down Expand Up @@ -58,6 +60,11 @@ public function getApplicationName() : ?string
return $this->getByKey(self::APPLICATION_NAME);
}

public function getApplicationTimezone() : ?string
{
return $this->getByKey(self::APPLICATION_TIMEZONE);
}

public function getApplicationVersion() : string
{
return $this->getByKey(self::APPLICATION_VERSION) ?? 'unknown';
Expand Down Expand Up @@ -138,6 +145,11 @@ public function isApplicationNameSetInEnvironment() : bool
return $this->isSetInEnvironment(self::APPLICATION_NAME);
}

public function isApplicationTimezoneSetInEnvironment() : bool
{
return $this->isSetInEnvironment(self::APPLICATION_TIMEZONE);
}

public function isApplicationUrlSetInEnvironment() : bool
{
return $this->isSetInEnvironment(self::APPLICATION_URL);
Expand Down Expand Up @@ -218,6 +230,11 @@ public function setApplicationName(string $applicationName) : void
$this->updateValue(self::APPLICATION_NAME, $applicationName);
}

public function setApplicationTimezone(string $applicationTimezone) : void
{
$this->updateValue(self::APPLICATION_TIMEZONE, $applicationTimezone);
}

public function setApplicationUrl(string $applicationUrl) : void
{
$this->updateValue(self::APPLICATION_URL, $applicationUrl);
Expand Down
2 changes: 1 addition & 1 deletion templates/page/settings-app.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
</p>
<p>
<span class="text-{{ theme == 'dark' ? 'light' : 'dark' }} theme-text-color">Timezone:</span>
<span class="fw-light">{{ timeZone }}</span>
<span class="fw-light">{{ applicationTimezone }}</span>
</p>

<hr>
Expand Down
20 changes: 17 additions & 3 deletions templates/page/settings-server-general.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
{{ applicationUrlSetInEnv == true ? 'disabled' }}
>
<div id="applicationUrlInput" class="form-text">
The public base url used to serve Movary.
The public base url used to reach Movary.
</div>
</div>

Expand All @@ -70,10 +70,24 @@
value="{{ applicationName }}"
id="applicationNameInput"
style="text-align: center"
{{ applicationNameSetInEnv == true ? 'disabled' }}

>
<div id="applicationNameInput" class="form-text">
Overwrite the default application name.
The display name used by the application.
</div>
</div>

<div class="mb-3">
<label for="applicationTimezoneSelect" class="form-label">
<b>Timezone</b>
</label>
<select class="form-control text-center" name="ge" id="applicationTimezoneSelect" {{ applicationTimezoneSetInEnv == true ? 'disabled' }}>
{% for applicationTimezoneAvailable in applicationTimezonesAvailable %}
<option value="{{ applicationTimezoneAvailable }}" {{ applicationTimezoneAvailable == applicationTimezone ?? applicationTimezoneDefault ? 'selected' }}>{{ applicationTimezoneAvailable }}</option>
{% endfor %}
</select>
<div id="applicationTimezoneInput" class="form-text">
The default timezone used by the application.
</div>
</div>

Expand Down

0 comments on commit 41e67a1

Please sign in to comment.