<?php
/**
* Created by PhpStorm.
* User: waldemar.abich
* Date: 12.12.14
* Time: 10:49
*/
namespace Mm\Beton\Planungsatlas\AtlasBundle\Entity\Main;
use Symfony\Component\HttpFoundation\Session\Session;
class AtlasSession
{
private $user;
/** @var CartElement[] $cartElements */
private $cartElements = array();
private $cartElementsSum = 0.0;
private $aGes = 0.0;
private $uWb = 0.0;
function __construct($user = null, $cartElements = array())
{
if (session_status() == PHP_SESSION_NONE) {
$session = new Session();
$session->start();
}
$this->cartElements = $cartElements;
$this->user = $user;
}
/**
* @param integer $cartElementId
*/
public function removeCartElementById($cartElementId)
{
unset($this->cartElements[$cartElementId]);
$this->sumCartElements();
}
/**
* @param $cartElementId
* @param $cartElementLength
* @param $cartElementPsiValue
*/
public function setElementLengthById($cartElementId, $cartElementLength, $cartElementPsiValue)
{
$cartElement = $this->getCartElementById($cartElementId);
$cartElement->setLength($cartElementLength);
$cartElement->setPsiValue($cartElementPsiValue);
$cartElement->multiplyLengthWithPsiValue();
$this->sumCartElements();
}
/**
* @param $aGes
* @return AtlasSession
*/
public function setAges($aGes)
{
$this->aGes = $aGes;
if ($aGes != 0) {
$uWb = $this->cartElementsSum / $aGes;
$this->setUwb(round($uWb, 2));
} else {
$this->setUwb(0.0);
}
return $this;
}
/**
* @return float
*/
public function getAges()
{
return $this->aGes;
}
/**
* @param $uWb
*/
public function setUwb($uWb)
{
$this->uWb = $uWb;
}
/**
* @return float
*/
public function getUwb()
{
return $this->uWb;
}
/**
* @param $cartElementSum
*/
private function setCartElementsSum($cartElementSum)
{
$this->cartElementsSum = $cartElementSum;
}
/**
* @return float
*/
public function getCartElementsSum()
{
return $this->cartElementsSum;
}
/**
* @param CartElement[] $cartElements
*/
public function setCartElements($cartElements)
{
$this->cartElements = $cartElements;
}
/**
* @return CartElement[]
*/
public function getCartElements()
{
return $this->cartElements;
}
/**
* @param mixed $user
*/
public function setUser($user)
{
$this->user = $user;
}
/**
* @return mixed
*/
public function getUser()
{
return $this->user;
}
/**
* @param CartElement $cartElement
*/
public function addCartElement($cartElement)
{
$this->cartElements[] = $cartElement;
}
/**
* @param integer $cartElementId
* @return CartElement
*/
public function getCartElementById($cartElementId)
{
return $this->cartElements[$cartElementId];
}
/**
* @param $cartElementId
* @param $cartElementName
*/
public function setCartElementNameById($cartElementId, $cartElementName)
{
$this->getCartElementById($cartElementId)->setCartElementName($cartElementName);
}
/**
* @return float
*/
private function sumCartElements()
{
$sum = 0;
foreach ($this->cartElements as $cartElement) {
$sum = $sum + $cartElement->getResult();
}
$this->cartElementsSum = round($sum, 2);
return $this->getCartElementsSum();
}
public function saveToSession()
{
$_SESSION['atlasSession'] = $this;
}
public function loadFromSession()
{
if (isset($_SESSION['atlasSession'])) {
$this->setUser($_SESSION['atlasSession']->getUser());
$this->setCartElements($_SESSION['atlasSession']->getCartElements());
$this->setCartElementsSum($_SESSION['atlasSession']->getCartElementsSum());
$this->setAges($_SESSION['atlasSession']->getAges());
$this->setUwb($_SESSION['atlasSession']->getUwb());
}
}
public function createFromSession()
{
if (!empty($_SESSION['atlasSession'])) {
$this->loadFromSession();
}
return $this;
}
}