src/Controller/NodeController.php line 133

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use w3des\AdminBundle\Entity\Node;
  6. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use w3des\AdminBundle\Service\Nodes;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use w3des\AdminBundle\Model\NodeView;
  11. use w3des\AdminBundle\Service\ModuleRegistry;
  12. use w3des\AdminBundle\Service\CMS;
  13. use w3des\AdminBundle\Service\Toolbar;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  16. use Symfony\Contracts\Cache\ItemInterface;
  17. use App\Twig\AppExtension;
  18. use w3des\AdminBundle\Cache\AppCacheInterface;
  19. use w3des\AdminBundle\Twig\Runtime\HelperRuntime;
  20. /**
  21. * @author zulus
  22. */
  23. class NodeController extends AbstractController
  24. {
  25. private AppCacheInterface $cache;
  26. private Nodes $nodes;
  27. private Toolbar $toolbar;
  28. private CMS $cms;
  29. private array $tmp = [];
  30. private ModuleRegistry $registry;
  31. public function __construct(AppCacheInterface $appCache, CMS $cms, Nodes $nodes, ModuleRegistry $registry, Toolbar $toolbar)
  32. {
  33. $this->cache = $appCache;
  34. $this->nodes = $nodes;
  35. $this->toolbar = $toolbar;
  36. $this->cms = $cms;
  37. $this->registry = $registry;
  38. }
  39. public function process(Request $request, $id)
  40. {
  41. $node = $this->cache->get($this->cache->tags()->node($id), function(ItemInterface $item) use ($id) {
  42. $item->tag($this->cache->tags()->node($id));
  43. $node = $this->nodes->getById((int) $id);
  44. $node->_fetch();
  45. return $node;
  46. });
  47. if (null === $node) {
  48. throw $this->createNotFoundException();
  49. }
  50. $request->attributes->set('node', $node);
  51. $cfg = $this->nodes->getNodeCfg($node->type);
  52. if ($this->toolbar->isAdminLogged()) {
  53. $this->toolbar->setEditPath($this->generateUrl('admin.node.edit', [
  54. 'id' => $node->id,
  55. 'type' => $node->type
  56. ]));
  57. } elseif ($node->vars->has('public') && !$node->vars->public) {
  58. throw $this->createNotFoundException();
  59. }
  60. $modules = [];
  61. foreach ($node->modules as $section => $list) {
  62. $modules[$section] = [];
  63. foreach ($list as $data) {
  64. $result = $this->registry->getModule($data->type)->frontend($request, $data, $cfg['modules'][$section]['options'][$data->type]['options']);
  65. if ($result instanceof Response) {
  66. if ($result->getStatusCode() == 200) {
  67. $modules[$section][] = $result->getBody();
  68. } else {
  69. return $result;
  70. }
  71. } elseif ($result === null) {
  72. continue;
  73. } else {
  74. $modules[$section][] = $this->renderView(\str_replace('module.', 'nodes/', $data->type) . '.html.twig', $result);
  75. }
  76. }
  77. }
  78. if ($cfg['redirectEmpty'] && count($node->modules[$cfg['redirectEmpty']]) == 0) {
  79. $redirect = $this->cache->get('node.redirect.' . $node->id, function (ItemInterface $item) use ($node) {
  80. $item->tag([
  81. 'node.' . $node->id,
  82. $this->cache->tags()->node('menu')
  83. ]);
  84. foreach ($node->children as $child) {
  85. if ($child->url) {
  86. return $child->url;
  87. }
  88. }
  89. $tmp = $this->nodes->getNodes('menu', [
  90. 'where' => [
  91. 'node' => $node->model
  92. ]
  93. ]);
  94. if (count($tmp['list'])) {
  95. $n = $tmp['list'][0];
  96. foreach ($n->children as $ch) {
  97. if ($ch->vars->node && $ch->vars->node->vars->public) {
  98. return $ch->vars->node->url;
  99. }
  100. }
  101. }
  102. return false;
  103. });
  104. if ($redirect !== false) {
  105. return $this->redirect($redirect);
  106. }
  107. }
  108. // return $this->render('home/index.html.twig', ['selected' => [], 'season' => []]);
  109. $currentService = $this->cms->getService();
  110. if ($currentService == 0) {
  111. return $this->render('cms/node_' . $node->type . '.html.twig', [
  112. 'node' => $node,
  113. 'modules' => $modules
  114. ]);
  115. } else {
  116. return $this->render('cms/node_' . $node->type . '_bip.html.twig', [
  117. 'node' => $node,
  118. 'modules' => $modules
  119. ]);
  120. }
  121. }
  122. private function getNode($id): ?NodeView
  123. {
  124. if (isset($this->tmp[$id])) {
  125. return $this->tmp[$id];
  126. }
  127. return $this->tmp[$id] = $this->nodes->getById($id);
  128. }
  129. private function saveNode(NodeView $item)
  130. {
  131. $this->tmp[$item->id] = $item;
  132. }
  133. private function lazyModule($info, $node)
  134. {
  135. if (isset($this->tmp[$info[1]])) {
  136. return $this->tmp[$info[1]];
  137. }
  138. $n = new NodeView($this->nodes, $node);
  139. $n->id = $info[1];
  140. return $n;
  141. }
  142. /**
  143. * @Route("/_download/{id}", name="download", methods={"GET"})
  144. */
  145. public function download(Node $node)
  146. {
  147. if ($node->getType() != 'file') {
  148. throw $this->createAccessDeniedException();
  149. }
  150. $response = new BinaryFileResponse($this->getParameter('upload.path') . $this->nodes->getVariable($node, 'file')['path']);
  151. $response->setContentDisposition('attachment', $this->nodes->getVariable($node, 'name'));
  152. return $response;
  153. }
  154. /**
  155. * @Route("/{_locale}/search", name="search")
  156. */
  157. public function search(AppExtension $ext, Request $request)
  158. {
  159. if (strlen(trim($request->query->get('query'))) < 3) {
  160. return $this->redirectToRoute('homepage');
  161. }
  162. return $this->render('cms/search.html.twig', [
  163. 'query' => $request->query->get('query'),
  164. 'search' => $this->nodes->search($request->query->get('type') ? [
  165. $request->query->get('type')
  166. ] : [
  167. 'page',
  168. 'offer',
  169. 'news',
  170. 'product'
  171. ], [
  172. 'page' => $request->query->get('page', 1),
  173. 'max' => 10,
  174. 'service' => $this->cms->getService() ===0 ? -1 : $this->cms->getService(),
  175. 'search' => trim($request->query->get('query')),
  176. 'locale' => $request->getLocale(),
  177. 'pagging' => true
  178. ])
  179. ]);
  180. }
  181. }