Skip to content

Commit 6a3a194

Browse files
author
Zane
committed
refactor set article authors via setAuthors
Article editor: images gallery
1 parent d1d65d3 commit 6a3a194

File tree

4 files changed

+36
-53
lines changed

4 files changed

+36
-53
lines changed

‎src/Controller/Editor/ArticleEditAuthors.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,18 @@ public function setAuthors(int $articleId) : JsonResponse|Response
5858
{
5959
try {
6060

61+
$currentUserAsAuthor = $this->getCurrentUserAsAuthor();
62+
63+
$this->loadArticleEditor($articleId);
64+
6165
$arrAuthorIds = $this->request->get('authors') ?? [];
62-
$this->loadArticleEditor($articleId)->setAuthorsFromIds($arrAuthorIds);
66+
67+
$authors = $this->factory->createUserCollection()->load($arrAuthorIds);
68+
69+
$this->articleEditor->setAuthors($authors, $currentUserAsAuthor);
70+
6371
$this->factory->getEntityManager()->flush();
72+
6473
return $this->jsonOKResponse("Autori salvati");
6574

6675
} catch(Exception|Error $ex) { return $this->textErrorResponse($ex); }

‎src/Controller/Editor/ArticleEditTags.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function setTags(int $articleId) : JsonResponse|Response
6767
$this->articleEditor->setTags($tags, $currentUserAsAuthor);
6868

6969
$this->factory->getEntityManager()->flush();
70+
7071
return $this->jsonOKResponse("Tag salvati");
7172

7273
} catch(Exception|Error $ex) { return $this->textErrorResponse($ex); }

‎src/Entity/Cms/Article.php

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

9898
public function addAuthor(ArticleAuthor $author) : static
9999
{
100+
$ranking = 0;
100101
$currentItems = $this->getAuthors();
101102
foreach($currentItems as $item) {
102103

103104
if( $item->getUser()->getId() == $author->getUser()->getId() ) {
104105
return $this;
105106
}
107+
108+
$itemRanking = $item->getRanking();
109+
$ranking = $itemRanking > $ranking ? $itemRanking : $ranking;
106110
}
107111

112+
$author
113+
->setArticle($this)
114+
->setRanking(++$ranking);
115+
108116
$this->authors->add($author);
109-
$author->setArticle($this);
110117

111118
return $this;
112119
}

‎src/Service/Cms/ArticleEditor.php

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -116,70 +116,36 @@ public function addAuthor(User $author) : static
116116
}
117117

118118

119-
public function setAuthorsFromIds(array $arrAuthorIds) : static
119+
public function setAuthors(iterable $newAuthors, User $author) : static
120120
{
121-
if( empty($arrAuthorIds) ) {
122-
throw new ArticleUpdateException('L\'articolo deve avere almeno 1 autore');
123-
}
124-
125-
$arrAuthorIds = array_unique($arrAuthorIds);
121+
// invalidate the cached author list
122+
$this->arrAuthors = null;
126123

127-
$collNewAuthors = $this->factory->createUserCollection()->load($arrAuthorIds);
128-
129-
if( $collNewAuthors->count() < 1 ) {
130-
throw new ArticleUpdateException('L\'articolo deve avere almeno 1 autore');
124+
$oldAuthorJunctions = $this->entity->getAuthors()->getValues();
125+
foreach($oldAuthorJunctions as $junction) {
126+
$this->entity->removeAuthor($junction);
131127
}
132128

133-
// rebuild the cached author list
134-
$this->arrAuthors = [];
135-
136-
$entityManager = $this->factory->getEntityManager();
137-
138-
// these would be junctions (array-of-ArticleAuthor)
139-
$currentAuthors = $this->entity->getAuthors();
140-
141-
// remove current authors who are no longer
142-
foreach($currentAuthors as $junction) {
143-
144-
$authorId = $junction->getUser()->getId();
145-
$newAuthor = $collNewAuthors->get($authorId);
129+
$oldAuthorJunctionByAuthorId = [];
130+
foreach($oldAuthorJunctions as $junction) {
146131

147-
if( empty($newAuthor) ) {
148-
$entityManager->remove($junction);
149-
}
132+
$authorId = $junction->getUser()->getId();
133+
$oldAuthorJunctionByAuthorId[$authorId] = $junction;
150134
}
151135

152-
// add new users
153-
$i=1;
154-
foreach($collNewAuthors as $author) {
136+
foreach($newAuthors as $user) {
155137

156-
$existingJunction = null;
157-
foreach($currentAuthors as $junction) {
138+
$userId = $user->getId();
139+
$existingAuthorJunction = $oldAuthorJunctionByAuthorId[$userId] ?? null;
158140

159-
if( $author->getId() == $junction->getUser()->getId() ) {
141+
if( empty($existingAuthorJunction) ) {
160142

161-
$existingJunction = $junction;
162-
break;
163-
}
164-
}
143+
$this->addAuthor($user);
165144

166-
if( empty($existingJunction) ) {
145+
} else {
167146

168-
$existingJunction =
169-
(new ArticleAuthor())
170-
->setArticle($this->entity)
171-
->setUser( $author->getEntity() );
147+
$this->entity->addAuthor($existingAuthorJunction);
172148
}
173-
174-
$existingJunction->setRanking($i);
175-
$entityManager->persist($existingJunction);
176-
177-
$i++;
178-
179-
// rebuild the cached author list so that $article->getAuthors() works without a reload
180-
$userEntity = $author->getEntity();
181-
$authorId = $userEntity->getId();
182-
$this->arrAuthors[$authorId] = $this->factory->createUser($userEntity);
183149
}
184150

185151
return $this;

0 commit comments

Comments
 (0)