1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Content List Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2016 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\ContentListBundle\Services; |
18
|
|
|
|
19
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
20
|
|
|
use SWP\Bundle\ContentListBundle\Event\ContentListEvent; |
21
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
22
|
|
|
use SWP\Component\ContentList\ContentListEvents; |
23
|
|
|
use SWP\Component\ContentList\Model\ContentListInterface; |
24
|
|
|
use SWP\Component\ContentList\Model\ContentListItemInterface; |
25
|
|
|
use SWP\Component\ContentList\Model\ListContentInterface; |
26
|
|
|
use SWP\Component\ContentList\Repository\ContentListItemRepositoryInterface; |
27
|
|
|
use SWP\Component\Storage\Factory\FactoryInterface; |
28
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
29
|
|
|
|
30
|
|
|
final class ContentListService implements ContentListServiceInterface |
31
|
|
|
{ |
32
|
|
|
private $eventDispatcher; |
33
|
|
|
|
34
|
|
|
private $listItemFactory; |
35
|
|
|
|
36
|
|
|
private $contentListItemRepository; |
37
|
|
|
|
38
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher, FactoryInterface $listItemFactory, ContentListItemRepositoryInterface $contentListItemRepository) |
39
|
|
|
{ |
40
|
|
|
$this->eventDispatcher = $eventDispatcher; |
41
|
|
|
$this->listItemFactory = $listItemFactory; |
42
|
|
|
$this->contentListItemRepository = $contentListItemRepository; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function addArticleToContentList(ContentListInterface $contentList, ArticleInterface $article, $position = null, bool $isSticky = false): ContentListItemInterface |
46
|
|
|
{ |
47
|
|
|
/* @var ContentListItemInterface $contentListItem */ |
48
|
|
|
$contentListItem = $this->listItemFactory->create(); |
49
|
|
|
|
50
|
|
|
if ($article instanceof ListContentInterface) { |
51
|
|
|
$contentListItem->setContent($article); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (null === $position) { |
55
|
|
|
$position = $contentList->getItems()->count(); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$contentListItem->setPosition((int) $position); |
59
|
|
|
$contentListItem->setContentList($contentList); |
60
|
|
|
$this->contentListItemRepository->add($contentListItem); |
61
|
|
|
|
62
|
|
|
$this->eventDispatcher->dispatch( |
63
|
|
|
ContentListEvents::POST_ITEM_ADD, |
64
|
|
|
new ContentListEvent($contentList, $contentListItem) |
65
|
|
|
); |
66
|
|
|
$contentList->setUpdatedAt(new \DateTime()); |
67
|
|
|
|
68
|
|
|
$this->toggleStickOnItemPosition($contentListItem, $isSticky, (int) $position); |
69
|
|
|
|
70
|
|
|
return $contentListItem; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function removeListItemsAboveTheLimit(ContentListInterface $contentList): void |
74
|
|
|
{ |
75
|
|
|
$items = $this->contentListItemRepository |
76
|
|
|
->getSortedItems(new Criteria(['contentList' => $contentList]), [], ['contentList' => $contentList]) |
77
|
|
|
->setMaxResults(null) |
78
|
|
|
->getQuery() |
79
|
|
|
->getResult(); |
80
|
|
|
|
81
|
|
|
if (null !== $contentList->getLimit() && $contentList->getLimit() > 0 && \count($items) > $contentList->getLimit()) { |
82
|
|
|
foreach ($items as $key => $item) { |
83
|
|
|
if ($key + 1 > $contentList->getLimit()) { |
84
|
|
|
$this->contentListItemRepository->remove($item); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function toggleStickOnItemPosition(ContentListItemInterface $contentListItem, bool $isSticky, int $position): void |
91
|
|
|
{ |
92
|
|
|
$contentListItem->setSticky($isSticky); |
93
|
|
|
|
94
|
|
|
if ($contentListItem->isSticky()) { |
95
|
|
|
$contentListItem->setStickyPosition($position); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function repositionStickyItems(ContentListInterface $contentList): void |
100
|
|
|
{ |
101
|
|
|
$stickyItems = $this->contentListItemRepository->findBy([ |
102
|
|
|
'sticky' => true, |
103
|
|
|
'contentList' => $contentList, |
104
|
|
|
]); |
105
|
|
|
|
106
|
|
|
foreach ($stickyItems as $stickyItem) { |
107
|
|
|
if (null !== $stickyItem->getStickyPosition()) { |
108
|
|
|
$stickyItem->setPosition($stickyItem->getStickyPosition()); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function isAnyItemPinnedOnPosition(int $listId, int $position): ?ContentListItemInterface |
114
|
|
|
{ |
115
|
|
|
return $this->contentListItemRepository->findOneBy([ |
116
|
|
|
'contentList' => $listId, |
117
|
|
|
'position' => $position, |
118
|
|
|
]); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.