1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Services; |
4
|
|
|
|
5
|
|
|
use App\Helpers\ImageHelpers; |
6
|
|
|
use App\Http\Requests\GlossarySearchRequest; |
7
|
|
|
use App\Http\Requests\GlossaryStoreRequest; |
8
|
|
|
use App\Models\Glossary; |
9
|
|
|
use App\Models\GlossaryVariant; |
10
|
|
|
use App\Repositories\GlossaryRepository; |
11
|
|
|
use App\Repositories\GlossaryVariantRepository; |
12
|
|
|
|
13
|
|
|
class GlossaryService |
14
|
|
|
{ |
15
|
|
|
private GlossaryRepository $glossaryRepository; |
16
|
|
|
private GlossaryVariantRepository $glossaryVariantRepository; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* GlossaryService constructor. |
20
|
17 |
|
* |
21
|
|
|
* @param \App\Repositories\GlossaryRepository $glossaryRepository |
22
|
|
|
* @param \App\Repositories\GlossaryVariantRepository $glossaryVariantRepository |
23
|
17 |
|
*/ |
24
|
17 |
|
public function __construct( |
25
|
|
|
GlossaryRepository $glossaryRepository, |
26
|
|
|
GlossaryVariantRepository $glossaryVariantRepository |
27
|
|
|
) { |
28
|
|
|
$this->glossaryRepository = $glossaryRepository; |
29
|
|
|
$this->glossaryVariantRepository = $glossaryVariantRepository; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
1 |
|
* Create a glossary term |
34
|
|
|
* |
35
|
1 |
|
* @param \App\Http\Requests\GlossaryStoreRequest $request |
36
|
1 |
|
* |
37
|
|
|
* @return \App\Models\Glossary |
38
|
1 |
|
*/ |
39
|
|
|
public function createGlossary(GlossaryStoreRequest $request): Glossary |
40
|
|
|
{ |
41
|
|
|
$glossary = $this->glossaryRepository->store($request->all()); |
42
|
|
|
ImageHelpers::storeImages($glossary, $request, 'introimage'); |
43
|
|
|
|
44
|
|
|
return $glossary; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Update the glossary |
49
|
1 |
|
* |
50
|
|
|
* @param \App\Http\Requests\GlossaryStoreRequest $request |
51
|
1 |
|
* @param int $glossaryId |
52
|
|
|
* |
53
|
1 |
|
* @return \App\Models\Glossary |
54
|
1 |
|
*/ |
55
|
|
|
public function updateGlossary(GlossaryStoreRequest $request, int $glossaryId): Glossary |
56
|
1 |
|
{ |
57
|
|
|
$glossary = $this->glossaryRepository->update($request->all(), $glossaryId); |
58
|
|
|
|
59
|
|
|
ImageHelpers::storeImages($glossary, $request, 'introimage'); |
60
|
|
|
ImageHelpers::deleteImages($glossary, $request, 'introimage'); |
61
|
|
|
|
62
|
|
|
return $glossary; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
1 |
|
* Return the glossary from the database |
67
|
|
|
* |
68
|
1 |
|
* @param int $glossaryId |
69
|
|
|
* |
70
|
|
|
* @return \App\Models\Glossary |
71
|
|
|
*/ |
72
|
|
|
public function getById(int $glossaryId): Glossary |
73
|
|
|
{ |
74
|
|
|
return $this->glossaryRepository->getById($glossaryId); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get all the glossary terms |
79
|
1 |
|
* |
80
|
|
|
* @param int|null $recordsPerPage |
81
|
1 |
|
* @param array|null $searchParameters |
82
|
|
|
* |
83
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator |
84
|
|
|
*/ |
85
|
|
|
public function getGlossaries(int $recordsPerPage = null, array $searchParameters = null) |
86
|
|
|
{ |
87
|
|
|
return $this->glossaryRepository->getAll($recordsPerPage, $searchParameters); |
88
|
|
|
} |
89
|
1 |
|
|
90
|
|
|
/** |
91
|
1 |
|
* Delete the glossary terms from the database |
92
|
1 |
|
* |
93
|
|
|
* @param int $glossaryId |
94
|
|
|
*/ |
95
|
|
|
public function deleteGlossary(int $glossaryId): void |
96
|
|
|
{ |
97
|
|
|
$this->glossaryRepository->delete($glossaryId); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Check in the text if there is any published Glossary Term, |
102
|
|
|
* and make them hoverable to show the tooltip. |
103
|
|
|
* |
104
|
|
|
* It adds also the hidden contents to show in the tooltips |
105
|
2 |
|
* when the terms are hovered. |
106
|
|
|
* |
107
|
2 |
|
* @param string $text |
108
|
2 |
|
* |
109
|
|
|
* @return string |
110
|
2 |
|
*/ |
111
|
1 |
|
public function markGlossaryTerms(string $text): string |
112
|
|
|
{ |
113
|
1 |
|
$glossaryTermsWithVariants = $this->glossaryRepository->getAllWithVariants(); |
114
|
1 |
|
|
115
|
|
|
$count = 1; |
116
|
|
|
$glossaryTermPresent = false; |
117
|
1 |
|
foreach ($glossaryTermsWithVariants as $glossaryTermId => $glossaryTerm) { |
118
|
|
|
foreach ($glossaryTerm->variants as $variant) { |
119
|
|
|
$currentLanguageVariantTerm = $variant->getTranslation('term', app()->getLocale()); |
|
|
|
|
120
|
2 |
|
|
121
|
|
|
$text = $this->replaceGlossaryVariant($currentLanguageVariantTerm, $glossaryTerm->id, $text, $count); |
122
|
|
|
|
123
|
|
|
if (self::variantIsPresent($text, $currentLanguageVariantTerm)) { |
|
|
|
|
124
|
|
|
$glossaryTermPresent = true; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ($glossaryTermPresent) { |
129
|
|
|
$text = $this->attachTermDescription($glossaryTerm, $text); |
130
|
|
|
} |
131
|
3 |
|
|
132
|
|
|
$count++; |
133
|
3 |
|
$glossaryTermPresent = false; |
134
|
2 |
|
} |
135
|
|
|
|
136
|
2 |
|
return $text; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Check if the term is present in the text |
141
|
|
|
* |
142
|
|
|
* @param string $text |
143
|
|
|
* @param string $term |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function variantIsPresent(string $text, string $term): bool |
148
|
2 |
|
{ |
149
|
|
|
if (strpos($text, $term) !== false) { |
150
|
|
|
return true; |
151
|
2 |
|
} |
152
|
|
|
return false; |
153
|
2 |
|
} |
154
|
2 |
|
|
155
|
2 |
|
/** |
156
|
2 |
|
* Replace glossary term |
157
|
2 |
|
* |
158
|
|
|
* @param string $currentLanguageVariantTerm |
159
|
2 |
|
* @param int $glossaryTermId |
160
|
|
|
* @param string $text |
161
|
|
|
* @param int $count |
162
|
|
|
* |
163
|
2 |
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function replaceGlossaryVariant(string $currentLanguageVariantTerm, int $glossaryTermId, string $text, int &$count): string |
166
|
|
|
{ |
167
|
|
|
//$pattern = "/\b$glossaryTerm->term\b/"; |
168
|
|
|
$pattern = "~<a .*?</a>(*SKIP)(*F)|\b$currentLanguageVariantTerm\b~"; |
169
|
|
|
|
170
|
|
|
$text = preg_replace_callback( |
171
|
|
|
$pattern, |
172
|
|
|
function ($matches) use ($currentLanguageVariantTerm, $glossaryTermId, $count) { |
|
|
|
|
173
|
|
|
$glossaryTermTemplate = "<a href='/glossaryTerms/".$glossaryTermId."' class='text-red-700 has-glossary-term glossary-term-".$count."' data-termFoundId='".$count."' data-definitionId='".$glossaryTermId."'>".$currentLanguageVariantTerm."</a>"; |
174
|
2 |
|
return $glossaryTermTemplate; |
175
|
|
|
$count++; |
|
|
|
|
176
|
2 |
|
}, |
177
|
2 |
|
$text |
178
|
2 |
|
); |
179
|
|
|
|
180
|
|
|
return $text; |
181
|
2 |
|
} |
182
|
2 |
|
|
183
|
2 |
|
/** |
184
|
2 |
|
* Attach the term tooltip content to the end of the text |
185
|
2 |
|
* |
186
|
2 |
|
* @param \App\Models\Glossary $glossaryTerm |
187
|
2 |
|
* @param string $text |
188
|
2 |
|
* |
189
|
2 |
|
* @return string |
190
|
2 |
|
*/ |
191
|
2 |
|
public function attachTermDescription(Glossary $glossaryTerm, string $text): string |
192
|
2 |
|
{ |
193
|
2 |
|
$termTooltipContent = "<div class='tooltip-painter' id='glossary-definition-" . $glossaryTerm->id . "' style='display:none'>"; |
194
|
2 |
|
$termTooltipContent .= "<div class='photo'>"; |
195
|
|
|
if($glossaryTerm->hasMedia('introimage')){ |
196
|
2 |
|
$termTooltipContent .= "<img src='".$glossaryTerm->getMedia('introimage')[0]->getUrl('thumb')."' alt=''/>"; |
197
|
2 |
|
} |
198
|
|
|
$termTooltipContent .= "</div>"; |
199
|
|
|
$termTooltipContent .= "<div class='content p-2 overflow-auto'>"; |
200
|
|
|
$termTooltipContent .= "<div class='padder'>"; |
201
|
|
|
$termTooltipContent .= "<div class='title font-bold'>" . ucfirst($glossaryTerm->term) . "</div>"; |
202
|
|
|
$termTooltipContent .= "<div class='description w-72 py-2'>"; |
203
|
|
|
$termTooltipContent .= $glossaryTerm->definition; |
204
|
|
|
$termTooltipContent .= "<br>"; |
205
|
|
|
$termTooltipContent .= "<div class='mt-2'>"; |
206
|
|
|
$termTooltipContent .= "<a href='" . route('glossary.show',$glossaryTerm->id) . "'>Read more</a>"; // route('glossary.show',$glossaryTerm->id) |
207
|
|
|
$termTooltipContent .= "</div>"; |
208
|
|
|
$termTooltipContent .= "</div>"; |
209
|
|
|
$termTooltipContent .= "</div>"; |
210
|
|
|
$termTooltipContent .= "</div>"; |
211
|
|
|
$termTooltipContent .= "</div>"; |
212
|
|
|
|
213
|
|
|
$ret = $text . $termTooltipContent; |
214
|
|
|
return $ret; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Return the title question to show in the Glossary show view |
219
|
|
|
* |
220
|
|
|
* @param \App\Models\Glossary $glossaryTerm |
221
|
|
|
* |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
public function getGlossaryTermTitleQuestion(Glossary $glossaryTerm): string |
225
|
|
|
{ |
226
|
|
|
$ret = ""; |
227
|
|
|
|
228
|
|
|
switch ($glossaryTerm->question_type) { |
229
|
|
|
case 1: |
230
|
|
|
$ret .= __('glossary.what_is'); |
231
|
|
|
break; |
232
|
|
|
case 2: |
233
|
|
|
$ret .= __('glossary.what_does_it_mean'); |
234
|
|
|
break; |
235
|
|
|
} |
236
|
|
|
$ret .= " {$glossaryTerm->term} ?" ; |
237
|
|
|
|
238
|
|
|
return $ret; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
} |
242
|
|
|
|