1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace sokolnikov911\YandexTurboPages\helpers; |
4
|
|
|
|
5
|
|
|
use sokolnikov911\YandexTurboPages\Channel; |
6
|
|
|
|
7
|
|
|
class Content |
8
|
|
|
{ |
9
|
|
|
const SHARE_TYPE_FACEBOOK = 'facebook'; |
10
|
|
|
const SHARE_TYPE_GOOGLE = 'google'; |
11
|
|
|
const SHARE_TYPE_ODNOKLASSNIKI = 'odnoklassniki'; |
12
|
|
|
const SHARE_TYPE_TELEGRAM = 'telegram'; |
13
|
|
|
const SHARE_TYPE_TWITTER = 'twitter'; |
14
|
|
|
const SHARE_TYPE_VKONTAKTE = 'vkontakte'; |
15
|
|
|
|
16
|
|
|
const SLIDER_DATA_VIEW_SQUARE = 'square'; |
17
|
|
|
const SLIDER_DATA_VIEW_PORTRAIT = 'portrait'; |
18
|
|
|
const SLIDER_DATA_VIEW_LANDSCAPE = 'landscape'; |
19
|
|
|
|
20
|
|
|
const SLIDER_DATA_ITEM_VIEW_COVER = 'cover'; |
21
|
|
|
const SLIDER_DATA_ITEM_VIEW_CONTAIN = 'contain'; |
22
|
|
|
|
23
|
|
|
const OWN_VIDEO_TYPE_MP4 = 'video/mp4'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Generate header element |
27
|
|
|
* @param string $h1 |
28
|
|
|
* @param string|null $h2 |
29
|
|
|
* @param string|null $imgUrl |
30
|
|
|
* @param string|null $imgCaption |
31
|
|
|
* @param array|null $menuArray array of arrays with pairs of url and content |
32
|
|
|
* [ |
33
|
|
|
* ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
34
|
|
|
* ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
35
|
|
|
* ] |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
9 |
|
public static function header(string $h1, string $h2 = null, string $imgUrl = null, |
39
|
|
|
string $imgCaption = null, array $menuArray = null): string |
40
|
|
|
{ |
41
|
9 |
|
$header = '<h1>' . $h1 . '</h1>'; |
42
|
9 |
|
$header .= $h2 ? '<h2>' . $h2 . '</h2>' : ''; |
43
|
9 |
|
$header .= $menuArray ? self::generateMenu($menuArray) : ''; |
44
|
9 |
|
$header .= $imgUrl ? self::img($imgUrl, $imgCaption) : ''; |
45
|
|
|
|
46
|
9 |
|
return '<header>' . $header . '</header>'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Generate image element |
51
|
|
|
* @param string $imgUrl |
52
|
|
|
* @param string|null $imgCaption |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
9 |
|
public static function img(string $imgUrl, string $imgCaption = null): string |
56
|
|
|
{ |
57
|
9 |
|
$imageString = '<img src="' . $imgUrl . '" />'; |
58
|
|
|
|
59
|
9 |
|
$imageString .= $imgCaption ? '<figcaption>' . $imgCaption . '</figcaption>' : ''; |
60
|
|
|
|
61
|
9 |
|
return '<figure>' . $imageString . '</figure>'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Generate video element |
66
|
|
|
* @param string $videoUrl |
67
|
|
|
* @param string|null $videoCaption |
68
|
|
|
* @param string $imgUrl |
69
|
|
|
* @param string $type |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
6 |
|
public static function ownVideo(string $videoUrl, string $videoCaption = null, string $type = self::OWN_VIDEO_TYPE_MP4, string $imgUrl = null): string |
73
|
|
|
{ |
74
|
6 |
|
$videoString = '<video><source src="' . $videoUrl . '" type="' . $type . '" /></video>'; |
75
|
6 |
|
$videoString .= $imgUrl ? '<img src="' . $imgUrl . '" />' : ''; |
76
|
6 |
|
$videoString .= $videoCaption ? '<figcaption>' . $videoCaption . '</figcaption>' : ''; |
77
|
|
|
|
78
|
6 |
|
return '<figure>' . $videoString . '</figure>'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Generate images gallery |
83
|
|
|
* @param array $imagesArray Array of images urls |
84
|
|
|
* ['http://example.com/image1.jpg', 'http://example.com/image2.jpg'] |
85
|
|
|
* @param string|null $header |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
6 |
|
public static function gallery(array $imagesArray, string $header = null): string |
89
|
|
|
{ |
90
|
6 |
|
$galleryString = $header ? '<header>' . $header . '</header>' : ''; |
91
|
|
|
|
92
|
6 |
|
foreach ($imagesArray as $image) { |
93
|
6 |
|
$galleryString .= '<img src="' . $image . '" />'; |
94
|
|
|
} |
95
|
|
|
|
96
|
6 |
|
return '<div data-block="gallery">' . $galleryString . '</div>'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Generate media slider |
101
|
|
|
* @param array $itemsArray Array of items with data |
102
|
|
|
* [ |
103
|
|
|
* ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''], |
104
|
|
|
* ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''], |
105
|
|
|
* ['url' => 'http://example.com/image3.jpg'], |
106
|
|
|
* ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1'] |
107
|
|
|
* ] |
108
|
|
|
* @param string|null $header |
109
|
|
|
* @param string $dataView |
110
|
|
|
* @param string $dataItemView |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
6 |
|
public static function slider(array $itemsArray, string $header = null, |
114
|
|
|
string $dataView = self::SLIDER_DATA_VIEW_SQUARE, |
115
|
|
|
string $dataItemView = self::SLIDER_DATA_ITEM_VIEW_COVER): string |
116
|
|
|
{ |
117
|
6 |
|
$sliderString = $header ? '<header>' . $header . '</header>' : ''; |
118
|
|
|
|
119
|
6 |
|
foreach ($itemsArray as $item) { |
120
|
6 |
|
if (isset($item['url']) || (isset($item['href']) && isset($item['text']))) { |
121
|
6 |
|
$sliderString .= '<figure>'; |
122
|
6 |
|
$sliderString .= isset($item['title']) |
123
|
6 |
|
? '<figcaption>' . $item['title'] . '</figcaption>' |
124
|
6 |
|
: ''; |
125
|
|
|
|
126
|
6 |
|
if (isset($item['url'])) { |
127
|
6 |
|
$sliderString .= '<img src="' . $item['url'] . '" />'; |
128
|
6 |
|
} elseif (isset($item['href']) && isset($item['text'])) { |
129
|
6 |
|
$sliderString .= '<a href="' . $item['href'] . '">' . $item['text'] . '</a>'; |
130
|
|
|
} |
131
|
|
|
|
132
|
6 |
|
$sliderString .= '</figure>'; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
6 |
|
return '<div data-block="slider" data-view="' . $dataView . '" data-item-view="' |
137
|
6 |
|
. $dataItemView . '">' . $sliderString . '</div>'; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Generate share block |
142
|
|
|
* @param array|null $networks Array of network names |
143
|
|
|
* [Content::SHARE_TYPE_GOOGLE, Content::SHARE_TYPE_TWITTER] |
144
|
|
|
* Can be empty, in this way all possible network types will be showed. |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
6 |
|
public static function share(array $networks = null): string |
148
|
|
|
{ |
149
|
6 |
|
$networksString = $networks |
150
|
3 |
|
? 'data-network="' . implode(',', $networks) . '"' |
151
|
6 |
|
: ''; |
152
|
|
|
|
153
|
6 |
|
return '<div data-block="share" ' . $networksString . '></div>'; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Generate rating block |
158
|
|
|
* @param float $currentRating |
159
|
|
|
* @param float $maxRating |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
12 |
|
public static function rating(float $currentRating, float $maxRating): string |
163
|
|
|
{ |
164
|
12 |
|
if (($currentRating > $maxRating) || ($maxRating <= 0) || ($currentRating < 0)) { |
165
|
9 |
|
throw new \UnexpectedValueException("Current rating can't be bigger than max value. And max value must be bigger than 0."); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return '<div itemscope="" itemtype="http://schema.org/Rating"> |
169
|
3 |
|
<meta itemprop="ratingValue" content="' . $currentRating . '" /> |
170
|
3 |
|
<meta itemprop="bestRating" content="' . $maxRating . '" /> |
171
|
|
|
</div>'; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Generate button |
176
|
|
|
* @param string $text |
177
|
|
|
* @param string $url |
178
|
|
|
* @param string $phone Phone number in RFC-3966 format |
179
|
|
|
* @param string|null $buttonColor Can be Text or HEX |
180
|
|
|
* @param string|null $textColor Can be Text or HEX |
181
|
|
|
* @param bool $isBoldText |
182
|
|
|
* @param bool $isDisabled |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
12 |
|
public static function button(string $text, string $url = '', string $phone = '', |
186
|
|
|
string $buttonColor = null, string $textColor = null, |
187
|
|
|
bool $isBoldText = false, bool $isDisabled = false): string |
188
|
|
|
{ |
189
|
12 |
|
if (!$url && !$phone) { |
190
|
3 |
|
throw new \UnexpectedValueException('Please set url or phone number for button'); |
191
|
|
|
} |
192
|
|
|
|
193
|
9 |
|
$formAction = $url ? $url : 'tel:' . $phone; |
194
|
9 |
|
$buttonColorString = $buttonColor ? 'data-background-color="' . $buttonColor . '"' : ''; |
195
|
9 |
|
$textColorString = $textColor ? 'data-color="' . $textColor . '"' : ''; |
196
|
9 |
|
$isBoldTextString = $isBoldText ? 'data-primary="true"' : ''; |
197
|
9 |
|
$isDisabledString = $isDisabled ? 'disabled="true"' : ''; |
198
|
|
|
|
199
|
|
|
return "<button |
200
|
9 |
|
formaction=\"" . $formAction . "\" |
201
|
9 |
|
" . $buttonColorString . " |
202
|
9 |
|
" . $textColorString . " |
203
|
9 |
|
" . $isBoldTextString . " |
204
|
9 |
|
" . $isDisabledString . ">" . $text . "</button>"; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Generate comment block |
209
|
|
|
* @param string $url URL to comments page |
210
|
|
|
* @param array $commentsArray multidimensional or one-dimensional array of comments, |
211
|
|
|
* can has unlimited includes, example: |
212
|
|
|
* [ |
213
|
|
|
* [ |
214
|
|
|
* 'author' => 'First Author Name', |
215
|
|
|
* 'avatar' => 'http://example.com/user1.jpg', |
216
|
|
|
* 'title' => 'Comment Title', |
217
|
|
|
* 'subtitle' => '2017-12-10', |
218
|
|
|
* 'content' => 'Somme comment text', |
219
|
|
|
* 'comments' => [ |
220
|
|
|
* [ |
221
|
|
|
* 'author' => 'Third Author Name', |
222
|
|
|
* 'avatar' => 'http://example.com/user3.jpg', |
223
|
|
|
* 'title' => 'Comment Title', |
224
|
|
|
* 'subtitle' => '2017-12-12', |
225
|
|
|
* 'content' => 'Some answer text' |
226
|
|
|
* ], |
227
|
|
|
* [ |
228
|
|
|
* 'author' => 'Another Author Name', |
229
|
|
|
* 'avatar' => 'http://example.com/user4.jpg', |
230
|
|
|
* 'title' => 'Comment Title', |
231
|
|
|
* 'subtitle' => '2017-12-13', |
232
|
|
|
* 'content' => 'Another answer text' |
233
|
|
|
* ], |
234
|
|
|
* ] |
235
|
|
|
* ], |
236
|
|
|
* [ |
237
|
|
|
* 'author' => 'Second Author Name', |
238
|
|
|
* 'avatar' => 'http://example.com/user2.jpg', |
239
|
|
|
* 'title' => 'Comment Title', |
240
|
|
|
* 'subtitle' => '2017-12-11', |
241
|
|
|
* 'content' => 'Some comment text' |
242
|
|
|
* ], |
243
|
|
|
* ] |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
3 |
|
public static function comment(string $url, array $commentsArray): string |
247
|
|
|
{ |
248
|
3 |
|
$commentBlock = self::generateCommentBlock($commentsArray); |
249
|
|
|
|
250
|
3 |
|
return '<div data-block="comments" data-url="' . $url . '">' . $commentBlock . '</div>'; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Generate accordion |
255
|
|
|
* @param array $accordionArray array accordion elements |
256
|
|
|
* [ |
257
|
|
|
* ['title' => 'Page title 1', 'text' => 'Text 1'], |
258
|
|
|
* ['title' => 'Page title 2', 'text' => 'Text 2', 'expanded' => true], |
259
|
|
|
* ] |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
3 |
|
public static function accordion(array $accordionArray): string |
263
|
|
|
{ |
264
|
3 |
|
$accordionString = '<div data-block="accordion">'; |
265
|
|
|
|
266
|
3 |
|
foreach ($accordionArray as $item) { |
267
|
3 |
|
$expanded = isset($item['expanded']) && $item['expanded'] ? ' data-expanded="true"' : ''; |
268
|
3 |
|
$accordionString .= '<div data-block="item" data-title="' . $item['title'] . '"' . $expanded . '>' . $item['text'] . '</div>'; |
269
|
|
|
} |
270
|
|
|
|
271
|
3 |
|
$accordionString .= '</div>'; |
272
|
|
|
|
273
|
3 |
|
return $accordionString; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Generate Ad block position element |
278
|
|
|
* @param string $turboAdId value of $turboAdId used in Channel() class |
279
|
|
|
* @return string |
280
|
|
|
* |
281
|
|
|
* @see Channel::$adTurboAdId |
282
|
|
|
*/ |
283
|
3 |
|
public static function adBlockPosition(string $turboAdId): string |
284
|
|
|
{ |
285
|
3 |
|
return '<figure data-turbo-ad-id="' . $turboAdId . '"></figure>'; |
286
|
|
|
} |
287
|
|
|
|
288
|
3 |
|
private static function generateCommentBlock(array $commentsArray) |
289
|
|
|
{ |
290
|
3 |
|
$commentBlock = ''; |
291
|
|
|
|
292
|
3 |
|
foreach ($commentsArray as $commentArray) { |
293
|
3 |
|
$author = isset($commentArray['author']) ? 'data-author="' . $commentArray['author'] . '"' : ''; |
294
|
3 |
|
$avatar = isset($commentArray['avatar']) ? 'data-avatar-url="' . $commentArray['avatar'] . '"' : ''; |
295
|
3 |
|
$subtitle = isset($commentArray['subtitle']) ? 'data-subtitle="' . $commentArray['subtitle'] . '"' : ''; |
296
|
|
|
|
297
|
|
|
$commentBlock .= '<div |
298
|
|
|
data-block="comment" |
299
|
3 |
|
' . $author . ' |
300
|
3 |
|
' . $avatar . ' |
301
|
3 |
|
' . $subtitle . ' |
302
|
|
|
><div data-block="content">'; |
303
|
|
|
|
304
|
3 |
|
$commentBlock .= isset($commentArray['title']) ? '<header>' . $commentArray['title'] . '</header>' : ''; |
305
|
3 |
|
$commentBlock .= isset($commentArray['content']) ? '<p>' . $commentArray['content'] . '</p></div>' : ''; |
306
|
|
|
|
307
|
3 |
|
if (isset($commentArray['comments'])) { |
308
|
3 |
|
$commentBlock .= '<div data-block="comments">'; |
309
|
3 |
|
$commentBlock .= self::generateCommentBlock($commentArray['comments']); |
310
|
3 |
|
$commentBlock .= '</div>'; |
311
|
|
|
} |
312
|
|
|
|
313
|
3 |
|
$commentBlock .= '</div>'; |
314
|
|
|
} |
315
|
|
|
|
316
|
3 |
|
return $commentBlock; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Generate header menu |
321
|
|
|
* @param array $menuArray array of arrays with pairs of url and title |
322
|
|
|
* [ |
323
|
|
|
* ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
324
|
|
|
* ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
325
|
|
|
* ] |
326
|
|
|
* @return string |
327
|
|
|
*/ |
328
|
3 |
|
private static function generateMenu(array $menuArray) |
329
|
|
|
{ |
330
|
3 |
|
$menuString = ''; |
331
|
|
|
|
332
|
3 |
|
foreach ($menuArray as $menuItem) { |
333
|
3 |
|
$menuString .= '<a href="' . $menuItem['url'] . '">' . $menuItem['title'] . '</a>'; |
334
|
|
|
} |
335
|
|
|
|
336
|
3 |
|
return '<menu>' . $menuString . '</menu>'; |
337
|
|
|
} |
338
|
|
|
} |