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