diff options
Diffstat (limited to 'src/View/NewBuilder.php')
| -rw-r--r-- | src/View/NewBuilder.php | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/src/View/NewBuilder.php b/src/View/NewBuilder.php new file mode 100644 index 0000000..598bb1c --- /dev/null +++ b/src/View/NewBuilder.php | |||
| @@ -0,0 +1,157 @@ | |||
| 1 | <?php | ||
| 2 | // src/View/NewBuilder.php | ||
| 3 | // | ||
| 4 | // new = nouvelle (du journal) | ||
| 5 | |||
| 6 | declare(strict_types=1); | ||
| 7 | |||
| 8 | namespace App\View; | ||
| 9 | |||
| 10 | use App\Entity\Node; | ||
| 11 | use App\Service\URL; | ||
| 12 | |||
| 13 | class NewBuilder extends AbstractBuilder | ||
| 14 | { | ||
| 15 | static public bool $new_article_mode = false; | ||
| 16 | |||
| 17 | public function __construct(Node $node) | ||
| 18 | { | ||
| 19 | $viewFile = self::VIEWS_PATH . $node->getName() . '.php'; | ||
| 20 | |||
| 21 | if(file_exists($viewFile)) | ||
| 22 | { | ||
| 23 | if(self::$new_article_mode){ | ||
| 24 | $id = $_GET['id']; // ici l'id est le nom du block news_block parent | ||
| 25 | $title = ''; | ||
| 26 | $preview = ''; | ||
| 27 | |||
| 28 | // lettre au début de l'id: t = title, p = preview, i = article, d = date | ||
| 29 | $id_title = $id; | ||
| 30 | $id_title[0] = 't'; | ||
| 31 | $id_preview = $id; | ||
| 32 | $id_preview[0] = 'p'; | ||
| 33 | $id_content = 'i' . $id; | ||
| 34 | $id_content[0] = 'i'; | ||
| 35 | $id_date = $id; | ||
| 36 | $id_date[0] = 'd'; | ||
| 37 | } | ||
| 38 | else{ | ||
| 39 | $id = (string)$node->getId(); | ||
| 40 | |||
| 41 | // html, date | ||
| 42 | $title = $node->getArticle()->getTitle(); | ||
| 43 | $preview = $node->getArticle()->getPreview(); | ||
| 44 | |||
| 45 | // lettre au début de l'id: t = title, p = preview, i = article, d = date | ||
| 46 | $id_title = 't' . $id; | ||
| 47 | $id_preview = 'p' . $id; | ||
| 48 | $id_content = 'i' . $id; | ||
| 49 | $id_date = 'd' . $id; | ||
| 50 | } | ||
| 51 | |||
| 52 | $content = ''; | ||
| 53 | if(CURRENT_PAGE === 'article'){ | ||
| 54 | $content = $node->getArticle()->getContent(); | ||
| 55 | $from_to_button = '<p><a class="link_to_article" href="' . new URL(isset($_GET['from']) ? ['page' => $_GET['from']] : []) . '"><button>Retour</button></a></p>'; | ||
| 56 | $overflow = ''; | ||
| 57 | } | ||
| 58 | else{ | ||
| 59 | $from_to_button = '<p><a class="link_to_article" href="' . new URL(['page' => 'article', 'id' => $id, 'from' => CURRENT_PAGE]) . '"><button><img class="action_icon" src="assets/book-open.svg">Lire la suite</button></a></p>'; | ||
| 60 | $overflow = ' overflow_hidden'; | ||
| 61 | } | ||
| 62 | |||
| 63 | $date = $node->getArticle()->getDateTime()->format('Y-m-d\TH:i:s.v\Z'); // format: 2025-07-17T13:54:00.000Z | ||
| 64 | // format(\DateTime::ATOM) produit le format: 2025-10-10T12:17:00+00:00, c'est aussi de la norme ISO, mais à éviter pour être compatible avec date.toISOString en JS | ||
| 65 | |||
| 66 | // partage | ||
| 67 | $share_link = new URL(['page' => 'article', 'id' => $id]); | ||
| 68 | $share_js = 'onclick="copyInClipBoard(\'' . $share_link . '\')"'; | ||
| 69 | if(isset($_GET['id']) && $_GET['id'][0] === 'n'){ | ||
| 70 | $class = 'class="share hidden"'; | ||
| 71 | } | ||
| 72 | else{ | ||
| 73 | $class = 'class="share"'; | ||
| 74 | } | ||
| 75 | $share_button = '<p ' . $class . ' ' . $share_js . '><img class="action_icon" src="assets/share.svg"></p>' . "\n"; | ||
| 76 | |||
| 77 | // modifier un article | ||
| 78 | $title_buttons = ''; | ||
| 79 | $preview_buttons = ''; | ||
| 80 | $article_buttons = ''; | ||
| 81 | $date_buttons = ''; | ||
| 82 | $admin_buttons = ''; | ||
| 83 | if(IS_ADMIN){ | ||
| 84 | if(CURRENT_PAGE === 'article'){ | ||
| 85 | $title_js = 'onclick="openEditor(\'' . $id_title . '\')"'; | ||
| 86 | $modify_title = '<p id="edit-' . $id_title . '"><button ' . $title_js . '><img class="action_icon" src="assets/edit.svg">Titre</button></p>' . "\n"; | ||
| 87 | $close_js_title = 'onclick="closeEditor(\'' . $id_title . '\')"'; | ||
| 88 | $close_editor_title = '<p id="cancel-' . $id_title . '" class="hidden"><button ' . $close_js_title . '>Annuler</button></p>'; | ||
| 89 | $submit_js_title = 'onclick="submitArticle(\'' . $id_title . '\')"'; | ||
| 90 | $submit_title = '<p id="submit-' . $id_title . '" class="hidden"><button ' . $submit_js_title . '>Valider</button></p>'; | ||
| 91 | $title_buttons = '<div class="button_zone">' . $modify_title . $close_editor_title . $submit_title . '</div>'; | ||
| 92 | |||
| 93 | $preview_js = 'onclick="openEditor(\'' . $id_preview . '\')"'; | ||
| 94 | $modify_preview = '<p id="edit-' . $id_preview . '"><button ' . $preview_js . '><img class="action_icon" src="assets/edit.svg">Aperçu</button></a></p>' . "\n"; | ||
| 95 | $close_js_preview = 'onclick="closeEditor(\'' . $id_preview . '\')"'; | ||
| 96 | $close_editor_preview = '<p id="cancel-' . $id_preview . '" class="hidden"><button ' . $close_js_preview . '>Annuler</button></p>'; | ||
| 97 | $submit_js_preview = 'onclick="submitArticle(\'' . $id_preview . '\')"'; | ||
| 98 | $submit_preview = '<p id="submit-' . $id_preview . '" class="hidden"><button ' . $submit_js_preview . '>Valider</button></p>'; | ||
| 99 | $preview_buttons = '<div class="button_zone">' . $modify_preview . $close_editor_preview . $submit_preview . '</div>'; | ||
| 100 | |||
| 101 | $article_js = 'onclick="openEditor(\'' . $id_content . '\')"'; | ||
| 102 | $modify_article = '<p id="edit-' . $id_content . '"><button ' . $article_js . '><img class="action_icon" src="assets/edit.svg">Article</button></p>' . "\n"; | ||
| 103 | $close_js_article = 'onclick="closeEditor(\'' . $id_content . '\')"'; | ||
| 104 | $close_editor_article = '<p id="cancel-' . $id_content . '" class="hidden"><button ' . $close_js_article . '>Annuler</button></p>'; | ||
| 105 | $submit_js_article = 'onclick="submitArticle(\'' . $id_content . '\')"'; | ||
| 106 | $submit_article = '<p id="submit-' . $id_content . '" class="hidden"><button ' . $submit_js_article . '>Valider</button></p>'; | ||
| 107 | $article_buttons = '<div class="button_zone">' . $modify_article . $close_editor_article . $submit_article . '</div>'; | ||
| 108 | |||
| 109 | $date_js = 'onclick="openDatetimeLocalInput(\'' . $id_date . '\', \'article\');'; | ||
| 110 | $modify_date = '<p id="edit-' . $id_date . '"><button ' . $date_js . '"><img class="action_icon" src="assets/edit.svg">Date</button></p>' . "\n"; | ||
| 111 | $close_js_date = 'onclick="closeInput(\'' . $id_date . '\')"'; | ||
| 112 | $close_editor_date = '<p id="cancel-' . $id_date . '" class="hidden"><button ' . $close_js_date . '>Annuler</button></p>'; | ||
| 113 | $submit_js_date = 'onclick="submitDate(\'' . $id_date . '\')"'; | ||
| 114 | $submit_date = '<p id="submit-' . $id_date . '" class="hidden"><button ' . $submit_js_date . '>Valider</button></p>'; | ||
| 115 | $date_buttons = '<div class="button_zone">' . $modify_date . $close_editor_date . $submit_date . '</div>'; | ||
| 116 | |||
| 117 | // mode nouvel article | ||
| 118 | if(self::$new_article_mode){ | ||
| 119 | $delete_article = ''; | ||
| 120 | // valider la création d'un nouvel article | ||
| 121 | $submit_js = 'onclick="submitArticle(\'' . $_GET['id'] . '\')"'; | ||
| 122 | $submit_article = '<p id="save-' . $id . '"><img class="action_icon delete_button" src="assets/save.svg" ' . $submit_js . '></p>' . "\n"; | ||
| 123 | } | ||
| 124 | // mode article existant | ||
| 125 | else{ | ||
| 126 | $url = new URL(['action' => 'delete_article', 'id' => $_GET['id'], 'from' => $_GET['from'] ?? '']); | ||
| 127 | $delete_article = '<form id="delete-' . $id . '" method="post" action="' . $url . '"> | ||
| 128 | <p> | ||
| 129 | <img src="assets/delete-bin.svg" alt="Supprimer l\'article" class="action_icon" style="cursor: pointer;" onclick="if(confirm(\'Voulez-vous vraiment supprimer cet article ?\')) { this.closest(\'form\').submit(); }" | ||
| 130 | </p> | ||
| 131 | </form>' . "\n"; // this.closest('form').submit() = submit du formulaire avec javascript | ||
| 132 | $submit_article = ''; | ||
| 133 | } | ||
| 134 | |||
| 135 | $admin_buttons = $share_button . $delete_article . $submit_article . $from_to_button; | ||
| 136 | } | ||
| 137 | // autre page | ||
| 138 | else{ | ||
| 139 | $delete_js = 'onclick="deleteArticle(\'' . $id . '\')"'; | ||
| 140 | $delete_article = '<p id="delete-' . $id . '"><img class="action_icon" src="assets/delete-bin.svg" ' . $delete_js . '></p>' . "\n"; | ||
| 141 | |||
| 142 | $close_editor = '<p id="cancel-' . $id . '" class="hidden"></p>'; | ||
| 143 | $submit_article = '<p id="submit-' . $id . '" class="hidden"></p>'; | ||
| 144 | |||
| 145 | $admin_buttons = $from_to_button . $share_button . $delete_article . $close_editor . $submit_article; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | else{ | ||
| 149 | $admin_buttons = $share_button . $from_to_button; | ||
| 150 | } | ||
| 151 | |||
| 152 | ob_start(); | ||
| 153 | require($viewFile); | ||
| 154 | $this->html .= ob_get_clean(); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
