123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * Category form input image element
- *
- * @author Magento Core Team <core@magentocommerce.com>
- */
- namespace Magento\Framework\Data\Form\Element;
- use Magento\Framework\UrlInterface;
- class Image extends \Magento\Framework\Data\Form\Element\AbstractElement
- {
- /**
- * @var UrlInterface
- */
- protected $_urlBuilder;
- /**
- * @param \Magento\Framework\Data\Form\Element\Factory $factoryElement
- * @param \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection
- * @param \Magento\Framework\Escaper $escaper
- * @param UrlInterface $urlBuilder
- * @param array $data
- */
- public function __construct(
- \Magento\Framework\Data\Form\Element\Factory $factoryElement,
- \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection,
- \Magento\Framework\Escaper $escaper,
- UrlInterface $urlBuilder,
- $data = []
- ) {
- $this->_urlBuilder = $urlBuilder;
- parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
- $this->setType('file');
- }
- /**
- * Return element html code
- *
- * @return string
- */
- public function getElementHtml()
- {
- $html = '';
- if ((string)$this->getValue()) {
- $url = $this->_getUrl();
- if (!preg_match("/^http\:\/\/|https\:\/\//", $url)) {
- $url = $this->_urlBuilder->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]) . $url;
- }
- $html = '<a href="' .
- $url .
- '"' .
- ' onclick="imagePreview(\'' .
- $this->getHtmlId() .
- '_image\'); return false;" ' .
- $this->_getUiId(
- 'link'
- ) .
- '>' .
- '<img src="' .
- $url .
- '" id="' .
- $this->getHtmlId() .
- '_image" title="' .
- $this->getValue() .
- '"' .
- ' alt="' .
- $this->getValue() .
- '" height="22" width="22" class="small-image-preview v-middle" ' .
- $this->_getUiId() .
- ' />' .
- '</a> ';
- }
- $this->setClass('input-file');
- $html .= parent::getElementHtml();
- $html .= $this->_getDeleteCheckbox();
- return $html;
- }
- /**
- * Return html code of delete checkbox element
- *
- * @return string
- */
- protected function _getDeleteCheckbox()
- {
- $html = '';
- if ($this->getValue()) {
- $label = (string)new \Magento\Framework\Phrase('Delete Image');
- $html .= '<span class="delete-image">';
- $html .= '<input type="checkbox"' .
- ' name="' .
- parent::getName() .
- '[delete]" value="1" class="checkbox"' .
- ' id="' .
- $this->getHtmlId() .
- '_delete"' .
- ($this->getDisabled() ? ' disabled="disabled"' : '') .
- '/>';
- $html .= '<label for="' .
- $this->getHtmlId() .
- '_delete"' .
- ($this->getDisabled() ? ' class="disabled"' : '') .
- '> ' .
- $label .
- '</label>';
- $html .= $this->_getHiddenInput();
- $html .= '</span>';
- }
- return $html;
- }
- /**
- * Return html code of hidden element
- *
- * @return string
- */
- protected function _getHiddenInput()
- {
- return '<input type="hidden" name="' . parent::getName() . '[value]" value="' . $this->getValue() . '" />';
- }
- /**
- * Get image preview url
- *
- * @return string
- */
- protected function _getUrl()
- {
- return $this->getValue();
- }
- /**
- * Return name
- *
- * @return string
- */
- public function getName()
- {
- return $this->getData('name');
- }
- }
|