xref: /plugin/combo/ComboStrap/Tag/ShareTag.php (revision 8b8569b7384a60bf4f631e3aa3bd7f9aab461558)
104fd306cSNickeau<?php
204fd306cSNickeau
304fd306cSNickeaunamespace ComboStrap\Tag;
404fd306cSNickeau
504fd306cSNickeauuse ComboStrap\Brand;
604fd306cSNickeauuse ComboStrap\BrandButton;
704fd306cSNickeauuse ComboStrap\BrandTag;
8*8b8569b7Sgerardnicouse ComboStrap\CallStack;
904fd306cSNickeauuse ComboStrap\ExceptionCompile;
1004fd306cSNickeauuse ComboStrap\ExceptionNotFound;
1104fd306cSNickeauuse ComboStrap\ExceptionRuntime;
1204fd306cSNickeauuse ComboStrap\ExecutionContext;
1304fd306cSNickeauuse ComboStrap\Icon;
1404fd306cSNickeauuse ComboStrap\LogUtility;
1504fd306cSNickeauuse ComboStrap\MarkupCacheDependencies;
1604fd306cSNickeauuse ComboStrap\MarkupPath;
1704fd306cSNickeauuse ComboStrap\PluginUtility;
1804fd306cSNickeauuse ComboStrap\TagAttributes;
1904fd306cSNickeau
2004fd306cSNickeauclass ShareTag
2104fd306cSNickeau{
2204fd306cSNickeau    const MARKUP = "share";
2304fd306cSNickeau    const CANONICAL = "share";
2404fd306cSNickeau
2504fd306cSNickeau
2604fd306cSNickeau    /**
2704fd306cSNickeau     * @param TagAttributes $shareAttributes
2804fd306cSNickeau     * @param $state
2904fd306cSNickeau     * @return string
3004fd306cSNickeau     */
3104fd306cSNickeau    public static function renderSpecialEnter(TagAttributes $shareAttributes, $state): string
3204fd306cSNickeau    {
3304fd306cSNickeau
3404fd306cSNickeau        /**
3504fd306cSNickeau         * The channel
3604fd306cSNickeau         */
3704fd306cSNickeau        try {
3804fd306cSNickeau            $brandButton = BrandTag::createButtonFromAttributes($shareAttributes, BrandButton::TYPE_BUTTON_SHARE);
3904fd306cSNickeau        } catch (ExceptionCompile $e) {
4004fd306cSNickeau            return LogUtility::wrapInRedForHtml("The brand creation returns an error ({$e->getMessage()}");
4104fd306cSNickeau        }
4204fd306cSNickeau
4304fd306cSNickeau        $rendererHtml = "";
4404fd306cSNickeau
4504fd306cSNickeau        /**
4604fd306cSNickeau         * Snippet
4704fd306cSNickeau         */
4804fd306cSNickeau        try {
4904fd306cSNickeau            $style = $brandButton->getStyle();
5004fd306cSNickeau        } catch (ExceptionCompile $e) {
5104fd306cSNickeau            $rendererHtml .= LogUtility::wrapInRedForHtml("The style of the share button ($brandButton) could not be determined. Error: {$e->getMessage()}");
5204fd306cSNickeau            return $rendererHtml;
5304fd306cSNickeau        }
5404fd306cSNickeau        $snippetId = $brandButton->getStyleScriptIdentifier();
5504fd306cSNickeau        PluginUtility::getSnippetManager()->attachCssInternalStyleSheet($snippetId, $style);
5604fd306cSNickeau
5704fd306cSNickeau        /**
5804fd306cSNickeau         * Standard link attribute
5904fd306cSNickeau         * and Runtime Cache key dependencies
6004fd306cSNickeau         */
6104fd306cSNickeau        try {
6204fd306cSNickeau            ExecutionContext::getActualOrCreateFromEnv()
6304fd306cSNickeau                ->getExecutingMarkupHandler()
6404fd306cSNickeau                ->getOutputCacheDependencies()
6504fd306cSNickeau                ->addDependency(MarkupCacheDependencies::REQUESTED_PAGE_DEPENDENCY);
6604fd306cSNickeau        } catch (ExceptionNotFound $e) {
6704fd306cSNickeau            // not a fetcher markup run
6804fd306cSNickeau        }
6904fd306cSNickeau
7004fd306cSNickeau        try {
7104fd306cSNickeau            $requestedPage = MarkupPath::createFromRequestedPage();
7204fd306cSNickeau        } catch (ExceptionNotFound $e) {
7304fd306cSNickeau            return LogUtility::wrapInRedForHtml("Share Error: Requested Page Not Found: ({$e->getMessage()}");
7404fd306cSNickeau        }
7504fd306cSNickeau        try {
7604fd306cSNickeau            $type = $shareAttributes->getType();
7704fd306cSNickeau            $buttonAttributes = $brandButton->getHtmlAttributes($requestedPage)
7804fd306cSNickeau                ->setType($type)
7904fd306cSNickeau                ->setLogicalTag($shareAttributes->getLogicalTag());
8004fd306cSNickeau        } catch (ExceptionCompile $e) {
8104fd306cSNickeau            return LogUtility::wrapInRedForHtml("The social channel creation returns an error when creating the link ({$e->getMessage()}");
8204fd306cSNickeau        }
8304fd306cSNickeau
8404fd306cSNickeau        /**
8504fd306cSNickeau         * Add the link
8604fd306cSNickeau         */
87*8b8569b7Sgerardnico        $element = $brandButton->getHtmlElement($buttonAttributes);
88*8b8569b7Sgerardnico        $rendererHtml = $buttonAttributes->toHtmlEnterTag($element);
8904fd306cSNickeau
9004fd306cSNickeau        /**
9104fd306cSNickeau         * Icon
9204fd306cSNickeau         */
9304fd306cSNickeau        if ($brandButton->hasIcon()) {
9404fd306cSNickeau            try {
9504fd306cSNickeau                $iconAttributes = $brandButton->getIconAttributes();
9604fd306cSNickeau                $tagIconAttributes = TagAttributes::createFromCallStackArray($iconAttributes);
9704fd306cSNickeau                $rendererHtml .= Icon::createFromTagAttributes($tagIconAttributes)
9804fd306cSNickeau                    ->toHtml();
9904fd306cSNickeau            } catch (ExceptionCompile $e) {
10004fd306cSNickeau                $message = "Getting the icon for the social channel ($brandButton) returns an error ({$e->getMessage()}";
10104fd306cSNickeau                if (PluginUtility::isDevOrTest()) {
10204fd306cSNickeau                    throw new ExceptionRuntime($message, self::CANONICAL, 1, $e);
10304fd306cSNickeau                }
10404fd306cSNickeau                $rendererHtml .= LogUtility::wrapInRedForHtml($message);
10504fd306cSNickeau                // don't return because the anchor link is open
10604fd306cSNickeau            }
10704fd306cSNickeau        }
10804fd306cSNickeau
10904fd306cSNickeau        /**
11004fd306cSNickeau         * When empty tag, close the link
11104fd306cSNickeau         */
11204fd306cSNickeau        if ($state === DOKU_LEXER_SPECIAL) {
113*8b8569b7Sgerardnico            $rendererHtml .= "</$element>";
11404fd306cSNickeau        }
11504fd306cSNickeau
11604fd306cSNickeau        return $rendererHtml;
11704fd306cSNickeau
11804fd306cSNickeau
11904fd306cSNickeau    }
12004fd306cSNickeau
12104fd306cSNickeau    public static function getKnownTypes(): array
12204fd306cSNickeau    {
12304fd306cSNickeau        return Brand::getBrandNamesForButtonType(BrandButton::TYPE_BUTTON_SHARE);
12404fd306cSNickeau    }
12504fd306cSNickeau
126*8b8569b7Sgerardnico    public static function renderExit(TagAttributes $tagAttributes): string
12704fd306cSNickeau    {
128*8b8569b7Sgerardnico        try {
129*8b8569b7Sgerardnico            $requestedPage = MarkupPath::createFromRequestedPage();
130*8b8569b7Sgerardnico        } catch (ExceptionNotFound $e) {
131*8b8569b7Sgerardnico            return LogUtility::wrapInRedForHtml("Share Error: Requested Page Not Found: ({$e->getMessage()}");
132*8b8569b7Sgerardnico        }
133*8b8569b7Sgerardnico        try {
134*8b8569b7Sgerardnico
135*8b8569b7Sgerardnico            $brandButton = BrandTag::createButtonFromAttributes($tagAttributes, BrandButton::TYPE_BUTTON_SHARE);
136*8b8569b7Sgerardnico            $element = $brandButton->getHtmlElement($brandButton->getHtmlAttributes($requestedPage));
137*8b8569b7Sgerardnico        } catch (ExceptionCompile $e) {
138*8b8569b7Sgerardnico            LogUtility::internalError($e->getMessage(), self::CANONICAL, $e);
139*8b8569b7Sgerardnico            $element = 'button';
140*8b8569b7Sgerardnico        }
141*8b8569b7Sgerardnico        return "</$element>";
142*8b8569b7Sgerardnico    }
143*8b8569b7Sgerardnico
144*8b8569b7Sgerardnico    public static function handleExit(\Doku_Handler $handler): array
145*8b8569b7Sgerardnico    {
146*8b8569b7Sgerardnico
147*8b8569b7Sgerardnico        $callStack = CallStack::createFromHandler($handler);
148*8b8569b7Sgerardnico        $openingTag = $callStack->moveToPreviousCorrespondingOpeningCall();
149*8b8569b7Sgerardnico        return [PluginUtility::ATTRIBUTES => $openingTag->getAttributes()];
150*8b8569b7Sgerardnico
15104fd306cSNickeau    }
15204fd306cSNickeau
15304fd306cSNickeau}
154