src/Controller/InformationController.php line 598

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. //Load core
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use \DateTime;
  6. //Load json rest
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\HttpFoundation\Session\Session;
  12. use FOS\RestBundle\Controller\Annotations as Rest; // alias pour toutes les annotations
  13. use App\Services\Utils;
  14. use App\Services\Tools;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. //Load entity
  18. use App\Entity\PositionGps;
  19. use App\Entity\User;
  20. use App\Entity\Hobit;
  21. use App\Entity\HobitDate;
  22. class InformationController extends AbstractController
  23. {
  24. protected $em;
  25. protected $utils;
  26. protected $tools;
  27. public function __construct(EntityManagerInterface $entityManager, Utils $utils, TranslatorInterface $translator, Tools $tools) {
  28. $this->em = $entityManager;
  29. $this->utils = $utils;
  30. $this->translator = $translator;
  31. $this->tools = $tools;
  32. }
  33. /**
  34. * @Rest\View(statusCode=200)
  35. * @Rest\Post("/api/information/position-gps")
  36. */
  37. public function addPositionGpsAction(Request $request)
  38. {
  39. //$positions = $request->get("positions");
  40. $positionsString = $request->getContent();
  41. $positions = [];
  42. $positions[] = json_decode($positionsString);
  43. //TEST VARDUMP FILE
  44. $coucou = $positionsString;
  45. $monfichier = fopen('coucou.txt', 'w+');
  46. $pages_vues = fgets($monfichier); // On lit la première ligne (nombre de pages vues)
  47. fseek($monfichier, 0); // On remet le curseur au début du fichier.
  48. fputs($monfichier, $coucou); // On écrit le nouveau nombre de pages vues.
  49. fclose($monfichier);
  50. $em = $this->em;
  51. $authTokenHeader = $request->headers->get('X-Auth-Token');
  52. $auth = null;
  53. /*
  54. $auth = $this->em
  55. ->getRepository(AuthToken::class)
  56. ->findOneBy(array("value" => $authTokenHeader));
  57. */
  58. $groupeId = $this->generateRandomString(15);
  59. foreach($positions as $position) {
  60. //For manuel post
  61. $position = $position->location;
  62. $coords = $position->coords;
  63. $latitude = $coords->latitude; // float
  64. $longitude = $coords->longitude; // float
  65. $speed = $coords->speed; // float
  66. $accuracy = $coords->accuracy; // float
  67. $altitude = $coords->altitude; // float
  68. //New coords
  69. $speedAccuracy = $coords->speed_accuracy; // float
  70. $ellipsoidalAltitude = $coords->ellipsoidal_altitude; // float
  71. $floor = $coords->floor; // float
  72. $headingAccuracy = $coords->heading_accuracy; // float
  73. $altitudeAccuracy = $coords->altitude_accuracy; // float
  74. $heading = $coords->heading; // Float
  75. //News index
  76. $mock = null;
  77. if(isset($position->mock)){
  78. $mock = $position->mock; // bool
  79. }
  80. $isMoving = null;
  81. if(isset($position->is_moving)){
  82. $isMoving = $position->is_moving; // bool
  83. }
  84. $event = null;
  85. if(isset($position->event)){
  86. $event = $position->event; // String
  87. }
  88. $odometer = $position->odometer; //Float
  89. $uuid = $position->uuid; // String
  90. //News activity
  91. $activityType = $position->activity->type;
  92. $activityConfidence = $position->activity->confidence;
  93. //News battery
  94. $batteryLevel = $position->battery->level;
  95. $batteryIsCharging = $position->battery->is_charging;
  96. //$bearing = $position['bearing'];
  97. $datePrintString = $position->timestamp;
  98. $datePrint = strtotime($datePrintString);
  99. //Set dateTime with unix timestamp
  100. /*
  101. $timestamp = substr($position['time'], 0, 10);
  102. $dateTime = new DateTime();
  103. $dateTime->setTimestamp($timestamp);
  104. */
  105. //$date = $dateTime;
  106. //New module (hihi) FOR GET SINCE POSITION WITH LAST POSITIONS
  107. /*
  108. if((date("YmdHis") - $dateTime->format("YmdHis")) > 60){
  109. $positionLasts = $this->em
  110. ->getRepository(PositionGps::class)
  111. ->findBy(array(), array("datePrinted" => "DESC"), 1);
  112. if($positionLasts){
  113. $latitude = $positionLasts[0]->getLatitude();
  114. $longitude = $positionLasts[0]->getLongitude();
  115. }
  116. else{
  117. $latitude = $position['latitude'];
  118. $longitude = $position['longitude'];
  119. }
  120. }
  121. else{
  122. $latitude = $position['latitude'];
  123. $longitude = $position['longitude'];
  124. }
  125. */
  126. $PositionGps = new PositionGps();
  127. $PositionGps->setGroupeId($groupeId);
  128. $PositionGps->setAccuracy($accuracy);
  129. $PositionGps->setAltitude($altitude);
  130. $PositionGps->setLatitude($latitude);
  131. $PositionGps->setLongitude($longitude);
  132. $PositionGps->setSpeed($speed);
  133. //NEW FIELDS
  134. $PositionGps->setSpeedAccuracy($speedAccuracy);
  135. $PositionGps->setAltitudeEllipsoidal($ellipsoidalAltitude);
  136. $PositionGps->setFloor($floor);
  137. $PositionGps->setHeadingAccuracy($headingAccuracy);
  138. $PositionGps->setAltitudeAccuracy($altitudeAccuracy);
  139. $PositionGps->setHeading($heading);
  140. $PositionGps->setMock($mock);
  141. $PositionGps->setIsMoving($isMoving);
  142. $PositionGps->setEvent($event);
  143. $PositionGps->setOdometer($odometer);
  144. $PositionGps->setUuid($uuid);
  145. $PositionGps->setActivityType($activityType);
  146. $PositionGps->setActivityConfidence($activityConfidence);
  147. $PositionGps->setBatteryLevel($batteryLevel);
  148. $PositionGps->setBatteryIsCharging($batteryIsCharging);
  149. //Temp
  150. $user = $this->em
  151. ->getRepository(User::class)
  152. ->findOneBy(array("id" => 4));
  153. $PositionGps->setUser($user);
  154. if($auth){
  155. $PositionGps->setUser($auth->getUser());
  156. }
  157. $PositionGps->setDatePrinted(new DateTime($datePrintString));
  158. $PositionGps->setWithFail(false);
  159. $PositionGps->setSync(false);
  160. $em->persist($PositionGps);
  161. }
  162. $em->flush();
  163. return true;
  164. }
  165. /**
  166. * @Rest\View(statusCode=200)
  167. * @Rest\Post("/api/information/position-gps-fail")
  168. */
  169. public function addPositionGpsFailAction(Request $request)
  170. {
  171. //$positions = $request->get("positions");
  172. $positions = $request->request->all();
  173. //TEST VARDUMP FILE
  174. /*
  175. $coucou = serialize($positions);
  176. $monfichier = fopen('/media/tr/www/var/logs/coucou.txt', 'w+');
  177. $pages_vues = fgets($monfichier); // On lit la première ligne (nombre de pages vues)
  178. fseek($monfichier, 0); // On remet le curseur au début du fichier.
  179. fputs($monfichier, $coucou); // On écrit le nouveau nombre de pages vues.
  180. fclose($monfichier);
  181. */
  182. $em = $this->em;
  183. $authTokenHeader = $request->headers->get('X-Auth-Token');
  184. $auth = $this->em
  185. ->getRepository(AuthToken::class)
  186. ->findOneBy(array("value" => $authTokenHeader));
  187. $groupeId = $this->generateRandomString(15);
  188. foreach($positions as $position) {
  189. //For manuel post
  190. $latitude = $position['latitude'];
  191. $longitude = $position['longitude'];
  192. //$date = $position['datePrinted'];
  193. $speed = $position['speed'];
  194. $provider = $position['provider'];
  195. $locationProvider = $position['locationProvider'];
  196. $accuracy = $position['accuracy'];
  197. $altitude = $position['altitude'];
  198. $bearing = $position['bearing'];
  199. //Set dateTime with unix timestamp
  200. $timestamp = substr($position['time'], 0, 10);
  201. $dateTime = new DateTime();
  202. $dateTime->setTimestamp($timestamp);
  203. $latitude = $position['latitude'];
  204. $longitude = $position['longitude'];
  205. $date = $dateTime->format("Y-m-d H:i:s");
  206. $speed = $position['speed'];
  207. $PositionGps = new PositionGps();
  208. if($auth){
  209. $PositionGps->setUser($auth->getUser());
  210. }
  211. $PositionGps->setLatitude($latitude);
  212. $PositionGps->setLongitude($longitude);
  213. $PositionGps->setSpeed($speed);
  214. $PositionGps->setGroupeId($groupeId);
  215. $PositionGps->setProvider($provider);
  216. $PositionGps->setLocationProvider($locationProvider);
  217. $PositionGps->setAccuracy($accuracy);
  218. $PositionGps->setAltitude($altitude);
  219. $PositionGps->setBearing($bearing);
  220. $PositionGps->setDatePrinted(new DateTime($date));
  221. $PositionGps->setWithFail(true);
  222. $PositionGps->setSync(true);
  223. $em->persist($PositionGps);
  224. }
  225. $em->flush();
  226. return true;
  227. }
  228. /**
  229. * @Rest\View(statusCode=200)
  230. * @Rest\Post("/api/information/position-gps-temp")
  231. */
  232. public function addPositionGpsTempAction(Request $request)
  233. {
  234. //TEST VARDUMP FILE
  235. /*
  236. $coucou = serialize($positions);
  237. $monfichier = fopen('/media/tr/www/var/logs/coucou.txt', 'w+');
  238. $pages_vues = fgets($monfichier); // On lit la première ligne (nombre de pages vues)
  239. fseek($monfichier, 0); // On remet le curseur au début du fichier.
  240. fputs($monfichier, $coucou); // On écrit le nouveau nombre de pages vues.
  241. fclose($monfichier);
  242. */
  243. $em = $this->em;
  244. $positions = $request->get("positions");
  245. //$positions = $request->request->all();
  246. foreach ($positions as $position) {
  247. //For manuel post
  248. $latitude = $position['latitude'];
  249. $longitude = $position['longitude'];
  250. $date = $position['datePrinted'];
  251. $speed = $position['speed'];
  252. /*
  253. //Set dateTime with unix timestamp
  254. $timestamp = substr($position['time'], 0, 10);
  255. $dateTime = new DateTime();
  256. $dateTime->setTimestamp($timestamp);
  257. */
  258. $latitude = $position['latitude'];
  259. $longitude = $position['longitude'];
  260. //$date = $dateTime->format("Y-m-d H:i:s");
  261. $speed = $position['speed'];
  262. $PositionGps = new PositionGps();
  263. $PositionGps->setLatitude($latitude);
  264. $PositionGps->setLongitude($longitude);
  265. $PositionGps->setSpeed($speed);
  266. $PositionGps->setDatePrinted(new DateTime($date));
  267. $em->persist($PositionGps);
  268. }
  269. $em->flush();
  270. return true;
  271. }
  272. /**
  273. * @Rest\View(statusCode=201)
  274. * @Rest\Get("/api/information/position-gps")
  275. */
  276. public function getPositionGpsAction(Request $request)
  277. {
  278. $positionGpsList = $this->em
  279. ->getRepository(PositionGps::class)
  280. ->findAll();
  281. return $positionGpsList;
  282. }
  283. /**
  284. * @Route("/debug/information/position", name="information_position_debug")
  285. */
  286. public function getPositionDebugAction(Request $request)
  287. {
  288. $session = new Session();
  289. $userCurrent = $session->get('currentUser');
  290. $positionGpsList = $this->em
  291. ->getRepository(PositionGps::class)
  292. ->findBy(array("user" => $userCurrent), array('datePrinted' => 'DESC', 'dateCreatedByServer' => 'DESC'), 6);
  293. return $this->render('debug/position.html.twig', Array(
  294. "positionGpsList" => $positionGpsList
  295. ));
  296. }
  297. /**
  298. * @Route("/debug/information/live", name="information_live_debug")
  299. */
  300. public function getLiveDebugAction(Request $request)
  301. {
  302. $session = new Session();
  303. $userCurrent = $session->get('currentUser');
  304. $positionGpsList = $this->em
  305. ->getRepository(PositionGps::class)
  306. ->findBy(array("user" => $userCurrent), array('datePrinted' => 'DESC', 'dateCreatedByServer' => 'DESC'), 25);
  307. $currentPostition = null;
  308. $currentAgo = null;
  309. $lastHobit = null;
  310. $currentHobitAgo = null;
  311. $newHobitAgo = null;
  312. $newHobit = null;
  313. if($positionGpsList){
  314. $currentPostition = $positionGpsList[0];
  315. $currentAgo = $this->TimeToJourJ($currentPostition->getDateCreatedByServer()->format("Y-m-d H-i-s"), time());
  316. //Last hobit
  317. $timeLimit = 600;
  318. $nextHobitAgo = "3 minutes";
  319. $tabHobit = [];
  320. $limitWidthHobit = 0.20;
  321. $limitTimeHobit = 600;
  322. $limitForCreateHobit = 30;
  323. $limitStable = 5;
  324. $maxSum = 100;
  325. $numberIntervalPrintedAndServer = 60;
  326. //Debug
  327. $debug = true;
  328. $debugAndFlush = false;
  329. $tabHobit = $this->tools->dreamHobit($debug, $debugAndFlush, $limitWidthHobit, $limitTimeHobit, $limitForCreateHobit, $limitStable, $maxSum, $numberIntervalPrintedAndServer, $userCurrent);
  330. if($tabHobit){
  331. $lastHobit = $tabHobit[(count($tabHobit) - 1)];
  332. $currentHobitAgo = $this->TimeToJourJ($lastHobit['startDateTime']->format("Y-m-d H-i-s"), time());
  333. $newHobitAgo = false;
  334. $newHobit = false;
  335. //Next Hobit
  336. if(($currentPostition->getDatePrinted()->getTimestamp() + 120) < time()){
  337. $newHobit = $currentPostition;
  338. $newHobitAgo = $this->TimeToJourJ($lastHobit['endDateTime']->format("Y-m-d H-i-s"), time());
  339. }
  340. if($newHobitAgo == false){
  341. $currentHobitAgo = $this->TimeToJourJ($lastHobit['startDateTime']->format("Y-m-d H-i-s"), time());
  342. }
  343. else{
  344. if(($lastHobit['startDateTime']->getTimestamp() - $lastHobit['endDateTime']->getTimestamp()) > 600){
  345. $currentHobitAgo = $this->TimeToJourJ($lastHobit['startDateTime']->format("Y-m-d H-i-s"), time());
  346. }
  347. else{
  348. $currentHobitAgo = $this->TimeToJourJ($lastHobit['startDateTime']->format("Y-m-d H-i-s"), $lastHobit['endDateTime']->getTimestamp());
  349. }
  350. }
  351. }
  352. }
  353. return $this->render('debug/live.html.twig', Array(
  354. "positionGpsList" => $positionGpsList,
  355. "currentPosition" => $currentPostition,
  356. "currentAgo" => $currentAgo,
  357. "lastHobit" => $lastHobit,
  358. "currentHobitAgo" => $currentHobitAgo,
  359. "newHobitAgo" => $newHobitAgo,
  360. "newHobit" => $newHobit,
  361. ));
  362. }
  363. /**
  364. * @Route("/debug/information/live-refresh", name="information_live_refresh_debug")
  365. */
  366. public function getLiveRefreshDebugAction(Request $request, $isApi = false)
  367. {
  368. $session = new Session();
  369. $userCurrent = $session->get('currentUser');
  370. $positionGpsList = $this->em
  371. ->getRepository(PositionGps::class)
  372. ->findBy(array("user" => $userCurrent), array('datePrinted' => 'DESC', 'dateCreatedByServer' => 'DESC'), 25);
  373. $currentPostition = null;
  374. $currentAgo = null;
  375. $lastHobit = null;
  376. $currentHobitAgo = null;
  377. $newHobitAgo = null;
  378. $newHobit = null;
  379. $dataHere = false;
  380. $currentPostionTab = [];
  381. $lastHobitTab = [];
  382. if($positionGpsList){
  383. $dataHere = true;
  384. $currentPostition = $positionGpsList[0];
  385. $currentAgo = $this->TimeToJourJ($currentPostition->getDateCreatedByServer()->format("Y-m-d H-i-s"), time());
  386. $currentPostionTab = array(
  387. "start" => $currentPostition->getDatePrinted()->format("d-m-Y H:i:s"),
  388. "latitude" => $currentPostition->getLatitude(),
  389. "longitude" => $currentPostition->getLongitude(),
  390. );
  391. //Last hobit
  392. $timeLimit = 600;
  393. $nextHobitAgo = "3 minutes";
  394. $tabHobit = [];
  395. $limitWidthHobit = 0.20;
  396. $limitTimeHobit = 600;
  397. $limitForCreateHobit = 30;
  398. $limitStable = 5;
  399. $maxSum = 100;
  400. $numberIntervalPrintedAndServer = 60;
  401. //Debug
  402. $debug = true;
  403. $debugAndFlush = false;
  404. $tabHobit = $this->tools->dreamHobit($debug, $debugAndFlush, $limitWidthHobit, $limitTimeHobit, $limitForCreateHobit, $limitStable, $maxSum, $numberIntervalPrintedAndServer, $userCurrent);
  405. if(count($tabHobit) > 0){
  406. $lastHobit = $tabHobit[(count($tabHobit) - 1)];
  407. }
  408. else{
  409. $lastHobit = null;
  410. }
  411. $newHobitAgo = false;
  412. $newHobit = false;
  413. if($lastHobit){
  414. //Next Hobit
  415. if(($currentPostition->getDatePrinted()->getTimestamp() + 120) < time()){
  416. $newHobit = $currentPostition;
  417. $newHobitAgo = $this->TimeToJourJ($lastHobit['endDateTime']->format("Y-m-d H-i-s"), time());
  418. }
  419. if($newHobitAgo == false){
  420. $currentHobitAgo = $this->TimeToJourJ($lastHobit['startDateTime']->format("Y-m-d H-i-s"), time());
  421. }
  422. else{
  423. if(($lastHobit['startDateTime']->getTimestamp() - $lastHobit['endDateTime']->getTimestamp()) > 600){
  424. $currentHobitAgo = $this->TimeToJourJ($lastHobit['startDateTime']->format("Y-m-d H-i-s"), time());
  425. }
  426. else{
  427. $currentHobitAgo = $this->TimeToJourJ($lastHobit['startDateTime']->format("Y-m-d H-i-s"), $lastHobit['endDateTime']->getTimestamp());
  428. }
  429. }
  430. }
  431. else{
  432. $newHobit = $currentPostition;
  433. $newHobitAgo = $this->TimeToJourJ($currentPostition->getDatePrinted()->format("Y-m-d H-i-s"), time());
  434. $currentHobitAgo = $this->TimeToJourJ($currentPostition->getDatePrinted()->format("Y-m-d H-i-s"), time());
  435. }
  436. $lastHobitTab = array(
  437. "start" => $lastHobit ? $lastHobit['startDateTime']->format("d-m-Y H:i:s") : null,
  438. "end" => $lastHobit ? $lastHobit['endDateTime']->format("d-m-Y H:i:s") : null,
  439. "latitude" => $lastHobit ? $lastHobit['data']->getLatitude() : null,
  440. "longitude" => $lastHobit ? $lastHobit['data']->getLongitude() : null,
  441. );
  442. }
  443. $data = array(
  444. "currentAgo" => $currentAgo,
  445. "currentPostition" => $currentPostionTab,
  446. "currentAgo" => $currentAgo,
  447. "lastHobit" => $lastHobitTab,
  448. "currentHobitAgo" => $currentHobitAgo,
  449. "newHobitAgo" => $newHobitAgo,
  450. "dataHere" => $dataHere
  451. );
  452. return new JsonResponse([ 'return' => 'success', 'data' => $data]);
  453. }
  454. /**
  455. * @Route("/debug/information/positions/{day}", name="information_positions_debug")
  456. */
  457. public function getPositionsDebugAction(Request $request, $day = "today")
  458. {
  459. if($day == "today"){
  460. $date = date("Y-m-d");
  461. }
  462. else if($day == "yesterday"){
  463. $date = date('Y-m-d', strtotime('-1 day'));
  464. }
  465. else{
  466. $date = date($day);
  467. }
  468. $session = new Session();
  469. $userCurrent = $session->get('currentUser');
  470. $positionGpsList = $this->em
  471. ->getRepository(PositionGps::class)
  472. ->findPositionByDate($date, $userCurrent);
  473. $positionGpsListArray = [];
  474. foreach ($positionGpsList as $key => $positionGps) {
  475. $positionGpsListArray[$key][0] = floatval($positionGps->getLatitude());
  476. $positionGpsListArray[$key][1] = floatval($positionGps->getLongitude());
  477. $positionGpsListArray[$key][2] = strval($positionGps->getDatePrinted()->format('d-m-Y H:i'));
  478. }
  479. return $this->render('debug/positions.html.twig', Array(
  480. "positionGpsList" => $positionGpsList,
  481. "positionGpsListArray" => $positionGpsListArray
  482. ));
  483. }
  484. function generateRandomString($length = 10) {
  485. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  486. $charactersLength = strlen($characters);
  487. $randomString = '';
  488. for ($i = 0; $i < $length; $i++) {
  489. $randomString .= $characters[rand(0, $charactersLength - 1)];
  490. }
  491. return $randomString;
  492. }
  493. function Date_ConvertSqlTab($date_sql) {
  494. $jour = substr($date_sql, 8, 2);
  495. $mois = substr($date_sql, 5, 2);
  496. $annee = substr($date_sql, 0, 4);
  497. $heure = substr($date_sql, 11, 2);
  498. $minute = substr($date_sql, 14, 2);
  499. $seconde = substr($date_sql, 17, 2);
  500. $key = array('annee', 'mois', 'jour', 'heure', 'minute', 'seconde');
  501. $value = array($annee, $mois, $jour, $heure, $minute, $seconde);
  502. $tab_retour = array_combine($key, $value);
  503. return $tab_retour;
  504. }
  505. function AuPluriel($chiffre) {
  506. if($chiffre>1) {
  507. return 's';
  508. };
  509. }
  510. function TimeToJourJ($date_sql, $mkt_now) {
  511. $tab_date = $this->Date_ConvertSqlTab($date_sql);
  512. $mkt_jourj = mktime($tab_date['heure'],
  513. $tab_date['minute'],
  514. $tab_date['seconde'],
  515. $tab_date['mois'],
  516. $tab_date['jour'],
  517. $tab_date['annee']);
  518. $diff = $mkt_jourj - $mkt_now;
  519. $unjour = 3600 * 24;
  520. if($diff>=$unjour) {
  521. // EN JOUR
  522. $calcul = $diff / $unjour;
  523. return 'Il reste <strong>'.ceil($calcul).' day'.$this->AuPluriel($calcul).
  524. '</strong>';
  525. } elseif($diff<$unjour && $diff>=0 && $diff>=3600) {
  526. // EN HEURE
  527. $calcul = $diff / 3600;
  528. return 'Il reste <strong>'.ceil($calcul).' hour'.$this->AuPluriel($calcul).
  529. '</strong>';
  530. } elseif($diff<$unjour && $diff>=0 && $diff<3600) {
  531. // EN MINUTES
  532. $calcul = $diff / 60;
  533. return 'Il reste <strong>'.ceil($calcul).' minute'.$this->AuPluriel($calcul).
  534. '</strong>';
  535. } elseif($diff<0 && abs($diff)<60) {
  536. // DEPUIS EN MINUTES
  537. $calcul = abs($diff);
  538. return '<strong>'.ceil($calcul).' second'.$this->AuPluriel($calcul).
  539. '</strong>';
  540. } elseif($diff<0 && abs($diff)<3600) {
  541. // DEPUIS EN MINUTES
  542. $calcul = abs($diff) + 3600;
  543. $minute = date('i', $calcul);
  544. $seconde = date('s', $calcul);
  545. return '<strong>'.$minute.' minute'.$this->AuPluriel($minute).
  546. ' and '.$seconde.'s</strong>';
  547. } elseif($diff<0 && abs($diff)<$unjour) {
  548. // DEPUIS EN HEURES
  549. $calcul = abs($diff) - 3600;
  550. $hour = date('H', $calcul);
  551. $minute = date('i', $calcul);
  552. $seconde = date('s', $calcul);
  553. return '<strong>'.$hour.' hour'.$this->AuPluriel($hour).', '.$minute.' minute'.$this->AuPluriel($minute).
  554. ' and '.$seconde.'s</strong>';
  555. } else {
  556. // DEPUIS EN JOUR
  557. $calculDay = (abs($diff) / $unjour) - 1;
  558. $calcul = (abs($diff) - ($unjour * ceil($calculDay)) - 3600);
  559. $hour = date('H', $calcul);
  560. $minute = date('i', $calcul);
  561. $seconde = date('s', $calcul);
  562. return '<strong>'.ceil($calculDay).' day, '.$hour.' h, '.$minute.' m'.
  563. ', '.$seconde.'s</strong>';
  564. };
  565. }
  566. function ConvertisseurTime($Time){
  567. if($Time < 3600){
  568. $heures = 0;
  569. if($Time < 60){$minutes = 0;}
  570. else{$minutes = round($Time / 60);}
  571. $secondes = floor($Time % 60);
  572. }
  573. else{
  574. $heures = round($Time / 3600);
  575. $secondes = round($Time % 3600);
  576. $minutes = floor($secondes / 60);
  577. }
  578. $secondes2 = round($secondes % 60);
  579. $TimeFinal = "$minutes minute".$this->AuPluriel($minutes)." and $secondes2 s";
  580. return $TimeFinal;
  581. }
  582. function dateDiff($date1, $date2){
  583. $diff = abs($date1 - $date2); // abs pour avoir la valeur absolute, ainsi éviter d'avoir une différence négative
  584. $retour = array();
  585. $tmp = $diff;
  586. $retour['second'] = $tmp % 60;
  587. $tmp = floor( ($tmp - $retour['second']) /60 );
  588. $retour['minute'] = $tmp % 60;
  589. $tmp = floor( ($tmp - $retour['minute'])/60 );
  590. $retour['hour'] = $tmp % 24;
  591. $tmp = floor( ($tmp - $retour['hour']) /24 );
  592. $retour['day'] = $tmp;
  593. return $retour;
  594. }
  595. function get_distance_m($lastHobitLat, $lastHobitLong, $newHobitLat, $newHobitLong) {
  596. $lat1 = $lastHobitLat;
  597. $lng1 = $lastHobitLong;
  598. $lat2 = $newHobitLat;
  599. $lng2 = $newHobitLong;
  600. $earth_radius = 6378137;
  601. $rlo1 = deg2rad($lng1);
  602. $rla1 = deg2rad($lat1);
  603. $rlo2 = deg2rad($lng2);
  604. $rla2 = deg2rad($lat2);
  605. $dlo = ($rlo2 - $rlo1) / 2;
  606. $dla = ($rla2 - $rla1) / 2;
  607. $a = (sin($dla) * sin($dla)) + cos($rla1) * cos($rla2) * (sin($dlo) * sin($dlo));
  608. $d = 2 * atan2(sqrt($a), sqrt(1 - $a));
  609. return ($earth_radius * $d);
  610. }
  611. }