|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelCards; |
|
4
|
|
|
|
|
5
|
|
|
class LaravelCards |
|
6
|
|
|
{ |
|
7
|
|
|
/*protected $cardModelConfig = []; |
|
8
|
|
|
|
|
9
|
|
|
public function __construct() |
|
10
|
|
|
{ |
|
11
|
|
|
$this->cardModelConfig = config('laravel-cards.models.card'); |
|
12
|
|
|
}*/ |
|
13
|
|
|
|
|
14
|
|
|
/**************************************************************************/ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Provide the card data array (card_title, card_body, card_image). |
|
18
|
|
|
* |
|
19
|
|
|
* @param int $cardId |
|
20
|
|
|
* @return \DavideCasiraghi\LaravelCards\Models\Card $ret |
|
21
|
|
|
**/ |
|
22
|
3 |
|
public static function getCard($cardId) |
|
23
|
|
|
{ |
|
24
|
|
|
//$cardModel = $this->cardModelConfig['class']; |
|
25
|
3 |
|
$cardModel = config('laravel-cards.models.card.class'); |
|
26
|
3 |
|
$ret = $cardModel::where('id', $cardId)->first(); |
|
27
|
|
|
|
|
28
|
3 |
|
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+(card_id)=\[([^]]*)] |
|
44
|
|
|
\h*\#}/x'; |
|
45
|
|
|
|
|
46
|
|
|
if (preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0)) { |
|
47
|
|
|
return $matches; |
|
48
|
|
|
} else { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
5 |
|
} |
|
52
|
4 |
|
|
|
53
|
|
|
/**************************************************************************/ |
|
54
|
1 |
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Returns the plugin parameters. |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $matches |
|
59
|
|
|
* @return array $ret |
|
60
|
|
|
**/ |
|
61
|
|
|
public static function getSnippetParameters($matches) |
|
62
|
|
|
{ |
|
63
|
|
|
$ret = []; |
|
64
|
|
|
|
|
65
|
|
|
// Get activation string parameters (from article) |
|
66
|
3 |
|
$ret['token'] = $matches[0]; |
|
67
|
|
|
//dump($matches); |
|
68
|
3 |
|
|
|
69
|
|
|
$ret['card_id'] = $matches[2]; |
|
70
|
|
|
|
|
71
|
3 |
|
return $ret; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
/**************************************************************************/ |
|
75
|
3 |
|
|
|
76
|
3 |
|
/** |
|
77
|
3 |
|
* Return the same text with the cards HTML replaced |
|
78
|
3 |
|
* where the token strings has been found. |
|
79
|
3 |
|
* |
|
80
|
3 |
|
* @param string $text |
|
81
|
3 |
|
* @return string $ret |
|
82
|
3 |
|
**/ |
|
83
|
3 |
|
public function replace_card_snippets_with_template($text) |
|
84
|
|
|
{ |
|
85
|
|
|
$matches = self::getCardSnippetOccurrences($text); |
|
86
|
|
|
|
|
87
|
|
|
if ($matches) { |
|
|
|
|
|
|
88
|
3 |
|
foreach ($matches as $key => $single_gallery_matches) { |
|
89
|
|
|
$snippetParameters = self::getSnippetParameters($single_gallery_matches); |
|
90
|
3 |
|
$card = self::getCard($snippetParameters['card_id']); |
|
91
|
3 |
|
$cardParameters = ($card) ? $this->getParametersArray($card) : null; |
|
92
|
2 |
|
|
|
93
|
2 |
|
$cardView = self::showCard($card, $cardParameters); |
|
|
|
|
|
|
94
|
2 |
|
$cardHtml = $cardView->render(); |
|
95
|
2 |
|
|
|
96
|
2 |
|
// Substitute the card html to the token that has been found |
|
97
|
2 |
|
$text = str_replace($parameters['token'], $cardHtml, $text); |
|
|
|
|
|
|
98
|
2 |
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$ret = $text; |
|
102
|
|
|
|
|
103
|
3 |
|
return $ret; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/***************************************************************************/ |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Show a Card. |
|
110
|
|
|
* |
|
111
|
|
|
* @param \DavideCasiraghi\LaravelCards\Models\Card $card |
|
112
|
|
|
* @return \Illuminate\Http\Response |
|
113
|
|
|
*/ |
|
114
|
|
|
public function showCard($card, $cardParameters) |
|
115
|
|
|
{ |
|
116
|
|
|
return view('laravel-cards::show-card', compact('card')) |
|
117
|
|
|
->with('cardParameters', $cardParameters); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/***************************************************************************/ |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Return an array with the parameters for the show-card. |
|
124
|
|
|
* @param \DavideCasiraghi\LaravelJumbotronImages\Models\Card $card |
|
|
|
|
|
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function getParametersArray($card) |
|
128
|
|
|
{ |
|
129
|
|
|
$ret = [ |
|
130
|
|
|
'img_col_size_class' => 'col-md-'.$card->img_col_size, |
|
131
|
|
|
'text_col_size_class' => 'col-md-'.(12-$card->img_col_size), |
|
132
|
|
|
'bkg_color' => 'background-color: '.$card->bkg_color.';', |
|
133
|
|
|
'text_color' => 'color: '.$card->text_color.';', |
|
134
|
|
|
'container_wrap' => ($card->container_wrap == 'true') ? 1 : 0, |
|
135
|
|
|
]; |
|
136
|
|
|
|
|
137
|
|
|
switch ($card->img_alignment) { |
|
138
|
|
|
case 'left': |
|
139
|
|
|
$ret['img_col_order_class'] = 'order-md-1'; |
|
140
|
|
|
$ret['text_col_order_class'] = 'order-md-2'; |
|
141
|
|
|
break; |
|
142
|
|
|
case 'right': |
|
143
|
|
|
$ret['img_col_order_class'] = 'order-md-2'; |
|
144
|
|
|
$ret['text_col_order_class'] = 'order-md-1'; |
|
145
|
|
|
break; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $ret; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
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.