From b76547767ae897b6de752c56e2cef6079ca4abcb Mon Sep 17 00:00:00 2001 From: polo Date: Thu, 11 Jun 2026 01:35:57 +0200 Subject: =?UTF-8?q?classe=20EmailForm=20=C3=A0=20Email=20et=20d=C3=A9tacha?= =?UTF-8?q?ble=20de=20Node?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/ContactFormController.php | 27 ++++++-------- src/controller/PageManagementController.php | 17 +++++++-- src/controller/UserController.php | 6 +-- src/model/entities/Email.php | 14 +++---- src/model/entities/EmailForm.php | 58 +++++++++++++++++++++++++++++ src/model/entities/Node.php | 10 +++-- src/model/entities/NodeData.php | 4 +- src/service/EmailService.php | 4 +- src/view/ShowEmailsBuilder.php | 2 +- 9 files changed, 102 insertions(+), 40 deletions(-) create mode 100644 src/model/entities/EmailForm.php (limited to 'src') diff --git a/src/controller/ContactFormController.php b/src/controller/ContactFormController.php index 6b89161..cbc1837 100644 --- a/src/controller/ContactFormController.php +++ b/src/controller/ContactFormController.php @@ -4,13 +4,15 @@ declare(strict_types=1); use Doctrine\ORM\EntityManager; +use App\Entity\EmailForm; +use App\Entity\Email; use Symfony\Component\HttpFoundation\JsonResponse; class ContactFormController { static public function keepEmails(EntityManager $entityManager, array $json): JsonResponse { - $form_data = $entityManager->find('App\Entity\NodeData', $json['id']); + $form_data = $entityManager->find(EmailForm::class, $json['id']); $form_data->updateData('keep_emails', $json['checked'] ? true : false); $entityManager->persist($form_data); $entityManager->flush(); @@ -18,7 +20,7 @@ class ContactFormController } static public function setEmailsRetentionPeriod(EntityManager $entityManager, array $json): JsonResponse { - $form_data = $entityManager->find('App\Entity\NodeData', $json['id']); + $form_data = $entityManager->find(EmailForm::class, $json['id']); $form_data->updateData($json['field'], (int)$json['months']); $entityManager->persist($form_data); $entityManager->flush(); @@ -28,22 +30,15 @@ class ContactFormController { $form = new FormValidation($json, 'email_params'); - $error = ''; if($form->validate()){ - $form_data = $entityManager->find('App\Entity\NodeData', $json['id']); + $form_data = $entityManager->find(EmailForm::class, $json['id']); $form_data->updateData($json['what_param'], trim($json['value'])); - $entityManager->persist($form_data); + $entityManager->persist($form_data); // ?? $entityManager->flush(); - } - else{ - $error = $form->getErrors()[0]; // la 1ère erreur sera affichée - } - - if(empty($error)){ return new JsonResponse(['success' => true]); } else{ - return new JsonResponse(['success' => false, 'error' => $error]); + return new JsonResponse(['success' => false, 'error' => $form->getErrors()[0]]); // la 1ère erreur sera affichée } } @@ -55,7 +50,7 @@ class ContactFormController $error = ''; if($form->validate()){ // destinataire = e-mail par défaut dans config.ini OU choisi par l'utilisateur - $form_data = $entityManager->find('App\Entity\NodeData', $json['id']); + $form_data = $entityManager->find(EmailForm::class, $json['id']); if($form_data === null){ return new JsonResponse(['success' => false, 'error' => 'server_error'], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); // code 500 } @@ -78,7 +73,7 @@ class ContactFormController static public function sendTestEmail(EntityManager $entityManager, array $json): JsonResponse { // destinataire = e-mail par défaut dans config.ini OU choisi par l'utilisateur - $form_data = $entityManager->find('App\Entity\NodeData', $json['id']); + $form_data = $entityManager->find(EmailForm::class, $json['id']); if($form_data === null){ return new JsonResponse(['success' => false, 'error' => 'server_error'], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); } @@ -92,14 +87,14 @@ class ContactFormController } static public function deleteEmail(EntityManager $entityManager, array $json): JsonResponse { - $email = $entityManager->find('App\Entity\Email', $json['id']); + $email = $entityManager->find(Email::class, $json['id']); $entityManager->remove($email); $entityManager->flush(); return new JsonResponse(['success' => true]); } static public function toggleSensitiveEmail(EntityManager $entityManager, array $json): JsonResponse { - $email = $entityManager->find('App\Entity\Email', $json['id']); + $email = $entityManager->find(Email::class, $json['id']); $email->makeSensitive($json['checked']); $entityManager->flush(); return new JsonResponse(['success' => true, 'checked' => $json['checked'], 'deletion_date' => $email->getDeletionDate()->format('d/m/Y')]); diff --git a/src/controller/PageManagementController.php b/src/controller/PageManagementController.php index a43f36c..c575077 100644 --- a/src/controller/PageManagementController.php +++ b/src/controller/PageManagementController.php @@ -6,6 +6,7 @@ declare(strict_types=1); use App\Entity\Page; use App\Entity\Node; use App\Entity\NodeData; +use App\Entity\EmailForm; //use App\Entity\Image; use Doctrine\ORM\EntityManager; use Symfony\Component\HttpFoundation\InputBag; @@ -144,7 +145,9 @@ class PageManagementController } $block = new Node($request->request->get("bloc_select"), $position, $main, $page); - $data = new NodeData(['title' => trim(htmlspecialchars($request->request->get("bloc_title")))], $block); + + $DataClass = $request->request->get("bloc_select") === 'form' ? EmailForm::class : NodeData::class; // cas particulier avec bloc 'email_form' + $data = new $DataClass(['title' => trim(htmlspecialchars($request->request->get("bloc_title")))], $block); // valeurs par défaut if($request->request->get("bloc_select") === 'post_block'){ @@ -200,8 +203,15 @@ class PageManagementController if(isset($page)){ $entityManager->persist($page); } + $block->getNodeData()->setNode(null); $entityManager->remove($block); - $entityManager->flush(); + try{ + $entityManager->flush(); + } + catch(Exception $e){ + // utiliser une flash error + return new RedirectResponse((string)new URL(['page' => $request->query->get('page'), 'mode' => 'page_modif', 'error' => $e->getMessage()])); + } } return new RedirectResponse((string)new URL(['page' => $request->query->get('page'), 'mode' => 'page_modif'])); @@ -277,8 +287,7 @@ class PageManagementController $chrono_order = false; } else{ - echo json_encode(['success' => false]); - die; + return new JsonResponse(['success' => false]); } $model->getNode()->getNodeData()->setChronoOrder($chrono_order); $entityManager->flush(); diff --git a/src/controller/UserController.php b/src/controller/UserController.php index ddba33a..03686ee 100644 --- a/src/controller/UserController.php +++ b/src/controller/UserController.php @@ -122,7 +122,7 @@ class UserController $url = new URL; isset($_GET['from']) ? $url->addParams(['page' => $_GET['from']]) : ''; isset($_GET['id']) ? $url->addParams(['id' => $_GET['id']]) : ''; - return new RedirectResponse('Location: ' . $url); + return new RedirectResponse((string)$url); } // user @@ -161,7 +161,7 @@ class UserController sleep(1); $url->addParams(['error_username' => $error]); } - return new RedirectResponse('Location: ' . $url); + return new RedirectResponse((string)$url); } // user @@ -200,7 +200,7 @@ class UserController sleep(1); $url->addParams(['error_password' => $error]); } - return new RedirectResponse('Location: ' . $url); + return new RedirectResponse((string)$url); } // dans une classe mère ou un trait après découpage de UserController? diff --git a/src/model/entities/Email.php b/src/model/entities/Email.php index ff76653..60fcc1f 100644 --- a/src/model/entities/Email.php +++ b/src/model/entities/Email.php @@ -48,18 +48,18 @@ class Email #[ORM\Column(type: 'datetime', nullable: true)] private ?\DateTime $is_sensitive_since; - #[ORM\ManyToOne(targetEntity: NodeData::class)] - #[ORM\JoinColumn(name: "node_data_id", referencedColumnName: "id_node_data", nullable: true)] - private ?NodeData $node_data; + #[ORM\ManyToOne(targetEntity: EmailForm::class)] + #[ORM\JoinColumn(name: "email_form_id", referencedColumnName: "id_email_form", nullable: true)] + private ?EmailForm $email_form; - public function __construct(string $sender_name, string $sender_address, string $recipient, string $content, NodeData $node_data, bool $sensitive = false){ + public function __construct(string $sender_name, string $sender_address, string $recipient, string $content, EmailForm $email_form, bool $sensitive = false){ $this->sender_name = strtolower($sender_name); $this->sender_address = strtolower($sender_address); $this->recipient = strtolower($recipient); $this->content = $content; $this->date_time = new \DateTime; $this->last_contact_date = new \DateTime; - $this->node_data = $node_data; + $this->email_form = $email_form; $this->makeSensitive($sensitive); } @@ -113,14 +113,14 @@ class Email $this->last_contact_date = new \DateTime; } - public function getDeletionDate(): \DateTime // utilise une durée de conservation $period qui est propre au bloc formulaire (à son NodeData) + public function getDeletionDate(): \DateTime // utilise une durée de conservation $period qui est propre au bloc formulaire (à son EmailForm) { // tests appliqués: // => e-mail associé à un formulaire? // => ce formulaire dispose d'une durée de stockage spécifique? // => cette donnée est un entier > 0 $key = $this->is_sensitive ? 'retention_period_sensible' : 'retention_period'; - $period = $this->node_data ? (int)($this->node_data->getData()[$key] ?? null) : null; + $period = $this->email_form ? (int)($this->email_form->getData()[$key] ?? null) : null; $default = $this->is_sensitive ? self::DEFAULT_RETENTION_PERIOD_SENSITIVE : self::DEFAULT_RETENTION_PERIOD; $period = ($period === null || $period <= 0) ? $default : $period; diff --git a/src/model/entities/EmailForm.php b/src/model/entities/EmailForm.php new file mode 100644 index 0000000..466a389 --- /dev/null +++ b/src/model/entities/EmailForm.php @@ -0,0 +1,58 @@ +data = $data; + $this->node = $node; + } + + public function getId(): int + { + return $this->id_email_form; + } + + // getData et updateData sont indentiques au code dans NodeData + // plutôt qu'une interface, pourquoi pas une classe abstraite? ou peut-être un trait? + public function getData(): array + { + return $this->data; + } + public function updateData(string $key, string|int|bool|array $value = ''): void + { + if($value !== ''){ + $this->data[$key] = $value; + } + // si $value est vide, supprime la clé + elseif(isset($this->data[$key])){ + unset($this->data[$key]); + } + } + + public function setNode(?Node $node): void + { + $this->node = $node; + } +} \ No newline at end of file diff --git a/src/model/entities/Node.php b/src/model/entities/Node.php index 71c159d..c3e4ec3 100644 --- a/src/model/entities/Node.php +++ b/src/model/entities/Node.php @@ -45,13 +45,14 @@ class Node #[ORM\OneToOne(targetEntity: NodeData::class, mappedBy: "node", cascade: ['persist', 'remove'])] private ?NodeData $node_data = null; + #[ORM\OneToOne(targetEntity: EmailForm::class, mappedBy: "node", cascade: ['persist'])] // pas de remove, les e-mails sont associés au EmailForm + private ?EmailForm $email_form = null; // attributs non destinés à doctrine private array $children = []; // tableau de Node private ?self $adopted = null; // = "new" est un enfant de "main" lorsque la page est "article" - public function __construct(string $name = '', int $position = 0, ?self $parent = null, ?Page $page = null, ?Article $article = null) - { + public function __construct(string $name = '', int $position = 0, ?self $parent = null, ?Page $page = null, ?Article $article = null){ $this->name_node = $name; $this->position = $position; $this->parent = $parent; @@ -105,9 +106,10 @@ class Node { $this->article = $article; }*/ - public function getNodeData(): ?NodeData + // une interface serait cool! + public function getNodeData(): NodeData|EmailForm|null { - return $this->node_data; + return $this->name_node === 'form' ? $this->email_form : $this->node_data; } public function getChildren(): array { diff --git a/src/model/entities/NodeData.php b/src/model/entities/NodeData.php index 49bfb3c..c55f6a1 100644 --- a/src/model/entities/NodeData.php +++ b/src/model/entities/NodeData.php @@ -44,9 +44,6 @@ class NodeData #[ORM\OneToMany(mappedBy: 'node_data', targetEntity: AssetEmployment::class, cascade: ['persist', 'remove'])] private Collection $asset_employment; - /*#[ORM\OneToMany(mappedBy: 'node_data', targetEntity: Email::class, cascade: ['persist', 'remove'])] // => noeud "form", inutilisé et conflit avec le tableau $emails - private Collection $emails;*/ - private int $nb_pages = 1; private array $emails = []; // => noeud "show_emails" @@ -157,6 +154,7 @@ class NodeData return $nda->getAsset() ?? null; } + // pour affichage page Courriels public function getEmails(): array // appelée dans ShowEmailsBuilder { return $this->emails; diff --git a/src/service/EmailService.php b/src/service/EmailService.php index 0ea8f39..1579bdb 100644 --- a/src/service/EmailService.php +++ b/src/service/EmailService.php @@ -7,13 +7,13 @@ use PHPMailer\PHPMailer\PHPMailer; //use PHPMailer\PHPMailer\Exception; use Doctrine\ORM\EntityManager; use App\Entity\Email; -use App\Entity\NodeData; +use App\Entity\EmailForm; class EmailService { const KEEP_EMAILS_DEFAULT = false; - static public function send(EntityManager $entityManager, NodeData $form_data, bool $test_email, string $name = '', string $email = '', string $message = ''): bool + static public function send(EntityManager $entityManager, EmailForm $form_data, bool $test_email, string $name = '', string $email = '', string $message = ''): bool { $mail = new PHPMailer(true); // true => exceptions $mail->CharSet = 'UTF-8'; diff --git a/src/view/ShowEmailsBuilder.php b/src/view/ShowEmailsBuilder.php index 3d2d6a9..e88b349 100644 --- a/src/view/ShowEmailsBuilder.php +++ b/src/view/ShowEmailsBuilder.php @@ -8,7 +8,7 @@ use App\Entity\Page; class ShowEmailsBuilder extends AbstractBuilder { - public function __construct(Node $node = null) + public function __construct(?Node $node) { //parent::__construct($node); $viewFile = self::VIEWS_PATH . $node->getName() . '.php'; -- cgit v1.2.3