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