2d4180b09d06a506c2c3509262da9911bea18fb6
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / article / LikeableArticleProvider.class.php
1 <?php
2
3 namespace wcf\data\article;
4
5 use wcf\data\like\ILikeObjectTypeProvider;
6 use wcf\data\like\object\ILikeObject;
7 use wcf\data\object\type\AbstractObjectTypeProvider;
8 use wcf\system\like\IViewableLikeProvider;
9 use wcf\system\WCF;
10
11 /**
12 * Like Object type provider for cms articles.
13 *
14 * @author Marcel Werk
15 * @copyright 2001-2019 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @since 3.0
18 *
19 * @method LikeableArticle getObjectByID($objectID)
20 * @method LikeableArticle[] getObjectsByIDs(array $objectIDs)
21 */
22 class LikeableArticleProvider extends AbstractObjectTypeProvider implements
23 ILikeObjectTypeProvider,
24 IViewableLikeProvider
25 {
26 /**
27 * @inheritDoc
28 */
29 public $className = Article::class;
30
31 /**
32 * @inheritDoc
33 */
34 public $listClassName = ArticleList::class;
35
36 /**
37 * @inheritDoc
38 */
39 public $decoratorClassName = LikeableArticle::class;
40
41 /**
42 * @inheritDoc
43 */
44 public function checkPermissions(ILikeObject $object)
45 {
46 /** @var LikeableArticle $object */
47 return $object->articleID && $object->canRead();
48 }
49
50 /**
51 * @inheritDoc
52 */
53 public function prepare(array $likes)
54 {
55 $articleIDs = [];
56 foreach ($likes as $like) {
57 $articleIDs[] = $like->objectID;
58 }
59
60 // fetch articles
61 $articleList = new ViewableArticleList();
62 $articleList->setObjectIDs($articleIDs);
63 $articleList->readObjects();
64 $articles = $articleList->getObjects();
65
66 // set message
67 foreach ($likes as $like) {
68 if (isset($articles[$like->objectID])) {
69 $article = $articles[$like->objectID];
70
71 // check permissions
72 if (!$article->canRead()) {
73 continue;
74 }
75 $like->setIsAccessible();
76
77 // short output
78 $text = WCF::getLanguage()->getDynamicVariable('wcf.like.title.com.woltlab.wcf.likeableArticle', [
79 'article' => $article,
80 'reaction' => $like,
81 // @deprecated 5.3 Use `$reaction` instead
82 'like' => $like,
83 ]);
84 $like->setTitle($text);
85
86 // output
87 $like->setDescription($article->getTeaser());
88 }
89 }
90 }
91 }