<h2>Raikograms</h2>
<?php
+ function raikogram_bgcolor($row, $column) {
+ global $race_colours;
+
+ $headers = $race_colours;
+ $cells = array('', '#f5f5f5');
+
+ if ($row == 0) return $headers[$column % 2];
+ if ($column == 0) return $headers[($row + 1) % 2];
+ return $cells[(($row % 2) + ($column % 2)) % 2];
+ }
+
+ function raikogram_fgcolor($row, $column) {
+ if ($row == 0 || $column == 0) return 'white';
+ return 'black';
+ }
+
+ function raikogram_header($name, $bgcolor, $fgcolor, $width) {
+ echo "[td=\"class: grid, bgcolor: $bgcolor, width: $width%, align: center\"]";
+ echo "[color=$fgcolor]";
+ echo "[b]" . $name . "[/b]";
+ echo "[/color][/td]\n";
+ }
+
function edit_raikogram($race_id, $readonly = false) {
+ global $race_colours;
+
$q = new CourseQuery;
+ $q->joinWith("Course.Race");
$q->joinWith("Course.Station");
$q->join("Station.System");
$q->withColumn("System.Id", "SystemId");
$name = $leg->getSystemName();
$systems[$name] = $id;
}
+ $race_colours = $course[0]->getRace()->getColours();
+ $width = count($systems);
ksort($systems);
if (! $readonly) {
if ($_POST['edit_raikogram'] == 'Update') {
}
form();
}
- echo "<table class=\"raikogram\">\n";
- /* Header. */
- echo "<tr>\n";
- echo "<th></th>\n";
- foreach ($systems as $name_x => $id_x) echo "<th>$name_x</th>\n";
- echo "</tr>\n";
- foreach ($systems as $name_y => $id_y) {
- echo "<tr>\n";
- echo "<th>$name_y</th>\n";
- foreach ($systems as $name_x => $id_x) {
- echo "<td>\n";
- if ($id_x == $id_y) continue;
- $distance = "";
- $rq = new RaikogramQuery;
- $rq->filterBySystem1(min($id_x, $id_y));
- $rq->filterBySystem2(max($id_x, $id_y));
- $raikogram = $rq->findOne();
- if ($raikogram) $distance = $raikogram->getDistance();
- if ($readonly) echo $distance;
- else input("from${id_x}to${id_y}", $distance);
- echo "</td>\n";
+ echo "<div class=\"raikogram\">\n";
+ foreach (array('html', 'bbcode') as $type) {
+ if (! $readonly && $type != 'html') continue;
+ echo "<div class=\"$type\">\n";
+ /* Header. */
+ $row = $column = 0;
+ if ($type == 'html') {
+ echo "<table>\n";
+ echo "<tr>\n";
+ echo "<th></th>\n";
+ foreach ($systems as $name_x => $id_x) echo "<th>$name_x</th>\n";
+ echo "</tr>\n";
}
- echo "</tr>\n";
- }
- echo "<td>";
- echo "</table>\n";
- if (! $readonly) {
- hidden('race_id', $race_id);
- submit("edit_raikogram", "Update");
- end_form();
+ else {
+ echo "<pre>\n";
+ echo "[table]\n";
+ echo "[tr]\n";
+ echo "[td=\"width: $width%\"][/td]\n";
+ foreach ($systems as $name_x => $id_x) raikogram_header($name_x, raikogram_bgcolor($row, $column), raikogram_fgcolor($row, $column++), $width);
+ echo "[/tr]\n";
+ }
+ foreach ($systems as $name_y => $id_y) {
+ $row++;
+ $column = 0;
+ $bgcolor = raikogram_bgcolor($row, $column);
+ $fgcolor = raikogram_fgcolor($row, $column);
+ if ($type == 'html') {
+ echo "<tr>\n";
+ echo "<th>$name_y</th>\n";
+ }
+ else {
+ echo "[tr=\"bgcolor: white\"]\n";
+ raikogram_header($name_y, $bgcolor, $fgcolor, $width);
+ }
+ foreach ($systems as $name_x => $id_x) {
+ $column++;
+ $bgcolor = raikogram_bgcolor($row, $column);
+ $fgcolor = raikogram_fgcolor($row, $column);
+ if ($type == 'html') echo "<td>\n";
+ if ($id_x == $id_y) {
+ if ($type == 'html') echo "</td>\n";
+ else echo "[td=\"class: grid\"][/td]\n";
+ continue;
+ }
+ $td = "class: grid";
+ if ($bgcolor) $td .= ", bgcolor: $bgcolor";
+ $td .= ", align: center";
+ if ($type == 'bbcode') echo "[td=\"$td\"][color=$fgcolor]";
+ $distance = "";
+ $rq = new RaikogramQuery;
+ $rq->filterBySystem1(min($id_x, $id_y));
+ $rq->filterBySystem2(max($id_x, $id_y));
+ $raikogram = $rq->findOne();
+ if ($raikogram) $distance = $raikogram->getDistance();
+ if ($readonly) echo $distance;
+ else input("from${id_x}to${id_y}", $distance);
+ if ($type == 'html') echo "</td>\n";
+ else echo "[/color][/td]\n";
+ }
+ if ($type == 'html') echo "</tr>\n";
+ else echo "[/tr]\n";
+ }
+ if ($type == 'html') echo "</table>\n";
+ else {
+ echo "[/table]\n";
+ echo "</pre>\n";
+ }
+ if (! $readonly) {
+ hidden('race_id', $race_id);
+ submit("edit_raikogram", "Update");
+ end_form();
+ }
+ echo "</div>\n";
}
+ echo "</div>\n";
echo "<hr>\n";
return false;
}
foreach ($races as $race) {
$id = $race->getId();
$name = $race->getName();
+ $flag = $race->getFlag();
$url = "$uri_root/raikogram/$id";
- echo "<p><a href=\"$url\">$name</a></p>\n";
+ echo "<p><a href=\"$url\">$name</a>$flag</p>\n";
}
form();
echo "<p>Edit race raikogram: ";
+/* Copy Raikogram to clipboard. */
+function copy_raikogram(event) {
+ try {
+ var text = event.currentTarget.getElementsByClassName('bbcode')[0].getElementsByTagName('pre')[0].innerHTML;
+ if (text) {
+ var area = document.createElement('textarea');
+ area.style.position = 'fixed';
+ area.style.top = 0;
+ area.style.left = 0;
+ area.style.height = '2em';
+ area.style.width = '2em';
+ area.style.padding = 0;
+ area.style.background = 'transparent';
+ area.value = text;
+ document.body.appendChild(area);
+ area.select();
+ if (document.execCommand('copy')) console.log('Copied bbcode to clipboard.');
+ else console.log('Failed to copy bbcode to clipboard.');
+ document.body.removeChild(area);
+ }
+ }
+ catch (e) {
+ console.log(e);
+ }
+}
+
/* Validate distances. */
function validate() {
var valid = true;
input.addEventListener('change', changed_distance, true);
}
validate();
+
+ for (var raikogram of document.getElementsByClassName('raikogram')) {
+ raikogram.addEventListener('dblclick', copy_raikogram, true);
+ }
}
document.onload = loaded();