<?php
namespace w3des\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use w3des\AdminBundle\Model\ValuesMap;
use w3des\AdminBundle\Model\ServiceAwareInterface;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;
/**
* @ORM\Table(indexes={
* @ORM\Index(name="node_type_idx", columns={"service", "type", "locale"}),
* @ORM\Index(name="node_external_id", columns={"external_id"})
* })
* @ORM\Entity(repositoryClass="w3des\AdminBundle\Repository\NodeRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Node implements ServiceAwareInterface, LocaleAwareInterface
{
/**
* @ORM\Id()
* @ORM\Column(type="bigint")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="integer")
*/
private $service;
/**
* @ORM\Column(type="integer")
*/
private $pos;
/**
* @ORM\Column(type="string")
*/
private $type;
/**
* @ORM\Column(type="string", length=2)
*/
private $locale;
/**
* @ORM\OneToMany(targetEntity="w3des\AdminBundle\Entity\NodeVariable", mappedBy="node", orphanRemoval=true, fetch="LAZY", cascade={"all"})
* @ORM\OrderBy({"pos" = "asc"})
*/
private $variables;
/**
* @ORM\ManyToOne(targetEntity="w3des\AdminBundle\Entity\Node", inversedBy="children")
* @ORM\JoinColumn(name="parent_id")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="w3des\AdminBundle\Entity\Node", mappedBy="parent", orphanRemoval=true)
* @ORM\OrderBy({"pos" = "ASC"})
*/
private $children;
/**
* @var ValuesMap
*/
private $variableMap;
/**
* @var string
* @ORM\Column(name="external_id", nullable=true)
*/
private $externalId;
/**
* @ORM\Column(type="json", nullable=true)
*/
private $meta;
/**
* @ORM\OneToMany(targetEntity="NodePos", mappedBy="node", cascade={"all"}, orphanRemoval=true)
*/
private $positions;
/**
* @ORM\ManyToMany(targetEntity="w3des\AdminBundle\Entity\Url", orphanRemoval=true, indexBy="locale", cascade={"all"})
* @ORM\JoinTable(name="node_urls", inverseJoinColumns={@ORM\JoinColumn(name="url_id")}, joinColumns={@ORM\JoinColumn("node_id")})
*/
private $urls;
public function __construct()
{
$this->variables = new ArrayCollection();
$this->children = new ArrayCollection();
$this->urls = new ArrayCollection();
$this->positions = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getPos()
{
return $this->pos;
}
public function setPos($pos)
{
$this->pos = (int)$pos;
return $this;
}
public function getType()
{
return $this->type;
}
public function setType(string $type)
{
$this->type = $type;
return $this;
}
public function getLocale()
{
return $this->locale;
}
public function setLocale(string $locale)
{
$this->locale = $locale;
return $this;
}
public function getVariables()
{
return $this->variables;
}
public function setVariables($variable)
{
$this->variables = $variable;
return $this;
}
public function getParent()
{
return $this->parent;
}
public function setParent($parent)
{
$this->parent = $parent;
return $this;
}
public function getChildren()
{
return $this->children;
}
public function setChildren($children)
{
$this->children = $children;
return $this;
}
/**
* @return $service
*/
public function getService(): int
{
return $this->service;
}
/**
* @param mixed $service
*/
public function setService(int $service)
{
$this->service = $service;
}
/**
* @return $variableMap
*/
public function getVariableMap()
{
return $this->variableMap;
}
/**
* @param mixed $variableMap
*/
public function setVariableMap($variableMap)
{
$this->variableMap = $variableMap;
}
public function __toString()
{
return sprintf('%s %s:%s', $this->locale, $this->type, $this->id);
}
/**
* @return $externalId
*/
public function getExternalId()
{
return $this->externalId;
}
/**
* @param string $externalId
*/
public function setExternalId($externalId)
{
$this->externalId = $externalId;
}
/**
* @return $meta
*/
public function getMeta()
{
return $this->meta;
}
/**
* @param mixed $meta
*/
public function setMeta($meta)
{
$this->meta = $meta;
}
/**
* @return $positions
*/
public function getPositions()
{
return $this->positions;
}
/**
* @param Ambigous <\Doctrine\Common\Collections\Collection, multitype:\w3des\AdminBundle\Entity\NodePos > $positions
*/
public function setPositions($positions)
{
$this->positions = $positions;
}
public function setPosition($target, $pos)
{
foreach ($this->positions as $model) {
if ($model->getTarget()->getId() == $target->getId()) {
$model->setPos($pos);
return;
}
}
$model = new NodePos();
$model->setTarget($target);
$model->setPos($pos);
$model->setNode($this);
$this->getPositions()->add($model);
}
public function findValue($name, $locale)
{
foreach ($this->variables as $v) {
if ($name == $v->getName() && $locale == $v->getLocale()) {
return $v->getValue();
}
}
return null;
}
/**
* @return $urls
*/
public function getUrls()
{
return $this->urls;
}
/**
* @param Ambigous <\Doctrine\Common\Collections\Collection, multitype:\w3des\AdminBundle\Entity\Url > $urls
*/
public function setUrls($urls)
{
$this->urls = $urls;
}
/**
* @ORM\PreRemove()
*/
public function preRemove()
{
$this->urls->clear();
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function postUpdate(LifecycleEventArgs $args)
{
$toFix = [];
foreach ($this->urls as $url) {
if ($url->getTarget() =='tmp') {
$toFix[] = $url->getId();
$url->setTarget($this->getId());
}
}
if (count($toFix) > 0) {
$args->getObjectManager()->createQuery('update w3des\AdminBundle\Entity\Url u set u.target = :target where u.id in (:ids)')->setParameters([
'target' => (string)$this->getId(),
'ids' => $toFix
])->execute();
}
}
}