This repository has been archived on 2023-12-29. You can view files and clone it, but cannot push or open issues or pull requests.
dogstats/web/pages/smart/controller.php

66 lines
2.2 KiB
PHP
Raw Normal View History

2023-11-26 10:30:00 +01:00
<?php
class Smart extends Page {
function setMenu()
{
$this->menu_text = 'Smart';
$this->menu_image = 'fas fa-robot';
$this->menu_priority = 1;
}
function index()
{
2023-12-11 19:54:21 +01:00
$this->set('user', $_SESSION['user']->data);
2023-11-26 10:30:00 +01:00
$this->set('template', "smart.html.php");
}
function search()
{
2023-11-26 14:00:23 +01:00
$db = new SQLite3(ROOT.DS.'../crawler/data.db', SQLITE3_OPEN_READONLY);
2023-11-26 10:30:00 +01:00
$q = $_REQUEST['q'];
2023-11-26 14:00:23 +01:00
if(!$q || strlen($q) < 3 || strpos($q, ' ') === false)
return partial('error.html', ['errorTitle' => 'Error', 'errorMessage' => 'Error: Bitte Nachname und Vorname eingeben.']);
2023-11-26 14:10:26 +01:00
$stmt = $db->prepare("SELECT DISTINCT(hund) FROM results WHERE teilnehmer LIKE :q");
$stmt->bindValue(':q', $q, SQLITE3_TEXT);
$res = $stmt->execute();
2023-11-26 10:30:00 +01:00
$results = [];
2023-11-26 14:00:23 +01:00
$dogs = [];
2023-11-26 10:30:00 +01:00
while($row = $res->fetchArray())
{
2023-11-26 14:00:23 +01:00
$dogs[] = $row['hund'];
2023-11-26 10:30:00 +01:00
}
2023-11-26 14:00:23 +01:00
if(count($dogs) == 0)
return partial('error.html', ['errorTitle' => 'Error', 'errorMessage' => 'Error: Keine Ergebnisse gefunden. Bitte Nachname und Vorname prüfen.']);
foreach($dogs as $dog)
{
$res = $db->query("SELECT * FROM results WHERE teilnehmer LIKE '$q' AND hund LIKE '$dog'");
while($row = $res->fetchArray())
{
$row['date'] = $db->querySingle("SELECT date FROM events WHERE id = ".$row['event']);
$row['event'] = $db->querySingle("SELECT name FROM events WHERE id = ".$row['event']);
$row['unixtimestamp'] = strtotime($row['date']);
$row['run'] = $db->querySingle("SELECT name FROM runs WHERE id = ".$row['run']);
$row['ago'] = printRelativeTime(time(),$row['unixtimestamp']);
$results[$dog][] = $row;
}
//sort results by date
usort($results[$dog], function($a, $b) {
return $a['unixtimestamp'] <=> $b['unixtimestamp'];
});
}
2023-11-26 10:52:43 +01:00
2023-11-26 14:00:23 +01:00
$this->set('results_dogs', $results);
$this->set('dogs', $dogs);
2023-11-26 10:39:08 +01:00
$this->set('query', $query);
2023-11-26 10:30:00 +01:00
$this->set('template', 'search.html.php');
}
}