1<?php
2
3namespace ComboStrap;
4
5use dokuwiki\Extension\Plugin;
6
7class Message
8{
9
10
11    const SIGNATURE_CLASS = "signature";
12    const TAG = "message";
13    private $content = "";
14    private $type = self::TYPE_CLASSIC;
15
16    const TYPE_CLASSIC = 'Classic';
17    const TYPE_WARNING = 'Warning';
18
19    /**
20     * @var Plugin
21     */
22    private $plugin;
23    private $signatureCanonical;
24    private $signatureName;
25    /**
26     * @var TagAttributes
27     */
28    private $tagAttributes;
29
30    /**
31     * @param Plugin $plugin
32     */
33    public function __construct($plugin = null)
34    {
35        $this->plugin = $plugin;
36        $this->tagAttributes = TagAttributes::createEmpty("message")
37            ->addClassName("alert")
38            ->addHtmlAttributeValue("role", "alert");
39    }
40
41
42    public function addContent($message)
43    {
44        $this->content .= $message;
45    }
46
47    public function setType($type)
48    {
49        $this->type = $type;
50    }
51
52    public function setSignatureCanonical($canonical)
53    {
54        $this->signatureCanonical = $canonical;
55    }
56
57    public function setClass($class)
58    {
59        $this->tagAttributes->addClassName($class);
60    }
61
62    public function getContent()
63    {
64        return $this->content;
65    }
66
67    public function getType()
68    {
69        return $this->type;
70    }
71
72    public function setSignatureName($signatureName)
73    {
74        $this->signatureName = $signatureName;
75    }
76
77    /**
78     * Used when sending message and in the main content
79     * @return string
80     */
81    public function toHtml()
82    {
83
84        PluginUtility::getSnippetManager()->upsertCssSnippetForRequest(self::TAG);
85        $message = "";
86        if ($this->getContent() <> "") {
87
88            if ($this->getType() == Message::TYPE_CLASSIC) {
89                $this->tagAttributes->addClassName("alert-success");
90            } else {
91                $this->tagAttributes->addClassName("alert-warning");
92            }
93
94            $message = $this->tagAttributes->toHtmlEnterTag("div");
95            $message .= $this->getContent();
96
97            /**
98             * If this is a test call without a plugin
99             * we have no plugin attached
100             */
101            $firedByLang = "This message was fired by the ";
102            if($this->plugin!=null){
103                $firedByLang = $this->plugin->getLang('message_come_from');
104            }
105
106            $message .= '<div class="' . self::SIGNATURE_CLASS . '">' . $firedByLang . PluginUtility::getUrl($this->signatureCanonical, $this->signatureName, false) . '</div>';
107            $message .= '</div>';
108
109            /**
110             * In dev, to spot the XHTML compliance error
111             */
112            if (PluginUtility::isDevOrTest()){
113                 $isXml = XmlUtility::isXml($message);
114                 if (!$isXml){
115                     LogUtility::msg("This message is not xml compliant ($message)");
116                     $message =<<<EOF
117<div class='alert alert-warning'>
118    <p>This message is not xml compliant</p>
119    <pre>$message</pre>
120</div>
121EOF;
122                 }
123            }
124
125        }
126        return $message;
127    }
128
129}
130