src/Application/Controller/AbstractController.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\Application\Controller;
  3. use App\Admin\Document\AbstractSEODocument;
  4. use App\Admin\Document\Client;
  5. use App\Admin\Document\Config;
  6. use App\Admin\Document\FeedItemType;
  7. use App\Admin\Document\Order;
  8. use App\Admin\Document\Page;
  9. use App\Admin\Document\UserAccount;
  10. use App\Application\Cart\Cart;
  11. use App\Application\Twig\GlobalLoader;
  12. use Doctrine\ODM\MongoDB\DocumentManager;
  13. use Doctrine\ODM\MongoDB\Query\Builder;
  14. use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
  15. use Psr\Container\ContainerInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use AvenueAdminBundle\Controller\AbstractApplicationController;
  18. abstract class AbstractController extends AbstractApplicationController
  19. {
  20.     /**
  21.      * @var Cart
  22.      */
  23.     protected $cart;
  24.     public function getCart(): Cart
  25.     {
  26.         return $this->cart;
  27.     }
  28.     /**
  29.      * @param Cart $cart
  30.      * @required
  31.      */
  32.     public function setCart(Cart $cart) {
  33.         $this->cart $cart;
  34.     }
  35.     /**
  36.      * @return Config
  37.      */
  38.     protected function getConfig() {
  39.         return $this->getDocumentRepository(Config::class)->findOneBy([]);
  40.     }
  41.     protected function setSeoByUrl($url$locale)
  42.     {
  43.         $page $this->getDocumentRepository(Page::class)->findOneBy(
  44.             ['url' => $url'active' => true]
  45.         );
  46.         if ($page) {
  47.             $this->setSeo($page$locale);
  48.         } else {
  49.             throw $this->createNotFoundException('page not found or not active: ' $url);
  50.         }
  51.         return $page;
  52.     }
  53.     protected function setSeo(AbstractSEODocument $document$locale$page null) {
  54.         $this->setSeoVariables(
  55.             $document->getValidSeoTitle($locale) . ($page ". Страница " $page ''),
  56.             $document->getSeoDescription($locale) . ($page ". Страница " $page ''),
  57.             $document->getSeoKeywords($locale)
  58.         );
  59.     }
  60.     protected function setSeoVariables($seoTitle$seoDescription ''$seoKeywords '')
  61.     {
  62.         parent::setSeoVariables($seoTitle ' ' $this->getConfig()->getSeoTail(), $seoDescription$seoKeywords);
  63.     }
  64.     protected function trimPhone($phone) {
  65.         $s mb_ereg_replace('[^0-9]'''$phone);
  66.         if (strlen($s) == 1) {
  67.             $s '';
  68.         }
  69.         return $s;
  70.     }
  71.     /**
  72.      * @return UserAccount
  73.      */
  74.     protected function getLoggedInUser() {
  75.         return $this->getIdentity();
  76.     }
  77.     private $directorTag;
  78.     protected function getDirectorTag() {
  79.         if (!$this->directorTag) {
  80.             $this->directorTag $this->getDocumentRepository(FeedItemType::class)->findOneBy(['url' => FeedItemType::URL_DIRECTOR]);
  81.         }
  82.         return $this->directorTag;
  83.     }
  84.     protected function printPdf($fileName$html) {
  85.         $filePath sys_get_temp_dir() . '/' uniqid();
  86.         @unlink($filePath);
  87.         $temp tempnam(sys_get_temp_dir(), 'PDF') . '.html';
  88.         if (!file_put_contents($temp$html)) {
  89.             throw new \Exception('cannot write to ' $temp);
  90.         }
  91.         $output = [];
  92.         $result = -1;
  93.         $documentFields '-T 10mm -B 10mm -L 15mm -R 15mm';
  94.         $command sprintf('/usr/local/bin/wkhtmltopdf' ' ' $documentFields ' '
  95.             '--footer-center "[page]" --footer-font-size 9 --footer-font-name "Times New Roman" --footer-spacing 5'
  96.             ' %1$s "%2$s" 2>&1'$temp$filePath);
  97.         exec($command$output$result);
  98.         @unlink($temp);
  99.         if ($result !== 0) {
  100.             throw new \Exception("error code $result: " $command ': ' var_export($outputtrue));
  101.         }
  102.         if (!file_exists($filePath)) {
  103.             throw new \Exception('cannot create pdf: ' $command ': ' var_export($outputtrue));
  104.         }
  105.         $content file_get_contents($filePath);
  106.         header('Content-Type: application/pdf');
  107.         header('Content-disposition: inline; filename="' $fileName '.pdf"');
  108.         header('Cache-Control: no-cache, no-store, must-revalidate');
  109.         echo $content;
  110.         die;
  111.     }
  112.     protected function sendEmailOrderPaid(Order $order) {
  113.         if ($order->getUserAccount() && $order->getUserAccount()->getEmail()
  114.         ) {
  115.             $text $this->renderView(
  116.                 'application/mail/paid.html.twig', [
  117.                     'order' => $order,
  118.                 ]
  119.             );
  120.             try {
  121.                 $this->getMail()->send($order->getUserAccount()->getEmail(), 'Спасибо за оплату заказа!'$text);
  122.             } catch (\Exception $e) {
  123.             }
  124.         }
  125.     }
  126.     /**
  127.      * @return GlobalLoader
  128.      */
  129.     protected function getGlobal()
  130.     {
  131.         return $this->getTwig()->getGlobals()['global'];
  132.     }
  133.     /**
  134.      * @return int
  135.      */
  136.     protected function getDefaultItemCountPerPage(){
  137.         return 6;
  138.     }
  139.     protected function detectCaptchaBot(Request $request)
  140.     {
  141.         $captcha $request->get('token');
  142.         if (!$captcha) {
  143.             throw new \Exception('no token');
  144.         }
  145.         $secret $this->getParameter('recaptcha_secret_key');
  146.         $url "https://www.google.com/recaptcha/api/siteverify";
  147.         $data = [
  148.             'secret' => $secret,
  149.             'response' => $captcha,
  150.         ];
  151.         $options = array(
  152.             'http' => array(
  153.                 'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
  154.                 'method'  => 'POST',
  155.                 'content' => http_build_query($data)
  156.             )
  157.         );
  158.         $context  = @stream_context_create($options);
  159.         $json = @file_get_contents($urlfalse$context);
  160.         $response = @json_decode($jsontrue);
  161.         if ($response !== null) {
  162.             if ($response['success'] == true) {
  163.                 if ($response['score'] < 0.5) {
  164.                     throw new \Exception('bot detected by recaptcha ' $response->score);
  165.                 }
  166.             } else {
  167.                 if (in_array('browser-error'$response['error-codes'])) {
  168.                     throw new \Exception('recaptcha error: ' $json);
  169.                 }
  170.             }
  171.         }
  172.     }
  173. }