[ 'name' => 'Song', 'filter' => false, 'form_type' => null, ], 'tracker' => [ 'name' => 'Tracker', 'filter' => true, 'form_type' => 'multiselect' ], 'compo' => [ 'name' => 'Compo', 'filter' => true, 'form_type' => 'multiselect' ], 'place' => [ 'name' => 'Place', 'filter' => true, 'form_type' => 'multiselect' ], 'date' => [ 'name' => 'Date', 'filter' => false, 'form_type' => 'daterange' ], ]; try { require_once("../src/autoload.php"); // form $input = []; foreach($config as $field_id => $field) { $value = null; $type = $field['form_type'] ?? null; if($type !== null) { if($type === 'multiselect') { $value = $_REQUEST[$field_id] ?? []; if(!is_array($value)) { throw new Exception("eat shit"); } $input[$field_id] = $value; } elseif($type === 'daterange') { $value = $id = $field_id . '-min'; $input[$id] = $_REQUEST[$id] ?? null; $id = $field_id . '-max'; $input[$id] = $_REQUEST[$id] ?? null; } else { throw new Exception("Unhandled form type: " . $field['form_type']); } } // load data $compo_map = Util::list_to_map( DB::query("SELECT * FROM compos" ), 'compoid' ); $song_map = Util::list_to_map( DB::query("SELECT * FROM songs" ), 'songid' ); $tracker_map = Util::list_to_map( DB::query("SELECT * FROM trackers"), 'trackerid' ); $logo_list = array_values(array_diff(scandir("../assets/logos"), ['.', '..'])); // construct view model $vm = []; { } //echo("
");
		//print_r($input);
		///exit;


		// logo 
		$vm['logo'] = Asset::load('/logos/' . $logo_list[rand() % count($logo_list)]);

		// filter configuration
		{
			$config['tracker']['options'] = array_map(function($record) {
				return [ 
					'value' => $record['trackerid'],
					'name'  => $record['nick']
				];
			}, $tracker_map);

			$config['compo']['options'] = array_map(function($record) {
				return [ 
					'value' => $record['compoid'],
					'name'  => $record['title']
				];
			}, $compo_map);

			$config['place']['options'] = array_keys(
				array_flip( 
					array_map(function($record) {
						return $record['place'];
					}, $song_map) 
				)
			);			

			$vm['field_config'] = $config;
		}

		// record data
		{
			$records = [];
			foreach($song_map as $song_id => $song)
			{
				// joins
				$compo   = $compo_map  [$song['compoid'  ]];
				$tracker = $tracker_map[$song['trackerid']];

				// projection
				$records[] = [
					'song'      => $song['title'],
					'song_href' => '/static/compos/' . $compo['directory'] . '/' . $song['filename'],
					'place'     => $song['place'],
					'tracker'   => $tracker['nick'],
					'date'      => $compo['date'],
					'compo'     => $compo['title']
				];
			}		

			$vm['recordset'] = $records;
		}
	}

	








	$min_date = null;
	$max_date = null;
	foreach($records as $record)
	{
		$date = strtotime($record['date']);

		if($min_date === null || $min_date > $date) { 
			$min_date = $date; 
		}
		
		if($max_date === null || $max_date < $date) { 
			$max_date = $date; 
		}
	}
	$min_date = gmdate("Y-m-d", $min_date);
	$max_date = gmdate("Y-m-d", $max_date + 24 * 60 * 60);


	$model = [
		'logo'         => $vm['logo'],
		'field_config' => $vm['field_config'],
		'recordset'    => $vm['recordset'],
		'date_min'     => $min_date,
		'date_max'     => $max_date,
		'search'       => $_REQUEST['search'] ?? ''
	];
}
catch(Throwable $e)
{
	throw $e;
}

include("../assets/templates/index.html");
?>