src/Controller/HomeController.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Cookie;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use w3des\AdminBundle\Service\ModuleRegistry;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use w3des\AdminBundle\Model\NodeView;
  10. use w3des\AdminBundle\Service\Nodes;
  11. use w3des\AdminBundle\Entity\Node;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use App\Kernel;
  14. use Symfony\Bundle\FrameworkBundle\Console\Application;
  15. use Symfony\Component\HttpFoundation\StreamedResponse;
  16. use Symfony\Component\Console\Input\ArrayInput;
  17. use Symfony\Component\Console\Output\StreamOutput;
  18. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  19. use Negotiation\Negotiator;
  20. use w3des\AdminBundle\Service\CMS;
  21. use Negotiation\LanguageNegotiator;
  22. use Symfony\Contracts\Translation\TranslatorInterface;
  23. use Symfony\Component\Routing\RouterInterface;
  24. class HomeController extends AbstractController
  25. {
  26. private $pageLocales;
  27. private CMS $cms;
  28. public function __construct($pageLocales, CMS $cms)
  29. {
  30. $this->pageLocales = $pageLocales;
  31. $this->cms = $cms;
  32. }
  33. public function home(Request $request, ModuleRegistry $registry, Nodes $nodes)
  34. {
  35. if ($request->isMethod('post') && $request->request->has('contact')) {
  36. $v = new NodeView($nodes);
  37. $v->model = new Node();
  38. $v->model->setType('module.contact_form');
  39. $res = $registry->getModule('module.contact_form')->frontend($request, $v, []);
  40. if ($res instanceof Response && $res->getStatusCode() != 200) {
  41. return $res;
  42. }
  43. }
  44. if ($request->isMethod('post') && ($request->request->has('service') || $request->request->has('offer'))) {
  45. $v = new NodeView($nodes);
  46. $v->model = new Node();
  47. $v->model->setType('module.offer_form');
  48. $res = $registry->getModule('module.offer_form')->frontend($request, $v, []);
  49. if ($res instanceof Response && $res->getStatusCode() != 200) {
  50. return $res;
  51. }
  52. }
  53. $currentService = $this->cms->getService();
  54. if ($currentService == 0) {
  55. return $this->render('home/index.html.twig', []);
  56. } else {
  57. return $this->render('home/bip.html.twig', []);
  58. }
  59. }
  60. public function root(Request $request)
  61. {
  62. if ($this->isBot()) {
  63. return $this->home($request);
  64. }
  65. if ($request->cookies->has('LOCALE')) {
  66. if ($request->getLocale() != 'pl') {
  67. return $this->redirectToRoute('homepage', [
  68. '_locale' => $request->getLocale()
  69. ]);
  70. }
  71. return $this->home($request);
  72. }
  73. $negotiator = new \Negotiation\LanguageNegotiator();
  74. $lang = $negotiator->getBest($request->headers->get('accept-language'), $this->pageLocales);
  75. $lang = $lang ? $lang->getType() : 'pl';
  76. $request->setLocale($lang);
  77. $resp = null;
  78. if ($lang == 'pl') {
  79. $resp = $this->home($request);
  80. } else {
  81. $resp = $this->redirectToRoute('homepage', [
  82. '_locale' => $request->getLocale()
  83. ]);
  84. }
  85. $resp->headers->setCookie(new Cookie('LOCALE', $lang, time() + 3600 * 24 * 7, "/", $request->getHost(), $request->isSecure(), ! $request->isSecure()));
  86. return $resp;
  87. }
  88. private function isBot()
  89. {
  90. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  91. return preg_match('/rambler|abacho|acoi|accona|aspseek|altavista|estyle|scrubby|lycos|geona|ia_archiver|alexa|sogou|skype|facebook|twitter|pinterest|linkedin|naver|bing|google|yahoo|duckduckgo|yandex|baidu|teoma|xing|java\/1.7.0_45|bot|crawl|slurp|spider|mediapartners|\sask\s|\saol\s/i', $_SERVER['HTTP_USER_AGENT']);
  92. }
  93. return false;
  94. }
  95. /**
  96. * @Route("/_upload_pim", name="upload_pim", methods={"POST"})
  97. */
  98. public function saveFile(Request $request, CacheManager $liipCache, string $publicDir)
  99. {
  100. if ($request->request->get('pass') != $_ENV['PIM_UPLOAD_PASS']) {
  101. throw $this->createAccessDeniedException();
  102. }
  103. if (empty($request->request->get('target'))) {
  104. throw $this->createNotFoundException();
  105. }
  106. /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
  107. $file = $request->files->get('file');
  108. $target = $publicDir . '/uploads/product' . $request->request->get('target');
  109. $liipCache->remove('/uploads/product' . $request->request->get('target'), null);
  110. if (!\file_exists(dirname($target))) {
  111. \mkdir(\dirname($target), 0777, true);
  112. }
  113. $file->move(dirname($target), \basename($target));
  114. return new JsonResponse([
  115. 'success' => true
  116. ]);
  117. }
  118. /**
  119. * @Route("/_drop_cache", name="drop_cache", methods={"POST"})
  120. */
  121. public function dropCache(Kernel $kernel, Request $request)
  122. {
  123. if ($request->request->get('pass') != $_ENV['PIM_UPLOAD_PASS']) {
  124. throw $this->createAccessDeniedException();
  125. }
  126. return new StreamedResponse(function() use ($kernel) {
  127. $cacheDir = $kernel->getCacheDir();
  128. $toMove = $cacheDir . \uniqid();
  129. \rename($cacheDir, $toMove);
  130. $dirObj= new \RecursiveDirectoryIterator($toMove, \RecursiveDirectoryIterator::SKIP_DOTS); //upper dirs not included,otherwise DISASTER HAPPENS :)
  131. $files = new \RecursiveIteratorIterator($dirObj, \RecursiveIteratorIterator::CHILD_FIRST);
  132. foreach ($files as $path) {
  133. echo $path->getPathname() . "\r\n";
  134. $path->isDir() && !$path->isLink() ? rmdir($path->getPathname()) : unlink($path->getPathname());
  135. }
  136. rmdir($toMove);
  137. });
  138. }
  139. /**
  140. * @Route("/open/{symbol}", name="index_url", requirements={"symbol" = ".+"})
  141. * @Route("/{service}/{symbol}", name="qr_url", requirements={"symbol" = ".+", "service" = "[0-9]"})
  142. */
  143. public function openProduct(string $symbol, Request $request, Nodes $nodes, CMS $cms, TranslatorInterface $trans, $service = null)
  144. {
  145. if ($service) {
  146. $cms->setService($service);
  147. }
  148. $result = $nodes->getNodes('index', [
  149. 'where' => [
  150. 'index' => $symbol
  151. ],
  152. 'pagging' => false
  153. ]);
  154. if (count($result['list']) == 0) {
  155. return $this->redirect($this->generateUrl('homepage', [], RouterInterface::ABSOLUTE_URL));
  156. }
  157. $node = $result['list'][0];
  158. $neg = new LanguageNegotiator();
  159. /** @var \Negotiation\AcceptLanguage $best */
  160. $best = $neg->getBest($request->headers->get('accept-language'), ['en', 'pl', 'ro', 'ru', 'hu' ]);
  161. $locale = null;
  162. if ($best->getQuality() == 1) {
  163. $locale = $best->getValue();
  164. } else {
  165. $locale = 'en';
  166. }
  167. $cms->setlocale($locale);
  168. $trans->setLocale($locale);
  169. $cms->setService($node->service);
  170. $product = $nodes->getNodes('product', [
  171. 'where' => [
  172. 'public' => true,
  173. 'indexes' => $node->id
  174. ],
  175. 'pagging' => false,
  176. ]);
  177. if (count($product['list']) == 0) {
  178. //return $this->redirect($this->generateUrl('homepage', [], RouterInterface::ABSOLUTE_URL));
  179. $url = 'https://' . $cms->getSettings()['domains'][0] . '/' . ($locale != 'pl' ? $locale : '');
  180. } else {
  181. $url = 'https://' . $cms->getSettings()['domains'][0] . $product['list'][0]->url($locale, RouterInterface::ABSOLUTE_PATH);
  182. }
  183. return $this->redirect($url);
  184. }
  185. /**
  186. * @Route("/{_locale}/_news.json")
  187. */
  188. public function newsApi(Request $request, Nodes $nodes, CacheManager $manager)
  189. {
  190. $max = $request->query->getInt('max', 10);
  191. $page = $request->query->getInt('page', 1);
  192. $category = $request->query->get('category', null);
  193. $wh = [
  194. 'public' => true
  195. ];
  196. if ($category) {
  197. $wh['category'] = $category;
  198. }
  199. foreach (['b2b', 'b2c', 'www'] as $flag) {
  200. if ($request->query->get($flag)) {
  201. $wh[$flag] = $request->query->getBoolean($flag);
  202. }
  203. }
  204. $list = $nodes->getNodes('news', [
  205. 'orderBy' => [
  206. 'publishDate' => 'desc'
  207. ],
  208. 'locale' => $request->attributes->get('_locale'),
  209. 'max' => $max,
  210. 'where' => $wh,
  211. 'page' => $page
  212. ]);
  213. $res = [
  214. 'current' => $list['current'],
  215. 'max' => $list['max'],
  216. 'total' => $list['total'],
  217. 'list' => [],
  218. 'main' => null
  219. ];
  220. foreach ($list['list'] as $node) {
  221. $item = [
  222. 'id' => $node->id,
  223. 'title' => $node->vars->title,
  224. 'lead' => $node->vars->lead,
  225. 'publishDate'=> $node->vars->publishDate->format('d.m.Y'),
  226. 'photo' => $node->vars->photo->getPath(),
  227. 'thmb' => $request->getSchemeAndHttpHost() . $request->getBaseUrl() . $manager->resolve($node->vars->photo->getPath(), 'news'),
  228. 'url' => $node->url(null, RouterInterface::ABSOLUTE_URL)
  229. ];
  230. $res['list'][] = $item;
  231. }
  232. $n = $nodes->getNodes('page', [
  233. 'locale' => $request->attributes->get('_locale'),
  234. 'max' => 1,
  235. 'module' => [
  236. 'content' => [
  237. 'list',
  238. [
  239. 'type' => 'news'
  240. ]
  241. ]
  242. ]
  243. ])['list'];
  244. if (count($n)) {
  245. $res['main'] = $n[0]->url(null, RouterInterface::ABSOLUTE_URL);
  246. }
  247. return new JsonResponse($res);
  248. }
  249. }