1<?php
2
3namespace ComboStrap;
4
5use ComboStrap\TagAttribute\StyleAttribute;
6
7/**
8 * Search Tag Implementation
9 *
10 * See also Command menu / Command Palettes :
11 *   * https://uiw.tf/cmdk
12 *   * https://tailwindui.com/components/application-ui/navigation/command-palettes
13 */
14class SearchTag
15{
16
17    const TAG = "search";
18    public const COMBO_SEARCH_BOX = "combo-search-box";
19    public const SNIPPET_ID = "search";
20    public const COMBO_DEBOUNCE = "combo-debounce";
21    const CANONICAL = "search";
22
23    public static function render(TagAttributes $tagAttributes): string
24    {
25        global $lang;
26        global $ACT;
27        global $QUERY; // $QUERY = $INPUT->str('q')
28
29        // don't print the search form if search action has been disabled
30        // if (!actionOK('search')) return false;
31
32        /**
33         * Add the debounce dependency first
34         */
35        PluginUtility::getSnippetManager()->attachJavascriptFromComponentId(self::COMBO_DEBOUNCE);
36        PluginUtility::getSnippetManager()->attachJavascriptFromComponentId(self::COMBO_SEARCH_BOX);
37
38        /**
39         * Doku Base is not defined when the
40         * {@link \ComboStrap\TplUtility::CONF_DISABLE_BACKEND_JAVASCRIPT}
41         * is used
42         */
43        $dokuBase = DOKU_BASE;
44        PluginUtility::getSnippetManager()->attachJavascriptFromComponentId(self::SNIPPET_ID, "var DOKU_BASE='$dokuBase';");
45        PluginUtility::getSnippetManager()->attachJavascriptFromComponentId(self::SNIPPET_ID);
46
47        try {
48            $extraClass = $tagAttributes->getClass("");
49        } catch (ExceptionNull $e) {
50            $extraClass = "";
51        }
52
53        try {
54            $id = WikiPath::createRequestedPagePathFromRequest()->getWikiId();
55        } catch (ExceptionNotFound $e) {
56            LogUtility::error($e->getMessage(), self::CANONICAL,$e);;
57            $id = "not_found";
58        }
59        $inputSearchId = 'internal-search-box';
60
61        // https://getbootstrap.com/docs/5.0/getting-started/accessibility/#visually-hidden-content
62        //
63        $visuallyHidden = "sr-only";
64        $bootStrapVersion = Bootstrap::getFromContext()->getMajorVersion();
65        if ($bootStrapVersion == Bootstrap::BootStrapFiveMajorVersion) {
66            $visuallyHidden = "visually-hidden";
67        }
68        if($bootStrapVersion===Bootstrap::BootStrapFourMajorVersion){
69            $formInlineClass = "form-inline";
70        } else {
71            $formInlineClass = "d-flex align-middle mb-0";
72        }
73        $valueKeyProp = "";
74        if ($ACT == 'search') $valueKeyProp = ' value="' . htmlspecialchars($QUERY) . '" ';
75        $browserAutoComplete = 'on';
76        if (!$tagAttributes->getBooleanValue('autocomplete')) {
77            $browserAutoComplete = 'off';
78        }
79        $tagClass = StyleAttribute::addComboStrapSuffix(self::TAG);
80        $action = wl();
81        return <<<EOF
82<form
83    id="dw__search"
84    action="$action"
85    accept-charset="utf-8"
86    method="get"
87    role="search"
88    class="$tagClass $formInlineClass $extraClass"
89    >
90<input type="hidden" name="do" value="search" />
91<input type="hidden" name="id" value="$id" />
92<label class="$visuallyHidden" for="$inputSearchId">Search Term</label>
93<input class="edit form-control" type="text" id="$inputSearchId"  name="q" $valueKeyProp placeholder="{$lang['btn_search']}... (Alt+Shift+F)" autocomplete="$browserAutoComplete" accesskey="f" title="[F]"/>
94</form>
95EOF;
96    }
97
98}
99