1<?php
2
3namespace ComboStrap;
4
5use syntax_plugin_combo_brand;
6
7class BrandListTag
8{
9
10    public const MARKUP = "brand-list";
11
12    public static function render(TagAttributes $tagAttributes): string
13    {
14        try {
15            $brandDictionary = Brand::getBrandDictionary();
16            $brandNames = array_keys($brandDictionary);
17            sort($brandNames);
18        } catch (ExceptionCompile $e) {
19            return "Error while creating the brand list. Error: {$e->getMessage()}";
20        }
21
22        $variants = BrandButton::getVariants();
23
24        $snippetManager = PluginUtility::getSnippetManager();
25        $snippetManager->attachCssInternalStyleSheet("table");
26        $html = <<<EOF
27<table class="table table-non-fluid">
28<thead>
29    <tr>
30    <th scope="col">
31Brand Name
32    </th>
33EOF;
34        foreach ($variants as $variant) {
35            $iconType = ucfirst($variant[BrandTag::ICON_ATTRIBUTE]);
36            $widgetType = ucfirst($variant[TagAttributes::TYPE_KEY]);
37            $html .= <<<EOF
38    <th scope="col">
39$widgetType <br/> $iconType
40    </th>
41EOF;
42        }
43
44        $html .= <<<EOF
45</tr>
46</thead>
47<tbody>
48EOF;
49
50        $type = $tagAttributes->getType();
51        foreach ($brandNames as $brandName) {
52
53            try {
54
55                $brandButton = new BrandButton($brandName, $type);
56                if (!$brandButton->getBrand()->supportButtonType($type)) {
57                    continue;
58                }
59                /**
60                 * Begin row
61                 */
62                $html .= "<tr>";
63
64                /**
65                 * First column
66                 */
67
68                $html .= "<td>" . ucfirst($brandName) . "</td>";
69
70
71                foreach ($variants as $variant) {
72                    $iconType = $variant[BrandTag::ICON_ATTRIBUTE];
73                    $widgetType = $variant[TagAttributes::TYPE_KEY];
74                    $brandButton
75                        ->setIconType($iconType)
76                        ->setWidget($widgetType);
77                    /**
78                     * Second column
79                     */
80                    $html .= "<td>";
81                    $page = null;
82                    if ($type === BrandButton::TYPE_BUTTON_SHARE) {
83                        $page = MarkupPath::createFromRequestedPage();
84                    }
85                    $brandTagAttributes = $brandButton->getHtmlAttributes($page);
86                    $buttonTag = $brandButton->getHtmlElement($brandTagAttributes);
87                    $html .= $brandTagAttributes->toHtmlEnterTag($buttonTag);
88
89                    if ($brandButton->hasIcon()) {
90                        $iconArrayAttributes = $brandButton->getIconAttributes();
91                        $iconAttributes = TagAttributes::createFromCallStackArray($iconArrayAttributes);
92                        $name = $iconAttributes->getValueAndRemoveIfPresent(FetcherSvg::NAME_ATTRIBUTE);
93                        if ($name !== null) {
94                            $html .= Icon::createFromName($name, $iconAttributes)->toHtml();
95                        } else {
96                            $html .= "Icon name is null for brand $brandName";
97                        }
98                    }
99                    $snippetManager->attachCssInternalStyleSheet($brandButton->getStyleScriptIdentifier(), $brandButton->getStyle());
100                    $html .= "</$buttonTag></td>";
101                }
102
103                /**
104                 * End row
105                 */
106                $html .= "</tr>" . PHP_EOL;
107            } catch (ExceptionCompile $e) {
108                $message = "Error while rendering the brand $brandName. Error: {$e->getMessage()}";
109                if (!PluginUtility::isDevOrTest()) {
110                    $rowSpan = sizeof($variants) + 1; // 1 for the brand column
111                    $html .= "<tr><td rowspan=\"$rowSpan\" class=\"text-danger\">$message</td></tr>";
112                } else {
113                    throw new ExceptionRuntime($message, BrandListTag::MARKUP, 0, $e);
114                }
115            }
116        }
117        /**
118         * End table
119         */
120        $html .= <<<EOF
121</tbody>
122</table>
123EOF;
124        return $html;
125    }
126}
127