|
2 | 2 | namespace App\Controller\Editor;
|
3 | 3 |
|
4 | 4 | use App\Controller\BaseController;
|
5 |
| -use App\Service\Cms\Article; |
6 |
| -use App\Service\Cms\ArticleEditor; |
7 |
| -use App\Service\Factory; |
8 |
| -use App\Service\FrontendHelper; |
9 | 5 | use Error;
|
10 | 6 | use Exception;
|
11 | 7 | use Symfony\Component\HttpFoundation\JsonResponse;
|
12 |
| -use Symfony\Component\HttpFoundation\RequestStack; |
13 | 8 | use Symfony\Component\HttpFoundation\Response;
|
14 | 9 | use Symfony\Component\Routing\Attribute\Route;
|
15 |
| -use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
16 |
| -use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; |
17 |
| -use Twig\Environment; |
18 | 10 |
|
19 | 11 |
|
20 | 12 | class ArticleEditorController extends BaseController
|
21 | 13 | {
|
22 |
| - const string TITLE_FIELD_NAME = 'new-article-title'; |
23 |
| - const string FORMAT_FIELD_NAME = 'new-article-format'; |
24 |
| - const string CSRF_TOKEN_ID = self::TITLE_FIELD_NAME; |
25 |
| - |
26 |
| - |
27 |
| - public function __construct( |
28 |
| - protected Factory $factory, |
29 |
| - protected ArticleEditor $articleEditor, protected Article $article, RequestStack $requestStack, |
30 |
| - protected FrontendHelper $frontendHelper, protected CsrfTokenManagerInterface $csrfTokenManager, |
31 |
| - protected Environment $twig |
32 |
| - ) |
33 |
| - { |
34 |
| - $this->request = $requestStack->getCurrentRequest(); |
35 |
| - } |
36 |
| - |
37 |
| - |
38 |
| - //<editor-fold defaultstate="collapsed" desc="*** 🆕 /scrivi ***"> |
39 |
| - #[Route('/scrivi', name: 'app_editor_new', methods: ['GET'])] |
40 |
| - public function new() : Response |
41 |
| - { |
42 |
| - $currentUser = $this->factory->getCurrentUser(); |
43 |
| - |
44 |
| - if( empty($currentUser) ) { |
45 |
| - |
46 |
| - $templateFilename = 'new-logged-out'; |
47 |
| - $arrSideArticlesSlices = null; |
48 |
| - |
49 |
| - } else { |
50 |
| - |
51 |
| - $templateFilename = 'new'; |
52 |
| - |
53 |
| - $sideArticles = $this->factory->createArticleCollection()->loadLatestUpdatedListable(); |
54 |
| - |
55 |
| - $numArticlesPerSlide = 7; |
56 |
| - $numSlides = ceil( $sideArticles->count() / $numArticlesPerSlide ); |
57 |
| - |
58 |
| - $arrSideArticlesSlices = []; |
59 |
| - for($i = 0; $i < $numSlides; $i++) { |
60 |
| - |
61 |
| - $arrSideArticlesSlices[$i] = |
62 |
| - $sideArticles->getItems($numArticlesPerSlide, $numArticlesPerSlide*$i, false, false); |
63 |
| - } |
64 |
| - } |
65 |
| - |
66 |
| - return $this->render("article/editor/$templateFilename.html.twig", [ |
67 |
| - 'metaTitle' => 'Scrivi nuovo articolo', |
68 |
| - 'metaCanonicalUrl' => $this->generateUrl('app_editor_new', [], UrlGeneratorInterface::ABSOLUTE_URL), |
69 |
| - 'activeMenu' => '', |
70 |
| - 'FrontendHelper' => $this->frontendHelper, |
71 |
| - 'ArticleHowTo' => $this->factory->createArticle()->load(Article::ID_PUBLISH_ARTICLE), |
72 |
| - 'currentUserUrl' => $currentUser?->getUrl(), |
73 |
| - 'CurrentUserDraftArticles' => $currentUser?->getArticlesDraft(), |
74 |
| - 'CurrentUserInReviewArticles' => $currentUser?->getArticlesInReview(), |
75 |
| - 'CurrentUserPublishedArticles' => $currentUser?->getArticlesLatestPublished(), |
76 |
| - 'CurrentUserKoArticles' => $currentUser?->getArticlesKo(), |
77 |
| - 'SideArticlesSlices' => $arrSideArticlesSlices, |
78 |
| - 'Views' => $this->frontendHelper->getViews()->get(['bozze', 'finiti']), |
79 |
| - // |
80 |
| - 'titleFieldName' => static::TITLE_FIELD_NAME, |
81 |
| - 'formatFieldName' => static::FORMAT_FIELD_NAME, |
82 |
| - 'formatArticle' => Article::FORMAT_ARTICLE, |
83 |
| - 'formatNews' => Article::FORMAT_NEWS, |
84 |
| - 'csrfTokenFieldName' => static::CSRF_TOKEN_PARAM_NAME, |
85 |
| - 'csrfToken' => $this->csrfTokenManager->getToken(static::CSRF_TOKEN_ID)->getValue() |
86 |
| - ]); |
87 |
| - } |
88 |
| - |
89 |
| - |
90 |
| - #[Route('/scrivi/salva', name: 'app_editor_new_submit', methods: ['POST'])] |
91 |
| - public function submit() : Response |
92 |
| - { |
93 |
| - $currentUser = $this->factory->getCurrentUser(); |
94 |
| - |
95 |
| - if( empty($currentUser) ) { |
96 |
| - |
97 |
| - throw $this->createAccessDeniedException( |
98 |
| - 'Non sei loggato! Solo gli utenti registrati possono creare nuovi articoli.' |
99 |
| - ); |
100 |
| - } |
101 |
| - |
102 |
| - $this->validateCsrfToken(); |
103 |
| - |
104 |
| - // TODO zaneee! Rate limiting on new article |
105 |
| - |
106 |
| - $newArticleTitle = $this->request->get(static::TITLE_FIELD_NAME); |
107 |
| - |
108 |
| - $this->articleEditor->setTitle($newArticleTitle); |
109 |
| - |
110 |
| - $articles = |
111 |
| - $this->factory->createArticleCollection()->loadByComparableSearch( |
112 |
| - $this->articleEditor->getTitleComparable(), 'title' |
113 |
| - ); |
114 |
| - |
115 |
| - if( $articles->count() ) { |
116 |
| - return $this->redirect( $articles->first()->getUrl() ); |
117 |
| - } |
118 |
| - |
119 |
| - $newArticleFormat = $this->request->get(static::FORMAT_FIELD_NAME); |
120 |
| - |
121 |
| - /* |
122 |
| - * $currentUser is unknown to Doctrine: if we try to set it as Author directly: |
123 |
| - * A new entity was found through the relationship 'App\Entity\Cms\ArticleAuthor#user' that was not configured to cascade persist operations for entity: App\Entity\PhpBB\User@-- |
124 |
| - */ |
125 |
| - $currentUserId = $currentUser->getId(); |
126 |
| - $author = $this->factory->createUser()->load($currentUserId); |
127 |
| - |
128 |
| - $this->articleEditor |
129 |
| - ->setFormat($newArticleFormat) |
130 |
| - ->addAuthor($author) |
131 |
| - ->autotag($author) |
132 |
| - ->save(); |
133 |
| - |
134 |
| - return $this->redirect( $this->articleEditor->getUrl() ); |
135 |
| - } |
136 |
| - //</editor-fold> |
137 |
| - |
138 | 14 | //<editor-fold defaultstate="collapsed" desc="*** 📜 Title and Body ***">
|
139 | 15 | #[Route('/ajax/editor/article/{articleId<[1-9]+[0-9]*>}', name: 'app_editor_article_update', methods: ['POST'])]
|
140 | 16 | public function update(int $articleId) : JsonResponse|Response
|
@@ -266,42 +142,4 @@ public function setTags(int $articleId) : JsonResponse|Response
|
266 | 142 | } catch(Exception|Error $ex) { return $this->textErrorResponse($ex); }
|
267 | 143 | }
|
268 | 144 | //</editor-fold>
|
269 |
| - |
270 |
| - //<editor-fold defaultstate="collapsed" desc="*** ⛑️ Helpers ***"> |
271 |
| - protected function loadArticleEditor(int $articleId) : ArticleEditor |
272 |
| - { |
273 |
| - $this->ajaxOnly(); |
274 |
| - |
275 |
| - if( empty($this->getUser()) ) { |
276 |
| - throw $this->createAccessDeniedException('Non sei loggato!'); |
277 |
| - } |
278 |
| - |
279 |
| - $this->articleEditor->load($articleId); |
280 |
| - |
281 |
| - if( !$this->articleEditor->currentUserCanEdit() ) { |
282 |
| - throw $this->createAccessDeniedException('Non sei autorizzato a modificare questo articolo'); |
283 |
| - } |
284 |
| - |
285 |
| - return $this->articleEditor; |
286 |
| - } |
287 |
| - |
288 |
| - |
289 |
| - protected function jsonOKResponse(string $okMessage) : JsonResponse |
290 |
| - { |
291 |
| - return $this->json([ |
292 |
| - "message" => "✅ OK! $okMessage - " . (new \DateTime())->format('Y-m-d H:i:s'), |
293 |
| - "path" => $this->articleEditor->getUrl(UrlGeneratorInterface::RELATIVE_PATH), |
294 |
| - "title" => $this->articleEditor->getTitleForHTMLAttribute(), |
295 |
| - "strip" => $this->twig->render('article/meta-strip.html.twig', [ |
296 |
| - "Article" => $this->articleEditor, |
297 |
| - ]), |
298 |
| - "bios" => $this->twig->render('article/authors-bio.html.twig', [ |
299 |
| - "Article" => $this->articleEditor |
300 |
| - ]), |
301 |
| - "tags" => $this->twig->render('article/tags.html.twig', [ |
302 |
| - "Article" => $this->articleEditor |
303 |
| - ]) |
304 |
| - ]); |
305 |
| - } |
306 |
| - //</editor-fold> |
307 | 145 | }
|
0 commit comments