ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
BadgeParent.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Badge;
22
23use Closure;
26use ilBadge;
27use ilLink;
28use ilObject;
29
31{
33 private readonly Closure $icon;
35 private readonly Closure $references_of;
37 private readonly Closure $link_to;
38
44 public function __construct(
45 private readonly Container $container,
46 $icon = [ilObject::class, '_getIcon'],
47 $references_of = [ilObject::class, '_getAllReferences'],
48 $link_to = [ilLink::class, '_getLink']
49 ) {
50 $this->icon = Closure::fromCallable($icon);
51 $this->references_of = Closure::fromCallable($references_of);
52 $this->link_to = Closure::fromCallable($link_to);
53 }
54
55 public function asComponent(ilBadge $badge): ?Component
56 {
57 $meta_data = $this->metaData($badge);
58 if (null === $meta_data) {
59 return null;
60 }
61
62 $parent_icon = $this->container->ui()->factory()->symbol()->icon()->custom(
63 ($this->icon)($meta_data['id'], 'big', $meta_data['type']),
64 $this->container->language()->txt('obj_' . $meta_data['type']),
65 'medium'
66 );
67
68 $parent_ref_id = current(($this->references_of)($meta_data['id']));
69 if ($parent_ref_id && $this->container->access()->checkAccess('read', '', $parent_ref_id)) {
70 $parent_link = $this->container->ui()
71 ->factory()
72 ->link()
73 ->standard($meta_data['title'], ($this->link_to)($parent_ref_id));
74 } else {
75 $parent_link = $this->container->ui()->factory()->legacy()->content($meta_data['title']);
76 }
77
78 return $this->container->ui()->factory()->listing()->descriptive([
79 $this->container->language()->txt('object') => $this->container->ui()->factory()->legacy()->content(
80 $this->container->ui()->renderer()->render([$parent_icon, $parent_link])
81 )
82 ]);
83 }
84
85 public function asProperty(ilBadge $badge): ?string
86 {
87 $meta_data = $this->metaData($badge);
88 if (null === $meta_data) {
89 return null;
90 }
91
92 $icon = [$this->container->ui()->factory()->symbol()->icon(), 'custom'];
93
94 return implode(' ', [
95 $this->container->ui()->renderer()->render($icon(($this->icon)($meta_data['id'], 'small', $meta_data['type']), $meta_data['title'])),
96 $meta_data['title'],
97 ]);
98 }
99
100 private function metaData(ilBadge $badge): ?array
101 {
102 if (!$badge->getParentId()) {
103 return null;
104 }
105 $parent = $badge->getParentMeta();
106 if ($parent['type'] === 'bdga') {
107 return null;
108 }
109
110 return $parent;
111 }
112}
readonly Closure $references_of
Definition: BadgeParent.php:35
readonly Closure $icon
Definition: BadgeParent.php:33
asComponent(ilBadge $badge)
Definition: BadgeParent.php:55
readonly Closure $link_to
Definition: BadgeParent.php:37
asProperty(ilBadge $badge)
Definition: BadgeParent.php:85
__construct(private readonly Container $container, $icon=[ilObject::class, '_getIcon'], $references_of=[ilObject::class, '_getAllReferences'], $link_to=[ilLink::class, '_getLink'])
Definition: BadgeParent.php:44
metaData(ilBadge $badge)
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
Class ilObject Basic functions for all objects.
A component is the most general form of an entity in the UI.
Definition: Component.php:28
$container
@noRector
Definition: wac.php:37