From 8a6c166774e088c9b3d32f63843552b58b457390 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sun, 7 Apr 2019 17:44:47 +0200 Subject: [PATCH] Add single media selection form field See #2509 --- .../__singleMediaSelectionFormField.tpl | 41 ++++++ syncTemplates.json | 1 + .../__singleMediaSelectionFormField.tpl | 41 ++++++ .../SingleMediaSelectionFormField.class.php | 121 ++++++++++++++++++ wcfsetup/install/lang/de.xml | 1 + wcfsetup/install/lang/en.xml | 1 + 6 files changed, 206 insertions(+) create mode 100644 com.woltlab.wcf/templates/__singleMediaSelectionFormField.tpl create mode 100644 wcfsetup/install/files/acp/templates/__singleMediaSelectionFormField.tpl create mode 100644 wcfsetup/install/files/lib/system/form/builder/field/media/SingleMediaSelectionFormField.class.php diff --git a/com.woltlab.wcf/templates/__singleMediaSelectionFormField.tpl b/com.woltlab.wcf/templates/__singleMediaSelectionFormField.tpl new file mode 100644 index 0000000000..e48f0a0052 --- /dev/null +++ b/com.woltlab.wcf/templates/__singleMediaSelectionFormField.tpl @@ -0,0 +1,41 @@ +{include file='__formFieldHeader'} + +{if $field->isImmutable() && $field->getValue()} + {if $field->getMedia()->isImage && $field->getMedia()->hasThumbnail('small')} +
+ {@$field->getMedia()->getThumbnailTag('small')} +
+ {else} +
+ {@$field->getMedia()->getElementTag(16)} + +

{$field->getMedia()->getTitle()}

+
+ {/if} +{else} + {if $field->isImageOnly()} +
+ {if $field->getValue() && $field->getMedia()->hasThumbnail('small')} + {@$field->getMedia()->getThumbnailTag('small')} + {/if} +
+ {/if} +

isImageOnly()} data-display="{@$field->getPrefixedId()}_preview"{/if}>{lang}wcf.media.choose{if $field->isImageOnly()}Image{else}File{/if}{/lang}

+ getValue()} value="{@$field->getValue()}"{/if}> + + +{/if} + +{include file='__formFieldFooter'} diff --git a/syncTemplates.json b/syncTemplates.json index f213ae4655..f1eb2bcb31 100644 --- a/syncTemplates.json +++ b/syncTemplates.json @@ -31,6 +31,7 @@ "__pollOptionsFormField", "__radioButtonFormField", "__ratingFormField", + "__singleMediaSelectionFormField", "__singleSelectionFormField", "__tabFormContainer", "__tabMenuFormContainer", diff --git a/wcfsetup/install/files/acp/templates/__singleMediaSelectionFormField.tpl b/wcfsetup/install/files/acp/templates/__singleMediaSelectionFormField.tpl new file mode 100644 index 0000000000..e48f0a0052 --- /dev/null +++ b/wcfsetup/install/files/acp/templates/__singleMediaSelectionFormField.tpl @@ -0,0 +1,41 @@ +{include file='__formFieldHeader'} + +{if $field->isImmutable() && $field->getValue()} + {if $field->getMedia()->isImage && $field->getMedia()->hasThumbnail('small')} +
+ {@$field->getMedia()->getThumbnailTag('small')} +
+ {else} +
+ {@$field->getMedia()->getElementTag(16)} + +

{$field->getMedia()->getTitle()}

+
+ {/if} +{else} + {if $field->isImageOnly()} +
+ {if $field->getValue() && $field->getMedia()->hasThumbnail('small')} + {@$field->getMedia()->getThumbnailTag('small')} + {/if} +
+ {/if} +

isImageOnly()} data-display="{@$field->getPrefixedId()}_preview"{/if}>{lang}wcf.media.choose{if $field->isImageOnly()}Image{else}File{/if}{/lang}

+ getValue()} value="{@$field->getValue()}"{/if}> + + +{/if} + +{include file='__formFieldFooter'} diff --git a/wcfsetup/install/files/lib/system/form/builder/field/media/SingleMediaSelectionFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/media/SingleMediaSelectionFormField.class.php new file mode 100644 index 0000000000..5a2833fafa --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/field/media/SingleMediaSelectionFormField.class.php @@ -0,0 +1,121 @@ + + * @package WoltLabSuite\Core\System\Form\Builder\Field + * @since 5.2 + */ +class SingleMediaSelectionFormField extends AbstractFormField implements IImmutableFormField { + use TImmutableFormField; + + /** + * is `true` if only images can be selected and `false` otherwise + * @var boolean + */ + protected $imageOnly = false; + + /** + * media object with the current value as id + * @var null|ViewableMedia + */ + protected $media; + + /** + * @inheritDoc + */ + protected $javaScriptDataHandlerModule = 'WoltLabSuite/Core/Form/Builder/Field/Value'; + + /** + * @inheritDoc + */ + protected $templateName = '__singleMediaSelectionFormField'; + + /** + * Returns the media object with the current value as id. + * + * @return ViewableMedia + * + * @throws \InvalidArgumentException if no or an invalid media id is set as value + */ + public function getMedia() { + if ($this->media === null) { + if (!$this->getValue()) { + throw new \BadMethodCallException("Cannot be media object if no valid media id is set as value."); + } + + $this->media = ViewableMedia::getMedia($this->getValue()); + if ($this->media === null) { + throw new \UnexpectedValueException("Cannot load media with id '{$this->getValue()}'."); + } + } + + return $this->media; + } + + /** + * Sets if only images can be selected and returns this field. + * + * @param boolean $imageOnly + * @return static this field + */ + public function imageOnly($imageOnly = true) { + $this->imageOnly = $imageOnly; + + return $this; + } + + /** + * Returns `true` if only images can be selected and `false` otherwise. + * + * By default, all images can be selected. + * + * @return boolean + */ + public function isImageOnly() { + return $this->imageOnly; + } + + /** + * @inheritDoc + */ + public function readValue() { + if ($this->getDocument()->hasRequestData($this->getPrefixedId())) { + $value = $this->getDocument()->getRequestData($this->getPrefixedId()); + + if ($value) { + $this->value = $value; + } + } + + return $this; + } + + /** + * @inheritDoc + */ + public function validate() { + parent::validate(); + + try { + $media = $this->getMedia(); + if (!$media->isAccessible() || ($this->isImageOnly() && !$media->isImage)) { + $this->value = null; + } + } + catch (\BadMethodCallException $e) { + $this->value = null; + } + catch (\UnexpectedValueException $e) { + $this->value = null; + } + } +} diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index d69c0fb505..04432fdb2a 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -3979,6 +3979,7 @@ Dateianhänge: + {$title} wirklich löschen?]]> diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index 477dd695b2..4ec7a031d7 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -3925,6 +3925,7 @@ Attachments: + {$title}?]]> -- 2.20.1