Skip to content

Commit

Permalink
feat: added custom date and duration format in user settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Vafilor committed Apr 22, 2021
1 parent 0df35af commit 56962bd
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 7 deletions.
38 changes: 38 additions & 0 deletions migrations/Version20210422010206.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210422010206 extends AbstractMigration
{
public function getDescription() : string
{
return 'Adds date and duration formats to user as settings.';
}

public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE users ADD date_format VARCHAR(255)');
$this->addSql("UPDATE users SET date_format = 'h:i:s A'");
$this->addSql('ALTER TABLE users ALTER COLUMN date_format SET NOT NULL');

$this->addSql('ALTER TABLE users ADD duration_format VARCHAR(255)');
$this->addSql("UPDATE users SET duration_format = '%hh %Im %Ss'");
$this->addSql('ALTER TABLE users ALTER COLUMN duration_format SET NOT NULL');
}

public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE users DROP date_format');
$this->addSql('ALTER TABLE users DROP duration_format');
}
}
5 changes: 5 additions & 0 deletions src/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ public function list(Request $request): Response {
$data = $form->getData();

$user->setTimezone($data->getTimezone());
$user->setDurationFormat($data->getDurationFormat());
$user->setDateFormat($data->getDateFormat());

$this->getDoctrine()->getManager()->flush();

$this->addFlash('success', 'User settings updated');
}

return $this->render('user/view.html.twig', [
Expand Down
40 changes: 40 additions & 0 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,23 @@ class User extends BaseUser
*/
private $timezone;

/**
* @ORM\Column(type="string")
*/
private $dateFormat;

/**
* @ORM\Column(type="string")
*/
private $durationFormat;

public function __construct()
{
parent::__construct();
$this->timeEntries = new ArrayCollection();
$this->timezone = "America/Los_Angeles";
$this->dateFormat = 'h:i:s A';
$this->durationFormat = '%hh %Im %Ss';
}

public function gravatarUrl(int $size = 30): string
Expand Down Expand Up @@ -68,4 +80,32 @@ public function setTimezone(string $timezone): self
$this->timezone = $timezone;
return $this;
}

/**
* @return string
*/
public function getDateFormat(): string
{
return $this->dateFormat;
}

public function setDateFormat(string $dateFormat): User
{
$this->dateFormat = $dateFormat;
return $this;
}

/**
* @return string
*/
public function getDurationFormat(): string
{
return $this->durationFormat;
}

public function setDurationFormat(string $durationFormat): self
{
$this->durationFormat = $durationFormat;
return $this;
}
}
28 changes: 27 additions & 1 deletion src/Form/Model/UserEditModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
class UserEditModel
{
private string $timezone;
private string $dateFormat;
private string $durationFormat;

public static function fromEntity(User $user): self
{
$model = new UserEditModel();
$model->setTimezone($user->getTimezone());
$model->setDateFormat($user->getDateFormat());
$model->setDurationFormat($user->getDurationFormat());

return $model;
}
Expand All @@ -27,9 +31,31 @@ public function getTimezone(): string
return $this->timezone;
}

public function setTimezone(string $timezone): UserEditModel
public function setTimezone(string $timezone): self
{
$this->timezone = $timezone;
return $this;
}

public function getDateFormat(): string
{
return $this->dateFormat;
}

public function setDateFormat(string $dateFormat): self
{
$this->dateFormat = $dateFormat;
return $this;
}

public function getDurationFormat(): string
{
return $this->durationFormat;
}

public function setDurationFormat(string $durationFormat): self
{
$this->durationFormat = $durationFormat;
return $this;
}
}
3 changes: 3 additions & 0 deletions src/Form/UserEditFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Form\Model\UserEditModel;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand All @@ -16,6 +17,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('timezone', TimezoneType::class)
->add('dateFormat', TextType::class)
->add('durationFormat', TextType::class)
;
}

Expand Down
7 changes: 7 additions & 0 deletions templates/time/partials.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% macro user_date(input_date, user) %}
{{ input_date|date(user.dateFormat, user.timezone) }}
{% endmacro %}

{% macro user_duration(input_duration, user) %}
{{ input_duration|date(user.durationFormat) }}
{% endmacro %}
7 changes: 4 additions & 3 deletions templates/time_entry/index.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends 'sidebar.html.twig' %}
{% import 'time/partials.html.twig' as time %}
{% import 'tag/partials.html.twig' as tags %}

{% block content_class %}w-100{% endblock %}
Expand Down Expand Up @@ -30,15 +31,15 @@
{{ tags.view(tag.name, tag.color) }}
{% endfor %}
</td>
<td>{{ timeEntry.createdAt|date('m/d/y h:i:s A', app.user.timezone) }}</td>
<td>{{ time.user_date(timeEntry.createdAt, app.user) }}</td>
<td>
{% if timeEntry.over %}
{{ timeEntry.endedAt|date('m/d/y h:i:s A', app.user.timezone) }}
{{ time.user_date(timeEntry.endedAt, app.user) }}
{% endif %}
</td>
<td>
{% if timeEntry.over %}
{{ timeEntry.duration|date("%hh %Im %Ss") }}
{{ time.user_duration(timeEntry.duration, app.user) }}
{% endif %}
</td>
<td>
Expand Down
7 changes: 4 additions & 3 deletions templates/time_entry/today.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends 'sidebar.html.twig' %}
{% import 'time/partials.html.twig' as time %}
{% import 'tag/partials.html.twig' as tags %}

{% block content_class %}w-100{% endblock %}
Expand Down Expand Up @@ -39,15 +40,15 @@
{{ tags.view(tag.name, tag.color) }}
{% endfor %}
</td>
<td>{{ timeEntry.createdAt|date('h:i:s A', app.user.timezone) }}</td>
<td>{{ time.user_date(timeEntry.createdAt, app.user) }}</td>
<td>
{% if timeEntry.over %}
{{ timeEntry.endedAt|date('h:i:s A', app.user.timezone) }}
{{ time.user_date(timeEntry.endedAt, app.user) }}
{% endif %}
</td>
<td>
{% if timeEntry.over %}
{{ timeEntry.duration|date("%hh %Im %Ss") }}
{{ time.user_duration(timeEntry.duration, app.user) }}
{% endif %}
</td>
<td>
Expand Down

0 comments on commit 56962bd

Please sign in to comment.