[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/inc/Ui/Media/ -> Display.php (source)

   1  <?php
   2  
   3  namespace dokuwiki\Ui\Media;
   4  
   5  use dokuwiki\File\MediaFile;
   6  
   7  class Display
   8  {
   9      /** @var MediaFile */
  10      protected $mediaFile;
  11  
  12      /** @var string should IDs be shown relative to this namespace? Used in search results */
  13      protected $relativeDisplay = null;
  14  
  15      /** @var bool scroll to this file on display? */
  16      protected $scrollIntoView = false;
  17  
  18      /**
  19       * Display constructor.
  20       * @param MediaFile $mediaFile
  21       */
  22      public function __construct(MediaFile $mediaFile)
  23      {
  24          $this->mediaFile = $mediaFile;
  25      }
  26  
  27      /**
  28       * Get the HTML to display a preview image if possible, otherwise show an icon
  29       *
  30       * @param int $w bounding box width to resize pixel based images to
  31       * @param int $h bounding box height to resize pixel based images to
  32       * @return string
  33       */
  34      public function getPreviewHtml($w, $h)
  35      {
  36          if ($this->mediaFile->isImage()) {
  37              $src = ml($this->mediaFile->getId(), ['w' => $w, 'h' => $h]);
  38          } else {
  39              $src = $this->getIconUrl();
  40          }
  41  
  42          return '<img src="' . $src . '" alt="' . hsc($this->mediaFile->getDisplayName()) . '" loading="lazy" />';
  43      }
  44  
  45      /**
  46       * Return the URL to the icon for this file
  47       *
  48       * @return string
  49       */
  50      public function getIconUrl()
  51      {
  52          $link = 'lib/images/fileicons/svg/' . $this->mediaFile->getIcoClass() . '.svg';
  53          if (!file_exists(DOKU_INC . $link)) $link = 'lib/images/fileicons/svg/file.svg';
  54          return DOKU_BASE . $link;
  55      }
  56  
  57      /**
  58       * Show IDs relative to this namespace
  59       *
  60       * @param string|null $ns Use null to disable
  61       */
  62      public function relativeDisplay($ns)
  63      {
  64          $this->relativeDisplay = $ns;
  65      }
  66  
  67      /**
  68       * Scroll to this file on display?
  69       *
  70       * @param bool $set
  71       */
  72      public function scrollIntoView($set = true)
  73      {
  74          $this->scrollIntoView = $set;
  75      }
  76  
  77      /** @return string */
  78      protected function formatDate()
  79      {
  80          return dformat($this->mediaFile->getLastModified());
  81      }
  82  
  83      /**
  84       * Output the image dimension if any
  85       *
  86       * @param string $empty what to show when no dimensions are available
  87       * @return string
  88       */
  89      protected function formatDimensions($empty = '&#160;')
  90      {
  91          $w = $this->mediaFile->getWidth();
  92          $h = $this->mediaFile->getHeight();
  93          if ($w && $h) {
  94              return $w . '&#215;' . $h;
  95          } else {
  96              return $empty;
  97          }
  98      }
  99  
 100      /** @return string */
 101      protected function formatFileSize()
 102      {
 103          return filesize_h($this->mediaFile->getFileSize());
 104      }
 105  
 106      /** @return string */
 107      protected function formatDisplayName()
 108      {
 109          if ($this->relativeDisplay !== null) {
 110              $id = $this->mediaFile->getId();
 111              if (substr($id, 0, strlen($this->relativeDisplay)) == $this->relativeDisplay) {
 112                  $id = substr($id, strlen($this->relativeDisplay));
 113              }
 114              return ltrim($id, ':');
 115          } else {
 116              return $this->mediaFile->getDisplayName();
 117          }
 118      }
 119  }