chiro-canto/public/search/index.php

120 lines
3.4 KiB
PHP
Executable File

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$root = realpath($_SERVER['DOCUMENT_ROOT']);
require("$root/lang/gettext.php");
require("$root/database/credentials.php");
// Connect the database
try{
$db = new PDO("mysql:host=$host;dbname=$database;charset=utf8",
$user,
$password,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
}catch (Exception $e){
die("Error : ".$e->getMessage());
}
if (isset($_POST['search']))
{
if (isset($_POST['query']))
{
$q = $_POST['query'];
// Search in `authors` table
$req = $db->prepare('SELECT id FROM `authors` WHERE firstname=:q OR lastname=:q OR username=:q OR website=:q');
$req->execute(array(
"q"=>$q
));
$result_author = $req->fetchAll();
if (empty($result_author))
{
$req = $db->prepare('SELECT * FROM `records` WHERE (recordist_name LIKE "%:q%" OR file_name LIKE "%:q%" OR species LIKE "%:q%" OR subspecies LIKE "%:q%" OR sound_type LIKE "%:q%" OR date=:q OR remarks LIKE "%:q%")');
$req->execute(array(
"q"=>$q
));
$result = $req->fetchAll();
// print_r($result);
} else
{
foreach ($result_author as $row) {
$author_id = $row['id'];
$req = $db->prepare('SELECT * FROM `records` WHERE author_id=:author_id');
$req->execute(array(
"author_id"=>$author_id
));
$result = $req->fetchAll();
}
}
}
} else
{
header('Location: /explore/search');
}
if (empty($result))
{
header('Location: /explore/search');
} else
{
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?=_('Explore')?> | Chiro - Canto</title>
<link rel="stylesheet" type="text/css" href="/styles/style.css">
</head>
<?php
include("$root/analytics/owa.php");
include("$root/analytics/matomo.php");
?>
<body>
<?php include("$root/menu.php");?>
<?php include("$root/header.php");?>
<section>
<h3><?=_('Explore')?></h3>
<h4><?=_('Search Results')?></h4>
<?php
if (empty($result)) {
echo _("No result for this query, please try again.");
} else {
?>
<table>
<thead>
<tr>
<th><?=_('File name')?></th>
<th><?=_('Author')?></th>
<th><?=_('License')?></th>
<th><?=_('Date')?></th>
</tr>
</thead>
<tbody>
<?php
foreach($result as $row) {
?>
<tr>
<td><a href="/explore/spectrograms?record=<?=$row['id']?>"><?=$row['file_name']?></a></td>
<td><?=$row['recordist_name']?></td>
<td><?=$row['license']?></td>
<td><?=$row['date']?> <?=$row['time']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
?>
</section>
<?php include("$root/footer.php");?>
</body>
<script src="/scripts/script.js"></script>
</html>
<?php
}