Merge pull request #5989 from WoltLab/wsc-rpc-api-const
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / tag / TagEditor.class.php
CommitLineData
04c06e85 1<?php
a9229942 2
04c06e85 3namespace wcf\data\tag;
a9229942 4
04c06e85
MW
5use wcf\data\DatabaseObjectEditor;
6use wcf\system\WCF;
7
8/**
9 * Provides functions to edit tags.
a9229942
TD
10 *
11 * @author Tim Duesterhus, Marcel Werk
12 * @copyright 2001-2019 WoltLab GmbH
13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
a9229942
TD
14 *
15 * @method static Tag create(array $parameters = [])
16 * @method Tag getDecoratedObject()
17 * @mixin Tag
04c06e85 18 */
a9229942
TD
19class TagEditor extends DatabaseObjectEditor
20{
21 /**
22 * @inheritDoc
23 */
24 protected static $baseClass = Tag::class;
25
26 /**
27 * Adds the given tag, and all of it's synonyms as a synonym.
28 *
29 * @param Tag $synonym
30 */
31 public function addSynonym(Tag $synonym)
32 {
33 if ($synonym->tagID == $this->tagID) {
34 return;
35 }
36
37 // assign all associations for the synonym with this tag
38 $sql = "UPDATE IGNORE wcf" . WCF_N . "_tag_to_object
39 SET tagID = ?
40 WHERE tagID = ?";
41 $statement = WCF::getDB()->prepareStatement($sql);
42 $statement->execute([$this->tagID, $synonym->tagID]);
43
44 // remove remaining associations (object was tagged with both tags => duplicate key previously ignored)
45 $sql = "DELETE FROM wcf" . WCF_N . "_tag_to_object
46 WHERE tagID = ?";
47 $statement = WCF::getDB()->prepareStatement($sql);
48 $statement->execute([$synonym->tagID]);
49
50 $editor = new self($synonym);
51 $editor->update(['synonymFor' => $this->tagID]);
52
53 $synonymList = new TagList();
54 $synonymList->getConditionBuilder()->add('synonymFor = ?', [$synonym->tagID]);
55 $synonymList->readObjects();
56
57 foreach ($synonymList as $synonym) {
58 $this->addSynonym($synonym);
59 }
60 }
04c06e85 61}