From 05959f5355f595d467e4438b19d9db377f20c68b Mon Sep 17 00:00:00 2001 From: Eric Gesemann Date: Wed, 6 Dec 2023 16:58:54 +0100 Subject: [PATCH 01/13] Added: delete product option --- .../contao/dca/tl_ml_product_archive.php | 24 ++++++++++++++++++- .../languages/de/tl_ml_product_archive.php | 5 +++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Resources/contao/dca/tl_ml_product_archive.php b/src/Resources/contao/dca/tl_ml_product_archive.php index fdc101d..44ce80b 100644 --- a/src/Resources/contao/dca/tl_ml_product_archive.php +++ b/src/Resources/contao/dca/tl_ml_product_archive.php @@ -82,7 +82,7 @@ ], 'palettes' => [ '__selector__' => ['type', 'protected', 'useExifDataForTags'], - 'default' => '{general_legend},title;{config_legend},type,additionalFields,keepProductTitleForDownloadItems;{protected_legend},protected;', + 'default' => '{general_legend},title;{config_legend},type,additionalFields,keepProductTitleForDownloadItems,includeDelete,redirectAfterDelete;{protected_legend},protected;', ], 'subpalettes' => [ 'type_'.\HeimrichHannot\MediaLibraryBundle\DataContainer\ProductContainer::TYPE_IMAGE => 'imageSizes', @@ -176,5 +176,27 @@ 'eval' => ['tl_class' => 'clr'], 'sql' => "char(1) NOT NULL default ''", ], + 'includeDelete' => [ + 'label' => &$GLOBALS['TL_LANG']['tl_ml_product_archive']['includeDelete'], + 'exclude' => true, + 'filter' => true, + 'inputType' => 'checkbox', + 'default' => true, + 'eval' => ['tl_class' => 'clr'], + 'sql' => "char(1) NOT NULL default ''", + ], + 'redirectAfterDelete' => [ + 'inputType' => 'pageTree', + 'foreignKey' => 'tl_page.id', + 'eval' => [ + 'fieldType' => 'radio', + 'tl_class' => 'clr' + ], + 'sql' => "int(10) unsigned NOT NULL default 0", + 'relation' => [ + 'type' => 'hasOne', + 'load' => 'lazy' + ] + ] ], ]; diff --git a/src/Resources/contao/languages/de/tl_ml_product_archive.php b/src/Resources/contao/languages/de/tl_ml_product_archive.php index 85a90a0..68d4ce8 100644 --- a/src/Resources/contao/languages/de/tl_ml_product_archive.php +++ b/src/Resources/contao/languages/de/tl_ml_product_archive.php @@ -21,7 +21,10 @@ $lang['groups'][1] = 'Wählen Sie hier die gewünschten Mitgliedergruppen für den geschützten Zugriff aus.'; $lang['keepProductTitleForDownloadItems'][0] = 'Produktnamen im Downloadtitel behalten'; $lang['keepProductTitleForDownloadItems'][1] = 'Wählen Sie diese Option, wenn der Titel des Produktes in den Titeln der Downloadelementen bestehen bleiben soll.'; - +$lang['includeDelete'][0] = 'Produkte können gelöscht werden'; +$lang['includeDelete'][1] = 'Wählen Sie diese Option, wenn dem Nutzer die Möglichkeit gegeben werden soll, das Produkt zu löschen.'; +$lang['redirectAfterDelete'][0] = 'Weiterleitungsseite nach dem Löschen'; +$lang['redirectAfterDelete'][1] = 'Wählen Sie hier die Seite aus, zu der der Nutzer nach dem Löschen des Produktes weitergeleitet werden soll.'; /** * Legends From 4a0a155c64c08a6d531e82d9ae0141cae947b9ed Mon Sep 17 00:00:00 2001 From: Eric Gesemann Date: Wed, 6 Dec 2023 16:59:33 +0100 Subject: [PATCH 02/13] Added: delete product option --- src/EventListener/DeleteProductListener.php | 71 +++++++++++++++++++++ src/Model/ProductArchiveModel.php | 2 + 2 files changed, 73 insertions(+) create mode 100644 src/EventListener/DeleteProductListener.php diff --git a/src/EventListener/DeleteProductListener.php b/src/EventListener/DeleteProductListener.php new file mode 100644 index 0000000..61d0d24 --- /dev/null +++ b/src/EventListener/DeleteProductListener.php @@ -0,0 +1,71 @@ +getItem(); + + if ($item->getDataContainer() !== 'tl_ml_product') { + return; + } + + $request = $this->requestStack->getCurrentRequest(); + + if (!$request->isMethod(Request::METHOD_DELETE)) { + return; + } + + /** @var class-string $modelClass */ + $modelClass = Model::getClassFromTable('tl_ml_product'); + $product = $modelClass::findByPk($item->getRawValue('id')); + if ($product === null) { + throw new BadRequestHttpException('Invalid product id'); + } + + if (!$product->delete()) { + throw new InternalServerErrorHttpException('Could not delete product'); + } + + /** @var ProductArchiveModel|null $productArchive */ + $productArchive = $product->getRelated('pid'); + + if ($productArchive === null) { + throw new InternalServerErrorHttpException('Product archive not found'); + } + + if (!$productArchive->includeDelete) { + throw new MethodNotAllowedHttpException([Request::METHOD_DELETE], 'Delete not allowed'); + } + + $pageId = $productArchive->redirectAfterDelete; + $page = PageModel::findByPk($pageId); + + if ($page === null) { + throw new InternalServerErrorHttpException('No redirect page found'); + } + + throw new RedirectResponseException($page->getAbsoluteUrl()); + } +} \ No newline at end of file diff --git a/src/Model/ProductArchiveModel.php b/src/Model/ProductArchiveModel.php index 7ef6ee4..36ef975 100644 --- a/src/Model/ProductArchiveModel.php +++ b/src/Model/ProductArchiveModel.php @@ -18,6 +18,8 @@ * @property string $type * @property string $additionalFields * @property bool $keepProductTitleForDownloadItems + * @property bool $includeDelete + * @property bool $redirectAfterDelete * @property bool $protected */ class ProductArchiveModel extends Model From 7e1489729f768786a4c3478f097e225f14368486 Mon Sep 17 00:00:00 2001 From: Eric Gesemann Date: Thu, 7 Dec 2023 10:15:37 +0100 Subject: [PATCH 03/13] Added: moved update to form type bundle --- src/FormType/MediaLibraryType.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/FormType/MediaLibraryType.php b/src/FormType/MediaLibraryType.php index a5c7ea7..4fc6b4a 100644 --- a/src/FormType/MediaLibraryType.php +++ b/src/FormType/MediaLibraryType.php @@ -106,6 +106,8 @@ public function onPrepareFormData(PrepareFormDataEvent $event): void $event->getForm()->storeValues = '1'; $event->getForm()->targetTable = ProductModel::getTable(); } + + parent::onPrepareFormData($event); } public function onStoreFormData(StoreFormDataEvent $event): void @@ -144,6 +146,8 @@ public function onStoreFormData(StoreFormDataEvent $event): void public function onProcessFormData(ProcessFormDataEvent $event): void { + parent::onProcessFormData($event); + if (!class_exists(HeimrichHannotFileCreditsBundle::class)) { return; } From a7ebf05db32b9203e622f809601f964e30d8c3ee Mon Sep 17 00:00:00 2001 From: Eric Gesemann Date: Tue, 12 Dec 2023 14:09:03 +0100 Subject: [PATCH 04/13] Added: parent calls --- src/FormType/MediaLibraryType.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/FormType/MediaLibraryType.php b/src/FormType/MediaLibraryType.php index 4fc6b4a..71325c8 100644 --- a/src/FormType/MediaLibraryType.php +++ b/src/FormType/MediaLibraryType.php @@ -112,7 +112,9 @@ public function onPrepareFormData(PrepareFormDataEvent $event): void public function onStoreFormData(StoreFormDataEvent $event): void { - if ($event->getForm()->ml_archive && ($archiveModel = ProductArchiveModel::findByPk($event->getForm()->ml_archive))) { + if ($event->getForm()->ml_archive + && ($archiveModel = ProductArchiveModel::findByPk($event->getForm()->ml_archive))) + { $data = $event->getData(); $data = array_intersect_key($data, array_flip(Database::getInstance()->getFieldNames(ProductModel::getTable()))); $data['pid'] = $event->getForm()->ml_archive; @@ -142,6 +144,8 @@ public function onStoreFormData(StoreFormDataEvent $event): void $event->setData($data); } + + parent::onStoreFormData($event); } public function onProcessFormData(ProcessFormDataEvent $event): void From 09c61efffd24ee380030c175e78f35ab6f6a1a4a Mon Sep 17 00:00:00 2001 From: Eric Gesemann Date: Tue, 12 Dec 2023 18:00:28 +0100 Subject: [PATCH 05/13] Added: _method DELETE --- src/EventListener/DeleteProductListener.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/EventListener/DeleteProductListener.php b/src/EventListener/DeleteProductListener.php index 61d0d24..3a18b73 100644 --- a/src/EventListener/DeleteProductListener.php +++ b/src/EventListener/DeleteProductListener.php @@ -4,6 +4,7 @@ use Contao\CoreBundle\Exception\InternalServerErrorHttpException; use Contao\CoreBundle\Exception\RedirectResponseException; +use Contao\Input; use Contao\Model; use Contao\PageModel; use HeimrichHannot\MediaLibraryBundle\Model\ProductArchiveModel; @@ -11,13 +12,15 @@ use Symfony\Component\EventDispatcher\Attribute\AsEventListener; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; class DeleteProductListener { public function __construct( - private RequestStack $requestStack + private RequestStack $requestStack, + private Session $session ) { } @@ -33,7 +36,9 @@ public function deleteProduct(ReaderBeforeRenderEvent $event): void $request = $this->requestStack->getCurrentRequest(); - if (!$request->isMethod(Request::METHOD_DELETE)) { + if (!$request->isMethod(Request::METHOD_DELETE) + && Input::post('_method') !== 'DELETE') + { return; } @@ -66,6 +71,10 @@ public function deleteProduct(ReaderBeforeRenderEvent $event): void throw new InternalServerErrorHttpException('No redirect page found'); } + # todo: translation + # $flashes = $this->session->getFlashBag(); + # $flashes->add('success', 'Bild erfolgreich gelöscht!'); + throw new RedirectResponseException($page->getAbsoluteUrl()); } } \ No newline at end of file From 8d1a49c63cf3e7fb7db5c0ee64b8942545c116a5 Mon Sep 17 00:00:00 2001 From: Eric Gesemann Date: Wed, 13 Dec 2023 11:00:24 +0100 Subject: [PATCH 06/13] Removed: unnecessary Session include --- src/EventListener/DeleteProductListener.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/EventListener/DeleteProductListener.php b/src/EventListener/DeleteProductListener.php index 3a18b73..42a99bb 100644 --- a/src/EventListener/DeleteProductListener.php +++ b/src/EventListener/DeleteProductListener.php @@ -12,15 +12,13 @@ use Symfony\Component\EventDispatcher\Attribute\AsEventListener; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; -use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; class DeleteProductListener { public function __construct( - private RequestStack $requestStack, - private Session $session + private RequestStack $requestStack ) { } From e4e24616c2d86a5f787b803a5fc2a527ebb5aac1 Mon Sep 17 00:00:00 2001 From: Eric Gesemann Date: Wed, 13 Dec 2023 11:02:41 +0100 Subject: [PATCH 07/13] Added: checkIncludeDelete in ProductArchiveContainer --- src/DataContainer/ProductArchiveContainer.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/DataContainer/ProductArchiveContainer.php b/src/DataContainer/ProductArchiveContainer.php index 3af1bbc..c361d50 100644 --- a/src/DataContainer/ProductArchiveContainer.php +++ b/src/DataContainer/ProductArchiveContainer.php @@ -10,6 +10,8 @@ use Contao\BackendUser; use Contao\Controller; +use Contao\Database; +use Contao\DataContainer; use Contao\System; use HeimrichHannot\UtilsBundle\File\FileUtil; use HeimrichHannot\UtilsBundle\Model\ModelUtil; @@ -195,6 +197,25 @@ public function checkPermission() } } + public function checkIncludeDelete(DataContainer $dc) + { + $record = Database::getInstance() + ->prepare('SELECT * FROM tl_ml_product_archive WHERE id=?') + ->limit(1) + ->execute($dc->id) + ; + + if (!$record->numRows) { + return; + } + + if ($record->includeDelete ?? false) { + $GLOBALS['TL_DCA']['tl_ml_product_archive']['fields']['redirectAfterDelete']['eval']['mandatory'] = true; + } else { + unset($GLOBALS['TL_DCA']['tl_ml_product_archive']['fields']['redirectAfterDelete']); + } + } + public function editHeader($row, $href, $label, $title, $icon, $attributes) { return \Contao\BackendUser::getInstance()->canEditFieldsOf('tl_ml_product_archive') ? 'groups))); + + $objGroup = $database->execute($sql); while ($objGroup->next()) { - $arrModulep = \StringUtil::deserialize($objGroup->contao_media_library_bundlep); + $arrModulep = StringUtil::deserialize($objGroup->contao_media_library_bundlep); if (\is_array($arrModulep) && \in_array('create', $arrModulep, true)) { - $arrModules = \StringUtil::deserialize($objGroup->contao_media_library_bundles, + $arrModules = StringUtil::deserialize($objGroup->contao_media_library_bundles, true); - $arrModules[] = \Contao\Input::get('id'); + $arrModules[] = Input::get('id'); $database->prepare('UPDATE tl_user_group SET contao_media_library_bundles=? WHERE id=?')->execute( serialize($arrModules), @@ -138,21 +150,19 @@ public function checkPermission() ->limit(1) ->execute($user->id); - $arrModulep = \StringUtil::deserialize($user->contao_media_library_bundlep); + $arrModulep = StringUtil::deserialize($user->contao_media_library_bundlep); if (\is_array($arrModulep) && \in_array('create', $arrModulep, true)) { - $arrModules = \StringUtil::deserialize($user->contao_media_library_bundles, true); - $arrModules[] = \Contao\Input::get('id'); + $arrModules = StringUtil::deserialize($user->contao_media_library_bundles, true); + $arrModules[] = Input::get('id'); - $database->prepare('UPDATE tl_user SET contao_media_library_bundles=? WHERE id=?')->execute( - serialize($arrModules), - $user->id - ); + $database->prepare('UPDATE tl_user SET contao_media_library_bundles=? WHERE id=?') + ->execute(serialize($arrModules), $user->id); } } // Add the new element to the user object - $root[] = \Contao\Input::get('id'); + $root[] = Input::get('id'); $user->contao_media_library_bundles = $root; } } @@ -161,14 +171,14 @@ public function checkPermission() case 'copy': case 'delete': case 'show': - if (!\in_array(\Contao\Input::get('id'), $root, true) - || ('delete' == \Contao\Input::get('act') + if (!\in_array(Input::get('id'), $root, true) + || ('delete' == Input::get('act') && !$user->hasAccess( 'delete', 'contao_media_library_bundlep' )) ) { - throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to '.\Contao\Input::get('act').' ml_product_archive ID '.\Contao\Input::get('id').'.'); + throw new AccessDeniedException('Not enough permissions to '. Input::get('act').' ml_product_archive ID '. Input::get('id').'.'); } break; @@ -178,7 +188,7 @@ public function checkPermission() case 'overrideAll': $session = $objSession->all(); - if ('deleteAll' == \Contao\Input::get('act') && !$user->hasAccess('delete', + if ('deleteAll' == Input::get('act') && !$user->hasAccess('delete', 'contao_media_library_bundlep')) { $session['CURRENT']['IDS'] = []; } else { @@ -189,8 +199,8 @@ public function checkPermission() break; default: - if (\strlen(\Contao\Input::get('act'))) { - throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to '.\Contao\Input::get('act').' ml_product_archives.'); + if (\strlen(Input::get('act'))) { + throw new AccessDeniedException('Not enough permissions to '. Input::get('act').' ml_product_archives.'); } break; @@ -218,33 +228,42 @@ public function checkIncludeDelete(DataContainer $dc) public function editHeader($row, $href, $label, $title, $icon, $attributes) { - return \Contao\BackendUser::getInstance()->canEditFieldsOf('tl_ml_product_archive') ? ''.\Image::getHtml( - $icon, - $label - ).' ' : \Image::getHtml(preg_replace('/\.svg$/i', '_.svg', $icon)).' '; + if ($this->security->isGranted(ContaoCorePermissions::USER_CAN_EDIT_FIELDS_OF_TABLE, 'tl_ml_product_archive')) + { + $anchor = sprintf( + '%s ', + Controller::addToUrl("$href&id={$row['id']}"), + RequestToken::get(), + StringUtil::specialchars($title), + $attributes, + Image::getHtml($icon, $label) + ); + + return $anchor; + } + + return Image::getHtml(preg_replace('/\.svg$/i', '_.svg', $icon)) . ' '; } public function copyArchive($row, $href, $label, $title, $icon, $attributes) { - return \Contao\BackendUser::getInstance()->hasAccess('create', + return BackendUser::getInstance()->hasAccess('create', 'contao_media_library_bundlep') ? ''.\Image::getHtml( + ).'&rt='.RequestToken::get().'" title="'. StringUtil::specialchars($title).'"'.$attributes.'>'.Image::getHtml( $icon, $label - ).' ' : \Image::getHtml(preg_replace('/\.svg$/i', '_.svg', $icon)).' '; + ).' ' : Image::getHtml(preg_replace('/\.svg$/i', '_.svg', $icon)).' '; } public function deleteArchive($row, $href, $label, $title, $icon, $attributes) { - return \Contao\BackendUser::getInstance()->hasAccess('delete', + return BackendUser::getInstance()->hasAccess('delete', 'contao_media_library_bundlep') ? ''.\Image::getHtml( + ).'&rt='.RequestToken::get().'" title="'.StringUtil::specialchars($title).'"'.$attributes.'>'.Image::getHtml( $icon, $label - ).' ' : \Image::getHtml(preg_replace('/\.svg$/i', '_.svg', $icon)).' '; + ).' ' : Image::getHtml(preg_replace('/\.svg$/i', '_.svg', $icon)).' '; } } From ddc6da9b7e36a4d5fbc17db95a4fd1571be32a82 Mon Sep 17 00:00:00 2001 From: Eric Gesemann Date: Wed, 13 Dec 2023 11:04:39 +0100 Subject: [PATCH 09/13] Added: onload callback to tl_ml_product_archive dca --- .../contao/dca/tl_ml_product_archive.php | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/Resources/contao/dca/tl_ml_product_archive.php b/src/Resources/contao/dca/tl_ml_product_archive.php index 44ce80b..23d23fe 100644 --- a/src/Resources/contao/dca/tl_ml_product_archive.php +++ b/src/Resources/contao/dca/tl_ml_product_archive.php @@ -1,12 +1,16 @@ [ @@ -14,7 +18,8 @@ 'ctable' => ['tl_ml_product'], 'enableVersioning' => true, 'onload_callback' => [ - [\HeimrichHannot\MediaLibraryBundle\DataContainer\ProductArchiveContainer::class, 'checkPermission'], + [ProductArchiveContainer::class, 'checkPermission'], + [ProductArchiveContainer::class, 'checkIncludeDelete'], ], 'onsubmit_callback' => [ ['huh.utils.dca', 'setDateAdded'], @@ -57,13 +62,13 @@ 'label' => &$GLOBALS['TL_LANG']['tl_ml_product_archive']['editheader'], 'href' => 'act=edit', 'icon' => 'header.svg', - 'button_callback' => [\HeimrichHannot\MediaLibraryBundle\DataContainer\ProductArchiveContainer::class, 'editHeader'], + 'button_callback' => [ProductArchiveContainer::class, 'editHeader'], ], 'copy' => [ 'label' => &$GLOBALS['TL_LANG']['tl_ml_product_archive']['copy'], 'href' => 'act=copy', 'icon' => 'copy.svg', - 'button_callback' => [\HeimrichHannot\MediaLibraryBundle\DataContainer\ProductArchiveContainer::class, 'copyArchive'], + 'button_callback' => [ProductArchiveContainer::class, 'copyArchive'], ], 'delete' => [ 'label' => &$GLOBALS['TL_LANG']['tl_ml_product_archive']['delete'], @@ -71,7 +76,7 @@ 'icon' => 'delete.svg', 'attributes' => 'onclick="if(!confirm(\''.($GLOBALS['TL_LANG']['MSC']['deleteConfirm'] ?? '') .'\'))return false;Backend.getScrollOffset()"', - 'button_callback' => [\HeimrichHannot\MediaLibraryBundle\DataContainer\ProductArchiveContainer::class, 'deleteArchive'], + 'button_callback' => [ProductArchiveContainer::class, 'deleteArchive'], ], 'show' => [ 'label' => &$GLOBALS['TL_LANG']['tl_ml_product_archive']['show'], @@ -85,7 +90,7 @@ 'default' => '{general_legend},title;{config_legend},type,additionalFields,keepProductTitleForDownloadItems,includeDelete,redirectAfterDelete;{protected_legend},protected;', ], 'subpalettes' => [ - 'type_'.\HeimrichHannot\MediaLibraryBundle\DataContainer\ProductContainer::TYPE_IMAGE => 'imageSizes', + 'type_'. ProductContainer::TYPE_IMAGE => 'imageSizes', 'protected' => 'groups', ], 'fields' => [ @@ -119,7 +124,7 @@ 'exclude' => true, 'filter' => true, 'inputType' => 'select', - 'options' => \HeimrichHannot\MediaLibraryBundle\DataContainer\ProductContainer::TYPES, + 'options' => ProductContainer::TYPES, 'reference' => &$GLOBALS['TL_LANG']['tl_ml_product']['reference'], 'eval' => ['tl_class' => 'w50', 'mandatory' => true, 'includeBlankOption' => true, 'submitOnChange' => true], 'sql' => "varchar(64) NOT NULL default ''", @@ -130,7 +135,7 @@ 'filter' => true, 'inputType' => 'checkboxWizard', 'options_callback' => function (Contao\DataContainer $dc) { - return \Contao\System::getContainer()->get('huh.utils.choice.field')->getCachedChoices( + return System::getContainer()->get('huh.utils.choice.field')->getCachedChoices( [ 'dataContainer' => 'tl_ml_product', 'evalConditions' => [ @@ -148,7 +153,7 @@ 'exclude' => true, 'flag' => 1, 'inputType' => 'checkboxWizard', - 'options_callback' => [\HeimrichHannot\MediaLibraryBundle\DataContainer\ProductArchiveContainer::class, 'getImageSizes'], + 'options_callback' => [ProductArchiveContainer::class, 'getImageSizes'], 'eval' => ['includeBlankOption' => true, 'multiple' => true, 'tl_class' => 'clr w50 autoheight'], 'sql' => 'blob NULL', ], @@ -177,12 +182,14 @@ 'sql' => "char(1) NOT NULL default ''", ], 'includeDelete' => [ - 'label' => &$GLOBALS['TL_LANG']['tl_ml_product_archive']['includeDelete'], 'exclude' => true, 'filter' => true, 'inputType' => 'checkbox', 'default' => true, - 'eval' => ['tl_class' => 'clr'], + 'eval' => [ + 'tl_class' => 'clr', + 'submitOnChange' => true, + ], 'sql' => "char(1) NOT NULL default ''", ], 'redirectAfterDelete' => [ From 507ba121df8ad85befee6ac916014179bcc1d1f4 Mon Sep 17 00:00:00 2001 From: Eric Gesemann Date: Wed, 13 Dec 2023 13:32:36 +0100 Subject: [PATCH 10/13] Fixed: delete after check --- src/EventListener/DeleteProductListener.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/EventListener/DeleteProductListener.php b/src/EventListener/DeleteProductListener.php index 42a99bb..3633530 100644 --- a/src/EventListener/DeleteProductListener.php +++ b/src/EventListener/DeleteProductListener.php @@ -47,10 +47,6 @@ public function deleteProduct(ReaderBeforeRenderEvent $event): void throw new BadRequestHttpException('Invalid product id'); } - if (!$product->delete()) { - throw new InternalServerErrorHttpException('Could not delete product'); - } - /** @var ProductArchiveModel|null $productArchive */ $productArchive = $product->getRelated('pid'); @@ -62,6 +58,10 @@ public function deleteProduct(ReaderBeforeRenderEvent $event): void throw new MethodNotAllowedHttpException([Request::METHOD_DELETE], 'Delete not allowed'); } + if (!$product->delete()) { + throw new InternalServerErrorHttpException('Could not delete product'); + } + $pageId = $productArchive->redirectAfterDelete; $page = PageModel::findByPk($pageId); From 4f37680c193d2869248ea3557c0f50dbc5d1ed22 Mon Sep 17 00:00:00 2001 From: Eric Gesemann Date: Wed, 13 Dec 2023 13:32:53 +0100 Subject: [PATCH 11/13] Added: success message flash --- src/EventListener/DeleteProductListener.php | 22 +++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/EventListener/DeleteProductListener.php b/src/EventListener/DeleteProductListener.php index 3633530..734eb0e 100644 --- a/src/EventListener/DeleteProductListener.php +++ b/src/EventListener/DeleteProductListener.php @@ -12,15 +12,28 @@ use Symfony\Component\EventDispatcher\Attribute\AsEventListener; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; +use Symfony\Contracts\Translation\TranslatorInterface; class DeleteProductListener { + private RequestStack $requestStack; + + private TranslatorInterface $translator; + + private SessionInterface $session; + public function __construct( - private RequestStack $requestStack + RequestStack $requestStack, + TranslatorInterface $translator, + SessionInterface $session ) { + $this->requestStack = $requestStack; + $this->translator = $translator; + $this->session = $session; } #[AsEventListener('huh.reader.event.reader_before_render')] @@ -69,9 +82,10 @@ public function deleteProduct(ReaderBeforeRenderEvent $event): void throw new InternalServerErrorHttpException('No redirect page found'); } - # todo: translation - # $flashes = $this->session->getFlashBag(); - # $flashes->add('success', 'Bild erfolgreich gelöscht!'); + $this->session + ->getFlashBag() + ->add('success', $this->translator->trans('huh.mediaLibrary.product.delete.success', ['title' => $product->title])) + ; throw new RedirectResponseException($page->getAbsoluteUrl()); } From 2d03c4f1f0a4da16c016de39d5c8df89ca31bcf9 Mon Sep 17 00:00:00 2001 From: Eric Gesemann Date: Wed, 13 Dec 2023 13:32:57 +0100 Subject: [PATCH 12/13] Added: success message flash --- src/Resources/translations/messages.de.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Resources/translations/messages.de.yml b/src/Resources/translations/messages.de.yml index e057c09..c7d7cde 100644 --- a/src/Resources/translations/messages.de.yml +++ b/src/Resources/translations/messages.de.yml @@ -6,4 +6,6 @@ huh.mediaLibrary.btn.abort.label: 'abbrechen' huh.mediaLibrary.alert.download.title: 'Option wählen' huh.mediaLibrary.downloadTitle.original: 'Originalgröße' -huh.mediaLibrary.downloadTitle.sizeWithProductTitle: '{title} ({size})' \ No newline at end of file +huh.mediaLibrary.downloadTitle.sizeWithProductTitle: '{title} ({size})' + +huh.mediaLibrary.product.delete.success: 'Bild "{title}" wurde erfolgreich gelöscht' From 2fe64ff368c595f2e596ed93c48b0ee9d24cb0c0 Mon Sep 17 00:00:00 2001 From: Eric Gesemann Date: Wed, 13 Dec 2023 13:33:35 +0100 Subject: [PATCH 13/13] Added: typed properties --- src/DataContainer/ProductArchiveContainer.php | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/DataContainer/ProductArchiveContainer.php b/src/DataContainer/ProductArchiveContainer.php index 6a384ee..7e734b7 100644 --- a/src/DataContainer/ProductArchiveContainer.php +++ b/src/DataContainer/ProductArchiveContainer.php @@ -27,20 +27,11 @@ class ProductArchiveContainer { - /** - * @var FileUtil - */ - protected $fileUtil; + protected FileUtil $fileUtil; - /** - * @var ModelUtil - */ - protected $modelUtil; + protected ModelUtil $modelUtil; - /** - * @var Security - */ - protected $security; + protected Security $security; public function __construct( FileUtil $fileUtil,