| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | class PageSearch extends Page
|
|---|
| 4 | {
|
|---|
| 5 | function __construct(System $System)
|
|---|
| 6 | {
|
|---|
| 7 | parent::__construct($System);
|
|---|
| 8 | $this->Title = 'Vyhledávání';
|
|---|
| 9 | $this->Description = 'Hledání v obsahu';
|
|---|
| 10 | $this->ParentClass = 'PagePortal';
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | function Show(): string
|
|---|
| 14 | {
|
|---|
| 15 | $Output = '';
|
|---|
| 16 | if (array_key_exists('t', $_GET)) $Text = $_GET['t'];
|
|---|
| 17 | else $Text = '';
|
|---|
| 18 | $Output .= '<form action="?" method="get">'.
|
|---|
| 19 | 'Hledaný text: <input name="t" value="'.$Text.'"/>'.
|
|---|
| 20 | '<input type="submit" value="Hledat"/>'.
|
|---|
| 21 | '</form>';
|
|---|
| 22 | if ($Text != '')
|
|---|
| 23 | foreach (ModuleSearch::Cast($this->System->GetModule('Search'))->Items as $Item)
|
|---|
| 24 | {
|
|---|
| 25 | $Columns = '';
|
|---|
| 26 | $Condition = '';
|
|---|
| 27 | foreach ($Item['Columns'] as $Column)
|
|---|
| 28 | {
|
|---|
| 29 | $Columns .= ', `'.$Column.'`';
|
|---|
| 30 | $Condition .= ' OR (`'.$Column.'` LIKE "%'.$Text.'%")';
|
|---|
| 31 | }
|
|---|
| 32 | $Columns = substr($Columns, 2);
|
|---|
| 33 | $Condition = substr($Condition, 3);
|
|---|
| 34 | $DbResult = $this->Database->Select($Item['Table'], $Columns, $Condition.' LIMIT '.
|
|---|
| 35 | ModuleSearch::Cast($this->System->GetModule('Search'))->MaxItemCount);
|
|---|
| 36 | if ($DbResult->num_rows > 0) $Output .= '<strong>'.$Item['Name'].'</strong><br/>';
|
|---|
| 37 | while ($Row = $DbResult->fetch_assoc())
|
|---|
| 38 | {
|
|---|
| 39 | $Output .= '<p>';
|
|---|
| 40 | foreach ($Item['Columns'] as $Column)
|
|---|
| 41 | $Output .= $Row[$Column].'<br/>';
|
|---|
| 42 | $Output .= '</p>';
|
|---|
| 43 | }
|
|---|
| 44 | }
|
|---|
| 45 | return $Output;
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | class ModuleSearch extends Module
|
|---|
| 50 | {
|
|---|
| 51 | public array $Items;
|
|---|
| 52 | public int $MaxItemCount;
|
|---|
| 53 |
|
|---|
| 54 | function __construct(System $System)
|
|---|
| 55 | {
|
|---|
| 56 | parent::__construct($System);
|
|---|
| 57 | $this->Name = 'Search';
|
|---|
| 58 | $this->Version = '1.0';
|
|---|
| 59 | $this->Creator = 'Chronos';
|
|---|
| 60 | $this->License = 'GNU/GPLv3';
|
|---|
| 61 | $this->Description = 'Allow search through registered content objects';
|
|---|
| 62 |
|
|---|
| 63 | $this->MaxItemCount = 10;
|
|---|
| 64 | $this->Items = array();
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | function DoStart(): void
|
|---|
| 68 | {
|
|---|
| 69 | $this->System->Pages['search'] = 'PageSearch';
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | function RegisterSearch(string $Title, string $TableName, array $Columns): void
|
|---|
| 73 | {
|
|---|
| 74 | $this->Items[] = array('Name' => $Title, 'Table' => $TableName, 'Columns' => $Columns);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | static function Cast(Module $Module): ModuleSearch
|
|---|
| 78 | {
|
|---|
| 79 | if ($Module instanceof ModuleSearch)
|
|---|
| 80 | {
|
|---|
| 81 | return $Module;
|
|---|
| 82 | }
|
|---|
| 83 | throw new Exception('Expected ModuleSearch type but got '.gettype($Module));
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|