Skip to content

Commit

Permalink
Merge pull request #20 from ThomasDaSilva/feat/multiple_tags
Browse files Browse the repository at this point in the history
Add feature multiple tags
  • Loading branch information
zawaze authored Mar 14, 2024
2 parents c92ec07 + 7bc8072 commit a70656e
Show file tree
Hide file tree
Showing 22 changed files with 696 additions and 13 deletions.
43 changes: 42 additions & 1 deletion Config/TheliaMain.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ CREATE TABLE `page`
`visible` TINYINT DEFAULT 0 NOT NULL,
`code` VARCHAR(255),
`type_id` INTEGER,
`tag` VARCHAR(255),
`is_home` TINYINT(1) DEFAULT 0,
`created_at` DATETIME,
`updated_at` DATETIME,
Expand Down Expand Up @@ -67,6 +66,48 @@ CREATE TABLE `page_document`
ON DELETE CASCADE
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- 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;

-- ---------------------------------------------------------------------
-- page_i18n
-- ---------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.2.2</version>
<version>1.2.3</version>
<authors>
<author>
<name>Damien Foulhoux</name>
Expand Down
24 changes: 23 additions & 1 deletion Config/schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<column name="meta_keywords" type="LONGVARCHAR"/>

<column name="type_id" type="INTEGER"/>
<column name="tag" size="255" type="VARCHAR"/>
<column name="is_home" defaultValue="false" type="BOOLEAN"/>

<foreign-key foreignTable="page_type" name="fk_page_type_page" onDelete="SET NULL">
Expand Down Expand Up @@ -72,5 +71,28 @@
<behavior name="timestampable"/>
</table>

<table name="page_tag" namespace="Page\Model">
<column autoIncrement="true" name="id" primaryKey="true" required="true" type="INTEGER"/>
<column name="tag" size="255" type="VARCHAR"/>

<behavior name="timestampable"/>
<unique name="page_tag_unique">
<unique-column name="tag"/>
</unique>
</table>

<table name="page_tag_combination" namespace="Page\Model">
<column name="page_id" primaryKey="true" required="true" type="INTEGER"/>
<column name="page_tag_id" primaryKey="true" required="true" type="INTEGER"/>

<foreign-key foreignTable="page" name="fk_page_tag_combination_page_id" onDelete="CASCADE" onUpdate="RESTRICT">
<reference foreign="id" local="page_id"/>
</foreign-key>
<foreign-key foreignTable="page_tag" name="fk_page_tag_combination_page_tag_id" onDelete="CASCADE" onUpdate="RESTRICT">
<reference foreign="id" local="page_tag_id"/>
</foreign-key>
<behavior name="timestampable"/>
</table>

<external-schema filename="local/modules/TheliaBlocks/Config/schema.xml" referenceOnly="true"/>
</database>
56 changes: 56 additions & 0 deletions Config/update/1.2.3.sql
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;
11 changes: 9 additions & 2 deletions Controller/Admin/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Page\Form\EditPageForm;
use Page\Form\EditPageSeoForm;
use Page\Model\Page;
use Page\Model\PageTagCombinationQuery;
use Page\Service\PageProvider;
use Page\Service\PageService;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand Down Expand Up @@ -135,12 +136,18 @@ function (Page $page) use ($locale) {
return $this->generateRedirect('/admin/page?error=' . $e->getMessage());
}

$tags = [];
$pageTagCombination= PageTagCombinationQuery::create()->findByPageId($pageId);
foreach ($pageTagCombination as $pageTag) {
$tags[] = $pageTag->getPageTag()->getTag();
}

return $this->render('edit-page', [
"page_id" => $pageId,
"page_url" => $page->getRewrittenUrl($locale),
"page_title" => $page->getTitle(),
"page_code" => $page->getCode(),
"page_tag" => $page->getTag(),
"page_tag" => implode(", ", $tags),
"page_tree_left" => $page->getTreeLeft(),
"page_tree_right" => $page->getTreeRight(),
"page_tree_level" => $page->getTreeLevel(),
Expand Down Expand Up @@ -177,7 +184,7 @@ public function updatePageAction(Request $request, Session $session, PageProvide
$pageId,
$formData['title'],
$formData['code'],
$formData['tag'],
explode(',', $formData['tag']),
$formData['type'] ?: null,
$formData['description'],
$formData['chapo'],
Expand Down
120 changes: 120 additions & 0 deletions Controller/Admin/PageTagController.php
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');
}
}
27 changes: 27 additions & 0 deletions Form/PageTagForm.php
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(),
]
]
)
;
}
}
6 changes: 5 additions & 1 deletion I18n/backOffice/default/fr_FR.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
return array(
'Actions' => 'Actions',
'Add a new page' => 'Ajouter une page',
'Add a new tag' => 'Ajouter une nouvelle étiquette',
'Block' => 'Bloc',
'Browse files' => 'Parcourir...',
'Can\'t load documents, please refresh this page.' => 'Les documents n\'ont pas pu être chargés, merci de raffraîcir cette page?',
'Configuration' => 'Configuration',
'Create' => 'Créer une nouvelle page',
'Create a new page' => 'Créer une nouvelle page',
'Create a new tag' => 'Créer une nouvelle étiquette',
'Delete this page' => 'Supprimer cette page',
'Documents' => 'Documents',
'Drop files to upload' => 'Déposer les fichiers ici',
Expand All @@ -17,6 +19,7 @@
'Home' => 'Accueil',
'Id' => 'ID',
'List of page types' => 'Liste de types de page',
'Manage page tags' => 'Gerer les étiquette',
'Manage page type' => 'Gérer les types de page',
'Modifying "%title"' => 'Modification de "%title"',
'Module' => 'Module',
Expand All @@ -30,8 +33,9 @@
'Position' => 'Position',
'SEO' => 'SEO',
'Send files' => 'Envoyer les fichiers',
'Slug' => 'Slug',
'Tag' => 'Étiquette',
'Tags' => 'Étiquette',
'Tags List' => 'Liste des Étiquette',
'Thelia block' => 'Thelia Block',
'There is no page document attached to this page.' => 'Aucun document n\'est attaché à cette page.',
'Title' => 'Titre',
Expand Down
1 change: 1 addition & 0 deletions I18n/fr_FR.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'Page type' => 'Type de page',
'Rewriting URL' => 'URL ré-écrite',
'Summary' => 'Résumé',
'Tag' => 'Étiquette',
'Thelia block' => 'Thelia block',
'This page tag' => 'L\'étiquette de cette page',
'Type' => 'Type',
Expand Down
Loading

0 comments on commit a70656e

Please sign in to comment.