|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelCards; |
|
4
|
|
|
|
|
5
|
|
|
class LaravelCards |
|
6
|
|
|
{ |
|
7
|
|
|
/*protected $postModelConfig = []; |
|
8
|
|
|
|
|
9
|
|
|
public function __construct() |
|
10
|
|
|
{ |
|
11
|
|
|
$this->postModelConfig = config('laravel-cards.models.post'); |
|
12
|
|
|
}*/ |
|
13
|
|
|
|
|
14
|
|
|
/**************************************************************************/ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Provide the post data array (post_title, post_body, post_image). |
|
18
|
|
|
* |
|
19
|
|
|
* @param int $postId |
|
20
|
|
|
* @return \DavideCasiraghi\LaravelCards\Models\Post $ret |
|
21
|
|
|
**/ |
|
22
|
4 |
|
public static function getPost($postId) |
|
23
|
|
|
{ |
|
24
|
|
|
//$postModel = $this->postModelConfig['class']; |
|
25
|
4 |
|
$postModel = config('laravel-cards.models.post.class'); |
|
26
|
4 |
|
$ret = $postModel::where('id', $postId)->first(); |
|
27
|
|
|
|
|
28
|
4 |
|
return $ret; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/**************************************************************************/ |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Find the card snippet occurances in the text. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $text |
|
37
|
|
|
* @return array $matches |
|
38
|
|
|
**/ |
|
39
|
5 |
|
public static function getCardSnippetOccurrences($text) |
|
40
|
|
|
{ |
|
41
|
5 |
|
$re = '/{\# |
|
42
|
|
|
\h+card |
|
43
|
|
|
\h+(post_id|img_alignment|img_col_size|bkg_color|text_color|container_wrap)=\[([^]]*)] |
|
44
|
|
|
\h+((?1))=\[([^]]*)] |
|
45
|
|
|
\h+((?1))=\[([^]]*)] |
|
46
|
|
|
\h+((?1))=\[([^]]*)] |
|
47
|
|
|
\h+((?1))=\[([^]]*)] |
|
48
|
|
|
\h+((?1))=\[([^]]*)] |
|
49
|
|
|
\h*\#}/x'; |
|
50
|
|
|
|
|
51
|
5 |
|
if (preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0)) { |
|
52
|
4 |
|
return $matches; |
|
53
|
|
|
} else { |
|
54
|
1 |
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/**************************************************************************/ |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns the plugin parameters. |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $matches |
|
64
|
|
|
* @return array $ret |
|
65
|
|
|
**/ |
|
66
|
3 |
|
public static function getParameters($matches) |
|
67
|
|
|
{ |
|
68
|
3 |
|
$ret = []; |
|
69
|
|
|
|
|
70
|
|
|
// Get activation string parameters (from article) |
|
71
|
3 |
|
$ret['token'] = $matches[0]; |
|
72
|
|
|
//dump($matches); |
|
73
|
|
|
|
|
74
|
3 |
|
$ret['post_id'] = $matches[2]; |
|
75
|
3 |
|
$ret['img_col_size_class'] = 'col-md-'.$matches[6]; |
|
76
|
3 |
|
$textColSize = 12 - $matches[6]; |
|
77
|
3 |
|
$ret['text_col_size_class'] = 'col-md-'.$textColSize; |
|
78
|
3 |
|
$backgroundColor = $matches[8]; |
|
79
|
3 |
|
$ret['bkg_color'] = 'background-color: '.$backgroundColor.';'; |
|
80
|
3 |
|
$textColor = $matches[10]; |
|
81
|
3 |
|
$ret['text_color'] = 'color: '.$textColor.';'; |
|
82
|
3 |
|
$containerWrap = $matches[12]; |
|
83
|
3 |
|
$ret['container_wrap'] = ($containerWrap == 'true') ? 1 : 0; |
|
84
|
|
|
|
|
85
|
|
|
//dd($ret['bkg_color']); |
|
86
|
|
|
// Image alignment |
|
87
|
|
|
//$ret['img_alignment'] = $matches[4]; |
|
88
|
3 |
|
$imageAlignment = $matches[4]; |
|
89
|
|
|
|
|
90
|
3 |
|
switch ($imageAlignment) { |
|
91
|
3 |
|
case 'left': |
|
92
|
2 |
|
$ret['img_col_order_class'] = 'order-md-1'; |
|
93
|
2 |
|
$ret['text_col_order_class'] = 'order-md-2'; |
|
94
|
2 |
|
break; |
|
95
|
2 |
|
case 'right': |
|
96
|
2 |
|
$ret['img_col_order_class'] = 'order-md-2'; |
|
97
|
2 |
|
$ret['text_col_order_class'] = 'order-md-1'; |
|
98
|
2 |
|
break; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
//dump($ret); |
|
102
|
|
|
|
|
103
|
3 |
|
return $ret; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/**************************************************************************/ |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Prepare the card HTML. |
|
110
|
|
|
* |
|
111
|
|
|
* @param array $parameters |
|
112
|
|
|
* @param \DavideCasiraghi\LaravelCards\Models\Post $post |
|
113
|
|
|
* |
|
114
|
|
|
* @return string $ret |
|
115
|
|
|
**/ |
|
116
|
1 |
|
public static function prepareCardHtml($parameters, $post) |
|
117
|
|
|
{ |
|
118
|
1 |
|
if (! is_null($post)) { |
|
119
|
1 |
|
$ret = "<div class='row featurette' style='".$parameters['bkg_color'].' '.$parameters['text_color']."'>"; |
|
120
|
1 |
|
if ($parameters['container_wrap']) { |
|
121
|
|
|
$ret .= "<div class='container'>"; |
|
122
|
|
|
} |
|
123
|
1 |
|
$ret .= "<div class='text ".$parameters['text_col_size_class'].' my-auto px-4 '.$parameters['text_col_order_class']."'>"; |
|
124
|
1 |
|
$ret .= "<h2 class='featurette-heading mt-5'>".$post['title'].'</h2>'; |
|
125
|
1 |
|
$ret .= "<div class='lead mb-4'>".$post['body'].'</div>'; |
|
126
|
1 |
|
$ret .= '</div>'; |
|
127
|
|
|
|
|
128
|
1 |
|
if (! empty($post['introimage'])) { |
|
129
|
|
|
$ret .= "<div class='image d-none d-md-block ".$parameters['img_col_size_class'].' '.$parameters['img_col_order_class']."' |
|
130
|
|
|
style=' |
|
131
|
|
|
background-size: cover; |
|
132
|
|
|
background-image: url(/storage/images/posts_intro_images/".$post['introimage']."); |
|
133
|
|
|
min-height: 400px; |
|
134
|
|
|
background-position: 50% 50%; |
|
135
|
|
|
'>"; |
|
136
|
|
|
$ret .= '</div>'; |
|
137
|
|
|
|
|
138
|
|
|
$ret .= "<div class='image col-12 d-md-none ".$parameters['img_col_order_class']."'>"; |
|
139
|
|
|
$ret .= "<img class='featurette-image img-fluid mx-auto' src='/storage/images/posts_intro_images/".$post['introimage']."' alt='".$post['introimage_alt']."'>"; |
|
140
|
|
|
$ret .= '</div>'; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/*$ret .= "<div class='image col-12 ".$parameters['img_col_order_class']."'>"; |
|
144
|
|
|
$ret .= "<img class='featurette-image img-fluid mx-auto' src='/storage/images/posts_intro_images/".$post['introimage']."' alt='".$post['introimage_alt']."'>"; |
|
145
|
|
|
$ret .= '</div>';*/ |
|
146
|
|
|
|
|
147
|
1 |
|
if ($parameters['container_wrap']) { |
|
148
|
|
|
$ret .= '</div>'; |
|
149
|
|
|
} |
|
150
|
1 |
|
$ret .= '</div>'; |
|
151
|
|
|
} else { |
|
152
|
|
|
$ret = "<div class='alert alert-warning' role='alert'>The post with id ".$parameters['post_id'].' has not been found.</div>'; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
1 |
|
return $ret; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/**************************************************************************/ |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Return the same text with the cards HTML replaced |
|
162
|
|
|
* where the token strings has been found. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $text |
|
165
|
|
|
* @return string $ret |
|
166
|
|
|
**/ |
|
167
|
2 |
|
public function replace_card_snippets_with_template($text) |
|
168
|
|
|
{ |
|
169
|
2 |
|
$matches = self::getCardSnippetOccurrences($text); |
|
170
|
|
|
|
|
171
|
2 |
|
if ($matches) { |
|
|
|
|
|
|
172
|
2 |
|
foreach ($matches as $key => $single_gallery_matches) { |
|
173
|
2 |
|
$parameters = self::getParameters($single_gallery_matches); |
|
174
|
2 |
|
$post = self::getPost($parameters['post_id']); |
|
175
|
|
|
//$cardHtml = self::prepareCardHtml($parameters, $post); |
|
176
|
|
|
|
|
177
|
2 |
|
$cardView = self::showCard($post, $parameters); |
|
|
|
|
|
|
178
|
2 |
|
$cardHtml = $cardView->render(); |
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
// Substitute the card html to the token that has been found |
|
182
|
2 |
|
$text = str_replace($parameters['token'], $cardHtml, $text); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
2 |
|
$ret = $text; |
|
187
|
|
|
|
|
188
|
2 |
|
return $ret; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
/***************************************************************************/ |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Show a Card. |
|
196
|
|
|
* |
|
197
|
|
|
* @param \DavideCasiraghi\LaravelCards\Models\Post $post |
|
198
|
|
|
* @return \Illuminate\Http\Response |
|
199
|
|
|
*/ |
|
200
|
2 |
|
public function showCard($post, $parameters) |
|
201
|
|
|
{ |
|
202
|
|
|
//$postParameters = ($post) ? $this->getParametersArray($post) : null; |
|
203
|
|
|
|
|
204
|
2 |
|
return view('laravel-cards::show-card', compact('post')) |
|
205
|
2 |
|
->with('parameters', $parameters); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
} |
|
210
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.