source: trunk/Modules/Search/Search.php

Last change on this file was 888, checked in by chronos, 17 months ago
  • Modified: Updated Common package to latest version.
  • Modified: Fixes related to PHP 8.x.
File size: 2.5 KB
Line 
1<?php
2
3class ModuleSearch extends Module
4{
5 var $SearchItems;
6
7 function __construct(System $System)
8 {
9 parent::__construct($System);
10 $this->Name = 'Search';
11 $this->Version = '1.0';
12 $this->Creator = 'Chronos';
13 $this->License = 'GNU/GPL';
14 $this->Description = 'Allow test search in other modules content.';
15 $this->Dependencies = array();
16 $this->SearchItems = array();
17 }
18
19 function DoStart(): void
20 {
21 $this->System->RegisterPage(['search'], 'PageSearch');
22 Core::Cast($this->System)->RegisterPageBarItem('Left', 'Search', array($this, 'ShowSearchBox'));
23 }
24
25 function RegisterSearch($Name, $Title, $Columns, $Query, $Link)
26 {
27 // Query can be table name or subselect query
28 $this->SearchItems[$Name] = array('Name' => $Title, 'Columns' => $Columns,
29 'Query' => $Query, 'Link' => $Link);
30 }
31
32 function UnregisterSearch($Name)
33 {
34 unset($this->SearchItems[$Name]);
35 }
36
37 function ShowSearchBox()
38 {
39 $Output = '<strong>'.T('Search').':</strong>'.
40 '<form action="'.$this->System->Link('/search/').'" method="get"><div>'.
41 '<table>'.
42 '<tr>'.
43 '<td><input type="text" name="text" size="13" /></td>'.
44 '</tr>'.
45 '<tr>'.
46 '<th><input type="submit" value="'.T('Do search').'" /></th>'.
47 '</tr>'.
48 '</table></div>'.
49 '</form>';
50 return $Output;
51 }
52}
53
54class PageSearch extends Page
55{
56 function Show(): string
57 {
58 $this->Title = T('Search');
59 if (array_key_exists('text', $_GET)) $Search = $_GET['text'];
60 else if (array_key_exists('text', $_POST)) $Search = $_POST['text'];
61 else $Search = '';
62 $SearchHTML = urlencode($Search);
63
64 $Output = '<table class="BaseTable"><tr><th>'.T('Section').'</th><th>'.T('Found count').'</th></tr>';
65 foreach ($this->System->ModuleManager->Modules['Search']->SearchItems as $SearchItem)
66 {
67 $ColumnQuery = array();
68 foreach ($SearchItem['Columns'] as $Column)
69 {
70 $ColumnQuery[] = '(`'.$Column.'` LIKE "%'.$Search.'%")';
71 }
72 $ColumnQuery = implode(' OR ', $ColumnQuery);
73 if ($SearchItem['Query'] != '')
74 {
75 $DbResult = $this->Database->query('SELECT COUNT(*) FROM '.$SearchItem['Query'].' WHERE '.$ColumnQuery);
76 $Line = $DbResult->fetch_row();
77 $Line = $Line[0];
78 } else $Line = '';
79 if ($Line <> 0)
80 $Output .= '<tr><td><a href="'.$SearchItem['Link'].$SearchHTML.'">'.$SearchItem['Name'].'</a></td><td>'.$Line.'</td></tr>';
81 }
82
83 $Output .= '</table>';
84 return $Output;
85 }
86}
Note: See TracBrowser for help on using the repository browser.