<?php
namespace Contao;
use DieSchittigs\IconicWorld\Controller\IconicApiController;
use Symfony\Component\HttpFoundation\Request;
use Jaybizzle\CrawlerDetect\CrawlerDetect;
class IconicHooks
{
protected static $description = null;
protected static $title = null;
protected static $titleOverride = null;
protected static $image = null;
public function buildSearchIndex($carry)
{
if (!count($carry)) return [];
$other = $carry[0];
$controller = System::getContainer()->get(IconicApiController::class);
$request = Request::create('/api/directory');
$request->request->set('locale', strpos($other, 'iconic-world.de') !== false ? 'de' : 'en');
$data = $controller->getDirectoryEntries($request);
$urls = array_column($data, 'url');
$continue = false;
foreach ($carry as $key => $url) {
$matches = [];
preg_match('/directory$/', $url, $matches);
if (count($matches)) {
$continue = true;
break;
}
}
if (!$continue) return $carry;
return array_merge($carry, $urls);
}
public function collectContentElementImages($model, $buffer)
{
if ($model->type === 'directory') {
$request = System::getContainer()->get('request_stack')->getCurrentRequest();
$alias = $request->attributes->get('id');
if (!$alias) return $buffer;
$controller = System::getContainer()->get(IconicApiController::class);
$request = Request::create('/api/directory');
$request->request->set('locale', $GLOBALS['TL_LANGUAGE']);
$data = $controller->getDirectoryEntries($request);
$key = array_search($alias, array_column($data, 'slug'));
if ($key === false) return $buffer;
$entry = $data[$key];
$host = \Environment::get('url');
foreach ($entry['images'] as $image) {
self::addMeta('og:image', "<meta property=\"og:image\" content=\"$host{$image['original']}\" />");
if (!self::$image) self::$image = ltrim($image['original'], '/');
break;
}
$description = [];
if ($entry['texts']['title']) $description[] = $entry['texts']['title'];
if ($entry['texts']['description']) $description[] = htmlspecialchars(strip_tags($entry['texts']['description']));
$description = implode(' - ', $description);
self::$description = $description;
self::$titleOverride = $entry['name'];
$name = htmlspecialchars($entry['name']);
self::addMeta('og:description', "<meta property=\"og:description\" content=\"$description\" />");
self::addMeta('og:title', "<meta property=\"og:title\" content=\"$name - Iconic World\" />");
self::addMeta('twitter:card', "<meta name=\"twitter:card\" content=\"summary_large_image\" />");
$image = self::$image;
self::addMeta('json-ld', "<script type=\"application/ld+json\">
{
\"@context\" : \"http://schema.org\",
\"@type\" : \"Product\",
\"name\" : \"$name\",
\"description\" : \"$description\",
\"image\" : \"$host/$image\"
}
</script>");
}
return $buffer;
}
public function collectNewsImage($template, $data, $module)
{
if ($module->type !== 'newsreader') return;
if (!self::$title) self::$title = $data['headline'];
$file = FilesModel::findByPk($data['singleSRC']);
if ($file && is_file(TL_ROOT . '/' . $file->path)) {
$host = \Environment::get('url');
self::addMeta('og:image', "<meta property=\"og:image\" content=\"$host/{$file->path}\" />");
if (!self::$image) self::$image = $file->path;
}
}
public function collectArticleImages($page, $column)
{
if (isset($GLOBALS['ICONIC_METATAGS']['og:image'])) return;
$articles = \ArticleModel::findPublishedByPidAndColumn($page, $column);
if (!$articles) return;
$request = System::getContainer()->get('request_stack')->getCurrentRequest();
$host = $request->getSchemeAndHttpHost();
foreach ($articles as $article) {
if (!$article->subGallery) continue;
$images = StringUtil::deserialize($article->orderSRC);
if (!$images) continue;
$files = FilesModel::findMultipleByUuids($images);
if (!$files) continue;
$first = $files[0];
$url = $host . '/' . $first->path;
self::addMeta('og:image', "<meta property=\"og:image\" content=\"$url\" />");
if (!self::$image) self::$image = $first->path;
break;
}
}
public function addMetaTags(\PageModel $page, \LayoutModel $layout)
{
self::addMeta('twitter:card', "<meta name=\"twitter:card\" content=\"summary\" />");
self::addMeta('twitter:site', "<meta name=\"twitter:site\" content=\"@DesignInGermany\" />");
$url = Environment::get('host') . strtok(Environment::get('requestUri'), '?');
self::addMeta('og:url', "<meta property=\"og:url\" content=\"$url\" />");
if (self::$title) {
$title = self::$title;
self::addMeta('og:title', "<meta property=\"og:title\" content=\"{$title}\" />");
}
// Override title for Crawlers
if (self::$titleOverride) {
$detector = new CrawlerDetect();
if ($detector->isCrawler()) {
$page->pageTitle = self::$titleOverride;
}
}
if (self::$description) {
$page->description = self::$description;
}
if (self::$image) {
try {
$path = TL_ROOT . '/web/' . self::$image;
$size = getimagesize($path);
if ($size) {
self::addMeta('og:image:width', "<meta property=\"og:image:width\" content=\"{$size[0]}\" />");
self::addMeta('og:image:height', "<meta property=\"og:image:height\" content=\"{$size[1]}\" />");
}
} catch (\Exception $e) {}
}
self::addMeta('og:title', "<meta property=\"og:title\" content=\"{$page->title} - {$page->parentPageTitle}\" />");
self::addMeta('og:description', "<meta property=\"og:description\" content=\"{$page->description}\" />");
global $objPage;
$root = PageModel::findById($objPage->rootId);
if($root->showInternalOgTags) {
$GLOBALS['TL_HEAD'][] = implode("\n", $GLOBALS['ICONIC_METATAGS']);
}
}
public static function addMeta($name, $value, $override = false)
{
if (!isset($GLOBALS['ICONIC_METATAGS'])) $GLOBALS['ICONIC_METATAGS'] = [];
if (isset($GLOBALS['ICONIC_METATAGS'][$name]) && !$override) return;
$GLOBALS['ICONIC_METATAGS'][$name] = $value;
}
public static function frontendLogin(User $user)
{
/*
if ($user instanceof FrontendUser) {
$objMemberGroup = \MemberGroupModel::findById($user->groups['0']);
$arrJumpTo = unserialize($objMemberGroup->langJumpTo);
$strRedirect = \PageModel::findById($arrJumpTo[$GLOBALS['TL_LANGUAGE']]['link'])->getAbsoluteUrl();
\Controller::redirect($strRedirect);
}
*/
}
}