|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Catalog\Block\Product\View; |
| 7 | + |
| 8 | +use Magento\Framework\View\Element\Block\ArgumentInterface; |
| 9 | +use Magento\Framework\Serialize\Serializer\Json; |
| 10 | +use Magento\Catalog\Block\Product\Context; |
| 11 | +use Magento\Framework\Stdlib\ArrayUtils; |
| 12 | + |
| 13 | +/** |
| 14 | + * Gallery options block. |
| 15 | + */ |
| 16 | +class GalleryOptions extends AbstractView implements ArgumentInterface |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var Json |
| 20 | + */ |
| 21 | + private $jsonSerializer; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var Gallery |
| 25 | + */ |
| 26 | + private $gallery; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param Context $context |
| 30 | + * @param ArrayUtils $arrayUtils |
| 31 | + * @param Json $jsonSerializer |
| 32 | + * @param Gallery $gallery |
| 33 | + * @param array $data |
| 34 | + */ |
| 35 | + public function __construct( |
| 36 | + Context $context, |
| 37 | + ArrayUtils $arrayUtils, |
| 38 | + Json $jsonSerializer, |
| 39 | + Gallery $gallery, |
| 40 | + array $data = [] |
| 41 | + ) { |
| 42 | + $this->gallery = $gallery; |
| 43 | + $this->jsonSerializer = $jsonSerializer; |
| 44 | + parent::__construct($context, $arrayUtils, $data); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Retrieve gallery options in JSON format |
| 49 | + * |
| 50 | + * @return string |
| 51 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
| 52 | + * @SuppressWarnings(PHPMD.NPathComplexity) |
| 53 | + * @SuppressWarnings(PHPMD.ElseExpression) |
| 54 | + */ |
| 55 | + public function getOptionsJson() |
| 56 | + { |
| 57 | + $optionItems = null; |
| 58 | + |
| 59 | + //Special case for gallery/nav which can be the string "thumbs/false/dots" |
| 60 | + if (is_bool($this->getVar("gallery/nav"))) { |
| 61 | + $optionItems['nav'] = $this->getVar("gallery/nav") ? 'true' : 'false'; |
| 62 | + } else { |
| 63 | + $optionItems['nav'] = $this->escapeHtml($this->getVar("gallery/nav")); |
| 64 | + } |
| 65 | + |
| 66 | + $optionItems['loop'] = $this->getVar("gallery/loop"); |
| 67 | + $optionItems['keyboard'] = $this->getVar("gallery/keyboard"); |
| 68 | + $optionItems['arrows'] = $this->getVar("gallery/arrows"); |
| 69 | + $optionItems['allowfullscreen'] = $this->getVar("gallery/allowfullscreen"); |
| 70 | + $optionItems['showCaption'] = $this->getVar("gallery/caption"); |
| 71 | + $optionItems['width'] = (int)$this->escapeHtml( |
| 72 | + $this->gallery->getImageAttribute('product_page_image_medium', 'width') |
| 73 | + ); |
| 74 | + $optionItems['thumbwidth'] = (int)$this->escapeHtml( |
| 75 | + $this->gallery->getImageAttribute('product_page_image_small', 'width') |
| 76 | + ); |
| 77 | + |
| 78 | + if ($this->gallery->getImageAttribute('product_page_image_small', 'height') || |
| 79 | + $this->gallery->getImageAttribute('product_page_image_small', 'width')) { |
| 80 | + $optionItems['thumbheight'] = (int)$this->escapeHtml( |
| 81 | + $this->gallery->getImageAttribute('product_page_image_small', 'height') ?: |
| 82 | + $this->gallery->getImageAttribute('product_page_image_small', 'width') |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + if ($this->gallery->getImageAttribute('product_page_image_medium', 'height') || |
| 87 | + $this->gallery->getImageAttribute('product_page_image_medium', 'width')) { |
| 88 | + $optionItems['height'] = (int)$this->escapeHtml( |
| 89 | + $this->gallery->getImageAttribute('product_page_image_medium', 'height') ?: |
| 90 | + $this->gallery->getImageAttribute('product_page_image_medium', 'width') |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + if ($this->getVar("gallery/transition/duration")) { |
| 95 | + $optionItems['transitionduration'] = |
| 96 | + (int)$this->escapeHtml($this->getVar("gallery/transition/duration")); |
| 97 | + } |
| 98 | + |
| 99 | + $optionItems['transition'] = $this->escapeHtml($this->getVar("gallery/transition/effect")); |
| 100 | + $optionItems['navarrows'] = $this->getVar("gallery/navarrows"); |
| 101 | + $optionItems['navtype'] = $this->escapeHtml($this->getVar("gallery/navtype")); |
| 102 | + $optionItems['navdir'] = $this->escapeHtml($this->getVar("gallery/navdir")); |
| 103 | + |
| 104 | + if ($this->getVar("gallery/thumbmargin")) { |
| 105 | + $optionItems['thumbmargin'] = (int)$this->escapeHtml($this->getVar("gallery/thumbmargin")); |
| 106 | + } |
| 107 | + |
| 108 | + return $this->jsonSerializer->serialize($optionItems); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Retrieve gallery fullscreen options in JSON format |
| 113 | + * |
| 114 | + * @return string |
| 115 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
| 116 | + * @SuppressWarnings(PHPMD.NPathComplexity) |
| 117 | + * @SuppressWarnings(PHPMD.ElseExpression) |
| 118 | + */ |
| 119 | + public function getFSOptionsJson() |
| 120 | + { |
| 121 | + $fsOptionItems = null; |
| 122 | + |
| 123 | + //Special case for gallery/nav which can be the string "thumbs/false/dots" |
| 124 | + if (is_bool($this->getVar("gallery/fullscreen/nav"))) { |
| 125 | + $fsOptionItems['nav'] = $this->getVar("gallery/fullscreen/nav") ? 'true' : 'false'; |
| 126 | + } else { |
| 127 | + $fsOptionItems['nav'] = $this->escapeHtml($this->getVar("gallery/fullscreen/nav")); |
| 128 | + } |
| 129 | + |
| 130 | + $fsOptionItems['loop'] = $this->getVar("gallery/fullscreen/loop"); |
| 131 | + $fsOptionItems['navdir'] = $this->escapeHtml($this->getVar("gallery/fullscreen/navdir")); |
| 132 | + $fsOptionItems['navarrows'] = $this->getVar("gallery/fullscreen/navarrows"); |
| 133 | + $fsOptionItems['navtype'] = $this->escapeHtml($this->getVar("gallery/fullscreen/navtype")); |
| 134 | + $fsOptionItems['arrows'] = $this->getVar("gallery/fullscreen/arrows"); |
| 135 | + $fsOptionItems['showCaption'] = $this->getVar("gallery/fullscreen/caption"); |
| 136 | + |
| 137 | + if ($this->getVar("gallery/fullscreen/transition/duration")) { |
| 138 | + $fsOptionItems['transitionduration'] = (int)$this->escapeHtml( |
| 139 | + $this->getVar("gallery/fullscreen/transition/duration") |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + $fsOptionItems['transition'] = $this->escapeHtml($this->getVar("gallery/fullscreen/transition/effect")); |
| 144 | + |
| 145 | + if ($this->getVar("gallery/fullscreen/keyboard")) { |
| 146 | + $fsOptionItems['keyboard'] = $this->getVar("gallery/fullscreen/keyboard"); |
| 147 | + } |
| 148 | + |
| 149 | + if ($this->getVar("gallery/fullscreen/thumbmargin")) { |
| 150 | + $fsOptionItems['thumbmargin'] = |
| 151 | + (int)$this->escapeHtml($this->getVar("gallery/fullscreen/thumbmargin")); |
| 152 | + } |
| 153 | + |
| 154 | + return $this->jsonSerializer->serialize($fsOptionItems); |
| 155 | + } |
| 156 | +} |
0 commit comments