ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
RepositoryOpenGraphExposer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
32 
37 {
39 
40  private bool $fetch_tile_image = false;
41 
42 
43  public function __construct(\ILIAS\DI\Container $dic)
44  {
45  $this->lom_services = $dic->learningObjectMetadata();
46  parent::__construct($dic);
47  }
48 
50  {
51  // the exposer is interested in any context, BUT the repository context
52  // will be handled differently.
53  return $this->context_collection
54  ->internal()
55  ->external()
56  ->repository();
57  }
58 
59  public function getContentModification(CalledContexts $screen_context_stack): ?ContentModification
60  {
61  $current_context = $this->ensureRepoContext($screen_context_stack)->current();
62  $ref_id = $current_context->getReferenceId()->toInt();
63 
64  if (
65  $ref_id > 0
66  && $this->dic->access()->checkAccess('visible', '', $ref_id)
67  && null !== ($object = $this->getObjectOfContext($current_context))
68  ) {
69  $this->exposeObjectOpenGraphMetaData($object);
70  } else {
72  }
73 
74  return null;
75  }
76 
77  private function exposeObjectOpenGraphMetaData(\ilObject $object): void
78  {
79  $object_translation = \ilObjectTranslation::getInstance($object->getId());
80 
81  $additional_locale_count = 0;
82  $additional_locales = [];
83 
84  foreach ($this->getLanguageCodesFromLOM($object->getId(), $object->getType()) as $language_code) {
85  if ($language_code !== $object_translation->getDefaultLanguage()) {
86  $additional_locales[] = $language_code;
87  $additional_locale_count++;
88  }
89  }
90 
91  $uri = $this->data->uri(\ilLink::_getStaticLink($object->getRefId(), $object->getType()));
92 
93  $image = $this->getPresentationImage($object);
94 
95  $this->globalScreen()->layout()->meta()->addOpenGraphMetaDatum(
96  $this->data->openGraphMetadata()->website(
97  $uri,
98  $image,
99  $object->getPresentationTitle(),
100  $uri->getHost(),
101  $object->getLongDescription() . ' ', // we add a space to ensure the description is not cut off
102  $object_translation->getDefaultLanguage(),
103  (1 < $additional_locale_count) ? array_slice($additional_locales, 1) : []
104  )
105  );
106  }
107 
108  private function exposeDefaultOpenGraphMetaData(): void
109  {
110  $uri = $this->data->uri(ILIAS_HTTP_PATH);
111 
112  $this->globalScreen()->layout()->meta()->addOpenGraphMetaDatum(
113  $this->data->openGraphMetadata()->website(
114  $uri,
115  $this->getDefaultImage(),
116  $this->dic->language()->txt('permission_denied'),
117  $uri->getHost(),
118  )
119  );
120  }
121 
123  {
124  if (!$context->hasReferenceId()) {
125  return null;
126  }
127 
128  try {
129  $current_object = \ilObjectFactory::getInstanceByRefId($context->getReferenceId()->toInt());
131  $current_object = null;
132  } finally {
133  return $current_object;
134  }
135  }
136 
137  protected function getDefaultImage(): OGImage
138  {
139  $image_path_resolver = new \ilImagePathResolver();
140 
141  return $this->data->openGraphMetadata()->image(
142  $this->data->uri(
143  ILIAS_HTTP_PATH . ltrim(
144  $image_path_resolver->resolveImagePath(
145  'logo/Sharing.jpg'
146  ),
147  '.'
148  )
149  ),
150  'image/jpg'
151  );
152  }
153 
157  protected function getLanguageCodesFromLOM(int $object_id, string $object_type): \Generator
158  {
159  $languages_path = $this->lom_services->paths()->languages();
160  $reader = $this->lom_services->read($object_id, 0, $object_type, $languages_path);
161  foreach ($reader->allData($languages_path) as $lang_data) {
162  yield $lang_data->value();
163  }
164  }
165 
166  protected function ensureRepoContext(CalledContexts $screen_context_stack): CalledContexts
167  {
168  $collection = new ContextCollection(
169  $this->dic->globalScreen()->tool()->context()->availableContexts()
170  );
171  $collection = $collection->repository();
172 
173  if (!$screen_context_stack->hasMatch($collection)) {
174  $screen_context_stack = $screen_context_stack->repository();
175  }
176  return $screen_context_stack;
177  }
178 
179  protected function getPresentationImage(\ilObject $object): OGImage
180  {
181  $image_factory = $this->dic->ui()->factory()->image();
182  $image = $this->getDefaultImage();
183  if (!$this->fetch_tile_image) {
184  return $image;
185  }
186  try {
187  // Use the tile image if available
188  $tile_image = $object->getObjectProperties()->getPropertyTileImage()->getTileImage();
189  if ($tile_image !== null && $tile_image->getRid() !== null) {
190  $uri_string = $tile_image->getImage($image_factory)->getAdditionalHighResSources()['960']
191  ?? $tile_image->getImage($image_factory)->getSource();
192 
193  $image = $this->data->openGraphMetadata()->image(
194  $this->data->uri($uri_string),
195  'image/jpg'
196  );
197  }
198  } catch (\Throwable $e) {
199  }
200  return $image;
201  }
202 }
$context
Definition: webdav.php:31
Interface Observer Contains several chained tasks and infos about them.
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
$ref_id
Definition: ltiauth.php:66
Class HTTPServicesTest.
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)
__construct(Container $dic, ilPlugin $plugin)
getLongDescription()
get object long description (stored in object_description)
$dic
Definition: ltiresult.php:33
getContentModification(CalledContexts $screen_context_stack)
getPresentationTitle()
get presentation title Normally same as title Overwritten for sessions