ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilDidacticTemplateIconFactory.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
6 
14 
21 {
22  private static ?ilDidacticTemplateIconFactory $instance = null;
23 
27  private array $icon_types = [];
29  private array $assignments = [];
30 
31  public function __construct()
32  {
33  global $DIC;
34 
35  $this->definition = $DIC['objDefinition'];
36  $this->initTemplates();
37  }
38 
39  public static function getInstance(): ilDidacticTemplateIconFactory
40  {
41  if (!isset(self::$instance)) {
42  self::$instance = new self();
43  }
44 
45  return self::$instance;
46  }
47 
48  public function getIconPathForReference(int $ref_id): string
49  {
50  $obj_id = ilObject::_lookupObjId($ref_id);
51  $type = ilObject::_lookupType($obj_id);
52 
53  if (!$type || !$this->supportsCustomIcon($type)) {
54  return '';
55  }
56  $assigned_template = $this->findAssignedTemplate($ref_id);
57  if (!$assigned_template) {
58  return '';
59  }
60 
61  $path = $this->getIconPathForTemplate($assigned_template);
62  return $path;
63  }
64 
65  protected function getIconPathForTemplate(int $template_id): ?string
66  {
67  foreach ($this->settings->getTemplates() as $template) {
68  if ($template->getId() === $template_id) {
69  return $template->getIconHandler()->getAbsolutePath();
70  }
71  }
72 
73  return null;
74  }
75 
76  protected function findAssignedTemplate(int $ref_id): int
77  {
78  foreach ($this->assignments as $tpl_id => $assignments) {
79  if (in_array($ref_id, $assignments, true)) {
80  return $tpl_id;
81  }
82  }
83 
84  return 0;
85  }
86 
93  public function getIconPathForObject(int $obj_id): string
94  {
95  // no support for referenced objects
96  if (!$this->definition->isContainer(ilObject::_lookupType($obj_id))) {
97  return '';
98  }
99  $refs = ilObject::_getAllReferences($obj_id);
100  $ref_id = end($refs);
101 
102  return $this->getIconPathForReference((int) $ref_id);
103  }
104 
105  protected function supportsCustomIcon(string $type): bool
106  {
107  return in_array($type, $this->icon_types, true);
108  }
109 
110  private function initTemplates(): void
111  {
113  $this->icon_types = [];
114 
115  $templates = [];
116  foreach ($this->settings->getTemplates() as $tpl) {
117  if ($tpl->getIconIdentifier() !== '') {
118  $templates[] = $tpl->getId();
119  foreach ($tpl->getAssignments() as $assignment) {
120  if (!in_array($assignment, $this->icon_types, true)) {
121  $this->icon_types[] = $assignment;
122  }
123  }
124  }
125  }
126  $this->assignments = ilDidacticTemplateObjSettings::getAssignmentsForTemplates($templates);
127  }
128 }
Icon factory for didactic template custom icons.
$type
static _getAllReferences(int $id)
get all reference ids for object ID
getIconPathForObject(int $obj_id)
Get icon path for object Not applicable to non container objects, use getIconPathForReference instead...
$path
Definition: ltiservices.php:32
static _lookupObjId(int $ref_id)
global $DIC
Definition: feed.php:28
parses the objects.xml it handles the xml-description of all ilias objects
$ref_id
Definition: ltiauth.php:67
static getAssignmentsForTemplates(array $template_ids)
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
static _lookupType(int $id, bool $reference=false)
static ilDidacticTemplateIconFactory $instance