-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from ThomasDaSilva/feat/multiple_tags
Add feature multiple tags
- Loading branch information
Showing
22 changed files
with
696 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
SET FOREIGN_KEY_CHECKS = 0; | ||
|
||
-- --------------------------------------------------------------------- | ||
-- page_tag | ||
-- --------------------------------------------------------------------- | ||
|
||
DROP TABLE IF EXISTS `page_tag`; | ||
|
||
CREATE TABLE `page_tag` | ||
( | ||
`id` INTEGER NOT NULL AUTO_INCREMENT, | ||
`tag` VARCHAR(255), | ||
`created_at` DATETIME, | ||
`updated_at` DATETIME, | ||
PRIMARY KEY (`id`), | ||
UNIQUE INDEX `page_tag_unique` (`tag`) | ||
) ENGINE=InnoDB; | ||
|
||
-- --------------------------------------------------------------------- | ||
-- page_tag_combination | ||
-- --------------------------------------------------------------------- | ||
|
||
DROP TABLE IF EXISTS `page_tag_combination`; | ||
|
||
CREATE TABLE `page_tag_combination` | ||
( | ||
`page_id` INTEGER NOT NULL, | ||
`page_tag_id` INTEGER NOT NULL, | ||
`created_at` DATETIME, | ||
`updated_at` DATETIME, | ||
PRIMARY KEY (`page_id`,`page_tag_id`), | ||
INDEX `fi_page_tag_combination_page_tag_id` (`page_tag_id`), | ||
CONSTRAINT `fk_page_tag_combination_page_id` | ||
FOREIGN KEY (`page_id`) | ||
REFERENCES `page` (`id`) | ||
ON UPDATE RESTRICT | ||
ON DELETE CASCADE, | ||
CONSTRAINT `fk_page_tag_combination_page_tag_id` | ||
FOREIGN KEY (`page_tag_id`) | ||
REFERENCES `page_tag` (`id`) | ||
ON UPDATE RESTRICT | ||
ON DELETE CASCADE | ||
) ENGINE=InnoDB; | ||
|
||
INSERT INTO `page_tag` (`tag`, `created_at`, `updated_at`) | ||
SELECT `tag`, `created_at`, `updated_at` | ||
FROM `page`; | ||
|
||
INSERT INTO `page_tag_combination` (`page_id`, `page_tag_id`, `created_at`, `updated_at`) | ||
SELECT p.id, pt.id, p.`created_at`, p.`updated_at` | ||
FROM `page` p | ||
JOIN `page_tag` pt ON p.`tag` = pt.`tag`; | ||
|
||
ALTER TABLE `page` DROP COLUMN `tag`; | ||
|
||
SET FOREIGN_KEY_CHECKS = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace Page\Controller\Admin; | ||
|
||
use Exception; | ||
use Page\Form\PageTagForm; | ||
use Page\Model\PageTag; | ||
use Page\Model\PageTagQuery; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
use Thelia\Controller\Admin\BaseAdminController; | ||
use Thelia\Core\HttpFoundation\Request; | ||
use Thelia\Core\HttpFoundation\Response; | ||
use Thelia\Core\Template\ParserContext; | ||
use Thelia\Form\Exception\FormValidationException; | ||
|
||
#[Route('/admin/page-tag', name:'page_tag')] | ||
class PageTagController extends BaseAdminController | ||
{ | ||
#[Route('', name:'_list', methods: 'GET')] | ||
public function listPageTagAction(): Response|RedirectResponse | ||
{ | ||
return $this->render('page-tags-list'); | ||
} | ||
|
||
#[Route('/new', name:'_new_tag', methods: 'GET')] | ||
public function newPageTagViewAction(Request $request): Response|RedirectResponse | ||
{ | ||
return $this->render('new-tag'); | ||
} | ||
|
||
#[Route('/create', name:'_create_tag_action', methods: 'POST')] | ||
public function createPageTagAction(ParserContext $parserContext): Response|RedirectResponse | ||
{ | ||
$form = $this->createForm(PageTagForm::class); | ||
|
||
try { | ||
$formData = $this->validateForm($form)->getData(); | ||
|
||
$pageTag = new PageTag(); | ||
$pageTag->setTag($formData['tag']); | ||
$pageTag->save(); | ||
|
||
return $this->generateSuccessRedirect($form); | ||
} catch (FormValidationException $e) { | ||
$error_message = $this->createStandardFormValidationErrorMessage($e); | ||
} catch (Exception $e) { | ||
$error_message = $e->getMessage(); | ||
} | ||
|
||
$form->setErrorMessage($error_message); | ||
$parserContext | ||
->addForm($form) | ||
->setGeneralError($error_message); | ||
|
||
return $this->generateErrorRedirect($form); | ||
} | ||
|
||
#[Route('/edit/{tagId}', name:'_edit_page', methods: 'GET')] | ||
public function editPageTagViewAction($tagId): Response|RedirectResponse | ||
{ | ||
if (null === $tag = PageTagQuery::create()->findOneById($tagId)) { | ||
return $this->generateRedirect('/admin/page-tags?error=' . "no tags found"); | ||
} | ||
|
||
return $this->render('edit-tag', [ | ||
"page_tag_id" => $tag->getId(), | ||
"page_tag" => $tag->getTag() | ||
] | ||
); | ||
} | ||
|
||
#[Route('/update/{tagId}', name:'_update_page', methods: 'POST')] | ||
public function updatePageTagViewAction(ParserContext $parserContext, $tagId): Response|RedirectResponse | ||
{ | ||
$form = $this->createForm(PageTagForm::class); | ||
|
||
try { | ||
$formData = $this->validateForm($form)->getData(); | ||
|
||
if (null !== $tag = PageTagQuery::create()->findOneById($tagId)) { | ||
$tag->setTag($formData['tag']); | ||
$tag->save(); | ||
} | ||
|
||
return $this->generateSuccessRedirect($form); | ||
} catch (FormValidationException $e) { | ||
$error_message = $this->createStandardFormValidationErrorMessage($e); | ||
} catch (Exception $e) { | ||
$error_message = $e->getMessage(); | ||
} | ||
|
||
$form->setErrorMessage($error_message); | ||
$parserContext | ||
->addForm($form) | ||
->setGeneralError($error_message); | ||
|
||
return $this->generateErrorRedirect($form); | ||
} | ||
|
||
#[Route('/delete/{tagId}', name:'_delete_page')] | ||
public function deletePageTagAction($tagId): Response|RedirectResponse | ||
{ | ||
try { | ||
$tag = PageTagQuery::create()->findOneById($tagId); | ||
|
||
if (!$tag) { | ||
throw new Exception("Tag not found"); | ||
} | ||
|
||
$tag->delete(); | ||
|
||
} catch (Exception $e) { | ||
$error_message = $e->getMessage(); | ||
return $this->generateRedirect('/admin/page-tag?error=' . $error_message); | ||
} | ||
|
||
return $this->generateRedirect('/admin/page-tag'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Page\Form; | ||
|
||
use OpenApi\Constraint\NotBlank; | ||
use Page\Page; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Thelia\Form\BaseForm; | ||
|
||
class PageTagForm extends BaseForm | ||
{ | ||
protected function buildForm(): void | ||
{ | ||
$this->formBuilder | ||
->add( | ||
'tag', | ||
TextType::class, | ||
[ | ||
'label' => $this->translator->trans('Tag', [], Page::DOMAIN_NAME), | ||
'constraints' => [ | ||
new NotBlank(), | ||
] | ||
] | ||
) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.