ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
RepositoryOpenGraphExposer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
30 
35 {
36  private bool $fetch_tile_image = false;
37 
38 
40  {
41  // the exposer is interested in any context, BUT the repository context
42  // will be handled differently.
43  return $this->context_collection
44  ->internal()
45  ->external()
46  ->repository();
47  }
48 
49  public function getContentModification(CalledContexts $screen_context_stack): ?ContentModification
50  {
51  $current_context = $this->ensureRepoContext($screen_context_stack)->current();
52  $ref_id = $current_context->getReferenceId()->toInt();
53 
54  if (
55  $ref_id > 0
56  && $this->dic->access()->checkAccess('visible', '', $ref_id)
57  && null !== ($object = $this->getObjectOfContext($current_context))
58  ) {
59  $this->exposeObjectOpenGraphMetaData($object);
60  } else {
62  }
63 
64  return null;
65  }
66 
67  private function exposeObjectOpenGraphMetaData(\ilObject $object): void
68  {
69  $object_translation = \ilObjectTranslation::getInstance($object->getId());
70  $general_meta_data = $this->getGeneralObjectMeta($object->getId());
71 
72  $additional_locale_count = 0;
73  $additional_locales = [];
74 
75  if (null !== $general_meta_data) {
76  foreach ($general_meta_data->getLanguageIds() as $language_id) {
77  $language = $general_meta_data->getLanguage($language_id);
78  if (null !== $language && $language->getLanguageCode() !== $object_translation->getDefaultLanguage()) {
79  $additional_locales[] = $language->getLanguageCode();
80  $additional_locale_count++;
81  }
82  }
83  }
84 
85  $uri = $this->data->uri(\ilLink::_getStaticLink($object->getRefId(), $object->getType()));
86 
87  $image = $this->getPresentationImage($object);
88 
89  $this->globalScreen()->layout()->meta()->addOpenGraphMetaDatum(
90  $this->data->openGraphMetadata()->website(
91  $uri,
92  $image,
93  $object->getPresentationTitle(),
94  $uri->getHost(),
95  $object->getLongDescription() . ' ', // we add a space to ensure the description is not cut off
96  $object_translation->getDefaultLanguage(),
97  (1 < $additional_locale_count) ? array_slice($additional_locales, 1) : []
98  )
99  );
100  }
101 
102  private function exposeDefaultOpenGraphMetaData(): void
103  {
104  $uri = $this->data->uri(ILIAS_HTTP_PATH);
105 
106  $this->globalScreen()->layout()->meta()->addOpenGraphMetaDatum(
107  $this->data->openGraphMetadata()->website(
108  $uri,
109  $this->getDefaultImage(),
110  $this->dic->language()->txt('permission_denied'),
111  $uri->getHost(),
112  )
113  );
114  }
115 
117  {
118  if (!$context->hasReferenceId()) {
119  return null;
120  }
121 
122  try {
123  $current_object = \ilObjectFactory::getInstanceByRefId($context->getReferenceId()->toInt());
125  $current_object = null;
126  } finally {
127  return $current_object;
128  }
129  }
130 
131  protected function getDefaultImage(): OGImage
132  {
133  $image_path_resolver = new \ilImagePathResolver();
134 
135  return $this->data->openGraphMetadata()->image(
136  $this->data->uri(
137  ILIAS_HTTP_PATH . ltrim(
138  $image_path_resolver->resolveImagePath(
139  'logo/Sharing.jpg'
140  ),
141  '.'
142  )
143  ),
144  'image/jpg'
145  );
146  }
147 
148  protected function getGeneralObjectMeta(int $object_id): ?\ilMDGeneral
149  {
150  if (0 < ($meta_id = \ilMDGeneral::_getId($object_id, $object_id))) {
151  $general = new \ilMDGeneral();
152  $general->setMetaId($meta_id);
153 
154  return $general;
155  }
156 
157  return null;
158  }
159 
160  protected function ensureRepoContext(CalledContexts $screen_context_stack): CalledContexts
161  {
162  $collection = new ContextCollection(
163  $this->dic->globalScreen()->tool()->context()->availableContexts()
164  );
165  $collection = $collection->repository();
166 
167  if (!$screen_context_stack->hasMatch($collection)) {
168  $screen_context_stack = $screen_context_stack->repository();
169  }
170  return $screen_context_stack;
171  }
172 
173  protected function getPresentationImage(\ilObject $object): OGImage
174  {
175  $image_factory = $this->dic->ui()->factory()->image();
176  $image = $this->getDefaultImage();
177  if (!$this->fetch_tile_image) {
178  return $image;
179  }
180  try {
181  // Use the tile image if available
182  $tile_image = $object->getObjectProperties()->getPropertyTileImage()->getTileImage();
183  if ($tile_image !== null && $tile_image->getRid() !== null) {
184  $uri_string = $tile_image->getImage($image_factory)->getAdditionalHighResSources()['960']
185  ?? $tile_image->getImage($image_factory)->getSource();
186 
187  $image = $this->data->openGraphMetadata()->image(
188  $this->data->uri($uri_string),
189  'image/jpg'
190  );
191  }
192  } catch (\Throwable $e) {
193  }
194  return $image;
195  }
196 }
$context
Definition: webdav.php:31
static _getId(int $a_rbac_id, int $a_obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$ref_id
Definition: ltiauth.php:67
$general
SECTIONS.
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
static getInstance(int $obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getLongDescription()
get object long description (stored in object_description)
getContentModification(CalledContexts $screen_context_stack)
getPresentationTitle()
get presentation title Normally same as title Overwritten for sessions