ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilDidacticTemplateIconFactory.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 declare(strict_types=1);
5 
13 
22 {
23  private static $instance = null;
24 
28  private $definition;
29 
33  private $settings;
34 
38  private $icon_types = [];
39 
43  private $assignments = [];
44 
45  private $logger;
46 
50  public function __construct()
51  {
52  global $DIC;
53 
54  $this->logger = $DIC->logger()->otpl();
55  $this->definition = $DIC['objDefinition'];
56  $this->initTemplates();
57  }
58 
62  public static function getInstance()
63  {
64  if (!isset(self::$instance)) {
65  self::$instance = new self();
66  }
67  return self::$instance;
68  }
69 
73  public function getIconPathForReference(int $ref_id) : string
74  {
75  $obj_id = ilObject::_lookupObjId($ref_id);
76  $type = ilObject::_lookupType($obj_id);
77 
78  if (!$type || !$this->supportsCustomIcon($type)) {
79  return '';
80  }
81  $assigned_template = $this->findAssignedTemplate($ref_id);
82  if (!$assigned_template) {
83  return '';
84  }
85  $path = $this->getIconPathForTemplate($assigned_template);
86  return $path;
87  }
88 
93  protected function getIconPathForTemplate(int $template_id) : string
94  {
95  foreach ($this->settings->getTemplates() as $template) {
96  if ($template->getId() == $template_id) {
97  return $template->getIconHandler()->getAbsolutePath();
98  }
99  }
100  }
101 
106  protected function findAssignedTemplate(int $ref_id) : int
107  {
108  foreach ($this->assignments as $tpl_id => $assignments) {
109  if (in_array($ref_id, $assignments)) {
110  return (int) $tpl_id;
111  }
112  }
113  return 0;
114  }
115 
122  public function getIconPathForObject(int $obj_id) : string
123  {
124  // no support for referenced objects
125  if (!$this->definition->isContainer(ilObject::_lookupType($obj_id))) {
126  return '';
127  }
128  $refs = ilObject::_getAllReferences($obj_id);
129  $ref_id = end($refs);
130  return $this->getIconPathForReference((int) $ref_id);
131  }
132 
137  protected function supportsCustomIcon(string $type) : bool
138  {
139  return in_array($type, $this->icon_types);
140  }
141 
142  private function initTemplates()
143  {
145  $this->icon_types = [];
146 
147  $templates = [];
148  foreach ($this->settings->getTemplates() as $tpl) {
149  if ($tpl->getIconIdentifier() != '') {
150  $templates[] = $tpl->getId();
151  foreach ($tpl->getAssignments() as $assignment) {
152  if (!in_array($assignment, $this->icon_types)) {
153  $this->icon_types[] = $assignment;
154  }
155  }
156  }
157  }
158  $this->assignments = ilDidacticTemplateObjSettings::getAssignmentsForTemplates($templates);
159  }
160 
161 }
settings()
Definition: settings.php:2
Icon factory for didactic template custom icons.
$type
static getInstance()
Get singelton instance.
getIconPathForObject(int $obj_id)
Get icon path for object not applicable to non container objects, use getIconPathForReference instead...
static _getAllReferences($a_id)
get all reference ids of object
static getAssignmentsForTemplates(array $template_ids)
static _lookupObjId($a_id)
global $DIC
Definition: goto.php:24
static _lookupType($a_id, $a_reference=false)
lookup object type
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
__construct()
ilDidacticTemplateIconFactory constructor.