| 1 | <?php |
| 2 | |
| 3 | namespace wcf\data\tag; |
| 4 | |
| 5 | use wcf\data\DatabaseObjectEditor; |
| 6 | use wcf\system\WCF; |
| 7 | |
| 8 | /** |
| 9 | * Provides functions to edit tags. |
| 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> |
| 14 | * |
| 15 | * @method static Tag create(array $parameters = []) |
| 16 | * @method Tag getDecoratedObject() |
| 17 | * @mixin Tag |
| 18 | */ |
| 19 | class 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 | } |
| 61 | } |