1<?php
2
3
4namespace ComboStrap;
5
6
7use ComboStrap\TagAttribute\StyleAttribute;
8use ComboStrap\Web\UrlEndpoint;
9
10/**
11 * Class ImageLink
12 * @package ComboStrap
13 *
14 * A media of image type
15 */
16abstract class ImageLink extends MediaLink
17{
18
19    const LIGHTBOX = "lightbox";
20    const IMG_LINK = "img-link";
21
22
23    /**
24     * This is mandatory for HTML
25     * The alternate text (the title in Dokuwiki media term)
26     *
27     *
28     * TODO: Try to extract it from the metadata file ?
29     *
30     * An img element must have an alt attribute, except under certain conditions.
31     * For details, consult guidance on providing text alternatives for images.
32     * https://www.w3.org/WAI/tutorials/images/
33     */
34    public function getAltNotEmpty(): string
35    {
36        try {
37            return $this->mediaMarkup->getLabel();
38        } catch (ExceptionNotFound $e) {
39            return $this->mediaMarkup->getFetcher()->getLabel();
40        }
41
42    }
43
44    /**
45     * @return string - the HTML of the image inside a link if asked
46     * @throws ExceptionNotFound
47     */
48    public
49    function wrapMediaMarkupWithLink(string $htmlMediaMarkup): string
50    {
51
52        /**
53         * Link to the media
54         *
55         */
56        $linkTagAttributes = TagAttributes::createEmpty()
57            ->setLogicalTag(self::IMG_LINK);
58        // https://www.dokuwiki.org/config:target
59        global $conf;
60        $target = $conf['target']['media'];
61        $linkTagAttributes->addOutputAttributeValueIfNotEmpty("target", $target);
62        if (!empty($target)) {
63            $linkTagAttributes->addOutputAttributeValue("rel", 'noopener');
64        }
65
66        /**
67         * Do we add a link to the image ?
68         */
69        $fetcher = $this->mediaMarkup->getFetcher();
70        if (!($fetcher instanceof IFetcherSource)) {
71            // not an internal image
72            return $htmlMediaMarkup;
73        }
74
75        $isImage = $fetcher->getMime()->isImage();
76        if (!$isImage) {
77            return $htmlMediaMarkup;
78        }
79
80        $dokuPath = $fetcher->getSourcePath();
81        try {
82            $linking = $this->mediaMarkup->getLinking();
83        } catch (ExceptionNotFound $e) {
84            $linking = MediaMarkup::LINKING_DIRECT_VALUE;
85        }
86        if($linking==="default"){
87            $linking = MediaMarkup::LINKING_DIRECT_VALUE;
88        }
89        switch ($linking) {
90            case MediaMarkup::LINKING_LINKONLY_VALUE:
91                // show only a url, no image
92                $href = FetcherRawLocalPath::createFromPath($dokuPath)->getFetchUrl()->toString();
93                $linkTagAttributes->addOutputAttributeValue("href", $href);
94                try {
95                    $title = $this->mediaMarkup->getLabel();
96                } catch (ExceptionNotFound $e) {
97                    $title = $dokuPath->getLastName();
98                }
99                return $linkTagAttributes->toHtmlEnterTag("a") . $title . "</a>";
100            case MediaMarkup::LINKING_NOLINK_VALUE:
101                // show only a the image
102                return $htmlMediaMarkup;
103            case MediaMarkup::LINKING_DIRECT_VALUE:
104                //directly to the image
105                $href = FetcherRawLocalPath::createFromPath($dokuPath)->getFetchUrl()->toString();
106                $linkTagAttributes->addOutputAttributeValue("href", $href);
107                $snippetId = self::LIGHTBOX;
108                $linkTagAttributes->addClassName(StyleAttribute::addComboStrapSuffix($snippetId));
109                $linkingClass = $this->mediaMarkup->getLinkingClass();
110                if ($linkingClass !== null) {
111                    $linkTagAttributes->addClassName($linkingClass);
112                }
113                $snippetManager = PluginUtility::getSnippetManager();
114                $snippetManager->attachJavascriptComboLibrary();
115                $snippetManager->attachJavascriptFromComponentId($snippetId);
116                $snippetManager->attachCssInternalStyleSheet($snippetId);
117                return $linkTagAttributes->toHtmlEnterTag("a") . $htmlMediaMarkup . "</a>";
118
119            case MediaMarkup::LINKING_DETAILS_VALUE:
120                //go to the details media viewer
121                $url = UrlEndpoint::createDetailUrl()
122                    ->addQueryParameter(DokuwikiId::DOKUWIKI_ID_ATTRIBUTE, $dokuPath->getWikiId())
123                    ->addQueryParameter(WikiPath::REV_ATTRIBUTE, $dokuPath->getRevision());
124                $linkTagAttributes->addOutputAttributeValue("href", $url->toString());
125                return $linkTagAttributes->toHtmlEnterTag("a") . $htmlMediaMarkup . "</a>";
126            default:
127                LogUtility::internalError("The linking ($linking) was not processed");
128                return $htmlMediaMarkup;
129
130        }
131
132
133    }
134
135}
136