303 Zeilen
13KB

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