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 OWN_VIDEO_TYPE_MP4 = 'video/mp4'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Generate header element |
20
|
|
|
* @param string $h1 |
21
|
|
|
* @param string|null $h2 |
22
|
|
|
* @param string|null $imgUrl |
23
|
|
|
* @param string|null $imgCaption |
24
|
|
|
* @param array|null $menuArray array of arrays with pairs of url and content |
25
|
|
|
* [ |
26
|
|
|
* ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
27
|
|
|
* ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
28
|
|
|
* ] |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
9 |
|
public static function header(string $h1, string $h2 = null, string $imgUrl = null, |
32
|
|
|
string $imgCaption = null, array $menuArray = null): string |
33
|
|
|
{ |
34
|
9 |
|
$header = '<h1>' . $h1 . '</h1>'; |
35
|
9 |
|
$header .= $h2 ? '<h2>' . $h2 . '</h2>' : ''; |
36
|
9 |
|
$header .= $menuArray ? self::generateMenu($menuArray) : ''; |
37
|
9 |
|
$header .= $imgUrl ? self::img($imgUrl, $imgCaption) : ''; |
38
|
|
|
|
39
|
9 |
|
return '<header>' . $header . '</header>'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Generate image element |
44
|
|
|
* @param string $imgUrl |
45
|
|
|
* @param string|null $imgCaption |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
9 |
|
public static function img(string $imgUrl, string $imgCaption = null): string |
49
|
|
|
{ |
50
|
9 |
|
$imageString = '<img src="' . $imgUrl . '" />'; |
51
|
|
|
|
52
|
9 |
|
$imageString .= $imgCaption ? '<figcaption>' . $imgCaption . '</figcaption>' : ''; |
53
|
|
|
|
54
|
9 |
|
return '<figure>' . $imageString . '</figure>'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Generate video element |
59
|
|
|
* @param string $videoUrl |
60
|
|
|
* @param string|null $videoCaption |
61
|
|
|
* @param string $imgUrl |
62
|
|
|
* @param string $type |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
6 |
|
public static function ownVideo(string $videoUrl, string $videoCaption = null, string $type = self::OWN_VIDEO_TYPE_MP4, string $imgUrl = null): string |
66
|
|
|
{ |
67
|
6 |
|
$videoString = '<video><source src="' . $videoUrl . '" type="' . $type . '" /></video>'; |
68
|
6 |
|
$videoString .= $imgUrl ? '<img src="' . $imgUrl . '" />' : ''; |
69
|
6 |
|
$videoString .= $videoCaption ? '<figcaption>' . $videoCaption . '</figcaption>' : ''; |
70
|
|
|
|
71
|
6 |
|
return '<figure>' . $videoString . '</figure>'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Generate images gallery |
76
|
|
|
* @param array $imagesArray Array of images urls |
77
|
|
|
* ['http://example.com/image1.jpg', 'http://example.com/image2.jpg'] |
78
|
|
|
* @param string|null $header |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
6 |
|
public static function gallery(array $imagesArray, string $header = null): string |
82
|
|
|
{ |
83
|
6 |
|
$galleryString = $header ? '<header>' . $header . '</header>' : ''; |
84
|
|
|
|
85
|
6 |
|
foreach ($imagesArray as $image) { |
86
|
6 |
|
$galleryString .= '<img src="' . $image . '" />'; |
87
|
|
|
} |
88
|
|
|
|
89
|
6 |
|
return '<div data-block="gallery">' . $galleryString . '</div>'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Generate share block |
94
|
|
|
* @param array|null $networks Array of network names |
95
|
|
|
* [Content::SHARE_TYPE_GOOGLE, Content::SHARE_TYPE_TWITTER] |
96
|
|
|
* Can be empty, in this way all possible network types will be showed. |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
6 |
|
public static function share(array $networks = null): string |
100
|
|
|
{ |
101
|
6 |
|
$networksString = $networks |
102
|
3 |
|
? 'data-network="' . implode(',', $networks) . '"' |
103
|
6 |
|
: ''; |
104
|
|
|
|
105
|
6 |
|
return '<div data-block="share" ' . $networksString . '></div>'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Generate rating block |
110
|
|
|
* @param float $currentRating |
111
|
|
|
* @param float $maxRating |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
12 |
|
public static function rating(float $currentRating, float $maxRating): string |
115
|
|
|
{ |
116
|
12 |
|
if (($currentRating > $maxRating) || ($maxRating <= 0) || ($currentRating < 0)) { |
117
|
9 |
|
throw new \UnexpectedValueException("Current rating can't be bigger than max value. And max value must be bigger than 0."); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return '<div itemscope="" itemtype="http://schema.org/Rating"> |
121
|
3 |
|
<meta itemprop="ratingValue" content="' . $currentRating . '" /> |
122
|
3 |
|
<meta itemprop="bestRating" content="' . $maxRating . '" /> |
123
|
|
|
</div>'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Generate button |
128
|
|
|
* @param string $text |
129
|
|
|
* @param string $url |
130
|
|
|
* @param string $phone Phone number in RFC-3966 format |
131
|
|
|
* @param string|null $buttonColor Can be Text or HEX |
132
|
|
|
* @param string|null $textColor Can be Text or HEX |
133
|
|
|
* @param bool $isBoldText |
134
|
|
|
* @param bool $isDisabled |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
12 |
|
public static function button(string $text, string $url = '', string $phone = '', |
138
|
|
|
string $buttonColor = null, string $textColor = null, |
139
|
|
|
bool $isBoldText = false, bool $isDisabled = false): string |
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(string $url, array $commentsArray): string |
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): string |
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 Ad block position element |
230
|
|
|
* @param string $turboAdId value of $turboAdId used in Channel() class |
231
|
|
|
* @return string |
232
|
|
|
* |
233
|
|
|
* @see Channel::$adTurboAdId |
234
|
|
|
*/ |
235
|
3 |
|
public static function adBlockPosition(string $turboAdId): string |
236
|
|
|
{ |
237
|
3 |
|
return '<figure data-turbo-ad-id="' . $turboAdId . '"></figure>'; |
238
|
|
|
} |
239
|
|
|
|
240
|
3 |
|
private static function generateCommentBlock(array $commentsArray) |
241
|
|
|
{ |
242
|
3 |
|
$commentBlock = ''; |
243
|
|
|
|
244
|
3 |
|
foreach ($commentsArray as $commentArray) { |
245
|
3 |
|
$author = isset($commentArray['author']) ? 'data-author="' . $commentArray['author'] . '"' : ''; |
246
|
3 |
|
$avatar = isset($commentArray['avatar']) ? 'data-avatar-url="' . $commentArray['avatar'] . '"' : ''; |
247
|
3 |
|
$subtitle = isset($commentArray['subtitle']) ? 'data-subtitle="' . $commentArray['subtitle'] . '"' : ''; |
248
|
|
|
|
249
|
|
|
$commentBlock .= '<div |
250
|
|
|
data-block="comment" |
251
|
3 |
|
' . $author . ' |
252
|
3 |
|
' . $avatar . ' |
253
|
3 |
|
' . $subtitle . ' |
254
|
|
|
><div data-block="content">'; |
255
|
|
|
|
256
|
3 |
|
$commentBlock .= isset($commentArray['title']) ? '<header>' . $commentArray['title'] . '</header>' : ''; |
257
|
3 |
|
$commentBlock .= isset($commentArray['content']) ? '<p>' . $commentArray['content'] . '</p></div>' : ''; |
258
|
|
|
|
259
|
3 |
|
if (isset($commentArray['comments'])) { |
260
|
3 |
|
$commentBlock .= '<div data-block="comments">'; |
261
|
3 |
|
$commentBlock .= self::generateCommentBlock($commentArray['comments']); |
262
|
3 |
|
$commentBlock .= '</div>'; |
263
|
|
|
} |
264
|
|
|
|
265
|
3 |
|
$commentBlock .= '</div>'; |
266
|
|
|
} |
267
|
|
|
|
268
|
3 |
|
return $commentBlock; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Generate header menu |
273
|
|
|
* @param array $menuArray array of arrays with pairs of url and title |
274
|
|
|
* [ |
275
|
|
|
* ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
276
|
|
|
* ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
277
|
|
|
* ] |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
3 |
|
private static function generateMenu(array $menuArray) |
281
|
|
|
{ |
282
|
3 |
|
$menuString = ''; |
283
|
|
|
|
284
|
3 |
|
foreach ($menuArray as $menuItem) { |
285
|
3 |
|
$menuString .= '<a href="' . $menuItem['url'] . '">' . $menuItem['title'] . '</a>'; |
286
|
|
|
} |
287
|
|
|
|
288
|
3 |
|
return '<menu>' . $menuString . '</menu>'; |
289
|
|
|
} |
290
|
|
|
} |