|
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
|
|
|
/** |
|
15
|
|
|
* Generate header element |
|
16
|
|
|
* @param string $h1 |
|
17
|
|
|
* @param string|null $h2 |
|
18
|
|
|
* @param string|null $imgUrl |
|
19
|
|
|
* @param string|null $imgCaption |
|
20
|
|
|
* @param array|null $menuArray array of arrays with pairs of url and content |
|
21
|
|
|
* [ |
|
22
|
|
|
* ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
|
23
|
|
|
* ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
|
24
|
|
|
* ] |
|
25
|
|
|
* @return string |
|
26
|
|
|
*/ |
|
27
|
18 |
|
public static function header($h1, $h2 = null, $imgUrl = null, |
|
28
|
|
|
$imgCaption = null, array $menuArray = null) |
|
29
|
|
|
{ |
|
30
|
18 |
|
$header = '<h1>' . $h1 . '</h1>'; |
|
31
|
18 |
|
$header .= $h2 ? '<h2>' . $h2 . '</h2>' : ''; |
|
32
|
18 |
|
$header .= $menuArray ? self::generateMenu($menuArray) : ''; |
|
33
|
18 |
|
$header .= $imgUrl ? self::img($imgUrl, $imgCaption) : ''; |
|
34
|
|
|
|
|
35
|
18 |
|
return '<header>' . $header . '</header>'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Generate image element |
|
40
|
|
|
* @param string $imgUrl |
|
41
|
|
|
* @param string|null $imgCaption |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
18 |
|
public static function img($imgUrl, $imgCaption = null) |
|
45
|
|
|
{ |
|
46
|
18 |
|
$imageString = '<img src="' . $imgUrl . '" />'; |
|
47
|
|
|
|
|
48
|
18 |
|
$imageString .= $imgCaption ? '<figcaption>' . $imgCaption . '</figcaption>' : ''; |
|
49
|
|
|
|
|
50
|
18 |
|
return '<figure>' . $imageString . '</figure>'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Generate images gallery |
|
55
|
|
|
* @param array $imagesArray Array of images urls |
|
56
|
|
|
* ['http://example.com/image1.jpg', 'http://example.com/image2.jpg'] |
|
57
|
|
|
* @param string|null $header |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
12 |
|
public static function gallery(array $imagesArray, $header = null) |
|
61
|
|
|
{ |
|
62
|
12 |
|
$galleryString = $header ? '<header>' . $header . '</header>' : ''; |
|
63
|
|
|
|
|
64
|
12 |
|
foreach ($imagesArray as $image) { |
|
65
|
12 |
|
$galleryString .= '<img src="' . $image . '" />'; |
|
66
|
6 |
|
} |
|
67
|
|
|
|
|
68
|
12 |
|
return '<div data-block="gallery">' . $galleryString . '</div>'; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Generate share block |
|
73
|
|
|
* @param array|null $networks Array of network names |
|
74
|
|
|
* [Content::SHARE_TYPE_GOOGLE, Content::SHARE_TYPE_TWITTER] |
|
75
|
|
|
* Can be empty, in this way all possible network types will be showed. |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
12 |
|
public static function share(array $networks = null) |
|
79
|
|
|
{ |
|
80
|
6 |
|
$networksString = $networks |
|
81
|
9 |
|
? 'data-network="' . implode(',', $networks) . '"' |
|
82
|
12 |
|
: ''; |
|
83
|
|
|
|
|
84
|
12 |
|
return '<div data-block="share" ' . $networksString . '></div>'; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Generate rating block |
|
89
|
|
|
* @param integer $currentRating |
|
90
|
|
|
* @param integer $maxRating |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
24 |
|
public static function rating($currentRating, $maxRating) |
|
94
|
|
|
{ |
|
95
|
24 |
|
if (($currentRating > $maxRating) || ($maxRating <= 0) || ($currentRating <= 0)) { |
|
96
|
18 |
|
throw new \UnexpectedValueException("Current rating can't be bigger than max value. And max value must be bigger than 0."); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return '<div itemscope="" itemtype="http://schema.org/Rating"> |
|
100
|
6 |
|
<meta itemprop="ratingValue" content="' . $currentRating . '" /> |
|
101
|
6 |
|
<meta itemprop="bestRating" content="' . $maxRating . '" /> |
|
102
|
3 |
|
</div>'; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Generate button |
|
107
|
|
|
* @param string $text |
|
108
|
|
|
* @param string $url |
|
109
|
|
|
* @param string $phone Phone number in RFC-3966 format |
|
110
|
|
|
* @param string|null $buttonColor Can be Text or HEX |
|
111
|
|
|
* @param string|null $textColor Can be Text or HEX |
|
112
|
|
|
* @param bool $isBoldText |
|
113
|
|
|
* @param bool $isDisabled |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
24 |
|
public static function button($text, $url = '', $phone = '', |
|
117
|
|
|
$buttonColor = null, $textColor = null, |
|
118
|
|
|
$isBoldText = false, $isDisabled = false) |
|
119
|
|
|
{ |
|
120
|
24 |
|
if (!$url && !$phone) { |
|
121
|
6 |
|
throw new \UnexpectedValueException('Please set url or phone number for button'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
18 |
|
$formAction = $url ? $url : 'tel:' . $phone; |
|
125
|
18 |
|
$buttonColorString = $buttonColor ? 'data-background-color="' . $buttonColor . '"' : ''; |
|
126
|
18 |
|
$textColorString = $textColor ? 'data-color="' . $textColor . '"' : ''; |
|
127
|
18 |
|
$isBoldTextString = $isBoldText ? 'data-primary="true"' : ''; |
|
128
|
18 |
|
$isDisabledString = $isDisabled ? 'disabled="true"' : ''; |
|
129
|
|
|
|
|
130
|
|
|
return "<button |
|
131
|
18 |
|
formaction=\"" . $formAction . "\" |
|
132
|
18 |
|
" . $buttonColorString . " |
|
133
|
18 |
|
" . $textColorString . " |
|
134
|
18 |
|
" . $isBoldTextString . " |
|
135
|
18 |
|
" . $isDisabledString . ">" . $text . "</button>"; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Generate comment block |
|
140
|
|
|
* @param string $url URL to comments page |
|
141
|
|
|
* @param array $commentsArray multidimensional or one-dimensional array of comments, |
|
142
|
|
|
* can has unlimited includes, example: |
|
143
|
|
|
* [ |
|
144
|
|
|
* [ |
|
145
|
|
|
* 'author' => 'First Author Name', |
|
146
|
|
|
* 'avatar' => 'http://example.com/user1.jpg', |
|
147
|
|
|
* 'title' => 'Comment Title', |
|
148
|
|
|
* 'subtitle' => '2017-12-10', |
|
149
|
|
|
* 'content' => 'Somme comment text', |
|
150
|
|
|
* 'comments' => [ |
|
151
|
|
|
* [ |
|
152
|
|
|
* 'author' => 'Third Author Name', |
|
153
|
|
|
* 'avatar' => 'http://example.com/user3.jpg', |
|
154
|
|
|
* 'title' => 'Comment Title', |
|
155
|
|
|
* 'subtitle' => '2017-12-12', |
|
156
|
|
|
* 'content' => 'Some answer text' |
|
157
|
|
|
* ], |
|
158
|
|
|
* [ |
|
159
|
|
|
* 'author' => 'Another Author Name', |
|
160
|
|
|
* 'avatar' => 'http://example.com/user4.jpg', |
|
161
|
|
|
* 'title' => 'Comment Title', |
|
162
|
|
|
* 'subtitle' => '2017-12-13', |
|
163
|
|
|
* 'content' => 'Another answer text' |
|
164
|
|
|
* ], |
|
165
|
|
|
* ] |
|
166
|
|
|
* ], |
|
167
|
|
|
* [ |
|
168
|
|
|
* 'author' => 'Second Author Name', |
|
169
|
|
|
* 'avatar' => 'http://example.com/user2.jpg', |
|
170
|
|
|
* 'title' => 'Comment Title', |
|
171
|
|
|
* 'subtitle' => '2017-12-11', |
|
172
|
|
|
* 'content' => 'Some comment text' |
|
173
|
|
|
* ], |
|
174
|
|
|
* ] |
|
175
|
|
|
* @return string |
|
176
|
|
|
*/ |
|
177
|
6 |
|
public static function comment($url, array $commentsArray) |
|
178
|
|
|
{ |
|
179
|
6 |
|
$commentBlock = self::generateCommentBlock($commentsArray); |
|
180
|
|
|
|
|
181
|
6 |
|
return '<div data-block="comments" data-url="' . $url . '">' . $commentBlock . '</div>'; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
6 |
|
private static function generateCommentBlock(array $commentsArray) |
|
185
|
|
|
{ |
|
186
|
6 |
|
$commentBlock = ''; |
|
187
|
|
|
|
|
188
|
6 |
|
foreach ($commentsArray as $commentArray) { |
|
189
|
6 |
|
$author = isset($commentArray['author']) ? 'data-author="' . $commentArray['author'] . '"' : ''; |
|
190
|
6 |
|
$avatar = isset($commentArray['avatar']) ? 'data-avatar-url="' . $commentArray['avatar'] . '"' : ''; |
|
191
|
6 |
|
$subtitle = isset($commentArray['subtitle']) ? 'data-subtitle="' . $commentArray['subtitle'] . '"' : ''; |
|
192
|
|
|
|
|
193
|
|
|
$commentBlock .= '<div |
|
194
|
|
|
data-block="comment" |
|
195
|
6 |
|
' . $author . ' |
|
196
|
6 |
|
' . $avatar . ' |
|
197
|
6 |
|
' . $subtitle . ' |
|
198
|
3 |
|
><div data-block="content">'; |
|
199
|
|
|
|
|
200
|
6 |
|
$commentBlock .= isset($commentArray['title']) ? '<header>' . $commentArray['title'] . '</header>' : ''; |
|
201
|
6 |
|
$commentBlock .= isset($commentArray['content']) ? '<p>' . $commentArray['content'] . '</p></div>' : ''; |
|
202
|
|
|
|
|
203
|
6 |
|
if (isset($commentArray['comments'])) { |
|
204
|
6 |
|
$commentBlock .= '<div data-block="comments">'; |
|
205
|
6 |
|
$commentBlock .= self::generateCommentBlock($commentArray['comments']); |
|
206
|
6 |
|
$commentBlock .= '</div>'; |
|
207
|
3 |
|
} |
|
208
|
|
|
|
|
209
|
6 |
|
$commentBlock .= '</div>'; |
|
210
|
3 |
|
} |
|
211
|
|
|
|
|
212
|
6 |
|
return $commentBlock; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Generate header menu |
|
217
|
|
|
* @param array $menuArray array of arrays with pairs of url and title |
|
218
|
|
|
* [ |
|
219
|
|
|
* ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
|
220
|
|
|
* ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
|
221
|
|
|
* ] |
|
222
|
|
|
* @return string |
|
223
|
|
|
*/ |
|
224
|
6 |
|
private static function generateMenu(array $menuArray) |
|
225
|
|
|
{ |
|
226
|
6 |
|
$menuString = ''; |
|
227
|
|
|
|
|
228
|
6 |
|
foreach ($menuArray as $menuItem) { |
|
229
|
6 |
|
$menuString .= '<a href="' . $menuItem['url'] . '">' . $menuItem['title'] . '</a>'; |
|
230
|
3 |
|
} |
|
231
|
|
|
|
|
232
|
6 |
|
return '<menu>' . $menuString . '</menu>'; |
|
233
|
|
|
} |
|
234
|
|
|
} |