﻿<?php

/*

================================================================================================
00_Infotext.php
================================================================================================
Function:
- reads the data from JSON file and creates image(s) for 15" ePaper
================================================================================================
Author: Norbert Hofer (nexturban technologies GmbH)
================================================================================================
Version History:
  ----------------------------------------------------------------------------------------------
  1.0.0.0: Production Version (2018-10-31)
  ----------------------------------------------------------------------------------------------
  1.1.0.0: Production Version (2018-11-07)
  - new statusCode from converters implemented to switch between "no departure" and "no data"
  ----------------------------------------------------------------------------------------------
  1.1.1.0: Production Version (2018-12-15)
  - when no realtime.json exists (or content is empty or error <> 0), then we do not 
    create image!
================================================================================================

*/

/* ---------------------------------------------------------------------------------------- */

error_reporting(E_ERROR | E_PARSE);

date_default_timezone_set('Europe/Vienna');

/* ---------------------------------------------------------------------------------------- */

include_once('../System/SvgFunctions.php');

$fontDirectory = getcwd() . '/../Fonts/';

/* ---------------------------------------------------------------------------------------- */

$lines = $_GET['Line'];

/* ---------------------------------------------------------------------------------------- */

if (isset($lines))
{
	// if only one line, json_decode skips the index level - so add level [0] before this single line for loop
	
	if (count($lines) == 1)
	{
		$line = $lines;
		$lines = array();
		$lines[0] = $line;
	}

	// calculate top positions of situation box

	$topSituations = 1500;

	$topSituations -= 20;

	foreach ($lines as $line)
	{
		if ($line['Content'] != '')
		{
			$font = 'OpenSans.ttf';
			$fontFamily = "Arial";

			$size = 44;
			$align = 0;
			
			if (isset($line['Font']))
			{
				$font = $line['Font'];

				if ($font == 'OpenSans-Bold.ttf')
				{
					$fontFamily = "Arial-Bold";
				}

				if ($font == 'OpenSans-Italic.ttf')
				{
					$fontFamily = "Arial-Italic";
				}
			}

			if (isset($line['Size']))
			{
				$size = $line['Size'];
			}

			if (isset($line['Align']))
			{
				$align = $line['Align'];
			}

			$box = svgttfbbox($size, 0, $fontDirectory . $font, 'Ög');
			$boxHeight = abs($box[5] - $box[1]);
			$boxHeight *= 1.2;

			$topSituations -= intval($boxHeight);
		}
	}

	$topSituations -= 20;


	$preview  = '
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="180" height="240" viewBox="0 0 1200 1600">
<defs>
	<style type="text/css">
		@font-face { font-family:"Arial"; src:url("./Fonts/OpenSans.ttf") format("truetype"); }
		@font-face { font-family:"Arial-Bold"; src:url("./Fonts/OpenSans-Bold.ttf") format("truetype"); }
		@font-face { font-family:"Arial-Kursiv"; src:url("./Fonts/OpenSans-Italic.ttf") format("truetype"); }
	</style>
</defs>
<g id="background">
	<rect x="0" y="0" width="1200" height="1600" fill="#fff" />
	<rect x="0" y="0" width="1200" height="150" fill="#000" />
	<text x="600" y="65" text-anchor="middle" font-family="Arial-Bold" font-size="44px" fill="#fff">Friedrichshafen Stadtmitte</text>
	<text x="600" y="115" text-anchor="middle" font-family="Arial" font-size="32px" fill="#fff">11.07.2023 - 19:10</text>
</g>
<g id="header">
	<rect x="0" y="150" width="1200" height="80" fill="#b4b4b4" />
	<text x="100" y="188" text-anchor="middle" font-family="Arial-Bold" font-size="32px" fill="#000">Abfahrt </text>
	<text x="260" y="188" text-anchor="middle" font-family="Arial-Bold" font-size="32px" fill="#000">Aktuell</text>
	<text x="395" y="188" text-anchor="middle" font-family="Arial-Bold" font-size="32px" fill="#000">Linie</text>
	<text x="485" y="188" font-family="Arial-Bold" font-size="32px" fill="#000">Richtung</text>
	<text x="100" y="220" text-anchor="middle" font-family="Arial-Kursiv" font-size="23px" fill="#000">departure </text>
	<text x="260" y="220" text-anchor="middle" font-family="Arial-Kursiv" font-size="23px" fill="#000">actual</text>
	<text x="395" y="220" text-anchor="middle" font-family="Arial-Kursiv" font-size="23px" fill="#000">route</text>
	<text x="485" y="220" font-family="Arial-Kursiv" font-size="23px" fill="#000">direction</text>
</g>
<g id="data">
	<g id="row-0">
		<rect x="0" y="230" width="1200" height="6" fill="#fff"  />
		<rect x="0" y="235" width="1200" height="56" fill="#fff"  />
		<text x="100" y="279" text-anchor="middle" font-family="Arial" font-size="38px" fill="#999">19:07</text>
		<text x="260" y="279"  text-anchor="middle" font-family="Arial" font-size="38px" fill="#000">19:11</text>
		<rect x="365" y="240" width="60" height="46" fill="#333"  />
		<text x="395" y="273"  text-anchor="middle" font-family="Arial" font-size="30px" fill="#fff">10</text>
		<text x="485" y="279"  font-family="Arial" font-size="38px" fill="#000">Friedrichshafen Hafenbahnhof</text>
		<rect x="0" y="291" width="1200" height="6" fill="#fff"  />
	</g>
	<g id="row-1">
		<rect x="0" y="296" width="1200" height="6" fill="#dedede"  />
		<rect x="0" y="301" width="1200" height="56" fill="#dedede"  />
		<text x="100" y="345" text-anchor="middle" font-family="Arial" font-size="38px" fill="#000">19:11</text>
		<text x="260" y="345"  text-anchor="middle" font-family="Arial" font-size="38px" fill="#000"></text>
		<rect x="365" y="306" width="60" height="46" fill="#333"  />
		<text x="395" y="339"  text-anchor="middle" font-family="Arial" font-size="30px" fill="#fff">5</text>
		<text x="485" y="345"  font-family="Arial" font-size="38px" fill="#000">Friedrichshafen Messe, West</text>
		<rect x="0" y="357" width="1200" height="6" fill="#dedede"  />
	</g>
	<g id="row-2">
		<rect x="0" y="362" width="1200" height="6" fill="#fff"  />
		<rect x="0" y="367" width="1200" height="56" fill="#fff"  />
		<text x="100" y="411" text-anchor="middle" font-family="Arial" font-size="38px" fill="#000">19:13</text>
		<text x="260" y="411"  text-anchor="middle" font-family="Arial" font-size="38px" fill="#000"></text>
		<rect x="365" y="372" width="60" height="46" fill="#333"  />
		<text x="395" y="405"  text-anchor="middle" font-family="Arial" font-size="30px" fill="#fff">17</text>
		<text x="485" y="411"  font-family="Arial" font-size="38px" fill="#000">FN Ailingen, Haldenweg</text>
		<rect x="0" y="423" width="1200" height="6" fill="#fff"  />
	</g>
	<g id="row-3">
		<rect x="0" y="428" width="1200" height="6" fill="#dedede"  />
		<rect x="0" y="433" width="1200" height="56" fill="#dedede"  />
		<text x="100" y="477" text-anchor="middle" font-family="Arial" font-size="38px" fill="#999">19:14</text>
		<text x="260" y="477"  text-anchor="middle" font-family="Arial" font-size="38px" fill="#000">19:16</text>
		<rect x="365" y="438" width="60" height="46" fill="#333"  />
		<text x="395" y="471"  text-anchor="middle" font-family="Arial" font-size="30px" fill="#fff">11</text>
		<text x="485" y="477"  font-family="Arial" font-size="38px" fill="#000">Friedrichshafen Hafenbahnhof</text>
		<rect x="0" y="489" width="1200" height="6" fill="#dedede"  />
	</g>	
</g>' . "\n";
	
	$preview .= '<g id="info">' . "\n";
	
	$x = 30;
	$y = $topSituations;

	$preview .= '	<rect x="0" y="' . intval($y) . '" width="1200" height="1" fill="#000" />' . "\n";

	$y += 1;

	$preview .= '	<rect x="0" y="' . intval($y) . '" width="1200" height="20" fill="#fff" />' . "\n";
	
	$y += 20;

	foreach ($lines as $row => $line)
	{
		if ($line['Content'] != '')
		{
			$font = 'OpenSans.ttf';
			$fontFamily = "Arial";

			$size = 44;
			$align = 0;
			
			if (isset($line['Font']))
			{
				$font = $line['Font'];

				if ($font == 'OpenSans-Bold.ttf')
				{
					$fontFamily = "Arial-Bold";
				}

				if ($font == 'OpenSans-Italic.ttf')
				{
					$fontFamily = "Arial-Italic";
				}
			}

			if (isset($line['Size']))
			{
				$size = $line['Size'];
			}

			if (isset($line['Align']))
			{
				$align = $line['Align'];
			}

			$x = 50;

			if ($align == 0)
			{
				$x = 600;
				$align = ' text-anchor="middle"';
			}
			else if ($align == 1)
			{
				$x = 1170;
				$align = ' text-anchor="end"';
			}
			else
			{
				$align = '';
			}

			$box = svgttfbbox($size, 0, $fontDirectory . $font, 'Ög');
			$boxHeight = abs($box[5] - $box[1]);
			$boxHeight *= 1.2;

			$preview .= '	<rect x="0" y="' . intval($y) . '" width="1200" height="' . intval($boxHeight) . '" fill="#fff" />' . "\n";
			$preview .= '	<text x="' . $x . '" y="' . intval($y + $boxHeight - ($boxHeight / 4)) . '" font-family="' . $fontFamily . '" font-size="' . $size . 'px" fill="#000" ' . $align . '>' . $line['Content'] . '</text>';

			$y += intval($boxHeight);
		}
	}

	$preview .= '	<rect x="0" y="' . intval($y) . '" width="1200" height="10" fill="#fff" />' . "\n";
	
	$y += 10;

	$preview .= '
<g id="tabs">
	<rect x="0" y="1500" width="1200" height="100" fill="#000" />
	<image href="./Images/Logo.png" x="20" y="1520" width="216" height="60" />
	<rect x="260" y="1500" width="240" height="80" fill="#fff"  />
	<text x="380" y="1560" text-anchor="middle" font-family="Arial-Bold" font-size="28px" fill="#000">Abfahrten</text>
	<text x="600" y="1560" text-anchor="middle" font-family="Arial" font-size="28px" fill="#fff">Fahrplan</text>
	<text x="820" y="1560" text-anchor="middle" font-family="Arial" font-size="28px" fill="#fff">Info</text>
</g>
</svg>' . "\n";

	/* Bild generieren --------------------------------------------------------------------------- */

	print('<!DOCTYPE html><html><head><style>body { background-color:#eee; text-align:center; }</style></head><body>');
	print($preview);
	print('</body></html>');
}
else
{
	if (file_exists('../../Clients/' . $clientId . '/Data/InfoText.xml'))
	{
		$json = json_encode(simplexml_load_string(file_get_contents('../../Clients/' . $clientId . '/Data/InfoText.xml'), 'SimpleXMLElement', LIBXML_NOCDATA));
		$array = json_decode($json, true);

		$lines = $array['Line'];

		// if only one line, json_decode skips the index level - so add level [0] before this single line for loop
		
		if (count($lines) == 1)
		{
			$line = $lines;
			$lines = array();
			$lines[0] = $line;
		}

		// calculate top positions of situation box

		$topSituations -= 2;

		$topSituations -= 20;

		foreach ($lines as $line)
		{
			if ($line['@attributes']['Content'] != '')
			{
				$font = 'OpenSans.ttf';
				$fontFamily = "Arial";

				$size = 44;
				$align = 0;
					
				if (isset($line['@attributes']['Font']))
				{
					$font = $line['@attributes']['Font'];

					if ($font == 'OpenSans-Bold.ttf')
					{
						$fontFamily = "Arial-Bold";
					}

					if ($font == 'OpenSans-Italic.ttf')
					{
						$fontFamily = "Arial-Italic";
					}
				}

				if (isset($line['@attributes']['Size']))
				{
					$size = $line['@attributes']['Size'];
				}

				if (isset($line['@attributes']['Align']))
				{
					$align = $line['@attributes']['Align'];
				}

				$box = svgttfbbox($size, 0, $fontDirectory . $font, 'Ög');
				$boxHeight = abs($box[5] - $box[1]);
				$boxHeight *= 1.2;

				$topSituations -= intval($boxHeight);
			}
		}

		$topSituations -= 20;

		/* ---------------------------------------------------------------------------------------- */

		// set top of block to end of last visible row (230 + (x * 64))

		// 230 = header height, 64 = line_height (adapt here also, when change)!
		
		for ($i = 20; $i >= 0; $i--)
		{
			// set top y to first row, which is more on top as our current info block y
			
			if ((230 + ($i * 64)) < $topSituations)
			{
				$topSituations = (230 + ($i * 64));
				
				break;
			}
		}

		/* ---------------------------------------------------------------------------------------- */

		$x = 30;
		$y = $topSituations;
		
		$svg .= '<g id="info">' . "\n";

		$svg .= '	<rect x="0" y="' . intval($y) . '" width="1200" height="2" fill="#000" />' . "\n";

		$y += 2;

		$svg .= '	<rect x="0" y="' . intval($y) . '" width="1200" height="20" fill="#fff" />' . "\n";
		
		$y += 20;
			
		foreach ($lines as $line)
		{
			if ($line['@attributes']['Content'] != '')
			{
				$font = 'OpenSans.ttf';
				$fontFamily = "Arial";

				$size = 44;
				$align = 0;
					
				if (isset($line['@attributes']['Font']))
				{
					$font = $line['@attributes']['Font'];

					if ($font == 'OpenSans-Bold.ttf')
					{
						$fontFamily = "Arial-Bold";
					}

					if ($font == 'OpenSans-Italic.ttf')
					{
						$fontFamily = "Arial-Italic";
					}
				}

				if (isset($line['@attributes']['Size']))
				{
					$size = $line['@attributes']['Size'];
				}

				if (isset($line['@attributes']['Align']))
				{
					$align = $line['@attributes']['Align'];
				}

				$x = 50;

				if ($align == 0)
				{
					$x = 600;
					$align = ' text-anchor="middle"';
				}
				else if ($align == 1)
				{
					$x = 1170;
					$align = ' text-anchor="end"';
				}
				else
				{
					$align = '';
				}

				$box = svgttfbbox($size, 0, $fontDirectory . $font, 'Ög');
				$boxHeight = abs($box[5] - $box[1]);
				$boxHeight *= 1.2;

				$svg .= '	<rect x="0" y="' . intval($y) . '" width="1200" height="' . intval($boxHeight) . '" fill="#fff" />' . "\n";
				$svg .= '	<text x="' . $x . '" y="' . intval($y + $boxHeight - ($boxHeight / 4)) . '" font-family="' . $fontFamily . '" font-size="' . $size . 'px" fill="#000" ' . $align . '>' . $line['@attributes']['Content'] . '</text>' . "\n";

				$y += intval($boxHeight);
			}
		}

		$svg .= '	<rect x="0" y="' . intval($y) . '" width="1200" height="10" fill="#fff" />' . "\n";
		
		$y += 10;
		
		$svg .= '</g>' . "\n";
	}
}

?>