1<?php
2
3namespace ComboStrap\Tag;
4
5use ComboStrap\Brand;
6use ComboStrap\BrandButton;
7use ComboStrap\BrandTag;
8use ComboStrap\ExceptionCompile;
9use ComboStrap\Icon;
10use ComboStrap\LogUtility;
11use ComboStrap\PluginUtility;
12use ComboStrap\TagAttributes;
13use syntax_plugin_combo_link;
14
15class FollowTag
16{
17
18
19    public const CANONICAL = FollowTag::MARKUP;
20    public const HANDLE_ATTRIBUTE = "handle";
21    public const MARKUP = "follow";
22
23    public static function getKnownTypes(): array
24    {
25        return Brand::getBrandNamesForButtonType(BrandButton::TYPE_BUTTON_FOLLOW);
26    }
27
28
29    public static function renderExit(): string
30    {
31        return '</a>';
32    }
33
34    public static function renderSpecialEnterNode(TagAttributes $tagAttributes, $state): string
35    {
36
37
38        if (
39            !$tagAttributes->hasAttribute(syntax_plugin_combo_link::MARKUP_REF_ATTRIBUTE)
40            && !$tagAttributes->hasAttribute(FollowTag::HANDLE_ATTRIBUTE)
41        ) {
42            $handleAttribute = FollowTag::HANDLE_ATTRIBUTE;
43            $urlAttribute = BrandTag::URL_ATTRIBUTE;
44            $message = "The brand button does not have any follow url. You need to set at minimum the `$handleAttribute` or `$urlAttribute` attribute";
45            return self::returnErrorString($message, $state);
46        }
47
48        /**
49         * The channel
50         */
51        try {
52            $brand = BrandTag::createButtonFromAttributes($tagAttributes, BrandButton::TYPE_BUTTON_FOLLOW);
53        } catch (ExceptionCompile $e) {
54            $message = "The brand button creation returns an error ({$e->getMessage()}";
55            return self::returnErrorString($message, $state);
56        }
57
58        /**
59         * Add the Icon / CSS / Javascript snippet
60         * It should happen only in rendering
61         */
62        try {
63            $style = $brand->getStyle();
64        } catch (ExceptionCompile $e) {
65            $message = "The style of the share button ($brand) could not be determined. Error: {$e->getMessage()}";
66            return self::returnErrorString($message, $state);
67        }
68        $snippetId = $brand->getStyleScriptIdentifier();
69        PluginUtility::getSnippetManager()->attachCssInternalStyleSheet($snippetId, $style);
70
71
72        /**
73         * Standard link attribute
74         * and add the link
75         */
76        try {
77            $tagAttributes = BrandTag::mixBrandButtonToTagAttributes($tagAttributes, $brand);
78            $html = $tagAttributes->toHtmlEnterTag("a");
79        } catch (ExceptionCompile $e) {
80            $message = "The brand button creation returns an error when creating the link ({$e->getMessage()}";
81            return self::returnErrorString($message, $state);
82        }
83
84        /**
85         * Icon
86         */
87        try {
88            $iconAttributes = TagAttributes::createFromCallStackArray($brand->getIconAttributes());
89            $html .= Icon::createFromTagAttributes($iconAttributes)->toHtml();
90        } catch (ExceptionCompile $e) {
91            $message = "Getting the icon for the brand ($brand) returns an error ({$e->getMessage()}";
92            return self::returnErrorString($message,$state);
93        }
94
95        if ($state === DOKU_LEXER_SPECIAL) {
96            $html .= "</a>";
97        }
98        return $html;
99
100    }
101
102    private static function returnErrorString($message, $state): string
103    {
104
105        $message = LogUtility::wrapInRedForHtml($message);
106        if ($state === DOKU_LEXER_SPECIAL) {
107            return $message;
108        }
109        /**
110         * An empty anchor to return in case of errors
111         * to have a valid document with the exit
112         */
113        return "<a>$message";
114    }
115
116}
117