src/Services/Utils.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use Symfony\Component\DependencyInjection\Tests\Fixtures\StdClassDecorator;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\File\File;
  6. use Symfony\Component\Finder\SplFileInfo;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\HttpFoundation\Session\Session;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\Translation\TranslatorInterface;
  15. use Symfony\Component\Form\FormEvent;
  16. use Symfony\Component\Form\FormEvents;
  17. use Symfony\Component\Form\FormError;
  18. use UserBundle\Entity\User;
  19. use App\Entity\Membership;
  20. use App\Entity\Dic;
  21. use App\Entity\Ba;
  22. use Symfony\Component\HttpFoundation\RequestStack;
  23. // Import the Twig Environment
  24. use Twig\Environment;
  25. class Utils
  26. {
  27.     private $container;
  28.     private $twig;
  29.     protected $fileUploader;
  30.     /**
  31.      * Constructor
  32.      *
  33.      * @param ContainerInterface $container
  34.      */
  35.     public function __construct(ContainerInterface $containerEntityManagerInterface $entityManagerEnvironment $twigFileUploader $fileUploader)
  36.     {
  37.         $this->container $container;
  38.         $this->em $entityManager;
  39.         $this->twig $twig;
  40.         $this->fileUploader $fileUploader;
  41.     }
  42.     public function translateUnit($unitOrigin$unit$value){
  43.         return $this->calculUnit($unitOrigin$unit$value);
  44.     }
  45.     public function translateUnitLong($unitOrigin$unit$value){
  46.         $unitTemp "";
  47.         $tabQuantity $this->calculUnit($unitOrigin$unit$valuetrue);
  48.         if($unit == "oz" || $unit == "cl"){
  49.             if($unit == "oz"){
  50.                 if($unitOrigin == "cl"){
  51.                     if($tabQuantity['typeUnit'] != null){
  52.                         $unitTemp $tabQuantity['typeUnit'];
  53.                     }
  54.                     else{
  55.                         $unitTemp $unit;
  56.                     }
  57.                 }
  58.                 else{
  59.                     $unitTemp $unitOrigin;
  60.                 }
  61.             }
  62.             else{
  63.                 if($unitOrigin == "cl" || $unitOrigin == "oz" || $unitOrigin == "cup" || $unitOrigin == "quart" || $unitOrigin == "gallon"){
  64.                     $unitTemp $unit;
  65.                 }
  66.                 else{
  67.                     $unitTemp $unitOrigin;
  68.                 }
  69.             }
  70.         }
  71.         else{
  72.             $unitTemp $unitOrigin;
  73.         }
  74.             
  75.         return $tabQuantity['valueReturn']." ".$unitTemp;
  76.     }
  77.     public function calculUnit($unitOrigin$unit$value$all false){
  78.         $valueReturn "";
  79.         $typeUnit null;
  80.         if($value == null || $value == ""){
  81.             $valueReturn "";
  82.         }
  83.         else{
  84.             if($unitOrigin == "cl" || $unitOrigin == "oz" || $unitOrigin == "cup" || $unitOrigin == "quart" || $unitOrigin == "gallon"){
  85.                 if($unit == "oz"){
  86.                     if($unitOrigin == "cl"){
  87.                         $valueBase round((floatval($value) / 2.96), 2);
  88.                         if($valueBase 2){
  89.                             if($valueBase 1){
  90.                                 $valueNew $this->MRound($valueBase 14);
  91.                             }
  92.                             else{
  93.                                 $valueNew $this->MRound($valueBase4);
  94.                             }
  95.                             if($valueNew 0.25){
  96.                                 $valueNew "¼";
  97.                             }
  98.                             else if($valueNew == "0.25"){
  99.                                 $valueNew "¼";
  100.                             }
  101.                             else if($valueNew == "0.50"){
  102.                                 $valueNew "½";
  103.                             }
  104.                             else if($valueNew == "0.75"){
  105.                                 $valueNew "¾";
  106.                             }
  107.                             else{
  108.                                 $valueNew "1";
  109.                             }
  110.                             if($valueBase 1){
  111.                                 if($valueNew == 1){
  112.                                     $valueNew 2;
  113.                                 }
  114.                                 else{
  115.                                     $valueNew "1 ".$valueNew;
  116.                                 }
  117.                             }
  118.                             $valueBase $valueNew;
  119.                         }
  120.                         else if($valueBase >= && $valueBase 8){
  121.                             //It's oz classic
  122.                             $valueBase round((floatval($valueBase)), 0);
  123.                             $typeUnit "oz";
  124.                         }
  125.                         else if($valueBase >= && $valueBase 32){
  126.                             //It's cup
  127.                             $valueBase round((floatval($valueBase) / 8), 0);
  128.                             $typeUnit "cup";
  129.                         }
  130.                         else if($valueBase >= 32 && $valueBase 128){
  131.                             
  132.                             //It's quart
  133.                             $valueBase round((floatval($valueBase) / 32), 0);
  134.                             $typeUnit "quart";
  135.                         }
  136.                         else if($valueBase >= 128){
  137.                             
  138.                             //It's gallon
  139.                             $valueBase round((floatval($valueBase) / 128), 0);
  140.                             $typeUnit "gallon";
  141.                         }
  142.                         $valueReturn $valueBase;
  143.                     }
  144.                     else{
  145.                         if($value == "0.25"){
  146.                             $value "¼";
  147.                         }
  148.                         if($value == "0.50"){
  149.                             $value "½";
  150.                         }
  151.                         if($value == "0.75"){
  152.                             $value "¾";
  153.                         }
  154.                         if($value == "1.25"){
  155.                             $value "1 ¼";
  156.                         }
  157.                         if($value == "1.50"){
  158.                             $value "1 ½";
  159.                         }
  160.                         if($value == "1.75"){
  161.                             $value "1 ¾";
  162.                         }
  163.                         if($value == "2.25"){
  164.                             $value "2 ¼";
  165.                         }
  166.                         if($value == "2.50"){
  167.                             $value "2 ½";
  168.                         }
  169.                         if($value == "2.75"){
  170.                             $value "2 ¾";
  171.                         }
  172.                         $valueReturn $value;
  173.                     }
  174.                 }
  175.                 else if($unit == "cl"){
  176.                     if($unitOrigin == "oz"){
  177.                         $valueReturn round((floatval($value) * 2.96), 0);
  178.                     }
  179.                     else if($unitOrigin == "cup"){
  180.                         $valueReturn round((floatval($value) * 23.7), 0);
  181.                     }
  182.                     else if($unitOrigin == "quart"){
  183.                         $valueReturn round((floatval($value) * 94.6), 0);
  184.                     }
  185.                     else if($unitOrigin == "gallon"){
  186.                         $valueReturn round((floatval($value) * 378.5), 0);
  187.                     }
  188.                     else{
  189.                         $valueReturn $value;
  190.                     }
  191.                 }
  192.                 else{
  193.                    $valueReturn $value;
  194.                 }
  195.             }
  196.             else{
  197.                 $valueReturn $value;
  198.             }
  199.         }
  200.         if($all){
  201.             return array(
  202.                 "valueReturn" => $valueReturn,
  203.                 "typeUnit" => $typeUnit
  204.             );
  205.         }
  206.         else{
  207.             return $valueReturn;
  208.         }
  209.     }
  210.     function MRound($num,$parts) {
  211.         $res $num $parts;
  212.         $res round($res);
  213.         return $res /$parts;
  214.     }
  215.     public function getMembershipByCustomer($customer){
  216.         $membership $this->em
  217.                 ->getRepository('App:Membership')
  218.                 ->findOneBy(['user' => $customer], array("id" => "DESC"));
  219.         if(!$membership){
  220.             $customer $this->em
  221.                 ->getRepository('App:User')
  222.                 ->findOneBy(['id' => $customer]);
  223.             $membership = new Membership;
  224.             $membership->setUser($customer);
  225.             $membership->setStatus("DRAFT");
  226.             $this->em->persist($membership);
  227.             $this->em->flush();
  228.             $membershipLast $membership;
  229.         }
  230.         else{
  231.             $membershipLast $membership;
  232.         }
  233.         if(!$membershipLast->getDic()){
  234.             $dic = new Dic;
  235.             $membershipLast->setDic($dic);
  236.             $this->em->persist($dic);
  237.             $this->em->persist($membershipLast);
  238.             $this->em->flush();
  239.         }
  240.         if(!$membershipLast->getBa()){
  241.             $ba = new Ba;
  242.             $membershipLast->setBa($ba);
  243.             $this->em->persist($ba);
  244.             $this->em->persist($membershipLast);
  245.             $this->em->flush();
  246.         }
  247.         return $membershipLast;
  248.     }
  249.     public function sendEmailEndSignin($user){
  250.         $mailNoReply $this->container->getParameter('mail_no_reply');
  251.         $senderName $this->container->getParameter('sender_name');
  252.         $url $this->container->get('router')->generate('signin_end', array('token' => $user->getTokenReset()), UrlGeneratorInterface::ABSOLUTE_URL);
  253.         $bodyEmail $this->twig->render(
  254.             'email/addUserWithToken.html.twig',
  255.             array('user' => $user'confirmationToken' => $url)
  256.         );
  257.         $message = (new \Swift_Message("Fin d'inscription"))
  258.             ->setSubject("Fin d'inscription")
  259.             ->setFrom(array($mailNoReply => $senderName))
  260.             ->setTo(array($user->getEmail() => $senderName))
  261.             ->setBody($bodyEmail,'text/html');
  262.         $this->mailer->send($message);
  263.     }
  264.     public function sendEmailEndSigninCustomer($user){
  265.         $mailNoReply $this->container->getParameter('mail_no_reply');
  266.         $senderName $this->container->getParameter('sender_name');
  267.         $url $this->container->get('router')->generate('signin_end', array('token' => $user->getTokenReset()), UrlGeneratorInterface::ABSOLUTE_URL);
  268.         
  269.         $bodyEmail $this->twig->render(
  270.             'email/addCustomerWithToken.html.twig',
  271.             array('user' => $user'confirmationToken' => $url)
  272.         );
  273.         $message = (new \Swift_Message("Finalisez votre adhésion"))
  274.             ->setSubject("Finalisez votre adhésion")
  275.             ->setFrom(array($mailNoReply => $senderName))
  276.             ->setTo(array($user->getEmail() => $senderName))
  277.             ->setBody($bodyEmail,'text/html');
  278.         $this->mailer->send($message);
  279.     }
  280.     public function getStatusMembership($customer){
  281.         $status "no-status";
  282.         $membershipCurrent $this->getMembershipByCustomer($customer);
  283.         if($membershipCurrent->getStatus() == "DRAFT"){
  284.             $status "Brouillon";
  285.         }
  286.         else if($membershipCurrent->getStatus() == "VALIDBYSELLER"){
  287.             $status "En attente de validation";
  288.         }
  289.         else if($membershipCurrent->getStatus() == "VALIDBYCUSTOMER"){
  290.             $status "En attente de signature";
  291.         }
  292.         else if($membershipCurrent->getStatus() == "SIGNBYCUSTOMER"){
  293.             $status "En attente de paiement";
  294.         }
  295.         else if($membershipCurrent->getStatus() == "PAYBYCUSTOMER"){
  296.             $status "Validé";
  297.         }
  298.         return $status;
  299.     }
  300.     function formatSizeUnits($bytes)
  301.     {
  302.         if ($bytes >= 1073741824)
  303.         {
  304.             $bytes number_format($bytes 10737418242) . ' GB';
  305.         }
  306.         elseif ($bytes >= 1048576)
  307.         {
  308.             $bytes number_format($bytes 10485762) . ' MB';
  309.         }
  310.         elseif ($bytes >= 1024)
  311.         {
  312.             $bytes number_format($bytes 10242) . ' KB';
  313.         }
  314.         elseif ($bytes 1)
  315.         {
  316.             $bytes $bytes ' bytes';
  317.         }
  318.         elseif ($bytes == 1)
  319.         {
  320.             $bytes $bytes ' byte';
  321.         }
  322.         else
  323.         {
  324.             $bytes '0 bytes';
  325.         }
  326.         return $bytes;
  327.     }
  328. }