Files.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Block\Adminhtml\Wysiwyg\Images\Content;
  7. /**
  8. * Directory contents block for Wysiwyg Images
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Files extends \Magento\Backend\Block\Template
  14. {
  15. /**
  16. * Files collection object
  17. *
  18. * @var \Magento\Framework\Data\Collection\Filesystem
  19. */
  20. protected $_filesCollection;
  21. /**
  22. * @var \Magento\Cms\Model\Wysiwyg\Images\Storage
  23. */
  24. protected $_imageStorage;
  25. /**
  26. * @var \Magento\Cms\Helper\Wysiwyg\Images
  27. */
  28. protected $_imageHelper;
  29. /**
  30. * @param \Magento\Backend\Block\Template\Context $context
  31. * @param \Magento\Cms\Model\Wysiwyg\Images\Storage $imageStorage
  32. * @param \Magento\Cms\Helper\Wysiwyg\Images $imageHelper
  33. * @param array $data
  34. */
  35. public function __construct(
  36. \Magento\Backend\Block\Template\Context $context,
  37. \Magento\Cms\Model\Wysiwyg\Images\Storage $imageStorage,
  38. \Magento\Cms\Helper\Wysiwyg\Images $imageHelper,
  39. array $data = []
  40. ) {
  41. $this->_imageHelper = $imageHelper;
  42. $this->_imageStorage = $imageStorage;
  43. parent::__construct($context, $data);
  44. }
  45. /**
  46. * Prepared Files collection for current directory
  47. *
  48. * @return \Magento\Framework\Data\Collection\Filesystem
  49. */
  50. public function getFiles()
  51. {
  52. if (!$this->_filesCollection) {
  53. $this->_filesCollection = $this->_imageStorage->getFilesCollection(
  54. $this->_imageHelper->getCurrentPath(),
  55. $this->_getMediaType()
  56. );
  57. }
  58. return $this->_filesCollection;
  59. }
  60. /**
  61. * Files collection count getter
  62. *
  63. * @return int
  64. */
  65. public function getFilesCount()
  66. {
  67. return $this->getFiles()->count();
  68. }
  69. /**
  70. * File identifier getter
  71. *
  72. * @param \Magento\Framework\DataObject $file
  73. * @return string
  74. */
  75. public function getFileId(\Magento\Framework\DataObject $file)
  76. {
  77. return $file->getId();
  78. }
  79. /**
  80. * File thumb URL getter
  81. *
  82. * @param \Magento\Framework\DataObject $file
  83. * @return string
  84. */
  85. public function getFileThumbUrl(\Magento\Framework\DataObject $file)
  86. {
  87. return $file->getThumbUrl();
  88. }
  89. /**
  90. * File name URL getter
  91. *
  92. * @param \Magento\Framework\DataObject $file
  93. * @return string
  94. */
  95. public function getFileName(\Magento\Framework\DataObject $file)
  96. {
  97. return $file->getName();
  98. }
  99. /**
  100. * Image file width getter
  101. *
  102. * @param \Magento\Framework\DataObject $file
  103. * @return string
  104. */
  105. public function getFileWidth(\Magento\Framework\DataObject $file)
  106. {
  107. return $file->getWidth();
  108. }
  109. /**
  110. * Image file height getter
  111. *
  112. * @param \Magento\Framework\DataObject $file
  113. * @return string
  114. */
  115. public function getFileHeight(\Magento\Framework\DataObject $file)
  116. {
  117. return $file->getHeight();
  118. }
  119. /**
  120. * File short name getter
  121. *
  122. * @param \Magento\Framework\DataObject $file
  123. * @return string
  124. */
  125. public function getFileShortName(\Magento\Framework\DataObject $file)
  126. {
  127. return $file->getShortName();
  128. }
  129. /**
  130. * Get image width
  131. *
  132. * @return int
  133. */
  134. public function getImagesWidth()
  135. {
  136. return $this->_imageStorage->getResizeWidth();
  137. }
  138. /**
  139. * Get image height
  140. *
  141. * @return int
  142. */
  143. public function getImagesHeight()
  144. {
  145. return $this->_imageStorage->getResizeHeight();
  146. }
  147. /**
  148. * Return current media type based on request or data
  149. * @return string
  150. */
  151. protected function _getMediaType()
  152. {
  153. if ($this->hasData('media_type')) {
  154. return $this->_getData('media_type');
  155. }
  156. return $this->getRequest()->getParam('type');
  157. }
  158. }