From 3c7a332778d22c09f4d37358d71b9a17a14f9d8a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michael=20Gro=C3=9Fe?= <grosse@cosmocode.de>
Date: Thu, 22 Mar 2018 13:57:43 +0100
Subject: [PATCH] refactor: make tpl_seachform use dokuwiki\Form

This makes it easier to read, reason and extend.
---
 inc/template.php | 41 +++++++++++++++++++++++++++++++----------
 1 file changed, 31 insertions(+), 10 deletions(-)

diff --git a/inc/template.php b/inc/template.php
index c717c82ef..abb4d8267 100644
--- a/inc/template.php
+++ b/inc/template.php
@@ -660,16 +660,37 @@ function tpl_searchform($ajax = true, $autocomplete = true) {
     // don't print the search form if search action has been disabled
     if(!actionOK('search')) return false;
 
-    print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search" method="get" role="search"><div class="no">';
-    print '<input type="hidden" name="do" value="search" />';
-    print '<input type="text" ';
-    if($ACT == 'search') print 'value="'.htmlspecialchars($QUERY).'" ';
-    print 'placeholder="'.$lang['btn_search'].'" ';
-    if(!$autocomplete) print 'autocomplete="off" ';
-    print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[F]" />';
-    print '<button type="submit" title="'.$lang['btn_search'].'">'.$lang['btn_search'].'</button>';
-    if($ajax) print '<div id="qsearch__out" class="ajax_qsearch JSpopup"></div>';
-    print '</div></form>';
+    $searchForm = new dokuwiki\Form\Form([
+        'action' => wl(),
+        'method' => 'get',
+        'role' => 'search',
+        'class' => 'search',
+        'id' => 'dw__search',
+    ]);
+    $searchForm->setHiddenField('do', 'search');
+    $searchForm->addTextInput('id')
+        ->addClass('edit')
+        ->attrs([
+            'title' => '[F]',
+            'accesskey' => 'f',
+            'placeholder' => $lang['btn_search'],
+            'autocomplete' => $autocomplete ? 'on' : 'off',
+        ])
+        ->id('qsearch__in')
+        ->val($ACT === 'search' ? $QUERY : '')
+        ->useInput(false)
+    ;
+    $searchForm->addButton('', $lang['btn_search'])->attrs([
+        'type' => 'submit',
+        'title' => $lang['btn_search'],
+    ]);
+    if ($ajax) {
+        $searchForm->addTagOpen('div')->id('qsearch__out')->addClass('ajax_qsearch JSpopup');
+        $searchForm->addTagClose('div');
+    }
+
+    echo $searchForm->toHTML();
+
     return true;
 }
 
-- 
GitLab