bundle/w3des/AdminBundle/src/Entity/Node.php line 19

Open in your IDE?
  1. <?php
  2. namespace w3des\AdminBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use w3des\AdminBundle\Model\ValuesMap;
  6. use w3des\AdminBundle\Model\ServiceAwareInterface;
  7. use Symfony\Contracts\Translation\LocaleAwareInterface;
  8. use Doctrine\ORM\Event\LifecycleEventArgs;
  9. /**
  10. * @ORM\Table(indexes={
  11. * @ORM\Index(name="node_type_idx", columns={"service", "type", "locale"}),
  12. * @ORM\Index(name="node_external_id", columns={"external_id"})
  13. * })
  14. * @ORM\Entity(repositoryClass="w3des\AdminBundle\Repository\NodeRepository")
  15. * @ORM\HasLifecycleCallbacks()
  16. */
  17. class Node implements ServiceAwareInterface, LocaleAwareInterface
  18. {
  19. /**
  20. * @ORM\Id()
  21. * @ORM\Column(type="bigint")
  22. * @ORM\GeneratedValue(strategy="AUTO")
  23. */
  24. private $id;
  25. /**
  26. * @ORM\Column(type="integer")
  27. */
  28. private $service;
  29. /**
  30. * @ORM\Column(type="integer")
  31. */
  32. private $pos;
  33. /**
  34. * @ORM\Column(type="string")
  35. */
  36. private $type;
  37. /**
  38. * @ORM\Column(type="string", length=2)
  39. */
  40. private $locale;
  41. /**
  42. * @ORM\OneToMany(targetEntity="w3des\AdminBundle\Entity\NodeVariable", mappedBy="node", orphanRemoval=true, fetch="LAZY", cascade={"all"})
  43. * @ORM\OrderBy({"pos" = "asc"})
  44. */
  45. private $variables;
  46. /**
  47. * @ORM\ManyToOne(targetEntity="w3des\AdminBundle\Entity\Node", inversedBy="children")
  48. * @ORM\JoinColumn(name="parent_id")
  49. */
  50. private $parent;
  51. /**
  52. * @ORM\OneToMany(targetEntity="w3des\AdminBundle\Entity\Node", mappedBy="parent", orphanRemoval=true)
  53. * @ORM\OrderBy({"pos" = "ASC"})
  54. */
  55. private $children;
  56. /**
  57. * @var ValuesMap
  58. */
  59. private $variableMap;
  60. /**
  61. * @var string
  62. * @ORM\Column(name="external_id", nullable=true)
  63. */
  64. private $externalId;
  65. /**
  66. * @ORM\Column(type="json", nullable=true)
  67. */
  68. private $meta;
  69. /**
  70. * @ORM\OneToMany(targetEntity="NodePos", mappedBy="node", cascade={"all"}, orphanRemoval=true)
  71. */
  72. private $positions;
  73. /**
  74. * @ORM\ManyToMany(targetEntity="w3des\AdminBundle\Entity\Url", orphanRemoval=true, indexBy="locale", cascade={"all"})
  75. * @ORM\JoinTable(name="node_urls", inverseJoinColumns={@ORM\JoinColumn(name="url_id")}, joinColumns={@ORM\JoinColumn("node_id")})
  76. */
  77. private $urls;
  78. public function __construct()
  79. {
  80. $this->variables = new ArrayCollection();
  81. $this->children = new ArrayCollection();
  82. $this->urls = new ArrayCollection();
  83. $this->positions = new ArrayCollection();
  84. }
  85. public function getId()
  86. {
  87. return $this->id;
  88. }
  89. public function setId($id)
  90. {
  91. $this->id = $id;
  92. return $this;
  93. }
  94. public function getPos()
  95. {
  96. return $this->pos;
  97. }
  98. public function setPos($pos)
  99. {
  100. $this->pos = (int)$pos;
  101. return $this;
  102. }
  103. public function getType()
  104. {
  105. return $this->type;
  106. }
  107. public function setType(string $type)
  108. {
  109. $this->type = $type;
  110. return $this;
  111. }
  112. public function getLocale()
  113. {
  114. return $this->locale;
  115. }
  116. public function setLocale(string $locale)
  117. {
  118. $this->locale = $locale;
  119. return $this;
  120. }
  121. public function getVariables()
  122. {
  123. return $this->variables;
  124. }
  125. public function setVariables($variable)
  126. {
  127. $this->variables = $variable;
  128. return $this;
  129. }
  130. public function getParent()
  131. {
  132. return $this->parent;
  133. }
  134. public function setParent($parent)
  135. {
  136. $this->parent = $parent;
  137. return $this;
  138. }
  139. public function getChildren()
  140. {
  141. return $this->children;
  142. }
  143. public function setChildren($children)
  144. {
  145. $this->children = $children;
  146. return $this;
  147. }
  148. /**
  149. * @return $service
  150. */
  151. public function getService(): int
  152. {
  153. return $this->service;
  154. }
  155. /**
  156. * @param mixed $service
  157. */
  158. public function setService(int $service)
  159. {
  160. $this->service = $service;
  161. }
  162. /**
  163. * @return $variableMap
  164. */
  165. public function getVariableMap()
  166. {
  167. return $this->variableMap;
  168. }
  169. /**
  170. * @param mixed $variableMap
  171. */
  172. public function setVariableMap($variableMap)
  173. {
  174. $this->variableMap = $variableMap;
  175. }
  176. public function __toString()
  177. {
  178. return sprintf('%s %s:%s', $this->locale, $this->type, $this->id);
  179. }
  180. /**
  181. * @return $externalId
  182. */
  183. public function getExternalId()
  184. {
  185. return $this->externalId;
  186. }
  187. /**
  188. * @param string $externalId
  189. */
  190. public function setExternalId($externalId)
  191. {
  192. $this->externalId = $externalId;
  193. }
  194. /**
  195. * @return $meta
  196. */
  197. public function getMeta()
  198. {
  199. return $this->meta;
  200. }
  201. /**
  202. * @param mixed $meta
  203. */
  204. public function setMeta($meta)
  205. {
  206. $this->meta = $meta;
  207. }
  208. /**
  209. * @return $positions
  210. */
  211. public function getPositions()
  212. {
  213. return $this->positions;
  214. }
  215. /**
  216. * @param Ambigous <\Doctrine\Common\Collections\Collection, multitype:\w3des\AdminBundle\Entity\NodePos > $positions
  217. */
  218. public function setPositions($positions)
  219. {
  220. $this->positions = $positions;
  221. }
  222. public function setPosition($target, $pos)
  223. {
  224. foreach ($this->positions as $model) {
  225. if ($model->getTarget()->getId() == $target->getId()) {
  226. $model->setPos($pos);
  227. return;
  228. }
  229. }
  230. $model = new NodePos();
  231. $model->setTarget($target);
  232. $model->setPos($pos);
  233. $model->setNode($this);
  234. $this->getPositions()->add($model);
  235. }
  236. public function findValue($name, $locale)
  237. {
  238. foreach ($this->variables as $v) {
  239. if ($name == $v->getName() && $locale == $v->getLocale()) {
  240. return $v->getValue();
  241. }
  242. }
  243. return null;
  244. }
  245. /**
  246. * @return $urls
  247. */
  248. public function getUrls()
  249. {
  250. return $this->urls;
  251. }
  252. /**
  253. * @param Ambigous <\Doctrine\Common\Collections\Collection, multitype:\w3des\AdminBundle\Entity\Url > $urls
  254. */
  255. public function setUrls($urls)
  256. {
  257. $this->urls = $urls;
  258. }
  259. /**
  260. * @ORM\PreRemove()
  261. */
  262. public function preRemove()
  263. {
  264. $this->urls->clear();
  265. }
  266. /**
  267. * @ORM\PostPersist()
  268. * @ORM\PostUpdate()
  269. */
  270. public function postUpdate(LifecycleEventArgs $args)
  271. {
  272. $toFix = [];
  273. foreach ($this->urls as $url) {
  274. if ($url->getTarget() =='tmp') {
  275. $toFix[] = $url->getId();
  276. $url->setTarget($this->getId());
  277. }
  278. }
  279. if (count($toFix) > 0) {
  280. $args->getObjectManager()->createQuery('update w3des\AdminBundle\Entity\Url u set u.target = :target where u.id in (:ids)')->setParameters([
  281. 'target' => (string)$this->getId(),
  282. 'ids' => $toFix
  283. ])->execute();
  284. }
  285. }
  286. }