Fixed typo
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / article / ArticleAction.class.php
CommitLineData
a5a4f02d
MW
1<?php
2namespace wcf\data\article;
a5a4f02d
MW
3use wcf\data\article\content\ArticleContent;
4use wcf\data\article\content\ArticleContentEditor;
7a08797b 5use wcf\data\AbstractDatabaseObjectAction;
2fd812d7 6use wcf\system\comment\CommentHandler;
a5a4f02d 7use wcf\system\language\LanguageFactory;
2fd812d7 8use wcf\system\like\LikeHandler;
ef17c746 9use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
2fd812d7 10use wcf\system\search\SearchIndexManager;
a5a4f02d
MW
11use wcf\system\tagging\TagEngine;
12
13/**
14 * Executes article related actions.
15 *
16 * @author Marcel Werk
cea1798f 17 * @copyright 2001-2017 WoltLab GmbH
a5a4f02d 18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4
MW
19 * @package WoltLabSuite\Core\Data\Article
20 * @since 3.0
a5a4f02d
MW
21 *
22 * @method ArticleEditor[] getObjects()
23 * @method ArticleEditor getSingleObject()
24 */
25class ArticleAction extends AbstractDatabaseObjectAction {
26 /**
27 * @inheritDoc
28 */
29 protected $className = ArticleEditor::class;
30
31 /**
32 * @inheritDoc
33 */
34 protected $permissionsCreate = ['admin.content.article.canManageArticle'];
35
36 /**
37 * @inheritDoc
38 */
39 protected $permissionsDelete = ['admin.content.article.canManageArticle'];
40
41 /**
42 * @inheritDoc
43 */
44 protected $permissionsUpdate = ['admin.content.article.canManageArticle'];
45
46 /**
47 * @inheritDoc
48 */
49 protected $requireACP = ['create', 'delete', 'update'];
50
51 /**
52 * @inheritDoc
53 * @return Article
54 */
55 public function create() {
56 /** @var Article $article */
57 $article = parent::create();
58
59 // save article content
60 if (!empty($this->parameters['content'])) {
61 foreach ($this->parameters['content'] as $languageID => $content) {
ef17c746
MW
62 if (!empty($content['htmlInputProcessor'])) {
63 /** @noinspection PhpUndefinedMethodInspection */
64 $content['content'] = $content['htmlInputProcessor']->getHtml();
65 }
66
2fd812d7 67 /** @var ArticleContent $articleContent */
a5a4f02d
MW
68 $articleContent = ArticleContentEditor::create([
69 'articleID' => $article->articleID,
63b9817b 70 'languageID' => $languageID ?: null,
a5a4f02d
MW
71 'title' => $content['title'],
72 'teaser' => $content['teaser'],
73 'content' => $content['content'],
79305986
MW
74 'imageID' => $content['imageID'],
75 'teaserImageID' => $content['teaserImageID']
a5a4f02d 76 ]);
ef17c746 77 $articleContentEditor = new ArticleContentEditor($articleContent);
a5a4f02d
MW
78
79 // save tags
80 if (!empty($content['tags'])) {
81 TagEngine::getInstance()->addObjectTags('com.woltlab.wcf.article', $articleContent->articleContentID, $content['tags'], ($languageID ?: LanguageFactory::getInstance()->getDefaultLanguageID()));
82 }
2fd812d7
MW
83
84 // update search index
e5adfbe2
MS
85 SearchIndexManager::getInstance()->set(
86 'com.woltlab.wcf.article',
87 $articleContent->articleContentID,
88 $articleContent->content,
89 $articleContent->title,
90 $article->time,
91 $article->userID,
92 $article->username,
93 $languageID ?: null,
94 $articleContent->teaser
95 );
ef17c746
MW
96
97 // save embedded objects
98 if (!empty($content['htmlInputProcessor'])) {
2f273839
MW
99 /** @noinspection PhpUndefinedMethodInspection */
100 $content['htmlInputProcessor']->setObjectID($articleContent->articleContentID);
bfb52dd9 101 if (MessageEmbeddedObjectManager::getInstance()->registerObjects($content['htmlInputProcessor'])) {
ef17c746
MW
102 $articleContentEditor->update(['hasEmbeddedObjects' => 1]);
103 }
104 }
a5a4f02d
MW
105 }
106 }
107
108 return $article;
109 }
110
111 /**
112 * @inheritDoc
113 */
114 public function update() {
115 parent::update();
116
117 // update article content
118 if (!empty($this->parameters['content'])) {
119 foreach ($this->getObjects() as $article) {
120 foreach ($this->parameters['content'] as $languageID => $content) {
ef17c746
MW
121 if (!empty($content['htmlInputProcessor'])) {
122 /** @noinspection PhpUndefinedMethodInspection */
123 $content['content'] = $content['htmlInputProcessor']->getHtml();
124 }
125
a5a4f02d 126 $articleContent = ArticleContent::getArticleContent($article->articleID, ($languageID ?: null));
ef17c746 127 $articleContentEditor = null;
a5a4f02d
MW
128 if ($articleContent !== null) {
129 // update
ef17c746
MW
130 $articleContentEditor = new ArticleContentEditor($articleContent);
131 $articleContentEditor->update([
a5a4f02d
MW
132 'title' => $content['title'],
133 'teaser' => $content['teaser'],
134 'content' => $content['content'],
79305986
MW
135 'imageID' => $content['imageID'],
136 'teaserImageID' => $content['teaserImageID']
a5a4f02d
MW
137 ]);
138
139 // delete tags
140 if (empty($content['tags'])) {
141 TagEngine::getInstance()->deleteObjectTags('com.woltlab.wcf.article', $articleContent->articleContentID, ($languageID ?: null));
142 }
143 }
144 else {
ef17c746 145 /** @var ArticleContent $articleContent */
a5a4f02d
MW
146 $articleContent = ArticleContentEditor::create([
147 'articleID' => $article->articleID,
63b9817b 148 'languageID' => $languageID ?: null,
a5a4f02d
MW
149 'title' => $content['title'],
150 'teaser' => $content['teaser'],
151 'content' => $content['content'],
79305986
MW
152 'imageID' => $content['imageID'],
153 'teaserImageID' => $content['teaserImageID']
a5a4f02d 154 ]);
ef17c746 155 $articleContentEditor = new ArticleContentEditor($articleContent);
a5a4f02d
MW
156 }
157
158 // save tags
159 if (!empty($content['tags'])) {
160 TagEngine::getInstance()->addObjectTags('com.woltlab.wcf.article', $articleContent->articleContentID, $content['tags'], ($languageID ?: LanguageFactory::getInstance()->getDefaultLanguageID()));
161 }
2fd812d7
MW
162
163 // update search index
e5adfbe2
MS
164 SearchIndexManager::getInstance()->set(
165 'com.woltlab.wcf.article',
166 $articleContent->articleContentID,
167 $articleContent->content,
168 $articleContent->title,
169 $article->time,
170 $article->userID,
171 $article->username,
172 $languageID ?: null,
173 $articleContent->teaser
174 );
ef17c746
MW
175
176 // save embedded objects
177 if (!empty($content['htmlInputProcessor'])) {
2f273839
MW
178 /** @noinspection PhpUndefinedMethodInspection */
179 $content['htmlInputProcessor']->setObjectID($articleContent->articleContentID);
bfb52dd9 180 if ($articleContent->hasEmbeddedObjects != MessageEmbeddedObjectManager::getInstance()->registerObjects($content['htmlInputProcessor'])) {
63b9817b 181 $articleContentEditor->update(['hasEmbeddedObjects' => $articleContent->hasEmbeddedObjects ? 0 : 1]);
ef17c746
MW
182 }
183 }
a5a4f02d
MW
184 }
185 }
186 }
187 }
2fd812d7
MW
188
189 /**
190 * @inheritDoc
191 */
192 public function delete() {
193 $articleIDs = $articleContentIDs = [];
194 foreach ($this->getObjects() as $article) {
195 $articleIDs[] = $article->articleID;
0c968ac8 196 foreach ($article->getArticleContents() as $articleContent) {
2fd812d7
MW
197 $articleContentIDs[] = $articleContent->articleContentID;
198 }
199 }
200
201 // delete articles
202 parent::delete();
203
204 if (!empty($articleIDs)) {
205 // delete like data
206 LikeHandler::getInstance()->removeLikes('com.woltlab.wcf.likeableArticle', $articleIDs);
207 // delete comments
62e2632a 208 CommentHandler::getInstance()->deleteObjects('com.woltlab.wcf.articleComment', $articleContentIDs);
2fd812d7
MW
209 // delete tag to object entries
210 TagEngine::getInstance()->deleteObjects('com.woltlab.wcf.article', $articleContentIDs);
211 // delete entry from search index
212 SearchIndexManager::getInstance()->delete('com.woltlab.wcf.article', $articleContentIDs);
213 }
214 }
a5a4f02d 215}