- md5
- 8a1e022313efb8cf3c87d3e4f9fce826
- sha1
- 95eb3a6eed48a44d10db52d2cf80bc9f51d4036d
- sha256
- fe6f8e2de89af2ab080e4c88521ce5b82090cb7da6deaddfda082133238e77e8
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
<?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2020 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Form\Field;
use Joomla\CMS\Language\Text;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* The Field to load the form inside current form
*
* @since 4.0.0
*/
class AccessiblemediaField extends SubformField
{
/**
* The form field type.
*
* @var string
* @since 4.0.0
*/
protected $type = 'Accessiblemedia';
/**
* The preview.
*
* @var string
* @since 4.0.0
*/
protected $preview;
/**
* The directory.
*
* @var string
* @since 4.0.0
*/
protected $directory;
/**
* The previewWidth.
*
* @var integer
* @since 4.0.0
*/
protected $previewWidth;
/**
* The previewHeight.
*
* @var integer
* @since 4.0.0
*/
protected $previewHeight;
/**
* Layout to render
*
* @var string
* @since 4.0.0
*/
protected $layout;
/**
* Media types like 'images, audios, documents, videos'
*
* @var string
* @since 6.1.0
*/
protected $types;
/**
* Method to get certain otherwise inaccessible properties from the form field object.
*
* @param string $name The property name for which to get the value.
*
* @return mixed The property value or null.
*
* @since 4.0.0
*/
public function __get($name)
{
switch ($name) {
case 'directory':
case 'preview':
case 'previewHeight':
case 'previewWidth':
case 'types':
return $this->$name;
}
return parent::__get($name);
}
/**
* Method to set certain otherwise inaccessible properties of the form field object.
*
* @param string $name The property name for which to set the value.
* @param mixed $value The value of the property.
*
* @return void
*
* @since 4.0.0
*/
public function __set($name, $value)
{
switch ($name) {
case 'directory':
case 'preview':
case 'types':
$this->$name = (string) $value;
break;
case 'previewHeight':
case 'previewWidth':
$this->$name = (int) $value;
break;
default:
parent::__set($name, $value);
}
}
/**
* Method to attach a Form object to the field.
*
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value.
*
* @return boolean True on success.
*
* @since 4.0.0
*/
public function setup(\SimpleXMLElement $element, $value, $group = null)
{
/**
* When you have subforms which are not repeatable (i.e. a subform custom field with the
* repeat attribute set to 0) you get an array here since the data comes from decoding the
* JSON into an associative array, including the media subfield's data.
*
* However, this method expects an object or a string, not an array. Typecasting the array
* to an object solves the data format discrepancy.
*/
$value = \is_array($value) ? (object) $value : $value;
/**
* If the value is not a string, it is
* most likely within a custom field of type subform
* and the value is a stdClass with properties
* imagefile and alt_text. So it is fine.
*/
if (\is_string($value)) {
json_decode($value);
// Check if value is a valid JSON string.
if ($value !== '' && json_last_error() !== JSON_ERROR_NONE) {
/**
* If the value is not empty and is not a valid JSON string,
* it is most likely a custom field created in Joomla 3 and
* the value is a string that contains the file name.
*/
if (is_file(JPATH_ROOT . '/' . $value)) {
$value = '{"imagefile":"' . $value . '","alt_text":""}';
} else {
$value = '';
}
}
} elseif (!\is_object($value)) {
return false;
} else {
$mediaTypes = explode(',', (string) ($element['types'] ?? 'images'));
$requiredProperties = \in_array('images', $mediaTypes, true) ? ['imagefile', 'alt_text'] : ['file'];
foreach ($requiredProperties as $property) {
if (!property_exists($value, $property)) {
return false;
}
}
}
if (!parent::setup($element, $value, $group)) {
return false;
}
$this->directory = (string)$this->element['directory'];
$this->preview = (string)$this->element['preview'];
$this->previewHeight = isset($this->element['preview_height']) ? (int)$this->element['preview_height'] : 200;
$this->previewWidth = isset($this->element['preview_width']) ? (int)$this->element['preview_width'] : 200;
$this->types = isset($this->element['types']) ? (string)$this->element['types'] : 'images';
$mediaTypes = explode(',', $this->types);
$fieldName = (\in_array('images', $mediaTypes)) ? 'imagefile' : 'file';
// Build the form source
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset
name="accessiblemedia"
label="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_LABEL"
>
<field
name="$fieldName"
type="media"
label="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_IMAGEFILE_LABEL"
directory="$this->directory"
preview="$this->preview"
preview_width="$this->previewWidth"
preview_height="$this->previewHeight"
types="$this->types"
schemes="http,https,ftp,ftps,data,file"
validate="url"
relative="true"
/>
XML;
if (\in_array('images', $mediaTypes)) {
$xml .= <<<XML
<field
name="alt_text"
type="text"
label="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_TEXT_LABEL"
/>
<field
name="alt_empty"
type="checkbox"
label="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_EMPTY_LABEL"
description="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_EMPTY_DESC"
/>
XML;
}
if (\in_array('videos', $mediaTypes) && $this->element['video_poster'] == 1) {
$xml .= <<<XML
<field
name="poster"
type="media"
label="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_VIDEO_POSTER_LABEL"
directory="$this->directory"
preview="$this->preview"
preview_width="$this->previewWidth"
preview_height="$this->previewHeight"
types="images"
schemes="http,https,ftp,ftps,data,file"
validate="url"
relative="true"
/>
XML;
}
if (\in_array('documents', $mediaTypes)) {
$xml .= <<<XML
<field
name="linktext"
type="text"
label="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_LINKTEXT_LABEL"
description="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_LINKTEXT_DESC"
filter="string"
hint="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_LINKTEXT_DEFAULT_VALUE"
/>
XML;
}
$xml .= <<<XML
</fieldset>
</form>
XML;
$this->formsource = $xml;
$this->layout = 'joomla.form.field.media.accessiblemedia';
return true;
}
/**
* Method to get the field input markup.
*
* @return string The field input markup.
*
* @since 6.1.0
*/
protected function getInput()
{
$subForm = $this->loadSubForm();
$mediaTypes = explode(',', $this->types);
$label = '';
if (\count($mediaTypes) > 1) {
$labels = [];
foreach ($mediaTypes as $type) {
$const = 'JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_MEDIA_TYPE_' . strtoupper($type);
$labels[] = Text::_($const);
}
if (!empty($labels)) {
$label = Text::sprintf('JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_LABEL', implode(', ', $labels));
}
} else {
$label = Text::_('JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_MEDIA_TYPE_' . strtoupper($mediaTypes[0]));
}
$fieldName = (\in_array('images', $mediaTypes)) ? 'imagefile' : 'file';
if (!empty($label) && $subForm->getField($fieldName)) {
$subForm->setFieldAttribute($fieldName, 'label', $label);
}
if ($subForm && $subForm->getField('linktext')) {
$subForm->setFieldAttribute('linktext', 'default', Text::_('JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_LINKTEXT_DEFAULT_VALUE'));
}
$this->form = $subForm;
return parent::getInput();
}
}
The same content is in 18 releases, Joomla 6.1.2-rc1 to 6.2.1-rc1. See them all, or every version of this file.