|
- <?php
-
- namespace Lc\ShopBundle\Services;
-
- use Cocur\Slugify\Slugify;
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\ShopBundle\Context\MerchantUtilsInterface;
- use Lc\ShopBundle\Context\PageInterface;
- use Lc\ShopBundle\Context\PointSaleInterface;
- use Lc\ShopBundle\Context\TaxRateInterface;
- use Lc\ShopBundle\Context\UnitInterface;
- use Lc\ShopBundle\Context\UserInterface;
- use Lc\ShopBundle\Context\UserPointSaleInterface;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Symfony\Component\HttpFoundation\ParameterBag;
-
- class Utils
- {
- protected $em ;
- protected $parameterBag ;
- protected $merchantUtils ;
-
- public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag)
- {
- $this->em = $em ;
- $this->parameterBag = $parameterBag ;
- }
-
-
- public function getElementByDevAlias($devAlias, $class = PageInterface::class)
- {
- $class = $this->em->getClassMetadata($class)->getName();
- return $this->em->getRepository($class)->findOneByDevAlias($devAlias) ;
- }
-
-
- public function isServerLocalhost()
- {
- return in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1']) ;
- }
-
- public function getCookieDomain()
- {
- return ($this->isServerLocalhost()) ? null : $this->parameterBag->get('app.cookie_domain_distant') ;
- }
-
- function limitText($text, $limit) {
- if (str_word_count($text, 0) > $limit) {
- $words = str_word_count($text, 2);
- $pos = array_keys($words);
- $text = substr($text, 0, $pos[$limit]) . '...';
- }
- return $text;
- }
-
- public function isBot()
- {
- if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'])) {
- return TRUE;
- } else {
- return FALSE;
- }
- }
-
- public function slugify($string)
- {
- $slugify = new Slugify();
- return $slugify->slugify($string) ;
- }
-
- public function getUnitsList()
- {
- $unitsList =array();
- $units = $this->em->getRepository(UnitInterface::class)->findAll();
- foreach ($units as $unit){
- $unitsList[$unit->getId()]['unit'] = $unit->getUnit();
- $unitsList[$unit->getId()]['wordingUnit'] = $unit->getWordingUnit();
- $unitsList[$unit->getId()]['wording'] = $unit->getWording();
- $unitsList[$unit->getId()]['wordingShort'] = $unit->getWordingShort();
- $unitsList[$unit->getId()]['coefficient'] = $unit->getCoefficient();
- $unitsList[$unit->getId()]['unitReference'] = $unit->getUnitReference()->getId();
- }
-
- return $unitsList;
- }
-
-
- public function isUserLinkedToPointSale(UserInterface $user, PointSaleInterface $pointSale)
- {
- foreach($user->getUserPointSales() as $userPointSale) {
- if($userPointSale->getPointSale()->getId() == $pointSale->getId()) {
- return true ;
- }
- }
- return false ;
- }
-
- public function linkUserToPointSale(UserInterface $user, PointSaleInterface $pointSale)
- {
- if(!$this->isUserLinkedToPointSale($user, $pointSale)) {
- $userPointSaleClass = $this->em->getClassMetadata(UserPointSaleInterface::class)->getName();
- $userPointSale = new $userPointSaleClass ;
-
- $userPointSale->setUser($user) ;
- $userPointSale->setPointSale($pointSale) ;
-
- $this->em->persist($userPointSale);
- $this->em->flush() ;
- }
- }
-
- function callCitiesApi($method, $url, $data = false)
- {
- $url = 'https://geo.api.gouv.fr/'.$url ;
- $curl = curl_init();
-
- switch ($method)
- {
- case "POST":
- curl_setopt($curl, CURLOPT_POST, 1);
-
- if ($data)
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- break;
- case "PUT":
- curl_setopt($curl, CURLOPT_PUT, 1);
- break;
- default:
- if ($data)
- $url = sprintf("%s?%s", $url, http_build_query($data));
- }
-
- // Optional Authentication:
- curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($curl, CURLOPT_USERPWD, "username:password");
-
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
-
- $result = curl_exec($curl);
-
- curl_close($curl);
-
- return $result;
- }
-
- public function getZipByCity($city)
- {
- $zip = null ;
- $returnCitiesSearchZip = json_decode($this->callCitiesApi('get', 'communes', ['nom' => $city, 'fields' => 'nom,codesPostaux'])) ;
-
- if($returnCitiesSearchZip) {
- foreach($returnCitiesSearchZip as $citySearchZip) {
- if(strtolower(trim($city)) == strtolower(trim($citySearchZip->nom))) {
- $zip = $citySearchZip->codesPostaux[0] ;
- }
- }
- }
-
- return $zip ;
- }
- public function date($format, $timestamp)
- {
- setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8', 'fr_FR.UTF-8', 'fr.UTF-8');
- return strftime($format, $timestamp) ;
- }
-
- public function getNextDay($day)
- {
- return new \DateTime('next '.$day) ;
- }
-
- public function getNextDayByNumber($number)
- {
- return $this->getNextDay($this->getDayByNumber($number, 'en')) ;
- }
-
- public function getDayByNumber($number, $lang = 'fr')
- {
- if($lang == 'fr') {
- $daysArray = [
- 1 => 'Lundi',
- 2 => 'Mardi',
- 3 => 'Mercredi',
- 4 => 'Jeudi',
- 5 => 'Vendredi',
- 6 => 'Samedi',
- 7 => 'Dimanche'
- ] ;
- }
- else {
- $daysArray = [
- 1 => 'Monday',
- 2 => 'Tuesday',
- 3 => 'Wednesday',
- 4 => 'Thursday',
- 5 => 'Friday',
- 6 => 'Saturday',
- 7 => 'Sunday',
- ] ;
- }
-
- if(isset($daysArray[$number])) {
- return $daysArray[$number] ;
- }
-
- return '' ;
- }
-
- }
|