PageRenderTime 29ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/module-customer/Block/Adminhtml/Form/Element/File.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 224 lines | 128 code | 18 blank | 78 comment | 10 complexity | 69c001fa72367211929aba8cc9c96757 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Adminhtml\Form\Element;
  7. /**
  8. * Customer Widget Form File Element Block
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class File extends \Magento\Framework\Data\Form\Element\AbstractElement
  13. {
  14. /**
  15. * @var \Magento\Framework\View\Asset\Repository
  16. */
  17. protected $_assetRepo;
  18. /**
  19. * Adminhtml data
  20. *
  21. * @var \Magento\Backend\Helper\Data
  22. */
  23. protected $_adminhtmlData = null;
  24. /**
  25. * @var \Magento\Framework\Url\EncoderInterface
  26. */
  27. protected $urlEncoder;
  28. /**
  29. * @param \Magento\Framework\Data\Form\Element\Factory $factoryElement
  30. * @param \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection
  31. * @param \Magento\Framework\Escaper $escaper
  32. * @param \Magento\Backend\Helper\Data $adminhtmlData
  33. * @param \Magento\Framework\View\Asset\Repository $assetRepo
  34. * @param \Magento\Framework\Url\EncoderInterface $urlEncoder
  35. * @param array $data
  36. */
  37. public function __construct(
  38. \Magento\Framework\Data\Form\Element\Factory $factoryElement,
  39. \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection,
  40. \Magento\Framework\Escaper $escaper,
  41. \Magento\Backend\Helper\Data $adminhtmlData,
  42. \Magento\Framework\View\Asset\Repository $assetRepo,
  43. \Magento\Framework\Url\EncoderInterface $urlEncoder,
  44. $data = []
  45. ) {
  46. $this->_adminhtmlData = $adminhtmlData;
  47. $this->_assetRepo = $assetRepo;
  48. $this->urlEncoder = $urlEncoder;
  49. parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
  50. $this->setType('file');
  51. }
  52. /**
  53. * Return Form Element HTML
  54. *
  55. * @return string
  56. */
  57. public function getElementHtml()
  58. {
  59. $this->addClass('input-file');
  60. if ($this->getRequired()) {
  61. $this->removeClass('required-entry');
  62. $this->addClass('required-file');
  63. }
  64. $element = sprintf(
  65. '<input id="%s" name="%s" %s />%s%s',
  66. $this->getHtmlId(),
  67. $this->getName(),
  68. $this->serialize($this->getHtmlAttributes()),
  69. $this->getAfterElementHtml(),
  70. $this->_getHiddenInput()
  71. );
  72. return $this->_getPreviewHtml() . $element . $this->_getDeleteCheckboxHtml();
  73. }
  74. /**
  75. * Return Delete File CheckBox HTML
  76. *
  77. * @return string
  78. */
  79. protected function _getDeleteCheckboxHtml()
  80. {
  81. $html = '';
  82. if ($this->getValue() && !$this->getRequired() && !is_array($this->getValue())) {
  83. $checkboxId = sprintf('%s_delete', $this->getHtmlId());
  84. $checkbox = [
  85. 'type' => 'checkbox',
  86. 'name' => sprintf('%s[delete]', $this->getName()),
  87. 'value' => '1',
  88. 'class' => 'checkbox',
  89. 'id' => $checkboxId
  90. ];
  91. $label = ['for' => $checkboxId];
  92. if ($this->getDisabled()) {
  93. $checkbox['disabled'] = 'disabled';
  94. $label['class'] = 'disabled';
  95. }
  96. $html .= '<span class="' . $this->_getDeleteCheckboxSpanClass() . '">';
  97. $html .= $this->_drawElementHtml('input', $checkbox) . ' ';
  98. $html .= $this->_drawElementHtml('label', $label, false) . $this->_getDeleteCheckboxLabel() . '</label>';
  99. $html .= '</span>';
  100. }
  101. return $html;
  102. }
  103. /**
  104. * Return Delete CheckBox SPAN Class name
  105. *
  106. * @return string
  107. */
  108. protected function _getDeleteCheckboxSpanClass()
  109. {
  110. return 'delete-file';
  111. }
  112. /**
  113. * Return Delete CheckBox Label
  114. *
  115. * @return \Magento\Framework\Phrase
  116. */
  117. protected function _getDeleteCheckboxLabel()
  118. {
  119. return __('Delete File');
  120. }
  121. /**
  122. * Return File preview link HTML
  123. *
  124. * @return string
  125. */
  126. protected function _getPreviewHtml()
  127. {
  128. $html = '';
  129. if ($this->getValue() && !is_array($this->getValue())) {
  130. $image = [
  131. 'alt' => __('Download'),
  132. 'title' => __('Download'),
  133. 'src' => $this->_assetRepo->getUrl('images/fam_bullet_disk.gif'),
  134. 'class' => 'v-middle'
  135. ];
  136. $url = $this->_getPreviewUrl();
  137. $html .= '<span>';
  138. $html .= '<a href="' . $url . '">' . $this->_drawElementHtml('img', $image) . '</a> ';
  139. $html .= '<a href="' . $url . '">' . __('Download') . '</a>';
  140. $html .= '</span>';
  141. }
  142. return $html;
  143. }
  144. /**
  145. * Return Hidden element with current value
  146. *
  147. * @return string
  148. */
  149. protected function _getHiddenInput()
  150. {
  151. return $this->_drawElementHtml(
  152. 'input',
  153. [
  154. 'type' => 'hidden',
  155. 'name' => sprintf('%s[value]', $this->getName()),
  156. 'id' => sprintf('%s_value', $this->getHtmlId()),
  157. 'value' => $this->getEscapedValue()
  158. ]
  159. );
  160. }
  161. /**
  162. * Return Preview/Download URL
  163. *
  164. * @return string
  165. */
  166. protected function _getPreviewUrl()
  167. {
  168. return $this->_adminhtmlData->getUrl(
  169. 'customer/index/viewfile',
  170. ['file' => $this->urlEncoder->encode($this->getValue())]
  171. );
  172. }
  173. /**
  174. * Return Element HTML
  175. *
  176. * @param string $element
  177. * @param array $attributes
  178. * @param bool $closed
  179. * @return string
  180. */
  181. protected function _drawElementHtml($element, array $attributes, $closed = true)
  182. {
  183. $parts = [];
  184. foreach ($attributes as $k => $v) {
  185. $parts[] = sprintf('%s="%s"', $k, $v);
  186. }
  187. return sprintf('<%s %s%s>', $element, implode(' ', $parts), $closed ? ' /' : '');
  188. }
  189. /**
  190. * Return escaped value
  191. *
  192. * @param int|null $index
  193. * @return string|false
  194. */
  195. public function getEscapedValue($index = null)
  196. {
  197. if (is_array($this->getValue())) {
  198. return false;
  199. }
  200. $value = $this->getValue();
  201. if (is_array($value) && $index === null) {
  202. $index = 'value';
  203. }
  204. return parent::getEscapedValue($index);
  205. }
  206. }