<?php
namespace Mm\Beton\Planungsatlas\AtlasBundle\Service;
use Exception;
use Mm\Beton\Planungsatlas\AtlasBundle\Entity\Main\AtlasSession;
use Mm\Beton\Planungsatlas\AtlasBundle\Entity\Main\CartElement;
use Mm\Beton\Planungsatlas\AtlasBundle\Entity\Main\ConstructionElement;
use Mm\Beton\Planungsatlas\AtlasBundle\Entity\Main\XmlModelThermalData;
class HeatProtectionService
{
/**
* @var AtlasDaoInterface
*/
private $dao;
private $dataDirectory = '';
/**
* Constructor
*
* @param AtlasDaoInterface $dao
*/
public function __construct(AtlasDaoInterface $dao)
{
$this->dao = $dao;
}
/**
* @param $productId
* @param $elementString
* @return array
*/
public function getElementPagination($productId, $elementString)
{
$pagination = array(
'previous' => 0,
'previous_name' => '',
'previous_construction_name' => '',
'previous_construction_id' => '',
'previous_type_name' => '',
'previous_type_id' => '',
'next' => 0,
'next_name' => '',
'next_type_name' => '',
'next_type_id' => '',
'next_construction_id' => '',
'next_construction_name' => '',
'element_string' => 0,
);
if ($elementString == null) {
return $pagination;
}
$pagination['element_string'] = $elementString;
$elementIds = explode(':', $pagination['element_string']);
$key = array_search($productId, $elementIds);
$pagination['previous'] = (array_key_exists($key - 1, $elementIds) ? $elementIds[$key - 1] : 0);
$pagination['next'] = (array_key_exists($key + 1, $elementIds) ? $elementIds[$key + 1] : 0);
if($this->dao->getConstructionElementById($pagination['previous']) != NULL) {
$pagination['previous_name'] = $this->dao->getConstructionElementById($pagination['previous'])->getName();
$pagination['previous_type_name'] = $this->dao->getConstructionElementById($pagination['previous'])
->getConstructionType()
->getName();
$pagination['previous_type_id'] = $this->dao->getConstructionElementById($pagination['previous'])
->getConstructionType()
->getId();
$pagination['previous_construction_name'] = $this->dao->getConstructionElementById($pagination['previous'])
->getConstructionType()
->getConstruction()
->getName();
$pagination['previous_construction_id'] = $this->dao->getConstructionElementById($pagination['previous'])
->getConstructionType()
->getConstruction()
->getId();
}
if($this->dao->getConstructionElementById($pagination['next']) != NULL) {
$pagination['next_name'] = $this->dao->getConstructionElementById($pagination['next'])->getName();
$pagination['next_type_name'] = $this->dao->getConstructionElementById($pagination['next'])
->getConstructionType()
->getName();
$pagination['next_type_id'] = $this->dao->getConstructionElementById($pagination['next'])
->getConstructionType()
->getId();
$pagination['next_construction_name'] = $this->dao->getConstructionElementById($pagination['next'])
->getConstructionType()
->getConstruction()
->getName();
$pagination['next_construction_id'] = $this->dao->getConstructionElementById($pagination['next'])
->getConstructionType()
->getConstruction()
->getId();
}
return $pagination;
}
/**
* @return array|null
*/
public function getConstructionTypesDropdown()
{
$types = $this->dao->getConstructionTypes();
if ($types != null) {
$typesDropdown = array();
foreach ($types as $type) {
if (!in_array($type->getName(), $typesDropdown, true)) {
array_push($typesDropdown, $type->getName());
}
}
return $typesDropdown;
}
return null;
}
/**
* @param \Mm\Beton\Planungsatlas\AtlasBundle\Entity\Construction[] $constructions
* @return array|null
*/
public function getConstructionTypesForConstructions($constructions)
{
if ($constructions != null) {
$types = array();
foreach ($constructions as $construction) {
$types[$construction->getId()] = $construction->getConstructionTypes();
}
return $types;
}
return null;
}
/**
* @return array
*/
public function getFilterModuleData()
{
$constructions = $this->dao->getConstructions();
/** @var Construction[] $constructions */
$data = array(
'constructions' => $constructions,
'typesOnly' => $this->getConstructionTypesDropdown(),
'typesConstruction' => $this->getConstructionTypesForConstructions($constructions)
);
return $data;
}
/**
* @param $constructionId
* @param $constructionTypeParam
* @param $searchTerm
* @return array
*/
public function getHeatProtectionData($constructionTypeParam = false, $constructionId = false, $searchTerm = false)
{
$data = $this->getFilterModuleData();
$elements = $this->search($constructionTypeParam, $constructionId, $searchTerm);
if ($elements != null) {
$t = array();
foreach ($elements as $element) {
if(!in_array($element['type_name'], $t)) {
$t[] = $element['type_name'];
}
$e[] = $element['element_id'];
$data['results'][$element['construction_name']][$element['element_id']] =
array(
'type_name' => $element['type_name'],
'element_name' => $element['element_name'],
'type_id' => $element['type_id'],
'construction_id' => $element['construction_id'],
'construction_name' => $element['construction_name'],
);
}
$data['element_string'] = implode(':', $e);
$data['type_names'] = $t;
}
return $data;
}
/**
* @param $constructionElementId
* @return array
*/
public function getConstructionsDetailData($constructionElementId)
{
$data = $this->getFilterModuleData();
$element = $this->dao->getConstructionElementById($constructionElementId);
if ($element != null) {
$this->incrementElementRequests($constructionElementId);
$data['element']['thermals'] = $this->dao->getThermalParametersByConstructionElementId($constructionElementId);
$data['element']['construction_name'] = $element->getConstructionType()->getConstruction()->getName();
$data['element']['construction_id'] = $element->getConstructionType()->getConstruction()->getId();
$data['element']['type_name'] = $element->getConstructionType()->getName();
$data['element']['type_id'] = $element->getConstructionType()->getId();
$data['element']['element'] = $element;
}
return $data;
}
/**
* @param $id
* @return XmlModelThermalData $xmlModelThermalData
*/
public function getThermalDataByElement($id)
{
$dataDirectory = $this->getDataDirectory();
if(!$element = $this->dao->getConstructionElementById($id)) {
return false;
}
$dataDirectoryJSON = $dataDirectory.DIRECTORY_SEPARATOR.'json';
$filePath = $dataDirectoryJSON.DIRECTORY_SEPARATOR.$element->getCode().'.json';
$xmlModelThermalData = new XmlModelThermalData($element->getCode());
if (is_file($filePath) && is_readable($filePath)) {
$xmlModelThermalData->loadData($dataDirectory);
$xmlModelThermalData->loadTpc($dataDirectory);
$xmlModelThermalData->setJson(file_get_contents($filePath));
} else {
$xmlModelThermalData->loadData($dataDirectory); //loads tpc
if(!file_exists($dataDirectoryJSON)) {
mkdir($dataDirectoryJSON, 0777, true);
}
file_put_contents($filePath, $xmlModelThermalData->getJson());
}
ini_set('memory_limit', '256M');
return $xmlModelThermalData;
}
public function findAllElements(){
return $this->dao->getConstructionElements();
}
/**
* @param $constructionTypeParam
* @param $constructionId
* @param $searchTerms
* @return array
*/
public function search($constructionTypeParam, $constructionId, $searchTerms)
{
$searchResult = array();
if(empty($searchTerms)){
$searchTerms = array(false);
}
foreach ($searchTerms as $searchTerm) {
$elements = array();
if ($constructionId == false AND $constructionTypeParam == false and $searchTerm == false) {
return $this->dao->search(array());
} elseif ($constructionTypeParam != 0 AND $constructionId != 0 AND $searchTerm != false) {
$elements = $this->dao->search(array(
'construction' => $constructionId,
'type' => $constructionTypeParam,
'element' => $searchTerm
));
} elseif ($constructionTypeParam != 0 AND $constructionId != 0) {
$elements = $this->dao->search(array(
'construction' => $constructionId,
'type' => $constructionTypeParam
));
} elseif (is_string($constructionTypeParam) AND $searchTerm != false) {
$elements = $this->dao->search(array('type' => $constructionTypeParam, 'element' => $searchTerm));
} elseif (is_string($constructionTypeParam)) {
$elements = $this->dao->search(array('type' => $constructionTypeParam));
} elseif ($constructionId != 0 AND $constructionTypeParam == false AND $searchTerm != false) {
$elements = $this->dao->search(array('construction' => $constructionId, 'element' => $searchTerm));
} elseif ($constructionId != 0 AND $constructionTypeParam == false AND $searchTerm == false) {
$elements = $this->dao->search(array('construction' => $constructionId));
} else {
// search by hotspot
$elements = $this->dao->searchByHotspotName($searchTerm);
// // search by titles? We don't have such situations for wdvs
// if (empty($elements)) {
// $elements = $this->dao->searchNames($searchTerm);
// }
}
$searchResult = array_merge($searchResult, $elements);
}
return $searchResult;
}
/**
* @param $constructionElementId
* @param $tpcKeyCode
* @param AtlasSession $atlasSession
* @return \stdClass
*/
public function addElementToCart($constructionElementId, $tpcKeyCode, $psiValue, AtlasSession $atlasSession)
{
$return = new \stdClass();
$element = $this->dao->getConstructionElementById($constructionElementId);
$cartElementName = $element->getConstructionType()
->getConstruction()->getName() . ',
' . $element->getConstructionType()->getName() . ',
' . $element->getName();
$atlasSession->addCartElement(new CartElement($element, $tpcKeyCode, $psiValue, $cartElementName));
$atlasSession->saveToSession();
$return->state = 0;
$return->msg = 'ok';
return $return;
}
/**
* @param $constructionElementId
* @param $tpcKeyCode
* @param AtlasSession $atlasSession
* @return \stdClass
*/
public function addEmpytElementToCart($constructionElementId, $tpcKeyCode, $psiValue, $atlasSession)
{
$return = new \stdClass();
if ($constructionElementId == null) {
$element = new ConstructionElement();
} else {
$element = $this->dao->getConstructionElementById($constructionElementId);
}
$cartElementName = $this->generateCartElementName($element);
$atlasSession->addCartElement(new CartElement($element, $tpcKeyCode, $psiValue, $cartElementName));
$atlasSession->saveToSession();
$return->state = 0;
$return->msg = 'ok';
return $return;
}
/**
* @param ConstructionElement $element
* @return string
*/
public function generateCartElementName($element)
{
if ($element->getConstructionType() == null) {
return 'Eigene Konstruktion';
}
$cartElementName = $element->getConstructionType()
->getConstruction()->getName() . ',' . $element->getConstructionType()->getName() . ',' . $element->getName();
return $cartElementName;
}
/**
* @param $elementId
* @param AtlasSession $atlasSession
* @return \stdClass
*/
public function removeElementFromCart($elementId, AtlasSession $atlasSession)
{
$return = new \stdClass();
$atlasSession->removeCartElementById($elementId);
$atlasSession->saveToSession();
$return->state = 0;
$return->msg = 'ok';
return $return;
}
/**
* @param $elementId
* @param $elementLength
* @param AtlasSession $atlasSession
* @param $elementPsiValue
* @return \stdClass
*/
public function setElementLengthById($elementId, $elementLength, AtlasSession $atlasSession, $elementPsiValue)
{
$return = new \stdClass();
$atlasSession->setElementLengthById($elementId, $elementLength, $elementPsiValue);
$atlasSession->saveToSession();
$return->state = 0;
$return->msg = 'ok';
return $return;
}
/**
* @param $aGes
* @param AtlasSession $atlasSession
* @return \stdClass
*/
public function setAges($aGes, AtlasSession $atlasSession)
{
$return = new \stdClass();
$atlasSession->setAges($aGes);
$atlasSession->saveToSession();
$return->state = 0;
$return->msg = 'ok';
return $return;
}
/**
* @param $cartElementId
* @param $cartElementName
* @param AtlasSession $atlasSession
* @return \stdClass
*/
public function setCartElementNameById($cartElementId, $cartElementName, AtlasSession $atlasSession)
{
$return = new \stdClass();
$atlasSession->setCartElementNameById(intval($cartElementId), $cartElementName);
$atlasSession->saveToSession();
$return->state = 0;
$return->msg = 'ok';
return $return;
}
/**
* @param $id
* @return int
*/
public function incrementElementRequests($id)
{
return $this->dao->incrementElementRequests($id);
}
public function setDataDirectory($value)
{
return $this->dataDirectory = (string) $value;
}
public function getDataDirectory()
{
return $this->dataDirectory;
}
/**
* Make sure json file for ConstructionElement exist, generate it if not.
*
* @param $element
* @param $dataDirectory
* @return bool true - if new file was created, false - if not (already exist)
* @throws Exception
*/
public function makeSureJsonFileExist($element, $dataDirectory): bool
{
$xmlModelThermalData = new XmlModelThermalData($element->getCode());
$dataDirectoryJSON = $dataDirectory . DIRECTORY_SEPARATOR . 'json';
$filePath = $dataDirectoryJSON . DIRECTORY_SEPARATOR . $element->getCode() . '.json';
if (!is_file($filePath) || !is_readable($filePath)) {
$xmlModelThermalData->loadData($dataDirectory); //loads tpc
if(!file_exists($dataDirectoryJSON)) {
mkdir($dataDirectoryJSON, 0777, true);
}
file_put_contents($filePath, $xmlModelThermalData->getJson());
return true;
}
return false;
}
}