Skip to content

Commit

Permalink
Merge pull request #66 from smartbooster/using_history_feature_from_c…
Browse files Browse the repository at this point in the history
…ore_bundle

New show_history_field based on tailwind class
  • Loading branch information
mathieu-ducrot authored Jun 25, 2024
2 parents addeff4 + e7010ab commit f7c60bb
Show file tree
Hide file tree
Showing 41 changed files with 967 additions and 66 deletions.
64 changes: 64 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,69 @@
CHANGELOG
===================
## v2.5.0 - (2024-06-25)

### Added
- `show_history_field.html.twig` based on tailwind class + Twig `HistoryExtension` required to autocomplete some data of the history rows
- `HistoryExtension` to automaticaly add the mentioned above template on the show view of every entity that implement the `Smart\CoreBundle\Entity\HistoryInterface`
- iconify cdn to use **iconify-icon Web Component** in `standard_layout.html.twig` and `empty_layout.html.twig `
- Twig `FormatExtension` to detect data type base on string value
- `templates/macros/badge.html.twig` to use in tailwind block
- `AbstractAdmin::showHistoryTemplate` property to ease the override of the show_history_template.html.twig per admin

### Changed
- **BC Break** `SendAccountCreationEmailTrait::sendAccountCreationEmailAction` the user subject is now entirely passed to the `BaseMailer` to log the email sent in
his history.
- You must add the `MailableInterface` to your User entity for `BaseMailer::setRecipientToEmail` to work properly
- `BaseMailer::setRecipientToEmail` advanced scenario to init the **to**, **cc** and **bcc** of the email based on the recipient type
- **BC Break** `AbstractApiCallAdmin` and `AbstractCronAdmin` now use **messages** for `choice_translation_domain` for their **type** properties
- You must move your cron.my_command.label translations on the **messages.%lang%.%format%** file instead of using the **admin.%lang%.%format%**
- `api_call_status.html.twig` now display null status code as "Ongoing" placeholder text
- `Parameter` entity now use `HistorizableInterface` from core-bundle
- Impact on `ParameterAdmin` : HistoryLogger DI removed + all previous log mention removed
- No more need to declare the `list_value.html.twig` and `timeline_history_field.html.twig` on the project as they are handle by the Sonata-Bundle
- **BC Break** database migration require to keep old history legacy :
```php
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20240619081425 extends AbstractMigration
{
public function getDescription(): string
{
return "Rename history field to historyLegacy for conversion and add new history field which is saved from the core-bundle";
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE smart_parameter CHANGE history history_legacy JSON DEFAULT NULL');
$this->addSql('ALTER TABLE smart_parameter ADD history JSON DEFAULT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE smart_parameter DROP history');
$this->addSql('ALTER TABLE smart_parameter CHANGE history_legacy history JSON DEFAULT NULL');
}
}
```

### Flag as deprecated

In anticipation for the v3.0.0 (cf. [UPGRADE-3.0.md](UPGRADE-3.0.md)) we mark the following classes as deprecated :

- `Smart\SonataBundle\Entity\Log\BatchLog`
- `Smart\SonataBundle\Entity\Log\HistorizableInterface`
- `Smart\SonataBundle\Entity\Log\HistorizableTrait`
- `Smart\SonataBundle\Logger\BatchLogger`
- `Smart\SonataBundle\Logger\HistoryLogger`
- `templates\admin\base_field\timeline_history_field.html.twig`

## v2.4.0 - (2024-06-12)
### Added
- `DocumentationController::renderMarkdown` action to render markdown documentation files stored in the **/documentation** directory
Expand Down
25 changes: 25 additions & 0 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
UPGRADE FROM 2.X to 3.0
===================

## BC Break

This section list all the changes that are considered BC Break and will node code adjustment from upgrading to version 3 (not released yet).

### Entity

- Remove `Smart\SonataBundle\Entity\Log\BatchLog`, use `Smart\CoreBundle\Entity\ProcessTrait` and `Smart\CoreBundle\Entity\ProcessInterface` instead
- Remove `Smart\SonataBundle\Entity\Log\HistorizableInterface`, use `Smart\CoreBundle\Entity\Log\HistorizableInterface` instead
- Remove `Smart\SonataBundle\Entity\Log\HistorizableTrait`, use `Smart\CoreBundle\Entity\Log\HistorizableTrait` instead

### Logger

- Remove `Smart\SonataBundle\Logger\BatchLogger`, use `Smart\CoreBundle\Monitor\ProcessMonitor` instead
- Remove `Smart\SonataBundle\Logger\HistoryLogger`, use `Smart\CoreBundle\Logger\HistoryLogger` instead

### Security

- `SmartUserInterface` now extends `MailableInterface` so you need to define his methods

### Templates

- Remove `timeline_history_field.html.twig`, use `show_history_field.html.twig` instead
5 changes: 5 additions & 0 deletions assets/styles/_documentation.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@
@apply font-semibold;
}
}
p {
img {
@apply block;
}
}
}
10 changes: 10 additions & 0 deletions assets/styles/_skin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
padding-top: 79px;
}
}
.content-header:has(.content-header-under-banner) {
margin-top: 29px;
}
.content-header-under-banner {
.sticky-wrapper {
.navbar.navbar-default.stuck {
margin-top: 29px;
}
}
}
.main-sidebar {
hr {
margin-bottom: 0;
Expand Down
6 changes: 6 additions & 0 deletions assets/styles/_tailwind.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.sb-tailwind {
// Additional overhead to have an ISO rendering with our platform-bundle
font-family: ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";

.ps-0 {
padding-inline-start: 0px;
}
Expand All @@ -15,4 +18,7 @@
.container-small {
@apply mx-auto w-full max-w-screen-md; /* 768px */
}
.sb-history-comment {
@apply prose prose-sm prose-stone prose-a:text-info prose-a:no-underline hover:prose-a:underline max-w-none rounded-lg border border-solid border-neutral-lighter py-2 px-4 my-2;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"yokai/security-token-bundle": "^3.3",
"sentry/sentry-symfony": "^4.1",
"symfony/expression-language": "^4.4 || ^5.4 || ^6.0",
"smartbooster/core-bundle": "^1.6",
"smartbooster/core-bundle": "^1.8",
"yokai/enum-bundle": "^4.1"
},
"require-dev": {
Expand Down
3 changes: 3 additions & 0 deletions config/bundle_prepend_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ sonata_admin:
admin.extension.encode_password:
uses:
- Smart\SonataBundle\Entity\User\UserTrait
admin.extension.history:
implements:
- Smart\CoreBundle\Entity\HistoryInterface

# Global Timezone Config
# https://stackoverflow.com/a/26469662
Expand Down
16 changes: 14 additions & 2 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ services:
- Smart\SonataBundle\Entity\Parameter
- ~
- '@Smart\SonataBundle\Enum\ParameterTypeEnum'
- '@Smart\SonataBundle\Logger\HistoryLogger'
tags:
- {name: sonata.admin, manager_type: orm, label: dashboard.label_parameter}

Expand Down Expand Up @@ -62,6 +61,10 @@ services:
- '@Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface'
tags:
- {name: sonata.admin.extension}
admin.extension.history:
class: Smart\SonataBundle\Admin\Extension\HistoryExtension
tags:
- {name: sonata.admin.extension, global: true}

# Command
smart_sonata.parameter_load_command:
Expand Down Expand Up @@ -146,7 +149,7 @@ services:
- '@Symfony\Component\Mailer\MailerInterface'
- '@Smart\SonataBundle\Mailer\EmailProvider'
- '@Symfony\Contracts\Translation\TranslatorInterface'
- '@Smart\SonataBundle\Logger\HistoryLogger'
- '@Smart\CoreBundle\Logger\HistoryLogger'
Smart\SonataBundle\Mailer\EmailProvider:
arguments:
- '@request_stack'
Expand Down Expand Up @@ -176,6 +179,7 @@ services:
arguments:
- '@security.processor.last_login'
- '@translator'
- '@Smart\CoreBundle\EventListener\HistoryDoctrineListener'
tags:
- { name: kernel.event_subscriber }
Smart\SonataBundle\Security\Handler\SmartSecurityHandler:
Expand All @@ -185,3 +189,11 @@ services:
# Templating
Sonata\AdminBundle\Templating\TemplateRegistry:
alias: 'sonata.admin.global_template_registry'

# Twig
Smart\SonataBundle\Twig\Extension\FormatExtension:
tags: [ 'twig.extension' ]
Smart\SonataBundle\Twig\Extension\HistoryExtension:
calls:
- setTranslator: [ '@translator' ]
tags: ['twig.extension']
1 change: 1 addition & 0 deletions src/Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ abstract class AbstractAdmin extends \Sonata\AdminBundle\Admin\AbstractAdmin imp
/** @var ContainerInterface $container */
private $container;
private TokenStorageInterface $tokenStorage;
public ?string $showHistoryTemplate = null;

public function __construct(?string $code = null, ?string $class = null, ?string $baseControllerName = null)
{
Expand Down
38 changes: 38 additions & 0 deletions src/Admin/Extension/HistoryExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Smart\SonataBundle\Admin\Extension;

use Smart\CoreBundle\Entity\Log\HistorizableInterface;
use Sonata\AdminBundle\Admin\AbstractAdminExtension;
use Sonata\AdminBundle\Show\ShowMapper;

/**
* @author Mathieu Ducrot <[email protected]>
*/
class HistoryExtension extends AbstractAdminExtension
{
public function configureShowFields(ShowMapper $show): void
{
$admin = $show->getAdmin();
$subject = $admin->getSubject();
if (!$subject instanceof HistorizableInterface) {
return;
}

$tabs = $admin->getShowTabs();
if (count($tabs) === 1 && $tabs[array_key_first($tabs)]['auto_created']) {
$show->end();
}
$groups = $admin->getShowGroups();
if (!isset($tabs['history_tab'])) {
$show->tab('history_tab', ['label' => 'label.history']);
if (!isset($groups['history_group'])) {
$show->with('history_group', ['label' => 'label.history']);
}
}

if (!$show->has('history')) {
$show->add('history', null, ['template' => $admin->showHistoryTemplate ?? '@SmartSonata/admin/base_field/show_history_field.html.twig']);
}
}
}
1 change: 1 addition & 0 deletions src/Admin/Monitoring/AbstractApiCallAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ protected function configureShowFields(ShowMapper $show): void
->add('type', FieldDescriptionInterface::TYPE_CHOICE, [
'label' => 'label.route',
'choices' => array_flip($this->getRouteChoices()),
'choice_translation_domain' => 'messages',
])
->add('startedAt', null, ['label' => 'label.started_at'])
->add('endedAt', null, ['label' => 'label.ended_at'])
Expand Down
6 changes: 3 additions & 3 deletions src/Admin/Monitoring/AbstractCronAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function configureDatagridFilters(DatagridMapper $filter): void
'field_type' => ChoiceType::class,
'field_options' => [
'choices' => $this->commandPoolHelper->getCronChoices(),
'choice_translation_domain' => 'admin',
'choice_translation_domain' => 'messages',
],
])
->add('status', ChoiceFilter::class, [
Expand Down Expand Up @@ -77,7 +77,7 @@ protected function configureListFields(ListMapper $list): void
->addIdentifier('type', FieldDescriptionInterface::TYPE_CHOICE, [
'label' => 'label.type',
'choices' => array_flip($this->commandPoolHelper->getCronChoices()),
'choice_translation_domain' => 'admin',
'choice_translation_domain' => 'messages',
'sortable' => false,
])
->add('startedAt', null, ['label' => 'label.started_at'])
Expand All @@ -101,7 +101,7 @@ protected function configureShowFields(ShowMapper $show): void
->add('type', FieldDescriptionInterface::TYPE_CHOICE, [
'label' => 'label.type',
'choices' => array_flip($this->commandPoolHelper->getCronChoices()),
'choice_translation_domain' => 'admin',
'choice_translation_domain' => 'messages',
])
->add('startedAt', null, ['label' => 'label.started_at'])
->add('endedAt', null, ['label' => 'label.ended_at'])
Expand Down
29 changes: 3 additions & 26 deletions src/Admin/ParameterAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

namespace Smart\SonataBundle\Admin;

use Doctrine\ORM\UnitOfWork;
use Smart\CoreBundle\Validator\Constraints\EmailChain;
use Smart\SonataBundle\Entity\Log\HistorizableInterface;
use Smart\SonataBundle\Entity\ParameterInterface;
use Smart\SonataBundle\Enum\ParameterTypeEnum;
use Smart\SonataBundle\Logger\HistoryLogger;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface;
Expand All @@ -29,19 +26,16 @@
class ParameterAdmin extends AbstractAdmin
{
private ParameterTypeEnum $typeEnum;
private HistoryLogger $historyLogger;
private array $updateInitialData = [];
public ?string $showHistoryTemplate = '@SmartSonata/admin/parameter_admin/show_history_field.html.twig';

public function __construct(
string $code,
?string $class,
string $baseControllerName,
ParameterTypeEnum $typeEnum,
HistoryLogger $historyLogger,
) {
parent::__construct($code, $class, $baseControllerName);
$this->typeEnum = $typeEnum;
$this->historyLogger = $historyLogger;
}

protected function configureRoutes(RouteCollectionInterface $collection): void
Expand All @@ -65,7 +59,7 @@ protected function configureListFields(ListMapper $list): void
->add('help', null, ['label' => 'field.label_help'])
->add('value', null, [
'label' => 'field.label_value',
'template' => 'admin/parameter_admin/list_value.html.twig'
'template' => '@SmartSonata/admin/parameter_admin/list_value.html.twig'
])
;
}
Expand Down Expand Up @@ -118,7 +112,7 @@ protected function configureShowFields(ShowMapper $show): void
->add('value', $valueType, ['label' => 'field.label_value'])
->end()
->with('fieldset.label_history', ['class' => 'col-md-12', 'label' => 'fieldset.label_history'])
->add('history', null, ['template' => 'admin/parameter_admin/timeline_history_field.html.twig'])
->add('historyLegacy', null, ['template' => '@SmartSonata/admin/parameter_admin/timeline_history_field.html.twig'])
->end()
;
}
Expand Down Expand Up @@ -197,21 +191,4 @@ protected function configureExportFields(): array
$this->trans('field.label_help') => 'help',
];
}

protected function preUpdate(object $object): void
{
/** @var UnitOfWork $uow @phpstan-ignore-next-line */
$uow = $this->getModelManager()->getEntityManager($this->getClass())->getUnitOfWork();
$this->updateInitialData = [
'value' => $uow->getOriginalEntityData($object)['value'],
];
}

protected function postUpdate(object $object): void
{
/** @var HistorizableInterface $object */
$this->historyLogger->logDiff($object, $this->updateInitialData, [
'author' => $this->getUser()->getFullName(), // @phpstan-ignore-line
]);
}
}
4 changes: 3 additions & 1 deletion src/Controller/AbstractSecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Smart\SonataBundle\Controller;

use Doctrine\ORM\EntityManagerInterface;
use Smart\CoreBundle\EventListener\HistoryDoctrineListener;
use Smart\SonataBundle\Form\Type\Security\ForgotPasswordType;
use Smart\SonataBundle\Mailer\BaseMailer;
use Smart\SonataBundle\Security\Form\Type\ResetPasswordType;
Expand Down Expand Up @@ -128,7 +129,7 @@ public function forgotPassword(Request $request, ParameterBagInterface $paramete
*
* @return Response
*/
public function resetPassword(Request $request)
public function resetPassword(Request $request, HistoryDoctrineListener $historyListener)
{
if ($this->getUser()) {
return $this->redirectToRoute($this->context . '_dashboard');
Expand Down Expand Up @@ -172,6 +173,7 @@ public function resetPassword(Request $request)

try {
if (null !== $user->getPlainPassword()) {
$historyListener->disable();
$this->updateUser($user);
$this->tokenManager->consume($token);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/CRUD/SendAccountCreationEmailTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function sendAccountCreationEmailAction(TokenManagerInterface $tokenManag
'security_reset_password_route' => $context . '_security_reset_password',
'token' => $token->getValue(),
]);
$mailer->send($email, $subject->getEmail());
$mailer->send($email, $subject);

$this->addFlash('success', $translator->trans('send_account_creation_email.success', [
'{email}' => $subject->getEmail()
Expand Down
Loading

0 comments on commit f7c60bb

Please sign in to comment.