bundles/dieschittigs/iconic/src/Resources/contao/classes/IconicHooks.php line 111

Open in your IDE?
  1. <?php
  2. namespace Contao;
  3. use DieSchittigs\IconicWorld\Controller\IconicApiController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Jaybizzle\CrawlerDetect\CrawlerDetect;
  6. class IconicHooks
  7. {
  8. protected static $description = null;
  9. protected static $title = null;
  10. protected static $titleOverride = null;
  11. protected static $image = null;
  12. public function buildSearchIndex($carry)
  13. {
  14. if (!count($carry)) return [];
  15. $other = $carry[0];
  16. $controller = System::getContainer()->get(IconicApiController::class);
  17. $request = Request::create('/api/directory');
  18. $request->request->set('locale', strpos($other, 'iconic-world.de') !== false ? 'de' : 'en');
  19. $data = $controller->getDirectoryEntries($request);
  20. $urls = array_column($data, 'url');
  21. $continue = false;
  22. foreach ($carry as $key => $url) {
  23. $matches = [];
  24. preg_match('/directory$/', $url, $matches);
  25. if (count($matches)) {
  26. $continue = true;
  27. break;
  28. }
  29. }
  30. if (!$continue) return $carry;
  31. return array_merge($carry, $urls);
  32. }
  33. public function collectContentElementImages($model, $buffer)
  34. {
  35. if ($model->type === 'directory') {
  36. $request = System::getContainer()->get('request_stack')->getCurrentRequest();
  37. $alias = $request->attributes->get('id');
  38. if (!$alias) return $buffer;
  39. $controller = System::getContainer()->get(IconicApiController::class);
  40. $request = Request::create('/api/directory');
  41. $request->request->set('locale', $GLOBALS['TL_LANGUAGE']);
  42. $data = $controller->getDirectoryEntries($request);
  43. $key = array_search($alias, array_column($data, 'slug'));
  44. if ($key === false) return $buffer;
  45. $entry = $data[$key];
  46. $host = \Environment::get('url');
  47. foreach ($entry['images'] as $image) {
  48. self::addMeta('og:image', "<meta property=\"og:image\" content=\"$host{$image['original']}\" />");
  49. if (!self::$image) self::$image = ltrim($image['original'], '/');
  50. break;
  51. }
  52. $description = [];
  53. if ($entry['texts']['title']) $description[] = $entry['texts']['title'];
  54. if ($entry['texts']['description']) $description[] = htmlspecialchars(strip_tags($entry['texts']['description']));
  55. $description = implode(' - ', $description);
  56. self::$description = $description;
  57. self::$titleOverride = $entry['name'];
  58. $name = htmlspecialchars($entry['name']);
  59. self::addMeta('og:description', "<meta property=\"og:description\" content=\"$description\" />");
  60. self::addMeta('og:title', "<meta property=\"og:title\" content=\"$name - Iconic World\" />");
  61. self::addMeta('twitter:card', "<meta name=\"twitter:card\" content=\"summary_large_image\" />");
  62. $image = self::$image;
  63. self::addMeta('json-ld', "<script type=\"application/ld+json\">
  64. {
  65. \"@context\" : \"http://schema.org\",
  66. \"@type\" : \"Product\",
  67. \"name\" : \"$name\",
  68. \"description\" : \"$description\",
  69. \"image\" : \"$host/$image\"
  70. }
  71. </script>");
  72. }
  73. return $buffer;
  74. }
  75. public function collectNewsImage($template, $data, $module)
  76. {
  77. if ($module->type !== 'newsreader') return;
  78. if (!self::$title) self::$title = $data['headline'];
  79. $file = FilesModel::findByPk($data['singleSRC']);
  80. if ($file && is_file(TL_ROOT . '/' . $file->path)) {
  81. $host = \Environment::get('url');
  82. self::addMeta('og:image', "<meta property=\"og:image\" content=\"$host/{$file->path}\" />");
  83. if (!self::$image) self::$image = $file->path;
  84. }
  85. }
  86. public function collectArticleImages($page, $column)
  87. {
  88. if (isset($GLOBALS['ICONIC_METATAGS']['og:image'])) return;
  89. $articles = \ArticleModel::findPublishedByPidAndColumn($page, $column);
  90. if (!$articles) return;
  91. $request = System::getContainer()->get('request_stack')->getCurrentRequest();
  92. $host = $request->getSchemeAndHttpHost();
  93. foreach ($articles as $article) {
  94. if (!$article->subGallery) continue;
  95. $images = StringUtil::deserialize($article->orderSRC);
  96. if (!$images) continue;
  97. $files = FilesModel::findMultipleByUuids($images);
  98. if (!$files) continue;
  99. $first = $files[0];
  100. $url = $host . '/' . $first->path;
  101. self::addMeta('og:image', "<meta property=\"og:image\" content=\"$url\" />");
  102. if (!self::$image) self::$image = $first->path;
  103. break;
  104. }
  105. }
  106. public function addMetaTags(\PageModel $page, \LayoutModel $layout)
  107. {
  108. self::addMeta('twitter:card', "<meta name=\"twitter:card\" content=\"summary\" />");
  109. self::addMeta('twitter:site', "<meta name=\"twitter:site\" content=\"@DesignInGermany\" />");
  110. $url = Environment::get('host') . strtok(Environment::get('requestUri'), '?');
  111. self::addMeta('og:url', "<meta property=\"og:url\" content=\"$url\" />");
  112. if (self::$title) {
  113. $title = self::$title;
  114. self::addMeta('og:title', "<meta property=\"og:title\" content=\"{$title}\" />");
  115. }
  116. // Override title for Crawlers
  117. if (self::$titleOverride) {
  118. $detector = new CrawlerDetect();
  119. if ($detector->isCrawler()) {
  120. $page->pageTitle = self::$titleOverride;
  121. }
  122. }
  123. if (self::$description) {
  124. $page->description = self::$description;
  125. }
  126. if (self::$image) {
  127. try {
  128. $path = TL_ROOT . '/web/' . self::$image;
  129. $size = getimagesize($path);
  130. if ($size) {
  131. self::addMeta('og:image:width', "<meta property=\"og:image:width\" content=\"{$size[0]}\" />");
  132. self::addMeta('og:image:height', "<meta property=\"og:image:height\" content=\"{$size[1]}\" />");
  133. }
  134. } catch (\Exception $e) {}
  135. }
  136. self::addMeta('og:title', "<meta property=\"og:title\" content=\"{$page->title} - {$page->parentPageTitle}\" />");
  137. self::addMeta('og:description', "<meta property=\"og:description\" content=\"{$page->description}\" />");
  138. global $objPage;
  139. $root = PageModel::findById($objPage->rootId);
  140. if($root->showInternalOgTags) {
  141. $GLOBALS['TL_HEAD'][] = implode("\n", $GLOBALS['ICONIC_METATAGS']);
  142. }
  143. }
  144. public static function addMeta($name, $value, $override = false)
  145. {
  146. if (!isset($GLOBALS['ICONIC_METATAGS'])) $GLOBALS['ICONIC_METATAGS'] = [];
  147. if (isset($GLOBALS['ICONIC_METATAGS'][$name]) && !$override) return;
  148. $GLOBALS['ICONIC_METATAGS'][$name] = $value;
  149. }
  150. public static function frontendLogin(User $user)
  151. {
  152. /*
  153. if ($user instanceof FrontendUser) {
  154. $objMemberGroup = \MemberGroupModel::findById($user->groups['0']);
  155. $arrJumpTo = unserialize($objMemberGroup->langJumpTo);
  156. $strRedirect = \PageModel::findById($arrJumpTo[$GLOBALS['TL_LANGUAGE']]['link'])->getAbsoluteUrl();
  157. \Controller::redirect($strRedirect);
  158. }
  159. */
  160. }
  161. }