You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 line
7.5KB

  1. <?php
  2. namespace Lc\ShopBundle\Services;
  3. use Cocur\Slugify\Slugify;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  6. use Lc\ShopBundle\Context\PageInterface;
  7. use Lc\ShopBundle\Context\PointSaleInterface;
  8. use Lc\ShopBundle\Context\TaxRateInterface;
  9. use Lc\ShopBundle\Context\UnitInterface;
  10. use Lc\ShopBundle\Context\UserInterface;
  11. use Lc\ShopBundle\Context\UserPointSaleInterface;
  12. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  13. use Symfony\Component\HttpFoundation\ParameterBag;
  14. class Utils
  15. {
  16. protected $em ;
  17. protected $parameterBag ;
  18. protected $merchantUtils ;
  19. public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag)
  20. {
  21. $this->em = $em ;
  22. $this->parameterBag = $parameterBag ;
  23. }
  24. public function getElementByDevAlias($devAlias, $class = PageInterface::class)
  25. {
  26. $class = $this->em->getClassMetadata($class)->getName();
  27. return $this->em->getRepository($class)->findOneByDevAlias($devAlias) ;
  28. }
  29. public function isServerLocalhost()
  30. {
  31. return in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1']) ;
  32. }
  33. public function getCookieDomain()
  34. {
  35. return ($this->isServerLocalhost()) ? null : $this->parameterBag->get('app.cookie_domain_distant') ;
  36. }
  37. function limitText($text, $limit) {
  38. if (str_word_count($text, 0) > $limit) {
  39. $words = str_word_count($text, 2);
  40. $pos = array_keys($words);
  41. $text = substr($text, 0, $pos[$limit]) . '...';
  42. }
  43. return $text;
  44. }
  45. public function isBot()
  46. {
  47. if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'])) {
  48. return TRUE;
  49. } else {
  50. return FALSE;
  51. }
  52. }
  53. public function slugify($string)
  54. {
  55. $slugify = new Slugify();
  56. return $slugify->slugify($string) ;
  57. }
  58. public function getUnitsList()
  59. {
  60. $unitsList =array();
  61. $units = $this->em->getRepository(UnitInterface::class)->findAll();
  62. foreach ($units as $unit){
  63. $unitsList[$unit->getId()]['unit'] = $unit->getUnit();
  64. $unitsList[$unit->getId()]['wordingUnit'] = $unit->getWordingUnit();
  65. $unitsList[$unit->getId()]['wording'] = $unit->getWording();
  66. $unitsList[$unit->getId()]['wordingShort'] = $unit->getWordingShort();
  67. $unitsList[$unit->getId()]['coefficient'] = $unit->getCoefficient();
  68. $unitsList[$unit->getId()]['unitReference'] = $unit->getUnitReference()->getId();
  69. }
  70. return $unitsList;
  71. }
  72. public function isUserLinkedToPointSale(UserInterface $user, PointSaleInterface $pointSale)
  73. {
  74. foreach($user->getUserPointSales() as $userPointSale) {
  75. if($userPointSale->getPointSale()->getId() == $pointSale->getId()) {
  76. return true ;
  77. }
  78. }
  79. return false ;
  80. }
  81. public function linkUserToPointSale(UserInterface $user, PointSaleInterface $pointSale)
  82. {
  83. if(!$this->isUserLinkedToPointSale($user, $pointSale)) {
  84. $userPointSaleClass = $this->em->getClassMetadata(UserPointSaleInterface::class)->getName();
  85. $userPointSale = new $userPointSaleClass ;
  86. $userPointSale->setUser($user) ;
  87. $userPointSale->setPointSale($pointSale) ;
  88. $this->em->persist($userPointSale);
  89. $this->em->flush() ;
  90. }
  91. }
  92. function callCitiesApi($method, $url, $data = false)
  93. {
  94. $url = 'https://geo.api.gouv.fr/'.$url ;
  95. $curl = curl_init();
  96. switch ($method)
  97. {
  98. case "POST":
  99. curl_setopt($curl, CURLOPT_POST, 1);
  100. if ($data)
  101. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  102. break;
  103. case "PUT":
  104. curl_setopt($curl, CURLOPT_PUT, 1);
  105. break;
  106. default:
  107. if ($data)
  108. $url = sprintf("%s?%s", $url, http_build_query($data));
  109. }
  110. // Optional Authentication:
  111. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  112. curl_setopt($curl, CURLOPT_USERPWD, "username:password");
  113. curl_setopt($curl, CURLOPT_URL, $url);
  114. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  115. $result = curl_exec($curl);
  116. curl_close($curl);
  117. return $result;
  118. }
  119. public function getZipByCity($city)
  120. {
  121. $zip = null ;
  122. $returnCitiesSearchZip = json_decode($this->callCitiesApi('get', 'communes', ['nom' => $city, 'fields' => 'nom,codesPostaux'])) ;
  123. if($returnCitiesSearchZip) {
  124. foreach($returnCitiesSearchZip as $citySearchZip) {
  125. if(strtolower(trim($city)) == strtolower(trim($citySearchZip->nom))) {
  126. $zip = $citySearchZip->codesPostaux[0] ;
  127. }
  128. }
  129. }
  130. return $zip ;
  131. }
  132. public function date($format, $timestamp)
  133. {
  134. setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8', 'fr_FR.UTF-8', 'fr.UTF-8');
  135. return strftime($format, $timestamp) ;
  136. }
  137. public function getNextDay($day)
  138. {
  139. return new \DateTime('next '.$day) ;
  140. }
  141. public function getNextDayByNumber($number)
  142. {
  143. return $this->getNextDay($this->getDayByNumber($number, 'en')) ;
  144. }
  145. public function getDayByNumber($number, $lang = 'fr')
  146. {
  147. if($lang == 'fr') {
  148. $daysArray = [
  149. 1 => 'Lundi',
  150. 2 => 'Mardi',
  151. 3 => 'Mercredi',
  152. 4 => 'Jeudi',
  153. 5 => 'Vendredi',
  154. 6 => 'Samedi',
  155. 7 => 'Dimanche'
  156. ] ;
  157. }
  158. else {
  159. $daysArray = [
  160. 1 => 'Monday',
  161. 2 => 'Tuesday',
  162. 3 => 'Wednesday',
  163. 4 => 'Thursday',
  164. 5 => 'Friday',
  165. 6 => 'Saturday',
  166. 7 => 'Sunday',
  167. ] ;
  168. }
  169. if(isset($daysArray[$number])) {
  170. return $daysArray[$number] ;
  171. }
  172. return '' ;
  173. }
  174. }