mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-30 04:23:11 +03:00
@ -12,6 +12,8 @@ use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|||||||
* @property int $id
|
* @property int $id
|
||||||
* @property string $name
|
* @property string $name
|
||||||
* @property string $value
|
* @property string $value
|
||||||
|
* @property int $entity_id
|
||||||
|
* @property string $entity_type
|
||||||
* @property int $order
|
* @property int $order
|
||||||
*/
|
*/
|
||||||
class Tag extends Model
|
class Tag extends Model
|
||||||
|
@ -3,17 +3,15 @@
|
|||||||
namespace BookStack\Activity\Tools;
|
namespace BookStack\Activity\Tools;
|
||||||
|
|
||||||
use BookStack\Activity\Models\Tag;
|
use BookStack\Activity\Models\Tag;
|
||||||
|
use BookStack\Entities\Models\BookChild;
|
||||||
|
use BookStack\Entities\Models\Entity;
|
||||||
|
use BookStack\Entities\Models\Page;
|
||||||
|
|
||||||
class TagClassGenerator
|
class TagClassGenerator
|
||||||
{
|
{
|
||||||
protected array $tags;
|
public function __construct(
|
||||||
|
protected Entity $entity
|
||||||
/**
|
) {
|
||||||
* @param Tag[] $tags
|
|
||||||
*/
|
|
||||||
public function __construct(array $tags)
|
|
||||||
{
|
|
||||||
$this->tags = $tags;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -22,14 +20,23 @@ class TagClassGenerator
|
|||||||
public function generate(): array
|
public function generate(): array
|
||||||
{
|
{
|
||||||
$classes = [];
|
$classes = [];
|
||||||
|
$tags = $this->entity->tags->all();
|
||||||
|
|
||||||
foreach ($this->tags as $tag) {
|
foreach ($tags as $tag) {
|
||||||
$name = $this->normalizeTagClassString($tag->name);
|
array_push($classes, ...$this->generateClassesForTag($tag));
|
||||||
$value = $this->normalizeTagClassString($tag->value);
|
}
|
||||||
$classes[] = 'tag-name-' . $name;
|
|
||||||
if ($value) {
|
if ($this->entity instanceof BookChild && userCan('view', $this->entity->book)) {
|
||||||
$classes[] = 'tag-value-' . $value;
|
$bookTags = $this->entity->book->tags;
|
||||||
$classes[] = 'tag-pair-' . $name . '-' . $value;
|
foreach ($bookTags as $bookTag) {
|
||||||
|
array_push($classes, ...$this->generateClassesForTag($bookTag, 'book-'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->entity instanceof Page && $this->entity->chapter && userCan('view', $this->entity->chapter)) {
|
||||||
|
$chapterTags = $this->entity->chapter->tags;
|
||||||
|
foreach ($chapterTags as $chapterTag) {
|
||||||
|
array_push($classes, ...$this->generateClassesForTag($chapterTag, 'chapter-'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,6 +48,22 @@ class TagClassGenerator
|
|||||||
return implode(' ', $this->generate());
|
return implode(' ', $this->generate());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string[]
|
||||||
|
*/
|
||||||
|
protected function generateClassesForTag(Tag $tag, string $prefix = ''): array
|
||||||
|
{
|
||||||
|
$classes = [];
|
||||||
|
$name = $this->normalizeTagClassString($tag->name);
|
||||||
|
$value = $this->normalizeTagClassString($tag->value);
|
||||||
|
$classes[] = "{$prefix}tag-name-{$name}";
|
||||||
|
if ($value) {
|
||||||
|
$classes[] = "{$prefix}tag-value-{$value}";
|
||||||
|
$classes[] = "{$prefix}tag-pair-{$name}-{$value}";
|
||||||
|
}
|
||||||
|
return $classes;
|
||||||
|
}
|
||||||
|
|
||||||
protected function normalizeTagClassString(string $value): string
|
protected function normalizeTagClassString(string $value): string
|
||||||
{
|
{
|
||||||
$value = str_replace(' ', '', strtolower($value));
|
$value = str_replace(' ', '', strtolower($value));
|
||||||
|
@ -1 +1 @@
|
|||||||
@push('body-class', e((new \BookStack\Activity\Tools\TagClassGenerator($entity->tags->all()))->generateAsString() . ' '))
|
@push('body-class', e((new \BookStack\Activity\Tools\TagClassGenerator($entity))->generateAsString() . ' '))
|
@ -230,4 +230,39 @@ class TagTest extends TestCase
|
|||||||
$resp->assertDontSee('tag-name-<>', false);
|
$resp->assertDontSee('tag-name-<>', false);
|
||||||
$resp->assertSee('tag-name-<>', false);
|
$resp->assertSee('tag-name-<>', false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_parent_tag_classes_visible()
|
||||||
|
{
|
||||||
|
$page = $this->entities->pageWithinChapter();
|
||||||
|
$page->chapter->tags()->create(['name' => 'My Chapter Tag', 'value' => 'abc123']);
|
||||||
|
$page->book->tags()->create(['name' => 'My Book Tag', 'value' => 'def456']);
|
||||||
|
$this->asEditor();
|
||||||
|
|
||||||
|
$html = $this->withHtml($this->get($page->getUrl()));
|
||||||
|
$html->assertElementExists('body.chapter-tag-pair-mychaptertag-abc123');
|
||||||
|
$html->assertElementExists('body.book-tag-pair-mybooktag-def456');
|
||||||
|
|
||||||
|
$html = $this->withHtml($this->get($page->chapter->getUrl()));
|
||||||
|
$html->assertElementExists('body.book-tag-pair-mybooktag-def456');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_parent_tag_classes_not_visible_if_cannot_see_parent()
|
||||||
|
{
|
||||||
|
$page = $this->entities->pageWithinChapter();
|
||||||
|
$page->chapter->tags()->create(['name' => 'My Chapter Tag', 'value' => 'abc123']);
|
||||||
|
$page->book->tags()->create(['name' => 'My Book Tag', 'value' => 'def456']);
|
||||||
|
$editor = $this->users->editor();
|
||||||
|
$this->actingAs($editor);
|
||||||
|
|
||||||
|
$this->permissions->setEntityPermissions($page, ['view'], [$editor->roles()->first()]);
|
||||||
|
$this->permissions->disableEntityInheritedPermissions($page->chapter);
|
||||||
|
|
||||||
|
$html = $this->withHtml($this->get($page->getUrl()));
|
||||||
|
$html->assertElementNotExists('body.chapter-tag-pair-mychaptertag-abc123');
|
||||||
|
$html->assertElementExists('body.book-tag-pair-mybooktag-def456');
|
||||||
|
|
||||||
|
$this->permissions->disableEntityInheritedPermissions($page->book);
|
||||||
|
$html = $this->withHtml($this->get($page->getUrl()));
|
||||||
|
$html->assertElementNotExists('body.book-tag-pair-mybooktag-def456');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user