IHMPlotContextFileClass.php 8.58 KB
<?php

class IHMPlotContextFileClass
{
	public static function parse($filePath)
	{
		if (!file_exists($filePath))
			return array("success" => false, "message" => "Cannot load plot context file");

		$doc = new DOMDocument("1.0", "UTF-8");
		$doc->preserveWhiteSpace = false;
		$doc->formatOutput = true;

		if (!$doc->load($filePath))
			return array("success" => false, "message" => "Cannot load plot context file");

		//<page>
		$pageNodes = $doc->getElementsByTagName('page');

		if ($pageNodes->length <= 0)
			return array("success" => false, "message" => "Cannot load context page node");

		$pageNode = $pageNodes->item(0);

		$isPortrait = $pageNode->getAttribute('portrait') == "true";

		$pageContext = array(
			'startTime' => $pageNode->getAttribute('startTime'),
			'stopTime'  => $pageNode->getAttribute('stopTime'),
			'superposeMode' => ($pageNode->getAttribute('superposeMode') == "true"),
			'ttName' => $pageNode->getAttribute('ttName'),
			'ttIndex' => ($pageNode->getAttribute('ttIndex') == '') ? 0 : intval($pageNode->getAttribute('ttIndex')),
			'ttNbIntervals' => ($pageNode->getAttribute('ttNbIntervals') == '') ? 0 : intval($pageNode->getAttribute('ttNbIntervals')),
			'portrait'  => $isPortrait,
			//An image rotation of 90 deg. is done after request execution if a page is in "portrait" mode
			'width'     => ($isPortrait ? intval($pageNode->getAttribute('height')) : intval($pageNode->getAttribute('width'))),
			'height'    => ($isPortrait ? intval($pageNode->getAttribute('width')) : intval($pageNode->getAttribute('height'))),
			'panels'    => array()
		);

		//<panel>
		$panelNodes = $pageNode->getElementsByTagName('panel');

		foreach ($panelNodes as $panelNode) {
			$panelContext = array(
				'id'     => $panelNode->getAttribute('id')
			);

			if ($isPortrait) {
				$panelContext['x']      = intval($panelNode->getAttribute('y'));
				$panelContext['width']  = intval($panelNode->getAttribute('height'));
				$panelContext['height'] = intval($panelNode->getAttribute('width'));
				$panelContext['y']      = $pageContext['height'] - intval($panelNode->getAttribute('x')) - $panelContext['height'];
			} else {
				$panelContext['x']      = intval($panelNode->getAttribute('x'));
				$panelContext['height'] = intval($panelNode->getAttribute('height'));
				$panelContext['y']      = $pageContext['height'] - intval($panelNode->getAttribute('y')) - $panelContext['height'];
				$panelContext['width']  = intval($panelNode->getAttribute('width'));
			}

			//Parameters
			$PARAMETERS_TAG = 'parameters';
			$parametersNodes = $panelNode->getElementsByTagName($PARAMETERS_TAG);
			$PARAMETER_TAG = 'parameter';
			if ($parametersNodes->length > 0) {
				$panelContext[$PARAMETERS_TAG] = array();
				$parametersNode = $parametersNodes->item(0);
				$items = $parametersNode->getElementsByTagName($PARAMETER_TAG);
				foreach ($items as $item) {
					$pContext = array(
						'id' => $item->getAttribute('id'),
						'MinSampling' => $item->getAttribute('MinSampling')
					);

                    //<catalog> or <timetable>
                    $catalogNodes = $item->getElementsByTagName('catalog');
                    $timetableNodes = $item->getElementsByTagName('timetable');
                    if (($catalogNodes->length > 0) || ($timetableNodes->length > 0)) {
                        $isCatalog = FALSE;
                        if ($catalogNodes->length > 0) {
                            $isCatalog = TRUE;
                            $ttorcatNode = $catalogNodes->item(0);
                        }
                        else {
                            $ttorcatNode = $timetableNodes->item(0);
                        }

                        $ttOrCatContext = array(
                            'name' => $ttorcatNode->getAttribute('name'),
                            'columnToShow' => $ttorcatNode->getAttribute('columnToShow'),
                        );

                        if ($isCatalog) {
                            //<columns>
                            $columnsNodes = $ttorcatNode->getElementsByTagName('columns');
                            if ($columnsNodes->length > 0) {
                                $ttOrCatContext['columns'] = [];
                                $columnNodes = $columnsNodes->item(0)->getElementsByTagName('column');
                                foreach ($columnNodes as $columnNode) {
                                    $ttOrCatContext['columns'][$columnNode->getAttribute('id')] = array(
                                        'name' => $columnNode->getAttribute('name')
                                    );
                                }
                            }

                        }

                        //<intervals>
                        $intervalsNodes = $ttorcatNode->getElementsByTagName('intervals');
                        if ($intervalsNodes->length > 0) {
                            $ttOrCatContext['intervals'] = [];
                            $intervalNodes = $intervalsNodes->item(0)->getElementsByTagName('interval');
                            foreach ($intervalNodes as $intervalNode) {
                                $intervalContext = array(
                                    'start' => $intervalNode->getAttribute('startTime'),
                                    'stop'  => $intervalNode->getAttribute('stopTime'),
                                );
                                if ($isCatalog) {
                                    $intervalContext['params'] = array();
                                    $paramNodes = $intervalNode->getElementsByTagName('param');
                                    if ($paramNodes->length > 0) {
                                        foreach ($paramNodes as $paramNode) {
                                            $intervalContext['params'][$paramNode->getAttribute('id')] = $paramNode->getAttribute('val');
                                        }
                                    }
                                }
                                $ttOrCatContext['intervals'][$intervalNode->getAttribute('id')] = $intervalContext;
                            }
                        }
                        $pContext['ttorcat'] = $ttOrCatContext;
                    }
                    $panelContext[$PARAMETERS_TAG][] = $pContext;
				}
			}

			//<plotArea>
			$plotAreaNodes = $panelNode->getElementsByTagName('plotArea');

			if ($plotAreaNodes->length > 0) {
				$panelContext['plotArea'] = array();
				$plotAreaNode = $plotAreaNodes->item(0);

				if ($isPortrait) {
					$panelContext['plotArea']['x']      = intval($plotAreaNode->getAttribute('y'));
					$panelContext['plotArea']['width']  = intval($plotAreaNode->getAttribute('height'));
					$panelContext['plotArea']['height'] = intval($plotAreaNode->getAttribute('width'));
					$panelContext['plotArea']['y']      = $pageContext['height'] - intval($plotAreaNode->getAttribute('x')) - $panelContext['plotArea']['height'];
				} else {
					$panelContext['plotArea']['x']      = intval($plotAreaNode->getAttribute('x'));
					$panelContext['plotArea']['height'] = intval($plotAreaNode->getAttribute('height'));
					$panelContext['plotArea']['y']      = $pageContext['height'] - intval($plotAreaNode->getAttribute('y')) - $panelContext['plotArea']['height'];
					$panelContext['plotArea']['width']  = intval($plotAreaNode->getAttribute('width'));
				}
				//BG COLOR
				$panelContext['plotArea']['plotAreaBackgroundColor'] = ($plotAreaNode->getAttribute('plotAreaBackgroundColor'));

				//hasSpectro
				$panelContext['plotArea']['hasSpectro']  = ($plotAreaNode->getAttribute('hasSpectro') == "true");

				//<axis>
				$panelContext['plotArea']['axes'] = array();

				$axisNodes = $plotAreaNode->getElementsByTagName('axis');

				foreach ($axisNodes as $axisNode) {
					$axisContext = array(
						'id' => $axisNode->getAttribute('id'),
						'logarithmic' => ($axisNode->getAttribute('logarithmic') == "true"),
						'min' => floatval($axisNode->getAttribute('min')),
						'max' => floatval($axisNode->getAttribute('max')),
						'colorsList' => $axisNode->getAttribute('colorsList')
					);

					$panelContext['plotArea']['axes'][] = $axisContext;
				}
			}

			$pageContext['panels'][] = $panelContext;
		}
		$instantTimeNavNodes = $pageNode->getElementsByTagName('instantTimeNav');
		foreach($instantTimeNavNodes as $instantTimeNavNode){
			if ($instantTimeNavNode != "")
			{
				$instantTimeNavContext = array(
					'prevTime' => $instantTimeNavNode->getAttribute('prevTime'),
					'nextTime' => $instantTimeNavNode->getAttribute('nextTime'),
				);
				$pageContext['instantTimeNav']= $instantTimeNavContext;
			}
			
		}
		return array("success" => true, "page" => $pageContext);
	}
}