<?php
namespace ApplicationBundle\Modules\LeadGen\Twig;
use ApplicationBundle\Modules\Authentication\Constants\UserConstants;
use ApplicationBundle\Modules\CentralControl\Support\PlatformFlagConfig;
use ApplicationBundle\Modules\LeadGen\Helper\BrandingHelper;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* GR1 — thin twig surface over BrandingHelper so the shared footer partial works on EVERY kind
* of template (public tokened pages get app_id as a var; logged-in prints fall back to the
* session; templates with neither still render — just without the t param).
*
* hb_branding_enabled() — tenant kill switch (acc_setting branding_removal_enabled)
* hb_backlink(surface, appId) — https://ourhoneybee.eu/?ref=<surface>&t=<tenantHash>
* hb_tagline() — the one approved copy line
*/
class BrandingExtension extends AbstractExtension
{
private $em;
private $container;
public function __construct($em, $container = null)
{
$this->em = $em;
$this->container = $container;
}
public function getFunctions()
{
return [
new TwigFunction('hb_branding_enabled', [$this, 'brandingEnabled']),
new TwigFunction('hb_backlink', [$this, 'backlink']),
new TwigFunction('hb_tagline', [$this, 'tagline']),
];
}
/**
* CC7 — central DESIRED state wins when present; otherwise the local acc_setting reader,
* UNCHANGED (fails OPEN to showing branding). appId comes from the template (public tokened
* pages pass it) or the logged-in session. A central hiccup ⇒ centralDesiredRaw() is null ⇒
* local path ⇒ branding shows: the flag can still only ever REMOVE a footer, never break a page.
*/
public function brandingEnabled($appId = 0)
{
$appId = (int) $appId;
if ($appId <= 0) {
$appId = $this->sessionAppId();
}
if ($this->container !== null && $appId > 0) {
$desired = PlatformFlagConfig::centralDesiredRaw($this->container, $appId, 'branding_removal_enabled');
if ($desired !== null) {
return !BrandingHelper::brandingRemoved($desired);
}
}
return BrandingHelper::brandingEnabled($this->em);
}
private function sessionAppId()
{
try {
if ($this->container !== null && $this->container->has('session')) {
$session = $this->container->get('session');
return (int) $session->get(UserConstants::USER_APP_ID, 0);
}
} catch (\Throwable $e) {
// no session (CLI / sessionless) — fall back to local read
}
return 0;
}
public function backlink($surface, $appId = 0)
{
return BrandingHelper::backlink($surface, $appId);
}
public function tagline()
{
return BrandingHelper::TAGLINE;
}
}