src/Controller/UserController.php line 79

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Env;
  4. use App\DTO\AppDTO;
  5. use App\Entity\Cart;
  6. use App\Entity\User;
  7. use App\Entity\Order;
  8. use App\Entity\Esystem;
  9. use App\Entity\Delivery;
  10. use App\Entity\Discount;
  11. use App\Service\Auth\Auth;
  12. use App\Entity\Subdelivery;
  13. use App\Model\Order as ModelOrder;
  14. use App\Repository\CartRepository;
  15. use App\Repository\UserRepository;
  16. use App\Repository\OrderRepository;
  17. use App\Repository\EsystemRepository;
  18. use App\Repository\DeliveryRepository;
  19. use App\Repository\DiscountRepository;
  20. use App\Service\Cart\Cart as CartCart;
  21. use App\Service\Discount\UserDiscount;
  22. use Doctrine\ORM\EntityManagerInterface;
  23. use App\Repository\SubdeliveryRepository;
  24. use Symfony\Contracts\Cache\CacheInterface;
  25. use Doctrine\ORM\Tools\Pagination\Paginator;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\Security\Core\Security;
  28. use App\Service\Paginator as ServicePaginator;
  29. use Symfony\Component\HttpFoundation\Response;
  30. use Symfony\Component\Routing\Annotation\Route;
  31. use Symfony\Component\HttpFoundation\RequestStack;
  32. class UserController extends AbstractASController
  33. {
  34.     protected EntityManagerInterface $em;
  35.     protected CacheInterface $Cache;
  36.     protected AppDTO $app;
  37.     protected Auth $Auth;
  38.     protected UserDiscount $UserDiscount;
  39.     protected ModelOrder $ModelOrder;
  40.     //Repositories
  41.     protected OrderRepository $Orders;
  42.     protected CartRepository $Carts;
  43.     protected UserRepository $Users;
  44.     protected DeliveryRepository $Deliveries;
  45.     protected EsystemRepository $Esystems;
  46.     protected SubdeliveryRepository $Subdeliveries;
  47.     protected DiscountRepository $Discounts;
  48.     private $order_total;
  49.     public function __construct(EntityManagerInterface $emCacheInterface $CacheAppDTO $appAuth $AuthUserDiscount $UserDiscountModelOrder $ModelOrderSecurity $securityRequestStack $requestStack)
  50.     {
  51.         $this->requestStack $requestStack;
  52.         $this->Cache $Cache;
  53.         $this->em $em;
  54.         $this->app $app;
  55.         $this->Auth $Auth;
  56.         $this->Auth->setUser($security->getUser());
  57.         $this->UserDiscount $UserDiscount;
  58.         $this->ModelOrder $ModelOrder;
  59.         $this->Orders $em->getRepository(Order::class);
  60.         $this->Carts $em->getRepository(Cart::class);
  61.         $this->Users $em->getRepository(User::class);
  62.         $this->Deliveries $em->getRepository(Delivery::class);
  63.         $this->Esystems $em->getRepository(Esystem::class);
  64.         $this->Subdeliveries $em->getRepository(Subdelivery::class);
  65.         $this->Discounts $em->getRepository(Discount::class);
  66.     }
  67.     // Главная    
  68.     #[Route(path'/user'name'user_no_locale'priority100defaults: ['_locale' => '%app.default_lang%'])]
  69.     #[Route(path'/{_locale}/user'name'user'priority100requirements: ['_locale' => '%app.langs%'])]
  70.     public function index(Request $request): Response
  71.     {
  72.         $ocount = [000000000000];
  73.         //        $Order = new Model_Order();
  74.         //        $orders = $Order->getall(array("where" => "user = '".Db::nq(Auth::getUserId())."'", "order" => "tstamp desc"));
  75.         //        foreach ($orders as $order) {
  76.         //            $ocount[$order->status]++;
  77.         return $this->render('user/index.html.twig', [
  78.             'ocount' => $ocount,
  79.             'order_total' => 0,
  80.             'discount' => 0,
  81.             'nextdiscount' => ['name' => '''value' => ''],
  82.             'tonextdiscount' => 0,
  83.             'controller_name' => 'UserController',
  84.             'ocount' => $this->_ocount(),
  85.             'order_total' => $this->_order_total(),
  86.             'discounts' => $this->_discounts(),
  87.             'discount' => $this->_discount(),
  88.             'nextdiscount' => $this->_nextdiscount(),
  89.             'tonextdiscount' => $this->_tonextdiscount(),
  90.             'is_opt' => Env::is_opt(),
  91.         ]);
  92.     }
  93.     
  94.     // История заказов. Фильтр по статусу
  95.     #[Route(path'/user/order-history'name'user_orders_no_locale'priority100defaults: ['_locale' => '%app.default_lang%'])]
  96.     #[Route(path'/user/order-history/status/{status}'name'user_orders_status_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  97.     #[Route(path'/{_locale}/user/order-history'name'user_orders'priority100requirements: ['_locale' => '%app.langs%'])]
  98.     #[Route(path'/{_locale}/user/order-history/status/{status}'name'user_orders_status'requirements: ['_locale' => '%app.langs%''status' => '1|2|3|4|5|6|7|8|9'])]
  99.     public function orderList(Request $requestint $status 0): Response
  100.     {
  101.         /** @var \App\Entity\User $user */
  102.         $user $this->getUser();
  103.         $userid $user $user->getId() : 0;
  104.         $results 50;
  105.         $start = (int) $request->query->get('start');
  106.         if ($status) {
  107.             $dql "SELECT o FROM App\Entity\Order o WHERE o.user = " $userid " and o.status = " $status " ORDER BY o.tstamp DESC";
  108.         } else {
  109.             $dql "SELECT o FROM App\Entity\Order o WHERE o.user = " $userid " ORDER BY o.tstamp DESC";
  110.         }
  111.         $query $this->em->createQuery($dql)
  112.             ->setFirstResult($start)
  113.             ->setMaxResults($results);
  114.         $paginator = new Paginator($query);
  115.         $cnt count($paginator);
  116.         $bc["/"] = $this->app->labels->get('user-title-3');
  117.         return $this->render('user/orders.html.twig', [
  118.             'bc' => $bc,
  119.             'orders' => $paginator// @todo: добавить order->amount для каждого
  120.             'paginator' => new ServicePaginator($request->attributes->get('_route'), $cnt$results$start),
  121.             'ocount' => $this->_ocount(),
  122.             'order_total' => $this->_order_total(),
  123.             'discounts' => $this->_discounts(),
  124.             'discount' => $this->_discount(),
  125.             'nextdiscount' => $this->_nextdiscount(),
  126.             'tonextdiscount' => $this->_tonextdiscount(),
  127.         ]);
  128.     }
  129.     
  130.     // История заказов. 1 Заказ
  131.     #[Route(path'/user/order-history/order/last'name'user_order_last_no_locale'priority100defaults: ['_locale' => '%app.default_lang%'])]
  132.     #[Route(path'/user/order-history/order/{order_id}'name'user_order_cont_no_locale'priority100defaults: ['_locale' => '%app.default_lang%'])]
  133.     #[Route(path'/{_locale}/user/order-history/order/last'name'user_order_last'priority100requirements: ['_locale' => '%app.langs%'])]
  134.     #[Route(path'/{_locale}/user/order-history/order/{order_id}'name'user_order_cont'priority100requirements: ['_locale' => '%app.langs%'])]
  135.     public function orderCont(CartCart $CartSecurity $securityint $order_id 0): Response
  136.     {
  137.         if ($order_id) {
  138.             $order $this->Orders->find($order_id);
  139.         } else {
  140.             $order $this->Orders->getLastOrder($security->getUser()->getId());
  141.         }
  142.         
  143.         if (!$order) {
  144.             return $this->redirectToRoute('user_orders');
  145.         }
  146.         $delivery $this->Deliveries->find((int) $order->getDelivery());
  147.         $subdelivery $this->Subdeliveries->find((int) $order->getSubdelivery());
  148.         $esystem $this->Esystems->find((int) $order->getEsystem());        
  149.         $cart $Cart->getFromOrder($order->getId());
  150.         
  151.         $bc["/"] = $this->app->labels->get('user-title-3');
  152.         $bc["/asdf/"] = $this->app->labels->get('user-title-4') . $order->getId();
  153.         return $this->render('user/order.html.twig', [
  154.             'bc' => $bc,
  155.             'order' => $order,
  156.             'delivery' => $delivery,
  157.             'subdelivery' => $subdelivery,
  158.             'esystem' => $esystem,
  159.             // шаблон не доделал, там огромное кол-во разных переменных, их нужно сначала сформировать
  160.             //'cart' => $Cart,
  161.             'prods' => $cart,
  162.             'ocount' => $this->_ocount(),
  163.             'order_total' => $this->_order_total(),
  164.             'discounts' => $this->_discounts(),
  165.             'discount' => $this->_discount(),
  166.             'nextdiscount' => $this->_nextdiscount(),
  167.             'tonextdiscount' => $this->_tonextdiscount(),
  168.         ]);
  169.     }
  170.     // Изменить профиль пользователя
  171.     #[Route(path'/user/profile'name'user_profile_no_locale'priority100defaults: ['_locale' => '%app.default_lang%'])]
  172.     #[Route(path'/{_locale}/user/profile'name'user_profile'priority100requirements: ['_locale' => '%app.langs%'])]
  173.     public function userProfile(Request $request): Response
  174.     {
  175.         /** @var \App\Entity\User $user */
  176.         $user $this->getUser();
  177.         if ($request->get("submit")) {
  178.             $country = (string) $request->get('country');
  179.             $city = (string) $request->get('city');
  180.             $address = (string) $request->get('address');
  181.             $name = (string) $request->get('name');
  182.             $surname = (string) $request->get('surname');
  183.             $phone = (string) $request->get('phone');
  184.             $User $this->Users->find((int) $user->getId());
  185.             $User->setCountry($country);
  186.             $User->setCity($city);
  187.             $User->setAddress($address);
  188.             $User->setName($name);
  189.             $User->setSurname($surname);
  190.             $User->setPhone($phone);
  191.             if ((Env::site() == Env::MIX) || (Env::site() == Env::OPT_MIX)) {
  192.                 $company_nip = (string) $request->get('company_nip');
  193.                 $company_name = (string) $request->get('company_name');
  194.                 $company_index = (string) $request->get('company_index');
  195.                 $company_city = (string) $request->get('company_city');
  196.                 $company_street = (string) $request->get('company_street');
  197.                 $company_house = (string) $request->get('company_house');
  198.                 $company_flat = (string) $request->get('company_flat');
  199.                 $User->setCompanyNip($company_nip);
  200.                 $User->setCompanyName($company_name);
  201.                 $User->setCompanyIndex($company_index);
  202.                 $User->setCompanyCity($company_city);
  203.                 $User->setCompanyStreet($company_street);
  204.                 $User->setCompanyHouse($company_house);
  205.                 $User->setCompanyFlat($company_flat);
  206.             }
  207.             
  208.             $this->em->flush();
  209.             return $this->redirectToRoute('user_profile', ['changed' => 1]);
  210.         } else {
  211.             $bc["/"] = $this->app->labels->get('user-title-6');
  212.             return $this->render('user/profile.html.twig', [
  213.                 'user' => $user,
  214.                 'bc' => $bc,
  215.                 'ocount' => $this->_ocount(),
  216.                 'order_total' => $this->_order_total(),
  217.                 'discounts' => $this->_discounts(),
  218.                 'discount' => $this->_discount(),
  219.                 'nextdiscount' => $this->_nextdiscount(),
  220.                 'tonextdiscount' => $this->_tonextdiscount(),
  221.             ]);
  222.         }
  223.     }
  224.     // Сменить пароль
  225.     #[Route(path'/user/change-pass'name'user_change_pass_no_locale'priority100defaults: ['_locale' => '%app.default_lang%'])]
  226.     #[Route(path'/{_locale}/user/change-pass'name'user_change_pass'priority100requirements: ['_locale' => '%app.langs%'])]
  227.     public function changePassword(Request $request): Response
  228.     {
  229.         /** @var \App\Entity\User $user */
  230.         $user $this->getUser();
  231.         if ($request->get("submit")) {
  232.             $User $this->Users->find((int) $user->getId());
  233.             $User->setPassword($request->get('pass'));
  234.             $this->em->flush();
  235.             return $this->redirectToRoute('user');
  236.         } else {
  237.             $bc["/"] = $this->app->labels->get('user-title-7');
  238.             return $this->render('user/change-password.html.twig', [
  239.                 'user' => $user,
  240.                 'bc' => $bc,
  241.                 'ocount' => $this->_ocount(),
  242.                 'order_total' => $this->_order_total(),
  243.                 'discounts' => $this->_discounts(),
  244.                 'discount' => $this->_discount(),
  245.                 'nextdiscount' => $this->_nextdiscount(),
  246.                 'tonextdiscount' => $this->_tonextdiscount(),
  247.             ]);
  248.         }
  249.     }
  250.     // Сменить пароль
  251.     #[Route(path'/user/logoff'name'user_logoff_no_locale'priority100defaults: ['_locale' => '%app.default_lang%'])]
  252.     #[Route(path'/{_locale}/user/logoff'name'user_logoff'priority100requirements: ['_locale' => '%app.langs%'])]
  253.     public function logoff(Request $request): Response
  254.     {
  255.         return $this->redirectToRoute('home');
  256.     }
  257.     #[Route(path'/user/unlink/facebook'name'user_facebook_unlink'priority100)]
  258.     public function unlinkFacebook(Request $request): Response
  259.     {
  260.         $user $this->Users->find($this->Auth->getUserId());
  261.         $user->setFacebookId('');
  262.         $this->em->flush();
  263.         return $this->redirectToRoute('user');
  264.     }
  265.     #[Route(path'/user/unlink/google'name'user_google_unlink'priority100)]
  266.     public function unlinkGoogle(Request $request): Response
  267.     {
  268.         $user $this->Users->find($this->Auth->getUserId());
  269.         $user->setGoogleId('');
  270.         $this->em->flush();
  271.         return $this->redirectToRoute('user');
  272.     }
  273.     private function _ocount(): array
  274.     {
  275.         /** @var \App\Entity\Order[] $orders */
  276.         $orders $this->Orders->findBy(['user' => $this->Auth->getUserId()]);
  277.         //Order->getall(array("where" => "user = '".Db::nq(Auth::getUserId())."'", "order" => "tstamp desc"));
  278.         $ocount = array(000000000000);
  279.         foreach ($orders as $order) {
  280.             $ocount[$order->getStatus()]++;
  281.         }
  282.         return $ocount;
  283.     }
  284.     private function _order_total(): float
  285.     {
  286.         $this->order_total $this->ModelOrder->total($this->Auth->getUserId());
  287.         return $this->order_total;
  288.     }
  289.     private function _discounts()
  290.     {
  291.         return $this->Discounts->findAll();
  292.     }
  293.     private function _discount()
  294.     {
  295.         return $this->UserDiscount->getnakop($this->order_total);
  296.     }
  297.     private function _nextdiscount()
  298.     {
  299.         return $this->UserDiscount->nextdiscount($this->order_total);
  300.     }
  301.     private function _tonextdiscount()
  302.     {
  303.         return $this->UserDiscount->tonextdiscount($this->order_total);
  304.     }
  305. }