src/Mm/Beton/Planungsatlas/AtlasBundle/Service/HeatProtectionService.php line 207

Open in your IDE?
  1. <?php
  2. namespace Mm\Beton\Planungsatlas\AtlasBundle\Service;
  3. use Exception;
  4. use Mm\Beton\Planungsatlas\AtlasBundle\Entity\Main\AtlasSession;
  5. use Mm\Beton\Planungsatlas\AtlasBundle\Entity\Main\CartElement;
  6. use Mm\Beton\Planungsatlas\AtlasBundle\Entity\Main\ConstructionElement;
  7. use Mm\Beton\Planungsatlas\AtlasBundle\Entity\Main\XmlModelThermalData;
  8. class HeatProtectionService
  9. {
  10.     /**
  11.      * @var AtlasDaoInterface
  12.      */
  13.     private $dao;
  14.     private $dataDirectory '';
  15.     /**
  16.      * Constructor
  17.      *
  18.      * @param AtlasDaoInterface $dao
  19.      */
  20.     public function __construct(AtlasDaoInterface $dao)
  21.     {
  22.         $this->dao $dao;
  23.     }
  24.     /**
  25.      * @param $productId
  26.      * @param $elementString
  27.      * @return array
  28.      */
  29.     public function getElementPagination($productId$elementString)
  30.     {
  31.         $pagination = array(
  32.             'previous' => 0,
  33.             'previous_name' => '',
  34.             'previous_construction_name' => '',
  35.             'previous_construction_id' => '',
  36.             'previous_type_name' => '',
  37.             'previous_type_id' => '',
  38.             'next' => 0,
  39.             'next_name' => '',
  40.             'next_type_name' => '',
  41.             'next_type_id' => '',
  42.             'next_construction_id' => '',
  43.             'next_construction_name' => '',
  44.             'element_string' => 0,
  45.             );
  46.         if ($elementString == null) {
  47.             return $pagination;
  48.         }
  49.         $pagination['element_string'] = $elementString;
  50.         $elementIds explode(':'$pagination['element_string']);
  51.         $key array_search($productId$elementIds);
  52.         $pagination['previous'] = (array_key_exists($key 1$elementIds) ? $elementIds[$key 1] : 0);
  53.         $pagination['next'] = (array_key_exists($key 1$elementIds) ? $elementIds[$key 1] : 0);
  54.         if($this->dao->getConstructionElementById($pagination['previous']) != NULL) {
  55.             $pagination['previous_name'] = $this->dao->getConstructionElementById($pagination['previous'])->getName();
  56.             $pagination['previous_type_name'] = $this->dao->getConstructionElementById($pagination['previous'])
  57.                 ->getConstructionType()
  58.                 ->getName();
  59.             $pagination['previous_type_id'] = $this->dao->getConstructionElementById($pagination['previous'])
  60.                 ->getConstructionType()
  61.                 ->getId();
  62.             $pagination['previous_construction_name'] = $this->dao->getConstructionElementById($pagination['previous'])
  63.                 ->getConstructionType()
  64.                 ->getConstruction()
  65.                 ->getName();
  66.             $pagination['previous_construction_id'] = $this->dao->getConstructionElementById($pagination['previous'])
  67.                 ->getConstructionType()
  68.                 ->getConstruction()
  69.                 ->getId();
  70.         }
  71.         if($this->dao->getConstructionElementById($pagination['next']) != NULL) {
  72.             $pagination['next_name'] = $this->dao->getConstructionElementById($pagination['next'])->getName();
  73.             $pagination['next_type_name'] = $this->dao->getConstructionElementById($pagination['next'])
  74.                 ->getConstructionType()
  75.                 ->getName();
  76.             $pagination['next_type_id'] = $this->dao->getConstructionElementById($pagination['next'])
  77.                 ->getConstructionType()
  78.                 ->getId();
  79.             $pagination['next_construction_name'] = $this->dao->getConstructionElementById($pagination['next'])
  80.                 ->getConstructionType()
  81.                 ->getConstruction()
  82.                 ->getName();
  83.             $pagination['next_construction_id'] = $this->dao->getConstructionElementById($pagination['next'])
  84.                 ->getConstructionType()
  85.                 ->getConstruction()
  86.                 ->getId();
  87.         }
  88.         return $pagination;
  89.     }
  90.     /**
  91.      * @return array|null
  92.      */
  93.     public function getConstructionTypesDropdown()
  94.     {
  95.         $types $this->dao->getConstructionTypes();
  96.         if ($types != null) {
  97.             $typesDropdown = array();
  98.             foreach ($types as $type) {
  99.                 if (!in_array($type->getName(), $typesDropdowntrue)) {
  100.                     array_push($typesDropdown$type->getName());
  101.                 }
  102.             }
  103.             return $typesDropdown;
  104.         }
  105.         return null;
  106.     }
  107.     /**
  108.      * @param \Mm\Beton\Planungsatlas\AtlasBundle\Entity\Construction[] $constructions
  109.      * @return array|null
  110.      */
  111.     public function getConstructionTypesForConstructions($constructions)
  112.     {
  113.         if ($constructions != null) {
  114.             $types = array();
  115.             foreach ($constructions as $construction) {
  116.                 $types[$construction->getId()] = $construction->getConstructionTypes();
  117.             }
  118.             return $types;
  119.         }
  120.         return null;
  121.     }
  122.     /**
  123.      * @return array
  124.      */
  125.     public function getFilterModuleData()
  126.     {
  127.         $constructions $this->dao->getConstructions();
  128.         /** @var Construction[] $constructions */
  129.         $data = array(
  130.             'constructions' => $constructions,
  131.             'typesOnly' => $this->getConstructionTypesDropdown(),
  132.             'typesConstruction' => $this->getConstructionTypesForConstructions($constructions)
  133.         );
  134.         return $data;
  135.     }
  136.     /**
  137.      * @param $constructionId
  138.      * @param $constructionTypeParam
  139.      * @param $searchTerm
  140.      * @return array
  141.      */
  142.     public function getHeatProtectionData($constructionTypeParam false$constructionId false$searchTerm false)
  143.     {
  144.         $data $this->getFilterModuleData();
  145.         $elements $this->search($constructionTypeParam$constructionId$searchTerm);
  146.         if ($elements != null) {
  147.             $t = array();
  148.             foreach ($elements as $element) {
  149.                 if(!in_array($element['type_name'], $t)) {
  150.                     $t[] = $element['type_name'];
  151.                 }
  152.                 $e[] = $element['element_id'];
  153.                 $data['results'][$element['construction_name']][$element['element_id']] =
  154.                     array(
  155.                         'type_name' => $element['type_name'],
  156.                         'element_name' => $element['element_name'],
  157.                         'type_id' => $element['type_id'],
  158.                         'construction_id' => $element['construction_id'],
  159.                         'construction_name' => $element['construction_name'],
  160.                     );
  161.             }
  162.             $data['element_string'] = implode(':'$e);
  163.             $data['type_names'] = $t;
  164.         }
  165.         return $data;
  166.     }
  167.     /**
  168.      * @param $constructionElementId
  169.      * @return array
  170.      */
  171.     public function getConstructionsDetailData($constructionElementId)
  172.     {
  173.         $data $this->getFilterModuleData();
  174.         $element $this->dao->getConstructionElementById($constructionElementId);
  175.         if ($element != null) {
  176.             $this->incrementElementRequests($constructionElementId);
  177.             $data['element']['thermals'] = $this->dao->getThermalParametersByConstructionElementId($constructionElementId);
  178.             $data['element']['construction_name'] = $element->getConstructionType()->getConstruction()->getName();
  179.             $data['element']['construction_id'] = $element->getConstructionType()->getConstruction()->getId();
  180.             $data['element']['type_name'] = $element->getConstructionType()->getName();
  181.             $data['element']['type_id'] = $element->getConstructionType()->getId();
  182.             $data['element']['element'] = $element;
  183.         }
  184.         return $data;
  185.     }
  186.     /**
  187.      * @param $id
  188.      * @return XmlModelThermalData $xmlModelThermalData
  189.      */
  190.     public function getThermalDataByElement($id)
  191.     {
  192.         $dataDirectory $this->getDataDirectory();
  193.         if(!$element $this->dao->getConstructionElementById($id)) {
  194.             return false;
  195.         }
  196.         $dataDirectoryJSON $dataDirectory.DIRECTORY_SEPARATOR.'json';
  197.         $filePath $dataDirectoryJSON.DIRECTORY_SEPARATOR.$element->getCode().'.json';
  198.         $xmlModelThermalData = new XmlModelThermalData($element->getCode());
  199.         if (is_file($filePath) && is_readable($filePath)) {
  200.             $xmlModelThermalData->loadData($dataDirectory);
  201.             $xmlModelThermalData->loadTpc($dataDirectory);
  202.             $xmlModelThermalData->setJson(file_get_contents($filePath));
  203.         } else {
  204.             $xmlModelThermalData->loadData($dataDirectory); //loads tpc
  205.             if(!file_exists($dataDirectoryJSON)) {
  206.                 mkdir($dataDirectoryJSON0777true);
  207.             }
  208.             file_put_contents($filePath,  $xmlModelThermalData->getJson());
  209.         }
  210.         ini_set('memory_limit''256M');
  211.         return $xmlModelThermalData;
  212.     }
  213.     public function findAllElements(){
  214.         return $this->dao->getConstructionElements();
  215.     }
  216.     /**
  217.      * @param $constructionTypeParam
  218.      * @param $constructionId
  219.      * @param $searchTerms
  220.      * @return array
  221.      */
  222.     public function search($constructionTypeParam$constructionId$searchTerms)
  223.     {
  224.         $searchResult = array();
  225.         if(empty($searchTerms)){
  226.             $searchTerms = array(false);
  227.         }
  228.         foreach ($searchTerms as $searchTerm) {
  229.             $elements = array();
  230.             if ($constructionId == false AND $constructionTypeParam == false and $searchTerm == false) {
  231.                 return $this->dao->search(array());
  232.             } elseif ($constructionTypeParam != AND $constructionId != AND $searchTerm != false) {
  233.                 $elements $this->dao->search(array(
  234.                     'construction' => $constructionId,
  235.                     'type' => $constructionTypeParam,
  236.                     'element' => $searchTerm
  237.                 ));
  238.             } elseif ($constructionTypeParam != AND $constructionId != 0) {
  239.                 $elements $this->dao->search(array(
  240.                     'construction' => $constructionId,
  241.                     'type' => $constructionTypeParam
  242.                 ));
  243.             } elseif (is_string($constructionTypeParam) AND $searchTerm != false) {
  244.                 $elements $this->dao->search(array('type' => $constructionTypeParam'element' => $searchTerm));
  245.             } elseif (is_string($constructionTypeParam)) {
  246.                 $elements $this->dao->search(array('type' => $constructionTypeParam));
  247.             } elseif ($constructionId != AND $constructionTypeParam == false AND $searchTerm != false) {
  248.                 $elements $this->dao->search(array('construction' => $constructionId'element' => $searchTerm));
  249.             } elseif ($constructionId != AND $constructionTypeParam == false AND $searchTerm == false) {
  250.                 $elements $this->dao->search(array('construction' => $constructionId));
  251.             } else {
  252.                 // search by hotspot
  253.                 $elements $this->dao->searchByHotspotName($searchTerm);
  254. //                // search by titles? We don't have such situations for wdvs
  255. //                if (empty($elements)) {
  256. //                    $elements = $this->dao->searchNames($searchTerm);
  257. //                }
  258.             }
  259.             $searchResult array_merge($searchResult$elements);
  260.         }
  261.         return $searchResult;
  262.     }
  263.     /**
  264.      * @param $constructionElementId
  265.      * @param $tpcKeyCode
  266.      * @param AtlasSession $atlasSession
  267.      * @return \stdClass
  268.      */
  269.     public function addElementToCart($constructionElementId$tpcKeyCode$psiValueAtlasSession $atlasSession)
  270.     {
  271.         $return = new \stdClass();
  272.         $element $this->dao->getConstructionElementById($constructionElementId);
  273.         $cartElementName $element->getConstructionType()
  274.                 ->getConstruction()->getName() . ',
  275.                 ' $element->getConstructionType()->getName() . ',
  276.                 ' $element->getName();
  277.         $atlasSession->addCartElement(new CartElement($element$tpcKeyCode$psiValue$cartElementName));
  278.         $atlasSession->saveToSession();
  279.         $return->state 0;
  280.         $return->msg 'ok';
  281.         return $return;
  282.     }
  283.     /**
  284.      * @param $constructionElementId
  285.      * @param $tpcKeyCode
  286.      * @param AtlasSession $atlasSession
  287.      * @return \stdClass
  288.      */
  289.     public function addEmpytElementToCart($constructionElementId$tpcKeyCode$psiValue$atlasSession)
  290.     {
  291.         $return = new \stdClass();
  292.         if ($constructionElementId == null) {
  293.             $element = new ConstructionElement();
  294.         } else {
  295.             $element $this->dao->getConstructionElementById($constructionElementId);
  296.         }
  297.         $cartElementName $this->generateCartElementName($element);
  298.         $atlasSession->addCartElement(new CartElement($element$tpcKeyCode$psiValue$cartElementName));
  299.         $atlasSession->saveToSession();
  300.         $return->state 0;
  301.         $return->msg 'ok';
  302.         return $return;
  303.     }
  304.     /**
  305.      * @param ConstructionElement $element
  306.      * @return string
  307.      */
  308.     public function generateCartElementName($element)
  309.     {
  310.         if ($element->getConstructionType() == null) {
  311.             return 'Eigene Konstruktion';
  312.         }
  313.         $cartElementName $element->getConstructionType()
  314.                 ->getConstruction()->getName() . ',' $element->getConstructionType()->getName() . ',' $element->getName();
  315.         return $cartElementName;
  316.     }
  317.     /**
  318.      * @param $elementId
  319.      * @param AtlasSession $atlasSession
  320.      * @return \stdClass
  321.      */
  322.     public function removeElementFromCart($elementIdAtlasSession $atlasSession)
  323.     {
  324.         $return = new \stdClass();
  325.         $atlasSession->removeCartElementById($elementId);
  326.         $atlasSession->saveToSession();
  327.         $return->state 0;
  328.         $return->msg 'ok';
  329.         return $return;
  330.     }
  331.     /**
  332.      * @param $elementId
  333.      * @param $elementLength
  334.      * @param AtlasSession $atlasSession
  335.      * @param $elementPsiValue
  336.      * @return \stdClass
  337.      */
  338.     public function setElementLengthById($elementId$elementLengthAtlasSession $atlasSession$elementPsiValue)
  339.     {
  340.         $return = new \stdClass();
  341.         $atlasSession->setElementLengthById($elementId$elementLength$elementPsiValue);
  342.         $atlasSession->saveToSession();
  343.         $return->state 0;
  344.         $return->msg 'ok';
  345.         return $return;
  346.     }
  347.     /**
  348.      * @param $aGes
  349.      * @param AtlasSession $atlasSession
  350.      * @return \stdClass
  351.      */
  352.     public function setAges($aGesAtlasSession $atlasSession)
  353.     {
  354.         $return = new \stdClass();
  355.         $atlasSession->setAges($aGes);
  356.         $atlasSession->saveToSession();
  357.         $return->state 0;
  358.         $return->msg 'ok';
  359.         return $return;
  360.     }
  361.     /**
  362.      * @param $cartElementId
  363.      * @param $cartElementName
  364.      * @param AtlasSession $atlasSession
  365.      * @return \stdClass
  366.      */
  367.     public function setCartElementNameById($cartElementId$cartElementNameAtlasSession $atlasSession)
  368.     {
  369.         $return = new \stdClass();
  370.         $atlasSession->setCartElementNameById(intval($cartElementId), $cartElementName);
  371.         $atlasSession->saveToSession();
  372.         $return->state 0;
  373.         $return->msg 'ok';
  374.         return $return;
  375.     }
  376.     /**
  377.      * @param $id
  378.      * @return int
  379.      */
  380.     public function incrementElementRequests($id)
  381.     {
  382.         return $this->dao->incrementElementRequests($id);
  383.     }
  384.     public function setDataDirectory($value)
  385.     {
  386.         return $this->dataDirectory = (string) $value;
  387.     }
  388.     public function getDataDirectory()
  389.     {
  390.         return $this->dataDirectory;
  391.     }
  392.     /**
  393.      * Make sure json file for ConstructionElement exist, generate it if not.
  394.      *
  395.      * @param $element
  396.      * @param $dataDirectory
  397.      * @return bool true - if new file was created, false - if not (already exist)
  398.      * @throws Exception
  399.      */
  400.     public function makeSureJsonFileExist($element$dataDirectory): bool
  401.     {
  402.         $xmlModelThermalData = new XmlModelThermalData($element->getCode());
  403.         $dataDirectoryJSON $dataDirectory DIRECTORY_SEPARATOR 'json';
  404.         $filePath $dataDirectoryJSON DIRECTORY_SEPARATOR $element->getCode() . '.json';
  405.         if (!is_file($filePath) || !is_readable($filePath)) {
  406.             $xmlModelThermalData->loadData($dataDirectory); //loads tpc
  407.             if(!file_exists($dataDirectoryJSON)) {
  408.                 mkdir($dataDirectoryJSON0777true);
  409.             }
  410.             file_put_contents($filePath,  $xmlModelThermalData->getJson());
  411.             return true;
  412.         }
  413.         return false;
  414.     }
  415. }