ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilDidacticTemplateIconFactory.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28
35{
37
41 private array $icon_types = [];
43 private array $assignments = [];
44
45 public function __construct()
46 {
47 global $DIC;
48
49 $this->definition = $DIC['objDefinition'];
50 $this->initTemplates();
51 }
52
54 {
55 if (!isset(self::$instance)) {
56 self::$instance = new self();
57 }
58
59 return self::$instance;
60 }
61
62 public function getIconPathForReference(int $ref_id): string
63 {
65 $type = ilObject::_lookupType($obj_id);
66
67 if (!$type || !$this->supportsCustomIcon($type)) {
68 return '';
69 }
70 $assigned_template = $this->findAssignedTemplate($ref_id);
71 if (!$assigned_template) {
72 return '';
73 }
74
75 $path = $this->getIconPathForTemplate($assigned_template);
76 return $path;
77 }
78
79 protected function getIconPathForTemplate(int $template_id): ?string
80 {
81 foreach ($this->settings->getTemplates() as $template) {
82 if ($template->getId() === $template_id) {
83 return $template->getIconHandler()->getAbsolutePath();
84 }
85 }
86
87 return null;
88 }
89
90 protected function findAssignedTemplate(int $ref_id): int
91 {
92 foreach ($this->assignments as $tpl_id => $assignments) {
93 if (in_array($ref_id, $assignments, true)) {
94 return $tpl_id;
95 }
96 }
97
98 return 0;
99 }
100
107 public function getIconPathForObject(int $obj_id): string
108 {
109 // no support for referenced objects
110 if (!$this->definition->isContainer(ilObject::_lookupType($obj_id))) {
111 return '';
112 }
113 $refs = ilObject::_getAllReferences($obj_id);
114 $ref_id = end($refs);
115
116 return $this->getIconPathForReference((int) $ref_id);
117 }
118
119 protected function supportsCustomIcon(string $type): bool
120 {
121 return in_array($type, $this->icon_types, true);
122 }
123
124 private function initTemplates(): void
125 {
127 $this->icon_types = [];
128
129 $templates = [];
130 foreach ($this->settings->getTemplates() as $tpl) {
131 if ($tpl->getIconIdentifier() !== '') {
132 $templates[] = $tpl->getId();
133 foreach ($tpl->getAssignments() as $assignment) {
134 if (!in_array($assignment, $this->icon_types, true)) {
135 $this->icon_types[] = $assignment;
136 }
137 }
138 }
139 }
140 $this->assignments = ilDidacticTemplateObjSettings::getAssignmentsForTemplates($templates);
141 }
142}
Indicates general problems with the input or output operations.
Definition: IOException.php:28
The IllegalStateException indicates a wrong state of the object.
Icon factory for didactic template custom icons.
static ilDidacticTemplateIconFactory $instance
getIconPathForObject(int $obj_id)
Get icon path for object Not applicable to non container objects, use getIconPathForReference instead...
static getAssignmentsForTemplates(array $template_ids)
parses the objects.xml it handles the xml-description of all ilias objects
static _lookupType(int $id, bool $reference=false)
static _getAllReferences(int $id)
get all reference ids for object ID
static _lookupObjId(int $ref_id)
Interface Location.
Definition: Location.php:33
The filesystem interface provides the public interface for the Filesystem service API consumer.
Definition: Filesystem.php:37
$ref_id
Definition: ltiauth.php:66
$path
Definition: ltiservices.php:30
global $DIC
Definition: shib_login.php:26