1
0
Fork 0
oldhaven/php/index.php

74 lines
2.3 KiB
PHP

<?php
date_default_timezone_set('Europe/Moscow');
$artist = 'Foo Fighters';
if (isset($_GET['q']) && $_GET['q'] != '') {
$q = $_GET['q'];
} else {
$q = $artist;
}
$c = pg_connect('host=localhost port=5432 dbname=beatheaven user=postgres password=password');
$t = pg_query($c, "SELECT * FROM beatheaven.bh_artist WHERE name = '{$q}'");
$artist = pg_fetch_assoc($t);
$t = pg_query($c, "SELECT * FROM beatheaven.bh_release_group WHERE artist_id = {$artist['id']} AND type = 1");
$release_groups = array();
$release_group_ids = array();
while ($row = pg_fetch_assoc($t)) {
$row['releases'] = array();
$release_groups[$row['id']] = $row;
$release_group_ids[] = $row['id'];
}
$t = pg_query($c, "SELECT * FROM beatheaven.bh_release WHERE release_group IN (". implode(',', $release_group_ids) .") AND type=1 ORDER BY date");
while ($row = pg_fetch_assoc($t)) {
$row['tracks'] = array();
$release_groups[$row['release_group']]['releases'][$row['id']] = $row;
$t1 = pg_query($c, "SELECT * FROM beatheaven.bh_track WHERE release_id = '{$row['id']}' ORDER BY position");
while ($row1 = pg_fetch_assoc($t1)) {
$release_groups[$row['release_group']]['releases'][$row['id']]['tracks'][$row1['position']] = $row1;
}
}
usort($release_groups, function($a, $b){
return strtotime($a['year']) > strtotime($b['year']);
});
$artist['albums'] = array();
while(list( $i, $rg ) = each($release_groups)) {
$t_list = array();
$b_list = array();
$tracks = array();
$bonus = array();
$r = current($rg['releases']);
if (count($r['tracks']) > 0) {
while(list( $j, $t ) = each($r['tracks'])) if ($t['name'] != '[silence]') {
$t_list[] = strtolower($t['name']);
$tracks[] = array('id' => $t['id'], 'name' => $t['name'], 'duration' => round($t['length'] / 1000));
}
while(list( $j, $r ) = each($rg['releases'])) {
while(list( $k, $t ) = each($r['tracks'])) if ($t['name'] != '[silence]') {
if (!in_array(strtolower($t['name']), $t_list) && !in_array(strtolower($t['name']), $b_list)) {
$bonus[] = array('id' => $t['id'], 'name' => $t['name'], 'duration' => round($t['length'] / 1000));
$b_list[] = strtolower($t['name']);
}
}
}
}
$artist['albums'][] = array(
'name' => $rg['name'],
'year' => $rg['year'],
'tracks' => $tracks,
'bonus' => $bonus
);
}
include 'tpl/index.php';