Utils.php 15KB

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