[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> MediaField.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Form\Field;
  11  
  12  use Joomla\CMS\Component\ComponentHelper;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\FormField;
  15  use Joomla\CMS\Helper\MediaHelper;
  16  use Joomla\CMS\Uri\Uri;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('JPATH_PLATFORM') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Provides a modal media selector including upload mechanism
  24   *
  25   * @since  1.6
  26   */
  27  class MediaField extends FormField
  28  {
  29      /**
  30       * The form field type.
  31       *
  32       * @var    string
  33       * @since  1.6
  34       */
  35      protected $type = 'Media';
  36  
  37      /**
  38       * The authorField.
  39       *
  40       * @var    string
  41       * @since  3.2
  42       */
  43      protected $authorField;
  44  
  45      /**
  46       * The asset.
  47       *
  48       * @var    string
  49       * @since  3.2
  50       */
  51      protected $asset;
  52  
  53      /**
  54       * The link.
  55       *
  56       * @var    string
  57       * @since  3.2
  58       */
  59      protected $link;
  60  
  61      /**
  62       * Modal width.
  63       *
  64       * @var    integer
  65       * @since  3.4.5
  66       */
  67      protected $width;
  68  
  69      /**
  70       * Modal height.
  71       *
  72       * @var    integer
  73       * @since  3.4.5
  74       */
  75      protected $height;
  76  
  77      /**
  78       * The preview.
  79       *
  80       * @var    string
  81       * @since  3.2
  82       */
  83      protected $preview;
  84  
  85      /**
  86       * The directory.
  87       *
  88       * @var    string
  89       * @since  3.2
  90       */
  91      protected $directory;
  92  
  93      /**
  94       * The previewWidth.
  95       *
  96       * @var    integer
  97       * @since  3.2
  98       */
  99      protected $previewWidth;
 100  
 101      /**
 102       * The previewHeight.
 103       *
 104       * @var    integer
 105       * @since  3.2
 106       */
 107      protected $previewHeight;
 108  
 109      /**
 110       * Comma separated types of files for Media Manager
 111       * Possible values: images,audios,videos,documents
 112       *
 113       * @var    string
 114       * @since  4.0.0
 115       */
 116      protected $types;
 117  
 118      /**
 119       * Layout to render
 120       *
 121       * @var    string
 122       * @since  3.5
 123       */
 124      protected $layout = 'joomla.form.field.media';
 125  
 126      /**
 127       * The parent class of the field
 128       *
 129       * @var  string
 130       * @since 4.0.0
 131       */
 132      protected $parentclass;
 133  
 134      /**
 135       * Method to get certain otherwise inaccessible properties from the form field object.
 136       *
 137       * @param   string  $name  The property name for which to get the value.
 138       *
 139       * @return  mixed  The property value or null.
 140       *
 141       * @since   3.2
 142       */
 143      public function __get($name)
 144      {
 145          switch ($name) {
 146              case 'authorField':
 147              case 'asset':
 148              case 'link':
 149              case 'width':
 150              case 'height':
 151              case 'preview':
 152              case 'directory':
 153              case 'previewWidth':
 154              case 'previewHeight':
 155              case 'types':
 156                  return $this->$name;
 157          }
 158  
 159          return parent::__get($name);
 160      }
 161  
 162      /**
 163       * Method to set certain otherwise inaccessible properties of the form field object.
 164       *
 165       * @param   string  $name   The property name for which to set the value.
 166       * @param   mixed   $value  The value of the property.
 167       *
 168       * @return  void
 169       *
 170       * @since   3.2
 171       */
 172      public function __set($name, $value)
 173      {
 174          switch ($name) {
 175              case 'authorField':
 176              case 'asset':
 177              case 'link':
 178              case 'width':
 179              case 'height':
 180              case 'preview':
 181              case 'directory':
 182              case 'types':
 183                  $this->$name = (string) $value;
 184                  break;
 185  
 186              case 'previewWidth':
 187              case 'previewHeight':
 188                  $this->$name = (int) $value;
 189                  break;
 190  
 191              default:
 192                  parent::__set($name, $value);
 193          }
 194      }
 195  
 196      /**
 197       * Method to attach a Form object to the field.
 198       *
 199       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 200       * @param   mixed              $value    The form field value to validate.
 201       * @param   string             $group    The field name group control value. This acts as an array container for the field.
 202       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
 203       *                                       full field name would end up being "bar[foo]".
 204       *
 205       * @return  boolean  True on success.
 206       *
 207       * @see     FormField::setup()
 208       * @since   3.2
 209       */
 210      public function setup(\SimpleXMLElement $element, $value, $group = null)
 211      {
 212          $result = parent::setup($element, $value, $group);
 213  
 214          if ($result === true) {
 215              $assetField = $this->element['asset_field'] ? (string) $this->element['asset_field'] : 'asset_id';
 216  
 217              $this->authorField   = $this->element['created_by_field'] ? (string) $this->element['created_by_field'] : 'created_by';
 218              $this->asset         = $this->form->getValue($assetField) ?: (string) $this->element['asset_id'];
 219              $this->link          = (string) $this->element['link'];
 220              $this->width         = isset($this->element['width']) ? (int) $this->element['width'] : 800;
 221              $this->height        = isset($this->element['height']) ? (int) $this->element['height'] : 500;
 222              $this->preview       = (string) $this->element['preview'];
 223              $this->directory     = (string) $this->element['directory'];
 224              $this->previewWidth  = isset($this->element['preview_width']) ? (int) $this->element['preview_width'] : 200;
 225              $this->previewHeight = isset($this->element['preview_height']) ? (int) $this->element['preview_height'] : 200;
 226              $this->types         = isset($this->element['types']) ? (string) $this->element['types'] : 'images';
 227          }
 228  
 229          return $result;
 230      }
 231  
 232      /**
 233       * Method to get the field input markup for a media selector.
 234       * Use attributes to identify specific created_by and asset_id fields
 235       *
 236       * @return  string  The field input markup.
 237       *
 238       * @since   1.6
 239       */
 240      protected function getInput()
 241      {
 242          if (empty($this->layout)) {
 243              throw new \UnexpectedValueException(sprintf('%s has no layout assigned.', $this->name));
 244          }
 245  
 246          return $this->getRenderer($this->layout)->render($this->getLayoutData());
 247      }
 248  
 249      /**
 250       * Get the data that is going to be passed to the layout
 251       *
 252       * @return  array
 253       */
 254      public function getLayoutData()
 255      {
 256          // Get the basic field data
 257          $data = parent::getLayoutData();
 258  
 259          $asset = $this->asset;
 260  
 261          if ($asset === '') {
 262              $asset = Factory::getApplication()->input->get('option');
 263          }
 264  
 265          // Value in new format such as images/headers/blue-flower.jpg#joomlaImage://local-images/headers/blue-flower.jpg?width=700&height=180
 266          if ($this->value && strpos($this->value, '#') !== false) {
 267              $uri     = new Uri(explode('#', $this->value)[1]);
 268              $adapter = $uri->getHost();
 269              $path    = $uri->getPath();
 270  
 271              // Remove filename from stored path to get the path to the folder which file is stored
 272              $pos = strrpos($path, '/');
 273  
 274              if ($pos !== false) {
 275                  $path = substr($path, 0, $pos);
 276              }
 277  
 278              if ($path === '') {
 279                  $path = '/';
 280              }
 281  
 282              $this->folder = $adapter . ':' . $path;
 283          } elseif ($this->value && is_file(JPATH_ROOT . '/' . $this->value)) {
 284              /**
 285               * Local image, for example images/sampledata/cassiopeia/nasa2-640.jpg . We need to validate and make sure
 286               * the top level folder is one of the directory configured in filesystem local plugin to avoid error message
 287               * displayed in manage when users click on Select button to select a new image
 288               */
 289              $paths = explode('/', $this->value);
 290  
 291              // Remove filename from $paths array
 292              array_pop($paths);
 293  
 294              if (MediaHelper::isValidLocalDirectory($paths[0])) {
 295                  $adapterName  = array_shift($paths);
 296                  $this->folder = 'local-' . $adapterName . ':/' . implode('/', $paths);
 297              }
 298          } elseif ($this->directory && is_dir(JPATH_ROOT . '/' . ComponentHelper::getParams('com_media')->get('image_path', 'images') . '/' . $this->directory)) {
 299              /**
 300               * This is the case where a folder is configured in directory attribute of the form field. The directory needs
 301               * to be a relative folder of the folder configured in Path to Images Folder config option of Media component.
 302               * Same with a already stored local image above, we need to validate and make sure top level folder is one of the directory
 303               * configured in filesystem local plugin
 304               */
 305              $path  = ComponentHelper::getParams('com_media')->get('image_path', 'images') . '/' . $this->directory;
 306              $paths = explode('/', $path);
 307  
 308              if (MediaHelper::isValidLocalDirectory($paths[0])) {
 309                  $adapterName  = array_shift($paths);
 310                  $this->folder = 'local-' . $adapterName . ':/' . implode('/', $paths);
 311              }
 312          } elseif ($this->directory && strpos($this->directory, ':')) {
 313              /**
 314               * Directory contains adapter information and path, for example via programming or directly defined in xml
 315               * via directory attribute
 316               */
 317              $this->folder = $this->directory;
 318          } else {
 319              $this->folder = '';
 320          }
 321  
 322          $mediaTypes   = array_map('trim', explode(',', $this->types));
 323          $types        = [];
 324          $imagesExt    = array_map(
 325              'trim',
 326              explode(
 327                  ',',
 328                  ComponentHelper::getParams('com_media')->get(
 329                      'image_extensions',
 330                      'bmp,gif,jpg,jpeg,png,webp'
 331                  )
 332              )
 333          );
 334          $audiosExt = array_map(
 335              'trim',
 336              explode(
 337                  ',',
 338                  ComponentHelper::getParams('com_media')->get(
 339                      'audio_extensions',
 340                      'mp3,m4a,mp4a,ogg'
 341                  )
 342              )
 343          );
 344          $videosExt = array_map(
 345              'trim',
 346              explode(
 347                  ',',
 348                  ComponentHelper::getParams('com_media')->get(
 349                      'video_extensions',
 350                      'mp4,mp4v,mpeg,mov,webm'
 351                  )
 352              )
 353          );
 354          $documentsExt = array_map(
 355              'trim',
 356              explode(
 357                  ',',
 358                  ComponentHelper::getParams('com_media')->get(
 359                      'doc_extensions',
 360                      'doc,odg,odp,ods,odt,pdf,ppt,txt,xcf,xls,csv'
 361                  )
 362              )
 363          );
 364  
 365          $imagesAllowedExt    = [];
 366          $audiosAllowedExt    = [];
 367          $videosAllowedExt    = [];
 368          $documentsAllowedExt = [];
 369  
 370          // Cleanup the media types
 371          array_map(
 372              function ($mediaType) use (&$types, &$imagesAllowedExt, &$audiosAllowedExt, &$videosAllowedExt, &$documentsAllowedExt, $imagesExt, $audiosExt, $videosExt, $documentsExt) {
 373                  switch ($mediaType) {
 374                      case 'images':
 375                          $types[] = '0';
 376                          $imagesAllowedExt = $imagesExt;
 377                          break;
 378                      case 'audios':
 379                          $types[] = '1';
 380                          $audiosAllowedExt = $audiosExt;
 381                          break;
 382                      case 'videos':
 383                          $types[] = '2';
 384                          $videosAllowedExt = $videosExt;
 385                          break;
 386                      case 'documents':
 387                          $types[] = '3';
 388                          $documentsAllowedExt = $documentsExt;
 389                          break;
 390                      default:
 391                          break;
 392                  }
 393              },
 394              $mediaTypes
 395          );
 396  
 397          sort($types);
 398  
 399          $extraData = array(
 400              'asset'               => $asset,
 401              'authorField'         => $this->authorField,
 402              'authorId'            => $this->form->getValue($this->authorField),
 403              'folder'              => $this->folder,
 404              'link'                => $this->link,
 405              'preview'             => $this->preview,
 406              'previewHeight'       => $this->previewHeight,
 407              'previewWidth'        => $this->previewWidth,
 408              'mediaTypes'          => implode(',', $types),
 409              'imagesExt'           => $imagesExt,
 410              'audiosExt'           => $audiosExt,
 411              'videosExt'           => $videosExt,
 412              'documentsExt'        => $documentsExt,
 413              'imagesAllowedExt'    => $imagesAllowedExt,
 414              'audiosAllowedExt'    => $audiosAllowedExt,
 415              'videosAllowedExt'    => $videosAllowedExt,
 416              'documentsAllowedExt' => $documentsAllowedExt,
 417          );
 418  
 419          return array_merge($data, $extraData);
 420      }
 421  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer