1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File: Gallery.php |
4
|
|
|
* |
5
|
|
|
* @author Maciej Sławik <[email protected]> |
6
|
|
|
* Github: https://github.com/maciejslawik |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace MSlwk\ICatalogue\Form\Element; |
10
|
|
|
|
11
|
|
|
use Magento\Framework\Data\Form\Element\AbstractElement; |
12
|
|
|
use Magento\Framework\Escaper; |
13
|
|
|
use Magento\Framework\Data\Form\Element\Factory; |
14
|
|
|
use Magento\Framework\Data\Form\Element\CollectionFactory; |
15
|
|
|
use Magento\Framework\UrlInterface; |
16
|
|
|
use Magento\Store\Model\StoreManagerInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Gallery |
20
|
|
|
* |
21
|
|
|
* @package MSlwk\ICatalogue\Form\Element |
22
|
|
|
*/ |
23
|
|
|
class Gallery extends AbstractElement |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var StoreManagerInterface |
27
|
|
|
*/ |
28
|
|
|
private $storeManager; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Factory $factoryElement |
32
|
|
|
* @param CollectionFactory $factoryCollection |
33
|
|
|
* @param Escaper $escaper |
34
|
|
|
* @param StoreManagerInterface $storeManager |
35
|
|
|
* @param array $data |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
Factory $factoryElement, |
39
|
|
|
CollectionFactory $factoryCollection, |
40
|
|
|
Escaper $escaper, |
41
|
|
|
StoreManagerInterface $storeManager, |
42
|
|
|
$data = [] |
43
|
|
|
) { |
44
|
|
|
parent::__construct($factoryElement, $factoryCollection, $escaper, $data); |
45
|
|
|
$this->setType('file'); |
46
|
|
|
$this->storeManager = $storeManager; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
52
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable) |
53
|
|
|
*/ |
54
|
|
|
public function getElementHtml() |
55
|
|
|
{ |
56
|
|
|
$name = $this->getName(); |
|
|
|
|
57
|
|
|
$parentName = parent::getName(); |
|
|
|
|
58
|
|
|
$widgetButton = $this->getForm()->getParent()->getLayout(); |
59
|
|
|
$buttonHtml = $widgetButton->createBlock( |
60
|
|
|
'Magento\Backend\Block\Widget\Button' |
61
|
|
|
)->setData( |
62
|
|
|
['label' => __('Add new image'), 'onclick' => 'addNewImg()', 'class' => 'add'] |
63
|
|
|
)->toHtml(); |
64
|
|
|
|
65
|
|
|
$html = '<table id="gallery" class="gallery" border="0" cellspacing="3" cellpadding="0">'; |
66
|
|
|
$html .= '<thead id="gallery_thead" class="gallery"><tr>'; |
67
|
|
|
$html .= '<td class="gallery" width="200" valign="middle" align="left">' . __('Image') . '</td>'; |
68
|
|
|
$html .= '<td class="gallery" width="100" valign="middle" align="center">' . __('Sort order') . '</td>'; |
69
|
|
|
$html .= '<td class="gallery" width="100" valign="middle" align="center">' . __('Delete') . '</td>'; |
70
|
|
|
$html .= '</tr></thead>'; |
71
|
|
|
$html .= '<tfoot class="gallery"><tr>'; |
72
|
|
|
$html .= '<td class="gallery" valign="middle" align="left" colspan="3">' . $buttonHtml . '</td>'; |
73
|
|
|
$html .= '</tr></tfoot>'; |
74
|
|
|
$html .= '<tbody class="gallery">'; |
75
|
|
|
|
76
|
|
|
if (!is_null($this->getValue())) { |
77
|
|
|
$i = 0; |
78
|
|
|
foreach ($this->getValue() as $image) { |
79
|
|
|
$i++; |
80
|
|
|
$html .= '<tr>'; |
81
|
|
|
$url = $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA) . $image['image_uri']; |
82
|
|
|
$html .= '<td class="gallery" align="center" style="vertical-align:bottom;">'; |
83
|
|
|
$html .= '<a href="' . $url . '" target="_blank" '; |
84
|
|
|
$html .= 'onclick="imagePreview(\'' . $this->getHtmlId() . '_image_' . $image['image_id'] . '\');'; |
85
|
|
|
$html .= 'return false;" ' . $this->_getUiId('image-' . $image['image_id']) . '>'; |
86
|
|
|
$html .= '<img id="' . $this->getHtmlId() . '_image_' . $image['image_id'] . '" '; |
87
|
|
|
$html .= 'src="' . $url . '" height="25" align="absmiddle" class="small-image-preview">'; |
88
|
|
|
$html .= '</a><br/>'; |
89
|
|
|
$html .= '<input type="file" name="' . $name . '[' . $image['image_id'] . ']" '; |
90
|
|
|
$html .= 'size="1"' . $this->_getUiId('file') . ' ></td>'; |
91
|
|
|
$html .= '<td class="gallery" align="center" style="vertical-align:bottom;">'; |
92
|
|
|
$html .= '<input type="input" name="' . $parentName . '[position][' . $image['image_id'] . ']" '; |
93
|
|
|
$html .= 'value="' . $image['sort_order'] . '" '; |
94
|
|
|
$html .= 'id="' . $this->getHtmlId() . '_position_' . $image['image_id'] . '" '; |
95
|
|
|
$html .= 'size="3" ' . $this->_getUiId('position-' . $image['image_id']) . '/></td>'; |
96
|
|
|
$html .= '<td class="gallery" align="center" style="vertical-align:bottom;">'; |
97
|
|
|
$html .= '<input type="checkbox" name="' . $parentName . '[delete][' . $image['image_id'] . ']" '; |
98
|
|
|
$html .= 'value="' . $image['image_id'] . '" '; |
99
|
|
|
$html .= 'id="' . $this->getHtmlId() . '_delete_' . $image['image_id'] . '" '; |
100
|
|
|
$html .= $this->_getUiId('delete-button-' . $image['image_id']) . '/></td>'; |
101
|
|
|
$html .= '</tr>'; |
102
|
|
|
} |
103
|
|
|
} else { |
104
|
|
|
$html .= '<script type="text/javascript">'; |
105
|
|
|
$html .= 'document.getElementById(\'gallery_thead\').style.visibility=\'hidden\';'; |
106
|
|
|
$html .= '</script>'; |
107
|
|
|
} |
108
|
|
|
$html .= '</tbody></table>'; |
109
|
|
|
$html .= '<script language="javascript">'; |
110
|
|
|
$html .= 'id = 0;'; |
111
|
|
|
$html .= 'function addNewImg(){'; |
112
|
|
|
$html .= 'document.getElementById(\'gallery_thead\').style.visibility=\'visible\';'; |
113
|
|
|
$html .= 'id--;'; |
114
|
|
|
$html .= 'new_file_input = \'<input type="file" name="' . $name . '[\'+id+\']" size="1" />\';'; |
115
|
|
|
$html .= 'var new_row_input = document.createElement( \'input\' );'; |
116
|
|
|
$html .= 'new_row_input.type = \'text\';'; |
117
|
|
|
$html .= 'new_row_input.name = \'' . $parentName . '[position][\'+id+\']\';'; |
118
|
|
|
$html .= 'new_row_input.size = \'1\';'; |
119
|
|
|
$html .= 'new_row_input.value = \'0\';'; |
120
|
|
|
$html .= 'var new_row_button = document . createElement(\'input\');'; |
121
|
|
|
$html .= 'new_row_button.type = \'checkbox\';'; |
122
|
|
|
$html .= 'new_row_button.value = \'Delete\';'; |
123
|
|
|
$html .= 'table = document.getElementById(\'gallery\');'; |
124
|
|
|
$html .= 'noOfRows = table.rows.length;'; |
125
|
|
|
$html .= 'noOfCols = table.rows[noOfRows - 2].cells.length;'; |
126
|
|
|
$html .= 'var x = table.insertRow(noOfRows - 1);'; |
127
|
|
|
$html .= 'newCell = x.insertCell(0);'; |
128
|
|
|
$html .= 'newCell.align = \'center\';'; |
129
|
|
|
$html .= 'newCell.valign = \'middle\';'; |
130
|
|
|
$html .= 'newCell.innerHTML = new_file_input;'; |
131
|
|
|
$html .= 'newCell = x.insertCell(1);'; |
132
|
|
|
$html .= 'newCell.align = \'center\';'; |
133
|
|
|
$html .= 'newCell.valign = \'middle\';'; |
134
|
|
|
$html .= 'newCell.appendChild(new_row_input);'; |
135
|
|
|
$html .= 'newCell = x.insertCell(2);'; |
136
|
|
|
$html .= 'newCell.align = \'center\';'; |
137
|
|
|
$html .= 'newCell.valign = \'middle\';'; |
138
|
|
|
$html .= 'newCell.appendChild(new_row_button);'; |
139
|
|
|
$html .= 'new_row_button.onclick = function () {'; |
140
|
|
|
$html .= 'this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);'; |
141
|
|
|
$html .= 'return false;};}'; |
142
|
|
|
$html .= '</script>'; |
143
|
|
|
$html .= $this->getAfterElementHtml(); |
144
|
|
|
return $html; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|