2 namespace wcf\system\form\builder\field\label
;
3 use wcf\data\IStorableObject
;
4 use wcf\data\label\group\ViewableLabelGroup
;
5 use wcf\data\label\Label
;
6 use wcf\system\form\builder\data\processor\CustomFormDataProcessor
;
7 use wcf\system\form\builder\field\AbstractFormField
;
8 use wcf\system\form\builder\field\validation\FormFieldValidationError
;
9 use wcf\system\form\builder\IFormDocument
;
10 use wcf\system\form\builder\IObjectTypeFormNode
;
11 use wcf\system\form\builder\TObjectTypeFormNode
;
12 use wcf\system\label\LabelHandler
;
15 * Implementation of a form field to select labels.
17 * @author Matthias Schmidt
18 * @copyright 2001-2019 WoltLab GmbH
19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
20 * @package WoltLabSuite\Core\System\Form\Builder\Field
23 class LabelFormField
extends AbstractFormField
implements IObjectTypeFormNode
{
24 use TObjectTypeFormNode
;
29 protected $javaScriptDataHandlerModule = 'WoltLabSuite/Core/Form/Builder/Field/Value';
32 * label group whose labels can be selected via this form field
33 * @var ViewableLabelGroup
35 protected $labelGroup;
40 protected $templateName = '__labelFormField';
43 * loaded labels grouped by label object type and object id to avoid loading the same labels
44 * over and over again for the same object and different label groups
47 protected static $loadedLabels = [];
50 * Returns the label group whose labels can be selected via this form field.
52 * @return ViewableLabelGroup label group whose labels can be selected
53 * @throws \BadMethodCallException if no label has been set
55 public function getLabelGroup() {
56 if ($this->labelGroup
=== null) {
57 throw new \
BadMethodCallException("No label group has been set.");
60 return $this->labelGroup
;
66 public function getObjectTypeDefinition() {
67 return 'com.woltlab.wcf.label.object';
73 public function hasSaveValue() {
78 * Sets the label group whose labels can be selected via this form field and returns this
81 * If no form field label has been set, the title of the label group will be set as label.
83 * @param ViewableLabelGroup $labelGroup label group whose labels can be selected
84 * @return static this form field
86 public function labelGroup(ViewableLabelGroup
$labelGroup) {
87 $this->labelGroup
= $labelGroup;
89 if ($this->label
=== null) {
90 $this->label($this->getLabelGroup()->getTitle());
99 public function loadValue(array $data, IStorableObject
$object) {
100 $objectTypeID = $this->getObjectType()->objectTypeID
;
101 $objectID = $object->{$object::getDatabaseTableIndexName()};
103 if (!isset(static::$loadedLabels[$objectTypeID])) {
104 static::$loadedLabels[$objectTypeID] = [];
106 if (!isset(static::$loadedLabels[$objectTypeID][$objectID])) {
107 static::$loadedLabels[$objectTypeID][$objectID] = LabelHandler
::getInstance()->getAssignedLabels(
113 $labelIDs = $this->getLabelGroup()->getLabelIDs();
114 /** @var Label $label */
115 foreach (static::$loadedLabels[$objectTypeID][$objectID] as $label) {
116 if (in_array($label->labelID
, $labelIDs)) {
117 $this->value($label->labelID
);
127 public function populate() {
130 $this->getDocument()->getDataHandler()->addProcessor(new CustomFormDataProcessor('label', function(IFormDocument
$document, array $parameters) {
131 $value = $this->getValue();
133 // `-1` and `0` are special values that are irrlevent for saving
135 if (!isset($parameters[$this->getObjectProperty()])) {
136 $parameters[$this->getObjectProperty()] = [];
139 $parameters[$this->getObjectProperty()][$this->getLabelGroup()->groupID
] = $value;
151 public function readValue() {
152 if ($this->getDocument()->hasRequestData($this->getPrefixedId())) {
153 $this->value
= intval($this->getDocument()->getRequestData($this->getPrefixedId()));
162 public function validate() {
163 if ($this->isRequired()) {
164 if ($this->value
<= 0) {
165 $this->addValidationError(new FormFieldValidationError('empty'));
168 else if ($this->value
> 0 && !in_array($this->value
, $this->getLabelGroup()->getLabelIDs())) {
169 $this->addValidationError(new FormFieldValidationError(
171 'wcf.global.form.error.noValidSelection'
177 * Returns label group fields based for the given label groups using the given object property.
179 * The id of each form fields is `{$objectProperty}{$labelGroupID}`.
181 * @param string $objectType `com.woltlab.wcf.label.object` object type
182 * @param ViewableLabelGroup[] $labelGroups label groups for which label form fields are created
183 * @param string $objectProperty object property of form fields
186 public static function createFields($objectType, array $labelGroups, $objectProperty = 'labelIDs') {
188 foreach ($labelGroups as $labelGroup) {
189 $formFields[] = static::create($objectProperty . $labelGroup->groupID
)
190 ->objectProperty($objectProperty)
191 ->objectType($objectType)
192 ->required($labelGroup->forceSelection
)
193 ->labelGroup($labelGroup);