Skip to content

Commit 2ce4ab0

Browse files
author
Graham Wharton
committed
Moved generation of Gallery options and Gallery fullscreen options to own
block functions.
1 parent 80469a6 commit 2ce4ab0

4 files changed

Lines changed: 434 additions & 39 deletions

File tree

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

0 commit comments

Comments
 (0)