Gallery.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Category form input image element
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Framework\Data\Form\Element;
  12. use Magento\Framework\Escaper;
  13. class Gallery extends AbstractElement
  14. {
  15. /**
  16. * @param Factory $factoryElement
  17. * @param CollectionFactory $factoryCollection
  18. * @param Escaper $escaper
  19. * @param array $data
  20. */
  21. public function __construct(
  22. Factory $factoryElement,
  23. CollectionFactory $factoryCollection,
  24. Escaper $escaper,
  25. $data = []
  26. ) {
  27. parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
  28. $this->setType('file');
  29. }
  30. /**
  31. * @return string
  32. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  33. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  34. */
  35. public function getElementHtml()
  36. {
  37. $gallery = $this->getValue();
  38. $html = '<table id="gallery" class="gallery" border="0" cellspacing="3" cellpadding="0">';
  39. $html .= '<thead id="gallery_thead" class="gallery">' .
  40. '<tr class="gallery">' .
  41. '<td class="gallery" valign="middle" align="center">Big Image</td>' .
  42. '<td class="gallery" valign="middle" align="center">Thumbnail</td>' .
  43. '<td class="gallery" valign="middle" align="center">Small Thumb</td>' .
  44. '<td class="gallery" valign="middle" align="center">Sort Order</td>' .
  45. '<td class="gallery" valign="middle" align="center">Delete</td>' .
  46. '</tr>'.
  47. '</thead>';
  48. $widgetButton = $this->getForm()->getParent()->getLayout();
  49. $buttonHtml = $widgetButton->createBlock(
  50. \Magento\Backend\Block\Widget\Button::class
  51. )->setData(
  52. ['label' => 'Add New Image', 'onclick' => 'addNewImg()', 'class' => 'add']
  53. )->toHtml();
  54. $html .= '<tfoot class="gallery">';
  55. $html .= '<tr class="gallery">';
  56. $html .= '<td class="gallery" valign="middle" align="left" colspan="5">' . $buttonHtml . '</td>';
  57. $html .= '</tr>';
  58. $html .= '</tfoot>';
  59. $html .= '<tbody class="gallery">';
  60. $i = 0;
  61. if ($this->getValue() !== null) {
  62. foreach ($this->getValue() as $image) {
  63. $i++;
  64. $html .= '<tr class="gallery">';
  65. foreach ($this->getValue()->getAttributeBackend()->getImageTypes() as $type) {
  66. $url = $image->setType($type)->getSourceUrl();
  67. $html .= '<td class="gallery" align="center" style="vertical-align:bottom;">';
  68. $html .= '<a href="' .
  69. $url .
  70. '" target="_blank" onclick="imagePreview(\'' .
  71. $this->getHtmlId() .
  72. '_image_' .
  73. $type .
  74. '_' .
  75. $image->getValueId() .
  76. '\');return false;" ' .
  77. $this->_getUiId(
  78. 'image-' . $image->getValueId()
  79. ) .
  80. '>
  81. <img id="' .
  82. $this->getHtmlId() .
  83. '_image_' .
  84. $type .
  85. '_' .
  86. $image->getValueId() .
  87. '" src="' .
  88. $url .
  89. '" alt="' .
  90. $image->getValue() .
  91. '" height="25" align="absmiddle" class="small-image-preview"></a><br/>';
  92. $html .= '<input type="file" name="' .
  93. $this->getName() .
  94. '_' .
  95. $type .
  96. '[' .
  97. $image->getValueId() .
  98. ']" size="1"' .
  99. $this->_getUiId(
  100. 'file'
  101. ) . ' ></td>';
  102. }
  103. $html .= '<td class="gallery" align="center" style="vertical-align:bottom;">' .
  104. '<input type="input" name="' .
  105. parent::getName() .
  106. '[position][' .
  107. $image->getValueId() .
  108. ']" value="' .
  109. $image->getPosition() .
  110. '" id="' .
  111. $this->getHtmlId() .
  112. '_position_' .
  113. $image->getValueId() .
  114. '" size="3" ' .
  115. $this->_getUiId(
  116. 'position-' . $image->getValueId()
  117. ) . '/></td>';
  118. $html .= '<td class="gallery" align="center" style="vertical-align:bottom;">' .
  119. '<input type="checkbox" name="' .
  120. parent::getName() .
  121. '[delete][' .
  122. $image->getValueId() .
  123. ']" value="' .
  124. $image->getValueId() .
  125. '" id="' .
  126. $this->getHtmlId() .
  127. '_delete_' .
  128. $image->getValueId() .
  129. '" ' .
  130. $this->_getUiId(
  131. 'delete-button-' . $image->getValueId()
  132. ) . '/></td>';
  133. $html .= '</tr>';
  134. }
  135. }
  136. if ($i == 0) {
  137. $html .= '<script type="text/javascript">' .
  138. 'document.getElementById("gallery_thead").style.visibility="hidden";' .
  139. '</script>';
  140. }
  141. $html .= '</tbody></table>';
  142. $name = $this->getName();
  143. $parentName = parent::getName();
  144. $html .= <<<EndSCRIPT
  145. <script language="javascript">
  146. id = 0;
  147. function addNewImg(){
  148. document.getElementById("gallery_thead").style.visibility="visible";
  149. id--;
  150. new_file_input = '<input type="file" name="{$name}_%j%[%id%]" size="1" />';
  151. // Sort order input
  152. var new_row_input = document.createElement( 'input' );
  153. new_row_input.type = 'text';
  154. new_row_input.name = '{$parentName}[position]['+id+']';
  155. new_row_input.size = '3';
  156. new_row_input.value = '0';
  157. // Delete button
  158. var new_row_button = document.createElement( 'input' );
  159. new_row_button.type = 'checkbox';
  160. new_row_button.value = 'Delete';
  161. table = document.getElementById( "gallery" );
  162. // no of rows in the table:
  163. noOfRows = table.rows.length;
  164. // no of columns in the pre-last row:
  165. noOfCols = table.rows[noOfRows-2].cells.length;
  166. // insert row at pre-last:
  167. var x=table.insertRow(noOfRows-1);
  168. // insert cells in row.
  169. for (var j = 0; j < noOfCols; j++) {
  170. newCell = x.insertCell(j);
  171. newCell.align = "center";
  172. newCell.valign = "middle";
  173. if (j==3) {
  174. newCell.appendChild( new_row_input );
  175. }
  176. else if (j==4) {
  177. newCell.appendChild( new_row_button );
  178. }
  179. else {
  180. newCell.innerHTML = new_file_input.replace(/%j%/g, j).replace(/%id%/g, id);
  181. }
  182. }
  183. // Delete function
  184. new_row_button.onclick= function(){
  185. this.parentNode.parentNode.parentNode.removeChild( this.parentNode.parentNode );
  186. // Appease Safari
  187. // without it Safari wants to reload the browser window
  188. // which nixes your already queued uploads
  189. return false;
  190. };
  191. }
  192. </script>
  193. EndSCRIPT;
  194. $html .= $this->getAfterElementHtml();
  195. return $html;
  196. }
  197. /**
  198. * @return mixed
  199. */
  200. public function getName()
  201. {
  202. return $this->getData('name');
  203. }
  204. /**
  205. * @return mixed
  206. */
  207. public function getParentName()
  208. {
  209. return parent::getName();
  210. }
  211. }