33namespace BookStack \Activity \Tools ;
44
55use BookStack \Activity \Models \Tag ;
6+ use BookStack \Entities \Models \BookChild ;
7+ use BookStack \Entities \Models \Entity ;
8+ use BookStack \Entities \Models \Page ;
69
710class TagClassGenerator
811{
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+ ) {
1715 }
1816
1917 /**
@@ -22,14 +20,23 @@ public function __construct(array $tags)
2220 public function generate (): array
2321 {
2422 $ 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+ }
2535
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- ' ));
3340 }
3441 }
3542
@@ -41,6 +48,22 @@ public function generateAsString(): string
4148 return implode (' ' , $ this ->generate ());
4249 }
4350
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+
4467 protected function normalizeTagClassString (string $ value ): string
4568 {
4669 $ value = str_replace (' ' , '' , strtolower ($ value ));
0 commit comments