Add clipboard support for tags
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / tag / TagAction.class.php
CommitLineData
04c06e85
MW
1<?php
2namespace wcf\data\tag;
3use wcf\data\AbstractDatabaseObjectAction;
4use wcf\data\ISearchAction;
ea3185a0 5use wcf\system\clipboard\ClipboardHandler;
04c06e85
MW
6use wcf\system\database\util\PreparedStatementConditionBuilder;
7use wcf\system\exception\UserInputException;
8use wcf\system\WCF;
9
10/**
11 * Executes tagging-related actions.
12 *
13 * @author Alexander Ebert
2b6cb5c2 14 * @copyright 2001-2015 WoltLab GmbH
04c06e85 15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
f4f05aa5 16 * @package com.woltlab.wcf
04c06e85
MW
17 * @subpackage data.tag
18 * @category Community Framework
19 */
20class TagAction extends AbstractDatabaseObjectAction implements ISearchAction {
21 /**
0ad90fc3 22 * @see \wcf\data\AbstractDatabaseObjectAction
04c06e85
MW
23 */
24 protected $allowGuestAccess = array('getSearchResultList');
25
26 /**
0ad90fc3 27 * @see \wcf\data\AbstractDatabaseObjectAction::$className
04c06e85
MW
28 */
29 protected $className = 'wcf\data\tag\TagEditor';
30
31 /**
32 * @see \wcf\data\AbstractDatabaseObjectAction::$permissionsDelete
33 */
34 protected $permissionsDelete = array('admin.content.tag.canManageTag');
35
36 /**
37 * @see \wcf\data\AbstractDatabaseObjectAction::$permissionsUpdate
38 */
39 protected $permissionsUpdate = array('admin.content.tag.canManageTag');
40
bae8dd1e 41 /**
0ad90fc3 42 * @see \wcf\data\AbstractDatabaseObjectAction::$requireACP
bae8dd1e
AE
43 */
44 protected $requireACP = array('delete', 'update');
45
ea3185a0
MS
46 /**
47 * tag for which other tags will be used as synonyms
48 * @var \wcf\data\tag\TagEditor
49 */
50 public $tagEditor = null;
51
04c06e85 52 /**
0ad90fc3 53 * @see \wcf\data\ISearchAction::validateGetSearchResultList()
04c06e85
MW
54 */
55 public function validateGetSearchResultList() {
56 $this->readString('searchString', false, 'data');
57
58 if (isset($this->parameters['data']['excludedSearchValues']) && !is_array($this->parameters['data']['excludedSearchValues'])) {
59 throw new UserInputException('excludedSearchValues');
60 }
61 }
62
63 /**
0ad90fc3 64 * @see \wcf\data\ISearchAction::getSearchResultList()
04c06e85
MW
65 */
66 public function getSearchResultList() {
67 $excludedSearchValues = array();
68 if (isset($this->parameters['data']['excludedSearchValues'])) {
69 $excludedSearchValues = $this->parameters['data']['excludedSearchValues'];
70 }
71 $list = array();
72
73 $conditionBuilder = new PreparedStatementConditionBuilder();
74 $conditionBuilder->add("name LIKE ?", array($this->parameters['data']['searchString'].'%'));
75 if (!empty($excludedSearchValues)) {
76 $conditionBuilder->add("name NOT IN (?)", array($excludedSearchValues));
77 }
78
79 // find tags
80 $sql = "SELECT tagID, name
81 FROM wcf".WCF_N."_tag
82 ".$conditionBuilder;
83 $statement = WCF::getDB()->prepareStatement($sql, 5);
84 $statement->execute($conditionBuilder->getParameters());
85 while ($row = $statement->fetchArray()) {
86 $list[] = array(
87 'label' => $row['name'],
88 'objectID' => $row['tagID']
89 );
90 }
91
92 return $list;
93 }
ea3185a0
MS
94
95 /**
96 * @see \wcf\data\IDeleteAction::delete()
97 */
98 public function delete() {
99 $returnValue = parent::delete();
100
101 $this->unmarkItems();
102
103 return $returnValue;
104 }
105
106 /**
107 * Validates the 'setAsSynonyms' action.
108 */
109 public function validateSetAsSynonyms() {
110 WCF::getSession()->checkPermissions([ 'admin.content.tag.canManageTag' ]);
111 if (empty($this->objects)) {
112 $this->readObjects();
113
114 if (count($this->objects) < 2) {
115 throw new UserInputException('objectIDs');
116 }
117 }
118
119 $this->readInteger('tagID');
120 $this->tagEditor = new TagEditor(new Tag($this->parameters['tagID']));
121 if (!$this->tagEditor->tagID) {
122 throw new UserInputException('tagID');
123 }
124 }
125
126 /**
127 * Sets a number of tags as a synonyms of another tag.
128 */
129 public function setAsSynonyms() {
130 // the "main" tag may not be a synonym itself
131 if ($this->tagEditor->synonymFor) {
132 $this->tagEditor->update([
133 'synonymFor' => null
134 ]);
135 }
136
137 foreach ($this->objects as $tagEditor) {
138 $this->tagEditor->addSynonym($tagEditor->getDecoratedObject());
139 }
140
141 $this->unmarkItems();
142 }
143
144 /**
145 * Unmarks tags.
146 *
147 * @param array<integer> $tagIDs
148 */
149 protected function unmarkItems(array $tagIDs = array()) {
150 if (empty($tagIDs)) {
151 $tagIDs = $this->objectIDs;
152 }
153
154 if (!empty($tagIDs)) {
155 ClipboardHandler::getInstance()->unmark($tagIDs, ClipboardHandler::getInstance()->getObjectTypeID('com.woltlab.wcf.tag'));
156 }
157 }
04c06e85 158}