3
3
namespace BookStack \Activity \Tools ;
4
4
5
5
use BookStack \Activity \Models \Tag ;
6
+ use BookStack \Entities \Models \BookChild ;
7
+ use BookStack \Entities \Models \Entity ;
8
+ use BookStack \Entities \Models \Page ;
6
9
7
10
class TagClassGenerator
8
11
{
9
- protected array $ tags ;
10
-
11
- /**
12
- * @param Tag[] $tags
13
- */
14
- public function __construct (array $ tags )
15
- {
16
- $ this ->tags = $ tags ;
12
+ public function __construct (
13
+ protected Entity $ entity
14
+ ) {
17
15
}
18
16
19
17
/**
@@ -22,14 +20,23 @@ public function __construct(array $tags)
22
20
public function generate (): array
23
21
{
24
22
$ classes = [];
23
+ $ tags = $ this ->entity ->tags ->all ();
24
+
25
+ foreach ($ tags as $ tag ) {
26
+ array_push ($ classes , ...$ this ->generateClassesForTag ($ tag ));
27
+ }
28
+
29
+ if ($ this ->entity instanceof BookChild && userCan ('view ' , $ this ->entity ->book )) {
30
+ $ bookTags = $ this ->entity ->book ->tags ;
31
+ foreach ($ bookTags as $ bookTag ) {
32
+ array_push ($ classes , ...$ this ->generateClassesForTag ($ bookTag , 'book- ' ));
33
+ }
34
+ }
25
35
26
- foreach ($ this ->tags as $ tag ) {
27
- $ name = $ this ->normalizeTagClassString ($ tag ->name );
28
- $ value = $ this ->normalizeTagClassString ($ tag ->value );
29
- $ classes [] = 'tag-name- ' . $ name ;
30
- if ($ value ) {
31
- $ classes [] = 'tag-value- ' . $ value ;
32
- $ classes [] = 'tag-pair- ' . $ name . '- ' . $ value ;
36
+ if ($ this ->entity instanceof Page && $ this ->entity ->chapter && userCan ('view ' , $ this ->entity ->chapter )) {
37
+ $ chapterTags = $ this ->entity ->chapter ->tags ;
38
+ foreach ($ chapterTags as $ chapterTag ) {
39
+ array_push ($ classes , ...$ this ->generateClassesForTag ($ chapterTag , 'chapter- ' ));
33
40
}
34
41
}
35
42
@@ -41,6 +48,22 @@ public function generateAsString(): string
41
48
return implode (' ' , $ this ->generate ());
42
49
}
43
50
51
+ /**
52
+ * @return string[]
53
+ */
54
+ protected function generateClassesForTag (Tag $ tag , string $ prefix = '' ): array
55
+ {
56
+ $ classes = [];
57
+ $ name = $ this ->normalizeTagClassString ($ tag ->name );
58
+ $ value = $ this ->normalizeTagClassString ($ tag ->value );
59
+ $ classes [] = "{$ prefix }tag-name- {$ name }" ;
60
+ if ($ value ) {
61
+ $ classes [] = "{$ prefix }tag-value- {$ value }" ;
62
+ $ classes [] = "{$ prefix }tag-pair- {$ name }- {$ value }" ;
63
+ }
64
+ return $ classes ;
65
+ }
66
+
44
67
protected function normalizeTagClassString (string $ value ): string
45
68
{
46
69
$ value = str_replace (' ' , '' , strtolower ($ value ));
0 commit comments