xref: /plugin/combo/ComboStrap/Tag/ShareTag.php (revision 04fd306c7c155fa133ebb3669986875d65988276)
1<?php
2
3namespace ComboStrap\Tag;
4
5use ComboStrap\Brand;
6use ComboStrap\BrandButton;
7use ComboStrap\BrandTag;
8use ComboStrap\ExceptionCompile;
9use ComboStrap\ExceptionNotFound;
10use ComboStrap\ExceptionRuntime;
11use ComboStrap\ExecutionContext;
12use ComboStrap\Icon;
13use ComboStrap\LogUtility;
14use ComboStrap\MarkupCacheDependencies;
15use ComboStrap\MarkupPath;
16use ComboStrap\PluginUtility;
17use ComboStrap\TagAttributes;
18
19class ShareTag
20{
21    const MARKUP = "share";
22    const CANONICAL = "share";
23
24
25    /**
26     * @param TagAttributes $shareAttributes
27     * @param $state
28     * @return string
29     */
30    public static function renderSpecialEnter(TagAttributes $shareAttributes, $state): string
31    {
32
33        /**
34         * The channel
35         */
36        try {
37            $brandButton = BrandTag::createButtonFromAttributes($shareAttributes, BrandButton::TYPE_BUTTON_SHARE);
38        } catch (ExceptionCompile $e) {
39            return LogUtility::wrapInRedForHtml("The brand creation returns an error ({$e->getMessage()}");
40        }
41
42        $rendererHtml = "";
43
44        /**
45         * Snippet
46         */
47        try {
48            $style = $brandButton->getStyle();
49        } catch (ExceptionCompile $e) {
50            $rendererHtml .= LogUtility::wrapInRedForHtml("The style of the share button ($brandButton) could not be determined. Error: {$e->getMessage()}");
51            return $rendererHtml;
52        }
53        $snippetId = $brandButton->getStyleScriptIdentifier();
54        PluginUtility::getSnippetManager()->attachCssInternalStyleSheet($snippetId, $style);
55
56        /**
57         * Standard link attribute
58         * and Runtime Cache key dependencies
59         */
60        try {
61            ExecutionContext::getActualOrCreateFromEnv()
62                ->getExecutingMarkupHandler()
63                ->getOutputCacheDependencies()
64                ->addDependency(MarkupCacheDependencies::REQUESTED_PAGE_DEPENDENCY);
65        } catch (ExceptionNotFound $e) {
66            // not a fetcher markup run
67        }
68
69        try {
70            $requestedPage = MarkupPath::createFromRequestedPage();
71        } catch (ExceptionNotFound $e) {
72            return LogUtility::wrapInRedForHtml("Share Error: Requested Page Not Found: ({$e->getMessage()}");
73        }
74        try {
75            $type = $shareAttributes->getType();
76            $buttonAttributes = $brandButton->getHtmlAttributes($requestedPage)
77                ->setType($type)
78                ->setLogicalTag($shareAttributes->getLogicalTag());
79        } catch (ExceptionCompile $e) {
80            return LogUtility::wrapInRedForHtml("The social channel creation returns an error when creating the link ({$e->getMessage()}");
81        }
82
83        /**
84         * Add the link
85         */
86        $rendererHtml = $buttonAttributes->toHtmlEnterTag("button");
87
88        /**
89         * Icon
90         */
91        if ($brandButton->hasIcon()) {
92            try {
93                $iconAttributes = $brandButton->getIconAttributes();
94                $tagIconAttributes = TagAttributes::createFromCallStackArray($iconAttributes);
95                $rendererHtml .= Icon::createFromTagAttributes($tagIconAttributes)
96                    ->toHtml();
97            } catch (ExceptionCompile $e) {
98                $message = "Getting the icon for the social channel ($brandButton) returns an error ({$e->getMessage()}";
99                if (PluginUtility::isDevOrTest()) {
100                    throw new ExceptionRuntime($message, self::CANONICAL, 1, $e);
101                }
102                $rendererHtml .= LogUtility::wrapInRedForHtml($message);
103                // don't return because the anchor link is open
104            }
105        }
106
107        /**
108         * When empty tag, close the link
109         */
110        if ($state === DOKU_LEXER_SPECIAL) {
111            $rendererHtml .= "</button>";
112        }
113
114        return $rendererHtml;
115
116
117
118    }
119
120    public static function getKnownTypes(): array
121    {
122        return Brand::getBrandNamesForButtonType(BrandButton::TYPE_BUTTON_SHARE);
123    }
124
125    public static function renderExit(): string
126    {
127        return "</button>";
128    }
129
130}
131