bundle/w3des/AdminBundle/src/Service/Nodes.php line 120

Open in your IDE?
  1. <?php
  2. namespace w3des\AdminBundle\Service;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\Routing\RouterInterface;
  5. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  6. use w3des\AdminBundle\Entity\Node;
  7. use w3des\AdminBundle\Model\ValueDefinition;
  8. use w3des\AdminBundle\Model\ModuleInfo;
  9. use w3des\AdminBundle\Model\ValuesMap;
  10. use w3des\AdminBundle\Model\NodeView;
  11. use w3des\AdminBundle\Entity\NodeVariable;
  12. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  13. class Nodes
  14. {
  15. private EntityManagerInterface $em;
  16. private array $cfg;
  17. private Values $vals;
  18. private RouterInterface $router;
  19. private CMS $cms;
  20. private TagAwareCacheInterface $cache;
  21. public function __construct(TagAwareCacheInterface $appCache, EntityManagerInterface $em, CMS $cms, Values $vals, RouterInterface $router, array $cfg)
  22. {
  23. $this->em = $em;
  24. $this->cfg = $cfg;
  25. $this->vals = $vals;
  26. $this->router = $router;
  27. $this->cms = $cms;
  28. $this->cache = $appCache;
  29. }
  30. public function getCfg()
  31. {
  32. return $this->cfg;
  33. }
  34. public function getNodeCfg($type)
  35. {
  36. return $this->cfg[$type];
  37. }
  38. public function getVariables(Node $node, $locale = null): ValuesMap
  39. {
  40. if ($locale == null) {
  41. $locale = $this->cms->getLocale();
  42. }
  43. if ($node->getVariableMap() == null) {
  44. $cfg = $this->getNodeCfg($node->getType());
  45. $node->setVariableMap($this->vals->createMap($node->getVariables(), $cfg['fields'], $locale));
  46. }
  47. return $node->getVariableMap();
  48. }
  49. public function getVariable(Node $node, $name, $locale = null)
  50. {
  51. return $this->getVariables($node, $locale)->get($name, $locale);
  52. }
  53. /**
  54. * @return array[]
  55. */
  56. public function getFields($type)
  57. {
  58. return $this->cfg[$type]['fields'];
  59. }
  60. public function getSectionModules(Node $node, $section, $trackParents = false)
  61. {
  62. $res = [];
  63. foreach ($this->getVariable($node, $section . '_modules') as $pos => $item) {
  64. $res[] = new ModuleInfo($item->getType(), $this->getVariables($item), $pos, $node, $section);
  65. }
  66. return $res;
  67. }
  68. public function hasUrl(Node $node, $locale = null)
  69. {
  70. return $node->getUrls()->containsKey($locale ?? $this->cms->getLocale());
  71. }
  72. public function getUrl(Node $node, $mode = UrlGeneratorInterface::ABSOLUTE_PATH, $locale = null)
  73. {
  74. if (! $this->hasUrl($node, $locale)) {
  75. if ($this->getNodeCfg($node->getType())['links']) {
  76. foreach ($node->getPositions() as $pos) {
  77. if ($pos->getType() === 'link' && $pos->getTarget()->getLocale() == $locale) {
  78. return $this->getUrl($pos->getTarget(), $mode, $locale);
  79. }
  80. }
  81. }
  82. return null;
  83. }
  84. $routeParams = [
  85. 'path' => $node->getUrls()[$locale ?? $this->cms->getLocale()]->getPath(),
  86. '_locale' => $locale ?? $this->cms->getLocale()
  87. ];
  88. $prefix = '';
  89. if ($node->getService() != $this->cms->getService()) {
  90. $ser = $this->cms->getServiceById($node->getService());
  91. $host = $ser['domains'][0];
  92. $prefix = $this->router->getContext()->getScheme() . '://' . $host;
  93. $mode = UrlGeneratorInterface::ABSOLUTE_PATH;
  94. }
  95. return $prefix . $this->router->generate('node', $routeParams, $mode);
  96. }
  97. public function getNodes(string $type, array $cfg = [])
  98. {
  99. if (! isset($cfg['locale'])) {
  100. if ($this->getNodeCfg($type)['locale']) {
  101. $cfg['locale'] = $this->cms->getLocale();
  102. } else {
  103. $cfg['locale'] = Values::MODEL_DEFAULT_LOCALE;
  104. }
  105. }
  106. $cfg['page_locale'] = $this->cms->getLocale();
  107. $res = $this->em->getRepository(Node::class)->findByConfig($this->cms->getService(), $type, $this, $cfg);
  108. if ($cfg['query'] ?? false) {
  109. return $res;
  110. }
  111. $res['list'] = $this->wrapArray($res['list']);
  112. return $res;
  113. }
  114. public function getModel(int $id): ?Node
  115. {
  116. return $this->em->find(Node::class, $id);
  117. }
  118. public function getById(int $id): ?NodeView
  119. {
  120. return $this->wrap($this->em->find(Node::class, $id));
  121. }
  122. public function getByPath(string $path, ?string $locale): ?NodeView
  123. {
  124. return $this->wrap($this->em->getRepository(Node::class)->findByPath($this->cms->getService(), $path, $locale ?? $this->cms->getLocale()));
  125. }
  126. public function inUse(Node $node, $except = null)
  127. {
  128. return $this->em->getRepository(Node::class)->checkInUse($node, $except);
  129. }
  130. /**
  131. * @param Node $node
  132. * @return NodeView
  133. */
  134. public function wrap(?Node $node, $parent = null): ?NodeView
  135. {
  136. if (null === $node) {
  137. return null;
  138. }
  139. return new NodeView($this, $node, $parent);
  140. }
  141. /**
  142. *
  143. * return NodeView[]
  144. */
  145. public function wrapArray(iterable $it)
  146. {
  147. $list = [];
  148. foreach ($it as $node) {
  149. $list[] = $this->wrap($node);
  150. }
  151. return $list;
  152. }
  153. public function wrapVariables(ValuesMap $vars): ValuesMap
  154. {
  155. $values = $vars->getValues();
  156. $definitions = $vars->getDefinitions();
  157. foreach ($values as $name => $localized) {
  158. if ($definitions[$name]['storeType'] == 'node') {
  159. foreach ($localized as $loc => $v) {
  160. if ($definitions[$name]['array']) {
  161. $v = $this->wrapArray($v);
  162. } else {
  163. $v = $this->wrap($v);
  164. }
  165. $values[$name][$loc] = $v;
  166. }
  167. }
  168. }
  169. return new ValuesMap($definitions, $values, $vars->getLocale());
  170. }
  171. public function search(array $types, array $cfg = [])
  172. {
  173. $res = $this->em->getRepository(Node::class)->searchByConfig($cfg['service'] ?? $this->cms->getService(), $types, $cfg);
  174. if ($cfg['query'] ?? false) {
  175. return $res;
  176. }
  177. $res['list'] = $this->wrapArray($res['list']);
  178. return $res;
  179. }
  180. }