src/Services/Utils.php line 48

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 $container, EntityManagerInterface $entityManager, Environment $twig, FileUploader $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, $value, true);
  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. return $tabQuantity['valueReturn']." ".$unitTemp;
  75. }
  76. public function calculUnit($unitOrigin, $unit, $value, $all = false){
  77. $valueReturn = "";
  78. $typeUnit = null;
  79. if($value == null || $value == ""){
  80. $valueReturn = "";
  81. }
  82. else{
  83. if($unitOrigin == "cl" || $unitOrigin == "oz" || $unitOrigin == "cup" || $unitOrigin == "quart" || $unitOrigin == "gallon"){
  84. if($unit == "oz"){
  85. if($unitOrigin == "cl"){
  86. $valueBase = round((floatval($value) / 2.96), 2);
  87. if($valueBase < 2){
  88. if($valueBase > 1){
  89. $valueNew = $this->MRound($valueBase - 1, 4);
  90. }
  91. else{
  92. $valueNew = $this->MRound($valueBase, 4);
  93. }
  94. if($valueNew < 0.25){
  95. $valueNew = "¼";
  96. }
  97. else if($valueNew == "0.25"){
  98. $valueNew = "¼";
  99. }
  100. else if($valueNew == "0.50"){
  101. $valueNew = "½";
  102. }
  103. else if($valueNew == "0.75"){
  104. $valueNew = "¾";
  105. }
  106. else{
  107. $valueNew = "1";
  108. }
  109. if($valueBase > 1){
  110. if($valueNew == 1){
  111. $valueNew = 2;
  112. }
  113. else{
  114. $valueNew = "1 ".$valueNew;
  115. }
  116. }
  117. $valueBase = $valueNew;
  118. }
  119. else if($valueBase >= 2 && $valueBase < 8){
  120. //It's oz classic
  121. $valueBase = round((floatval($valueBase)), 0);
  122. $typeUnit = "oz";
  123. }
  124. else if($valueBase >= 8 && $valueBase < 32){
  125. //It's cup
  126. $valueBase = round((floatval($valueBase) / 8), 0);
  127. $typeUnit = "cup";
  128. }
  129. else if($valueBase >= 32 && $valueBase < 128){
  130. //It's quart
  131. $valueBase = round((floatval($valueBase) / 32), 0);
  132. $typeUnit = "quart";
  133. }
  134. else if($valueBase >= 128){
  135. //It's gallon
  136. $valueBase = round((floatval($valueBase) / 128), 0);
  137. $typeUnit = "gallon";
  138. }
  139. $valueReturn = $valueBase;
  140. }
  141. else{
  142. if($value == "0.25"){
  143. $value = "¼";
  144. }
  145. if($value == "0.50"){
  146. $value = "½";
  147. }
  148. if($value == "0.75"){
  149. $value = "¾";
  150. }
  151. if($value == "1.25"){
  152. $value = "1 ¼";
  153. }
  154. if($value == "1.50"){
  155. $value = "1 ½";
  156. }
  157. if($value == "1.75"){
  158. $value = "1 ¾";
  159. }
  160. if($value == "2.25"){
  161. $value = "2 ¼";
  162. }
  163. if($value == "2.50"){
  164. $value = "2 ½";
  165. }
  166. if($value == "2.75"){
  167. $value = "2 ¾";
  168. }
  169. $valueReturn = $value;
  170. }
  171. }
  172. else if($unit == "cl"){
  173. if($unitOrigin == "oz"){
  174. $valueReturn = round((floatval($value) * 2.96), 0);
  175. }
  176. else if($unitOrigin == "cup"){
  177. $valueReturn = round((floatval($value) * 23.7), 0);
  178. }
  179. else if($unitOrigin == "quart"){
  180. $valueReturn = round((floatval($value) * 94.6), 0);
  181. }
  182. else if($unitOrigin == "gallon"){
  183. $valueReturn = round((floatval($value) * 378.5), 0);
  184. }
  185. else{
  186. $valueReturn = $value;
  187. }
  188. }
  189. else{
  190. $valueReturn = $value;
  191. }
  192. }
  193. else{
  194. $valueReturn = $value;
  195. }
  196. }
  197. if($all){
  198. return array(
  199. "valueReturn" => $valueReturn,
  200. "typeUnit" => $typeUnit
  201. );
  202. }
  203. else{
  204. return $valueReturn;
  205. }
  206. }
  207. function MRound($num,$parts) {
  208. $res = $num * $parts;
  209. $res = round($res);
  210. return $res /$parts;
  211. }
  212. public function getMembershipByCustomer($customer){
  213. $membership = $this->em
  214. ->getRepository('App:Membership')
  215. ->findOneBy(['user' => $customer], array("id" => "DESC"));
  216. if(!$membership){
  217. $customer = $this->em
  218. ->getRepository('App:User')
  219. ->findOneBy(['id' => $customer]);
  220. $membership = new Membership;
  221. $membership->setUser($customer);
  222. $membership->setStatus("DRAFT");
  223. $this->em->persist($membership);
  224. $this->em->flush();
  225. $membershipLast = $membership;
  226. }
  227. else{
  228. $membershipLast = $membership;
  229. }
  230. if(!$membershipLast->getDic()){
  231. $dic = new Dic;
  232. $membershipLast->setDic($dic);
  233. $this->em->persist($dic);
  234. $this->em->persist($membershipLast);
  235. $this->em->flush();
  236. }
  237. if(!$membershipLast->getBa()){
  238. $ba = new Ba;
  239. $membershipLast->setBa($ba);
  240. $this->em->persist($ba);
  241. $this->em->persist($membershipLast);
  242. $this->em->flush();
  243. }
  244. return $membershipLast;
  245. }
  246. public function sendEmailEndSignin($user){
  247. $mailNoReply = $this->container->getParameter('mail_no_reply');
  248. $senderName = $this->container->getParameter('sender_name');
  249. $url = $this->container->get('router')->generate('signin_end', array('token' => $user->getTokenReset()), UrlGeneratorInterface::ABSOLUTE_URL);
  250. $bodyEmail = $this->twig->render(
  251. 'email/addUserWithToken.html.twig',
  252. array('user' => $user, 'confirmationToken' => $url)
  253. );
  254. $message = (new \Swift_Message("Fin d'inscription"))
  255. ->setSubject("Fin d'inscription")
  256. ->setFrom(array($mailNoReply => $senderName))
  257. ->setTo(array($user->getEmail() => $senderName))
  258. ->setBody($bodyEmail,'text/html');
  259. $this->mailer->send($message);
  260. }
  261. public function sendEmailEndSigninCustomer($user){
  262. $mailNoReply = $this->container->getParameter('mail_no_reply');
  263. $senderName = $this->container->getParameter('sender_name');
  264. $url = $this->container->get('router')->generate('signin_end', array('token' => $user->getTokenReset()), UrlGeneratorInterface::ABSOLUTE_URL);
  265. $bodyEmail = $this->twig->render(
  266. 'email/addCustomerWithToken.html.twig',
  267. array('user' => $user, 'confirmationToken' => $url)
  268. );
  269. $message = (new \Swift_Message("Finalisez votre adhésion"))
  270. ->setSubject("Finalisez votre adhésion")
  271. ->setFrom(array($mailNoReply => $senderName))
  272. ->setTo(array($user->getEmail() => $senderName))
  273. ->setBody($bodyEmail,'text/html');
  274. $this->mailer->send($message);
  275. }
  276. public function getStatusMembership($customer){
  277. $status = "no-status";
  278. $membershipCurrent = $this->getMembershipByCustomer($customer);
  279. if($membershipCurrent->getStatus() == "DRAFT"){
  280. $status = "Brouillon";
  281. }
  282. else if($membershipCurrent->getStatus() == "VALIDBYSELLER"){
  283. $status = "En attente de validation";
  284. }
  285. else if($membershipCurrent->getStatus() == "VALIDBYCUSTOMER"){
  286. $status = "En attente de signature";
  287. }
  288. else if($membershipCurrent->getStatus() == "SIGNBYCUSTOMER"){
  289. $status = "En attente de paiement";
  290. }
  291. else if($membershipCurrent->getStatus() == "PAYBYCUSTOMER"){
  292. $status = "Validé";
  293. }
  294. return $status;
  295. }
  296. function formatSizeUnits($bytes)
  297. {
  298. if ($bytes >= 1073741824)
  299. {
  300. $bytes = number_format($bytes / 1073741824, 2) . ' GB';
  301. }
  302. elseif ($bytes >= 1048576)
  303. {
  304. $bytes = number_format($bytes / 1048576, 2) . ' MB';
  305. }
  306. elseif ($bytes >= 1024)
  307. {
  308. $bytes = number_format($bytes / 1024, 2) . ' KB';
  309. }
  310. elseif ($bytes > 1)
  311. {
  312. $bytes = $bytes . ' bytes';
  313. }
  314. elseif ($bytes == 1)
  315. {
  316. $bytes = $bytes . ' byte';
  317. }
  318. else
  319. {
  320. $bytes = '0 bytes';
  321. }
  322. return $bytes;
  323. }
  324. }