pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. class Utils
  17. {
  18. protected $em ;
  19. protected $parameterBag ;
  20. protected $merchantUtils ;
  21. protected $session;
  22. protected $translator;
  23. public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag, SessionInterface $session, TranslatorInterface $translator)
  24. {
  25. $this->em = $em ;
  26. $this->parameterBag = $parameterBag ;
  27. $this->session = $session;
  28. $this->translator = $translator;
  29. }
  30. public function getElementByDevAlias($devAlias, $class = PageInterface::class)
  31. {
  32. $class = $this->em->getClassMetadata($class)->getName();
  33. return $this->em->getRepository($class)->findOneByDevAlias($devAlias) ;
  34. }
  35. public function isServerLocalhost()
  36. {
  37. return in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1']) ;
  38. }
  39. public function getCookieDomain()
  40. {
  41. return ($this->isServerLocalhost()) ? null : $this->parameterBag->get('app.cookie_domain_distant') ;
  42. }
  43. public function limitText($text, $limit) {
  44. $text = strip_tags($text) ;
  45. if (str_word_count($text, 0) > $limit) {
  46. $words = str_word_count($text, 2);
  47. $pos = array_keys($words);
  48. $text = substr($text, 0, $pos[$limit]) . '...';
  49. }
  50. return $text;
  51. }
  52. function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) {
  53. if ($considerHtml) {
  54. // if the plain text is shorter than the maximum length, return the whole text
  55. if (strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
  56. return $text;
  57. }
  58. // splits all html-tags to scanable lines
  59. preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER);
  60. $total_length = strlen($ending);
  61. $open_tags = array();
  62. $truncate = '';
  63. foreach ($lines as $line_matchings) {
  64. // if there is any html-tag in this line, handle it and add it (uncounted) to the output
  65. if (!empty($line_matchings[1])) {
  66. // if it's an "empty element" with or without xhtml-conform closing slash
  67. if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) {
  68. // do nothing
  69. // if tag is a closing tag
  70. } else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) {
  71. // delete tag from $open_tags list
  72. $pos = array_search($tag_matchings[1], $open_tags);
  73. if ($pos !== false) {
  74. unset($open_tags[$pos]);
  75. }
  76. // if tag is an opening tag
  77. } else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) {
  78. // add tag to the beginning of $open_tags list
  79. array_unshift($open_tags, strtolower($tag_matchings[1]));
  80. }
  81. // add html-tag to $truncate'd text
  82. $truncate .= $line_matchings[1];
  83. }
  84. // calculate the length of the plain text part of the line; handle entities as one character
  85. $content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2]));
  86. if ($total_length+$content_length> $length) {
  87. // the number of characters which are left
  88. $left = $length - $total_length;
  89. $entities_length = 0;
  90. // search for html entities
  91. if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) {
  92. // calculate the real length of all entities in the legal range
  93. foreach ($entities[0] as $entity) {
  94. if ($entity[1]+1-$entities_length <= $left) {
  95. $left--;
  96. $entities_length += strlen($entity[0]);
  97. } else {
  98. // no more characters left
  99. break;
  100. }
  101. }
  102. }
  103. $truncate .= substr($line_matchings[2], 0, $left+$entities_length);
  104. // maximum lenght is reached, so get off the loop
  105. break;
  106. } else {
  107. $truncate .= $line_matchings[2];
  108. $total_length += $content_length;
  109. }
  110. // if the maximum length is reached, get off the loop
  111. if($total_length>= $length) {
  112. break;
  113. }
  114. }
  115. } else {
  116. if (strlen($text) <= $length) {
  117. return $text;
  118. } else {
  119. $truncate = substr($text, 0, $length - strlen($ending));
  120. }
  121. }
  122. // if the words shouldn't be cut in the middle...
  123. if (!$exact) {
  124. // ...search the last occurance of a space...
  125. $spacepos = strrpos($truncate, ' ');
  126. if (isset($spacepos)) {
  127. // ...and cut the text in this position
  128. $truncate = substr($truncate, 0, $spacepos);
  129. }
  130. }
  131. // add the defined ending to the text
  132. $truncate .= $ending;
  133. if($considerHtml) {
  134. // close all unclosed html-tags
  135. foreach ($open_tags as $tag) {
  136. $truncate .= '</' . $tag . '>';
  137. }
  138. }
  139. return $truncate;
  140. }
  141. public function isBot()
  142. {
  143. if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'])) {
  144. return TRUE;
  145. } else {
  146. return FALSE;
  147. }
  148. }
  149. public function slugify($string)
  150. {
  151. $slugify = new Slugify();
  152. return $slugify->slugify($string) ;
  153. }
  154. public function getUnitsList()
  155. {
  156. $unitsList =array();
  157. $units = $this->em->getRepository(UnitInterface::class)->findAll();
  158. foreach ($units as $unit){
  159. $unitsList[$unit->getId()]['unit'] = $unit->getUnit();
  160. $unitsList[$unit->getId()]['wordingUnit'] = $unit->getWordingUnit();
  161. $unitsList[$unit->getId()]['wording'] = $unit->getWording();
  162. $unitsList[$unit->getId()]['wordingShort'] = $unit->getWordingShort();
  163. $unitsList[$unit->getId()]['coefficient'] = $unit->getCoefficient();
  164. $unitsList[$unit->getId()]['unitReference'] = $unit->getUnitReference()->getId();
  165. }
  166. return $unitsList;
  167. }
  168. public function isUserLinkedToPointSale(UserInterface $user, PointSaleInterface $pointSale)
  169. {
  170. foreach($user->getUserPointSales() as $userPointSale) {
  171. if($userPointSale->getPointSale()->getId() == $pointSale->getId()) {
  172. return true ;
  173. }
  174. }
  175. return false ;
  176. }
  177. public function linkUserToPointSale(UserInterface $user, PointSaleInterface $pointSale)
  178. {
  179. if(!$this->isUserLinkedToPointSale($user, $pointSale)) {
  180. $userPointSaleClass = $this->em->getClassMetadata(UserPointSaleInterface::class)->getName();
  181. $userPointSale = new $userPointSaleClass ;
  182. $userPointSale->setUser($user) ;
  183. $userPointSale->setPointSale($pointSale) ;
  184. $this->em->persist($userPointSale);
  185. $this->em->flush() ;
  186. }
  187. }
  188. function callCitiesApi($method, $url, $data = false)
  189. {
  190. $url = 'https://geo.api.gouv.fr/'.$url ;
  191. $curl = curl_init();
  192. switch ($method)
  193. {
  194. case "POST":
  195. curl_setopt($curl, CURLOPT_POST, 1);
  196. if ($data)
  197. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  198. break;
  199. case "PUT":
  200. curl_setopt($curl, CURLOPT_PUT, 1);
  201. break;
  202. default:
  203. if ($data)
  204. $url = sprintf("%s?%s", $url, http_build_query($data));
  205. }
  206. // Optional Authentication:
  207. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  208. curl_setopt($curl, CURLOPT_USERPWD, "username:password");
  209. curl_setopt($curl, CURLOPT_URL, $url);
  210. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  211. $result = curl_exec($curl);
  212. curl_close($curl);
  213. return $result;
  214. }
  215. public function getZipByCity($city)
  216. {
  217. $zip = null ;
  218. $returnCitiesSearchZip = json_decode($this->callCitiesApi('get', 'communes', ['nom' => $city, 'fields' => 'nom,codesPostaux'])) ;
  219. if($returnCitiesSearchZip) {
  220. foreach($returnCitiesSearchZip as $citySearchZip) {
  221. if(strtolower(trim($city)) == strtolower(trim($citySearchZip->nom))) {
  222. $zip = $citySearchZip->codesPostaux[0] ;
  223. }
  224. }
  225. }
  226. return $zip ;
  227. }
  228. public function date($format, $timestamp)
  229. {
  230. setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8', 'fr_FR.UTF-8', 'fr.UTF-8');
  231. return strftime($format, $timestamp) ;
  232. }
  233. public function getNextDay($day)
  234. {
  235. return new \DateTime('next '.$day) ;
  236. }
  237. public function getNextDayByNumber($number)
  238. {
  239. return $this->getNextDay($this->getDayByNumber($number, 'en')) ;
  240. }
  241. public function getDayByNumber($number, $lang = 'fr')
  242. {
  243. if($lang == 'fr') {
  244. $daysArray = [
  245. 1 => 'Lundi',
  246. 2 => 'Mardi',
  247. 3 => 'Mercredi',
  248. 4 => 'Jeudi',
  249. 5 => 'Vendredi',
  250. 6 => 'Samedi',
  251. 7 => 'Dimanche'
  252. ] ;
  253. }
  254. else {
  255. $daysArray = [
  256. 1 => 'Monday',
  257. 2 => 'Tuesday',
  258. 3 => 'Wednesday',
  259. 4 => 'Thursday',
  260. 5 => 'Friday',
  261. 6 => 'Saturday',
  262. 7 => 'Sunday',
  263. ] ;
  264. }
  265. if(isset($daysArray[$number])) {
  266. return $daysArray[$number] ;
  267. }
  268. return '' ;
  269. }
  270. public function addFlash($success, $message, $extraMessages = array(), $params=array(), $domain='lcshop'){
  271. $message = $this->translator->trans($message, $params,$domain);
  272. if (count($extraMessages)) {
  273. $message .= '<ul>';
  274. foreach ($extraMessages as $extraMessage) {
  275. $message .= '<li> <i>' . $this->translator->trans($extraMessage, array(), $domain) . '</i></li>';
  276. }
  277. $message .= '</ul>';
  278. }
  279. $this->session->getFlashBag()->add($success, $message);
  280. }
  281. public function getFlashMessages(){
  282. return $this->session->getFlashBag()->all();
  283. }
  284. }