Skip to content

Commit a66c6ea

Browse files
author
Zane
committed
refactor tagging via TagEditorCollection
Article editor: images gallery
1 parent ab01f38 commit a66c6ea

File tree

11 files changed

+92
-171
lines changed

11 files changed

+92
-171
lines changed

‎src/Controller/Editor/ArticleEditTags.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,18 @@ public function setTags(int $articleId) : JsonResponse|Response
5454
{
5555
try {
5656

57+
$currentUserAsAuthor = $this->getCurrentUserAsAuthor();
58+
59+
$this->loadArticleEditor($articleId);
60+
5761
$arrIdsAndTags = $this->request->get('tags') ?? [];
58-
$this->loadArticleEditor($articleId)->setTagsFromIdsAndTags($arrIdsAndTags);
62+
63+
$tags =
64+
$this->factory->createTagEditorCollection()
65+
->setFromIdsAndTags($arrIdsAndTags, $currentUserAsAuthor);
66+
67+
$this->articleEditor->setTags($tags, $currentUserAsAuthor);
68+
5969
$this->factory->getEntityManager()->flush();
6070
return $this->jsonOKResponse("Tag salvati");
6171

‎src/Controller/Editor/ArticleUploadImages.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function upload(int $articleId) : JsonResponse|Response
2525
$this->factory->createImageEditorCollection()
2626
->setFromUpload($uploadedImages, $currentUserAsAuthor);
2727

28-
$this->articleEditor->addImages($images);
28+
$this->articleEditor->addImages($images, $currentUserAsAuthor);
2929

3030
$this->factory->getEntityManager()->flush();
3131

‎src/Entity/Cms/Article.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,23 @@ public function getTags() : Collection { return $this->tags; }
172172

173173
public function addTag(ArticleTag $tag) : static
174174
{
175+
$ranking = 0;
175176
$currentItems = $this->getTags();
176177
foreach($currentItems as $item) {
177178

178179
if( $item->getTag()->getId() == $tag->getTag()->getId() ) {
179180
return $this;
180181
}
182+
183+
$itemRanking = $item->getRanking();
184+
$ranking = $itemRanking > $ranking ? $itemRanking : $ranking;
181185
}
182186

187+
$tag
188+
->setArticle($this)
189+
->setRanking(++$ranking);
190+
183191
$this->tags->add($tag);
184-
$tag->setArticle($this);
185192

186193
return $this;
187194
}

‎src/Entity/Cms/Tag.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,23 @@ public function getAuthors() : Collection { return $this->authors; }
5656

5757
public function addAuthor(TagAuthor $author) : static
5858
{
59+
$ranking = 0;
5960
$currentItems = $this->getAuthors();
6061
foreach($currentItems as $item) {
6162

6263
if( $item->getUser()->getId() == $author->getUser()->getId() ) {
6364
return $this;
6465
}
66+
67+
$itemRanking = $item->getRanking();
68+
$ranking = $itemRanking > $ranking ? $itemRanking : $ranking;
6569
}
6670

71+
$author
72+
->setTag($this)
73+
->setRanking(++$ranking);
74+
6775
$this->authors->add($author);
68-
$author->setTag($this);
6976

7077
return $this;
7178
}

‎src/Service/Cms/Article.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,19 +236,12 @@ public function getSpotlightOrDefault() : ImageService
236236
//<editor-fold defaultstate="collapsed" desc="*** 🏷️ Tags ***">
237237
public function getTags() : array
238238
{
239-
if( !is_array($this->arrTags) ) {
240-
241-
$tagJunctionEntities = $this->entity->getTags()->getValues();
242-
$this->setCachedTagsFromJunctions($tagJunctionEntities);
239+
if( is_array($this->arrTags) ) {
240+
return $this->arrTags;
243241
}
244242

245-
return $this->arrTags;
246-
}
247-
248-
249-
protected function setCachedTagsFromJunctions(?array $tagJunctionEntities) : static
250-
{
251243
$this->arrTags = [];
244+
$tagJunctionEntities = $this->entity->getTags()->getValues();
252245

253246
usort($tagJunctionEntities, function(ArticleTag $junction1, ArticleTag $junction2) {
254247
return
@@ -263,7 +256,7 @@ protected function setCachedTagsFromJunctions(?array $tagJunctionEntities) : sta
263256
$this->arrTags[$tagId] = $this->factory->createTag($tagEntity);
264257
}
265258

266-
return $this;
259+
return $this->arrTags;
267260
}
268261

269262

‎src/Service/Cms/ArticleEditor.php

Lines changed: 34 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
use App\Entity\Cms\ArticleImage;
66
use App\Entity\Cms\ArticleTag;
77
use App\Entity\Cms\Tag as TagEntity;
8+
use App\Entity\Cms\Image as ImageEntity;
89
use App\Exception\ArticleUpdateException;
910
use App\Service\Factory;
1011
use App\Service\PhpBB\Topic;
1112
use App\Service\TextProcessor;
1213
use App\Service\User;
13-
use App\ServiceCollection\Cms\ImageCollection;
1414
use DateTimeInterface;
1515
use Exception;
1616

@@ -187,128 +187,53 @@ public function setAuthorsFromIds(array $arrAuthorIds) : static
187187
//</editor-fold>
188188

189189
//<editor-fold defaultstate="collapsed" desc="*** 🏷️ Tags ***">
190-
public function addTag(Tag|TagEntity $tag, User $user) : static
190+
public function addTag(Tag|TagEntity $tag, User $author) : static
191191
{
192192
$tagToAdd = $tag instanceof Tag ? $tag->getEntity() : $tag;
193193

194194
$this->entity->addTag(
195195
(new ArticleTag())
196-
->setArticle($this->entity)
197196
->setTag($tagToAdd)
198-
->setUser( $user->getEntity() )
197+
->setUser( $author->getEntity() )
199198
);
200199

201200
return $this;
202201
}
203202

204203

205-
public function setTagsFromIdsAndTags(array $arrIdsAndTags) : static
204+
public function setTags(iterable $newTags, User $author) : static
206205
{
207-
// validation
208-
$arrValidItems = [];
209-
$arrUniqueIds = [];
210-
$arrUniqueTags = [];
211-
212-
foreach($arrIdsAndTags as $item) {
213-
214-
if( !array_key_exists('id', $item) || !array_key_exists('title', $item) ) {
215-
continue;
216-
}
217-
218-
$id = $item["id"];
219-
220-
if( in_array($id, $arrUniqueIds) ) {
221-
continue;
222-
}
223-
224-
if( !empty($id) ) {
225-
$arrUniqueIds[] = $id;
226-
}
227-
228-
$tagService = $this->factory->createTagEditor()->setTitle($item["title"]);
229-
$title = $tagService->getTitle();
230-
231-
if( in_array($title, $arrUniqueTags) ) {
232-
continue;
233-
}
206+
// invalidate the cached tag list
207+
$this->arrTags = null;
234208

235-
$arrUniqueTags[] = $title;
236-
237-
$arrValidItems[] = [
238-
"id" => $id,
239-
"title" => $title,
240-
"service" => $tagService
241-
];
242-
}
243-
244-
if( empty($arrValidItems) ) {
245-
throw new ArticleUpdateException('L\'articolo deve avere almeno 1 tag');
209+
$oldTagJunctions = $this->entity->getTags()->getValues();
210+
foreach($oldTagJunctions as $junction) {
211+
$this->entity->removeTag($junction);
246212
}
247213

248-
$collNewTags = $this->factory->createTagCollection()->load($arrUniqueIds);
249-
$arrFinalTags = [];
250-
foreach($arrValidItems as $item) {
251-
252-
$tagService = $collNewTags->get( $item["id"] );
253-
$arrFinalTags[] = empty($tagService) ? $item["service"] : $tagService;
254-
}
255-
256-
$collNewTags->setData($arrFinalTags);
257-
258-
// rebuild the cached tag list
259-
$this->arrTags = [];
260-
261-
$entityManager = $this->factory->getEntityManager();
262-
263-
// these would be junctions (array-of-ArticleTag)
264-
$currentTags = $this->entity->getTags();
265-
266-
// remove unassigned tags
267-
foreach($currentTags as $junction) {
214+
$oldTagJunctionByTagId = [];
215+
foreach($oldTagJunctions as $junction) {
268216

269217
$tagId = $junction->getTag()->getId();
270-
271-
if( !in_array($tagId, $arrUniqueIds) ) {
272-
$entityManager->remove($junction);
273-
}
218+
$oldTagJunctionByTagId[$tagId] = $junction;
274219
}
275220

276-
// add new tags
277-
$i = 1;
278-
$arrJunctions = [];
221+
foreach($newTags as $tag) {
279222

280-
foreach($collNewTags as $tag) {
223+
$tagId = $tag->getId();
224+
$existingTagJunction = $oldTagJunctionByTagId[$tagId] ?? null;
281225

282-
$existingJunction = null;
283-
foreach($currentTags as $junction) {
226+
if( empty($existingTagJunction) ) {
284227

285-
if( $tag->getId() == $junction->getTag()->getId() ) {
228+
$this->addTag($tag, $author);
286229

287-
$existingJunction = $junction;
288-
break;
289-
}
290-
}
230+
} else {
291231

292-
if( empty($existingJunction) ) {
293-
294-
$tagEntity = $tag->getEntity();
295-
$entityManager->persist($tagEntity);
296-
297-
$existingJunction =
298-
(new ArticleTag())
299-
->setArticle($this->entity)
300-
->setTag($tagEntity)
301-
->setUser( $this->getCurrentUserAsAuthor()->getEntity() );
232+
$this->entity->addTag($existingTagJunction);
302233
}
303-
304-
$existingJunction->setRanking($i);
305-
$entityManager->persist($existingJunction);
306-
307-
$i++;
308-
$arrJunctions[] = $existingJunction;
309234
}
310235

311-
return $this->setCachedTagsFromJunctions($arrJunctions);
236+
return $this;
312237
}
313238

314239

@@ -371,15 +296,22 @@ function(Tag $candidateTag, Tag $realTag) {
371296
//</editor-fold>
372297

373298
//<editor-fold defaultstate="collapsed" desc="*** 🖼️ Images ***">
374-
public function addImages(iterable $images) : static
299+
public function addImage(Image|ImageEntity $image, User $author) : static
375300
{
376-
foreach($images as $image) {
301+
$imageToAdd = $image instanceof Image ? $image->getEntity() : $image;
302+
303+
$this->entity->addImage(
304+
(new ArticleImage())
305+
->setImage($imageToAdd)
306+
);
307+
308+
return $this;
309+
}
377310

378-
$image = $image instanceof Image ? $image->getEntity() : $image;
379-
$this->entity->addImage(
380-
(new ArticleImage())
381-
->setImage($image)
382-
);
311+
public function addImages(iterable $images, User $author) : static
312+
{
313+
foreach($images as $image) {
314+
$this->addImage($image, $author);
383315
}
384316

385317
return $this;

‎src/Service/Cms/ImageEditor.php

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
<?php
22
namespace App\Service\Cms;
33

4-
use App\Entity\Cms\ArticleImage;
54
use App\Entity\Cms\ImageAuthor;
65
use App\Service\Factory;
76
use App\Service\TextProcessor;
87
use App\Service\User;
8+
use App\Trait\SaveableTrait;
99
use Imagine\Exception\NotSupportedException;
1010
use Symfony\Component\HttpFoundation\File\UploadedFile;
1111

1212

1313
class ImageEditor extends Image
1414
{
15+
use SaveableTrait;
16+
1517
protected UploadedFile $file;
1618

1719
public function __construct(Factory $factory, protected TextProcessor $textProcessor)
@@ -27,18 +29,6 @@ public function setTitle(string $newTitle) : static
2729
return $this;
2830
}
2931

30-
/*
31-
public function loadOrCreateFromUploadedFile(UploadedFile $uploadedFile) : ImageEditor
32-
{
33-
if( !str_starts_with($uploadedFile->getMimeType(), 'image') ) {
34-
throw new NotSupportedException('The MIME is not image/*');
35-
}
36-
37-
$fileHash =
38-
39-
$image = $this->loadByHash();
40-
}
41-
*/
4232

4333
public function createFromUploadedFile(UploadedFile $file) : ImageEditor
4434
{
@@ -76,24 +66,9 @@ public function addAuthor(User $author) : static
7666
{
7767
$this->entity->addAuthor(
7868
(new ImageAuthor())
79-
->setImage($this->entity)
8069
->setUser( $author->getEntity() )
8170
);
8271

8372
return $this;
8473
}
85-
86-
87-
//<editor-fold defaultstate="collapsed" desc="*** 💾 Save ***">
88-
public function save(bool $persist = true) : static
89-
{
90-
if($persist) {
91-
92-
$this->factory->getEntityManager()->persist($this->entity);
93-
$this->factory->getEntityManager()->flush();
94-
}
95-
96-
return $this;
97-
}
98-
//</editor-fold>
9974
}

0 commit comments

Comments
 (0)