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