1<?php 2/** 3 * DokuWiki Plugin elasticsearch (Form Helper Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Andreas Gohr <gohr@cosmocode.de> 7 * @author Anna Dabrowska <dabrowska@cosmocode.de> 8 */ 9 10use dokuwiki\Form\Form; 11 12class helper_plugin_elasticsearch_form extends DokuWiki_Plugin 13{ 14 15 /** 16 * Replacement for the standard search form 17 * 18 * @param array $aggregations 19 */ 20 public function tpl($aggregations) 21 { 22 global $lang; 23 global $QUERY; 24 25 $searchForm = (new Form(['method' => 'get'], true))->addClass('search-results-form'); 26 $searchForm->setHiddenField('do', 'search'); 27 28 $searchForm->addFieldsetOpen()->addClass('search-form'); 29 $searchForm->addTextInput('q')->val($QUERY)->useInput(false); 30 $searchForm->addButton('', $lang['btn_search'])->attr('type', 'submit'); 31 32 $this->addAdvancedSearch($searchForm, $aggregations); 33 34 $searchForm->addFieldsetClose(); 35 36 echo $searchForm->toHTML(); 37 } 38 39 /** 40 * Advanced search 41 * 42 * @param Form $searchForm 43 * @param array $aggregations 44 */ 45 protected function addAdvancedSearch(Form $searchForm, array $aggregations) 46 { 47 $searchForm->addTagOpen('div') 48 ->addClass('advancedOptions') 49 ->attr('style', 'display: none;') 50 ->attr('aria-hidden', 'true'); 51 52 $this->addNamespaceSelector($searchForm, $aggregations); 53 $this->addDateSelector($searchForm); 54 $searchForm->addTagClose('div'); 55 } 56 57 /** 58 * Namespace filter 59 * 60 * @param Form $searchForm 61 * @param array $aggregations Namespace aggregations 62 */ 63 protected function addNamespaceSelector(Form $searchForm, array $aggregations) 64 { 65 if (!empty($aggregations)) { 66 $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 67 68 // popup toggler 69 $searchForm->addTagOpen('div')->addClass('current'); 70 $searchForm->addHTML($this->getLang('nsp')); 71 $searchForm->addTagClose('div'); 72 73 // options 74 $i = 0; 75 $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 76 foreach ($aggregations as $agg) { 77 $searchForm->addTagOpen('li'); 78 $searchForm->addCheckbox('ns[]')->val($agg['key'])->id('__ns-' . $i); 79 $searchForm->addLabel(shorten('', $agg['key'], 25) . ' (' . $agg['doc_count'] . ')', '__ns-' . $i) 80 ->attr('title', $agg['key']); 81 $searchForm->addTagClose('li'); 82 $i++; 83 } 84 $searchForm->addTagClose('ul'); 85 86 $searchForm->addTagClose('div'); 87 } 88 } 89 90 /** 91 * Date range filter 92 * 93 * @param Form $searchForm 94 */ 95 protected function addDateSelector(Form $searchForm) 96 { 97 global $lang; 98 99 $options = [ 100 'any' => $lang['search_any_time'], 101 'week' => $lang['search_past_7_days'], 102 'month' => $lang['search_past_month'], 103 'year' => $lang['search_past_year'], 104 ]; 105 106 $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 107 108 // popup toggler 109 $searchForm->addTagOpen('div')->addClass('current'); 110 $searchForm->addHTML($this->getLang('lastmod')); 111 $searchForm->addTagClose('div'); 112 113 // options 114 $i = 0; 115 $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 116 foreach ($options as $opt => $label) { 117 $searchForm->addTagOpen('li'); 118 $searchForm->addRadioButton('min')->val($opt)->id('__min-' . $i); 119 $searchForm->addLabel($label, '__min-' . $i); 120 $searchForm->addTagClose('li'); 121 $i++; 122 } 123 $searchForm->addTagClose('ul'); 124 125 $searchForm->addTagClose('div'); 126 } 127} 128