Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Gallery.php
Go to the documentation of this file.
1 <?php
13 
15 
16 class Gallery extends AbstractElement
17 {
24  public function __construct(
25  Factory $factoryElement,
26  CollectionFactory $factoryCollection,
27  Escaper $escaper,
28  $data = []
29  ) {
30  parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
31  $this->setType('file');
32  }
33 
39  public function getElementHtml()
40  {
41  $gallery = $this->getValue();
42 
43  $html = '<table id="gallery" class="gallery" border="0" cellspacing="3" cellpadding="0">';
44  $html .= '<thead id="gallery_thead" class="gallery">' .
45  '<tr class="gallery">' .
46  '<td class="gallery" valign="middle" align="center">Big Image</td>' .
47  '<td class="gallery" valign="middle" align="center">Thumbnail</td>' .
48  '<td class="gallery" valign="middle" align="center">Small Thumb</td>' .
49  '<td class="gallery" valign="middle" align="center">Sort Order</td>' .
50  '<td class="gallery" valign="middle" align="center">Delete</td>' .
51  '</tr>'.
52  '</thead>';
53  $widgetButton = $this->getForm()->getParent()->getLayout();
54  $buttonHtml = $widgetButton->createBlock(
55  \Magento\Backend\Block\Widget\Button::class
56  )->setData(
57  ['label' => 'Add New Image', 'onclick' => 'addNewImg()', 'class' => 'add']
58  )->toHtml();
59 
60  $html .= '<tfoot class="gallery">';
61  $html .= '<tr class="gallery">';
62  $html .= '<td class="gallery" valign="middle" align="left" colspan="5">' . $buttonHtml . '</td>';
63  $html .= '</tr>';
64  $html .= '</tfoot>';
65 
66  $html .= '<tbody class="gallery">';
67 
68  $i = 0;
69  if ($this->getValue() !== null) {
70  foreach ($this->getValue() as $image) {
71  $i++;
72  $html .= '<tr class="gallery">';
73  foreach ($this->getValue()->getAttributeBackend()->getImageTypes() as $type) {
74  $url = $image->setType($type)->getSourceUrl();
75  $html .= '<td class="gallery" align="center" style="vertical-align:bottom;">';
76  $html .= '<a href="' .
77  $url .
78  '" target="_blank" onclick="imagePreview(\'' .
79  $this->getHtmlId() .
80  '_image_' .
81  $type .
82  '_' .
83  $image->getValueId() .
84  '\');return false;" ' .
85  $this->_getUiId(
86  'image-' . $image->getValueId()
87  ) .
88  '>
89  <img id="' .
90  $this->getHtmlId() .
91  '_image_' .
92  $type .
93  '_' .
94  $image->getValueId() .
95  '" src="' .
96  $url .
97  '" alt="' .
98  $image->getValue() .
99  '" height="25" align="absmiddle" class="small-image-preview"></a><br/>';
100  $html .= '<input type="file" name="' .
101  $this->getName() .
102  '_' .
103  $type .
104  '[' .
105  $image->getValueId() .
106  ']" size="1"' .
107  $this->_getUiId(
108  'file'
109  ) . ' ></td>';
110  }
111  $html .= '<td class="gallery" align="center" style="vertical-align:bottom;">' .
112  '<input type="input" name="' .
113  parent::getName() .
114  '[position][' .
115  $image->getValueId() .
116  ']" value="' .
117  $image->getPosition() .
118  '" id="' .
119  $this->getHtmlId() .
120  '_position_' .
121  $image->getValueId() .
122  '" size="3" ' .
123  $this->_getUiId(
124  'position-' . $image->getValueId()
125  ) . '/></td>';
126  $html .= '<td class="gallery" align="center" style="vertical-align:bottom;">' .
127  '<input type="checkbox" name="' .
128  parent::getName() .
129  '[delete][' .
130  $image->getValueId() .
131  ']" value="' .
132  $image->getValueId() .
133  '" id="' .
134  $this->getHtmlId() .
135  '_delete_' .
136  $image->getValueId() .
137  '" ' .
138  $this->_getUiId(
139  'delete-button-' . $image->getValueId()
140  ) . '/></td>';
141  $html .= '</tr>';
142  }
143  }
144  if ($i == 0) {
145  $html .= '<script type="text/javascript">' .
146  'document.getElementById("gallery_thead").style.visibility="hidden";' .
147  '</script>';
148  }
149 
150  $html .= '</tbody></table>';
151 
152  $name = $this->getName();
153  $parentName = parent::getName();
154 
155  $html .= <<<EndSCRIPT
156 
157  <script language="javascript">
158  id = 0;
159 
160  function addNewImg(){
161 
162  document.getElementById("gallery_thead").style.visibility="visible";
163 
164  id--;
165  new_file_input = '<input type="file" name="{$name}_%j%[%id%]" size="1" />';
166 
167  // Sort order input
168  var new_row_input = document.createElement( 'input' );
169  new_row_input.type = 'text';
170  new_row_input.name = '{$parentName}[position]['+id+']';
171  new_row_input.size = '3';
172  new_row_input.value = '0';
173 
174  // Delete button
175  var new_row_button = document.createElement( 'input' );
176  new_row_button.type = 'checkbox';
177  new_row_button.value = 'Delete';
178 
179  table = document.getElementById( "gallery" );
180 
181  // no of rows in the table:
182  noOfRows = table.rows.length;
183 
184  // no of columns in the pre-last row:
185  noOfCols = table.rows[noOfRows-2].cells.length;
186 
187  // insert row at pre-last:
188  var x=table.insertRow(noOfRows-1);
189 
190  // insert cells in row.
191  for (var j = 0; j < noOfCols; j++) {
192 
193  newCell = x.insertCell(j);
194  newCell.align = "center";
195  newCell.valign = "middle";
196 
197  if (j==3) {
198  newCell.appendChild( new_row_input );
199  }
200  else if (j==4) {
201  newCell.appendChild( new_row_button );
202  }
203  else {
204  newCell.innerHTML = new_file_input.replace(/%j%/g, j).replace(/%id%/g, id);
205  }
206 
207  }
208 
209  // Delete function
210  new_row_button.onclick= function(){
211 
212  this.parentNode.parentNode.parentNode.removeChild( this.parentNode.parentNode );
213 
214  // Appease Safari
215  // without it Safari wants to reload the browser window
216  // which nixes your already queued uploads
217  return false;
218  };
219 
220  }
221  </script>
222 
223 EndSCRIPT;
224  $html .= $this->getAfterElementHtml();
225  return $html;
226  }
227 
231  public function getName()
232  {
233  return $this->getData('name');
234  }
235 
239  public function getParentName()
240  {
241  return parent::getName();
242  }
243 }
$type
Definition: item.phtml:13
jquery extjs ext tree checkbox
Definition: tree.phtml:41
$i
Definition: gallery.phtml:31
if(!isset($_GET['name'])) $name
Definition: log.php:14