timesheet source code
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

174 lines
5.3KB

  1. <?php
  2. namespace Biukop;
  3. require_once(ABSPATH . "wp-config.php");
  4. require_once(ABSPATH . "wp-includes/wp-db.php");
  5. class AddrMap{
  6. private $user1; //wp-user
  7. private $user2; //wp-user
  8. private $origin;//addr
  9. private $destination; //addr
  10. private $json;//google response
  11. private $dist;//distance in meter
  12. private $error;//bool error
  13. public function __construct(String $user1_login, String $user2_login) {
  14. $this->error = false;
  15. $this->user1 = get_user_by('login', $user1_login);
  16. $this->user2 = get_user_by('login', $user2_login);
  17. //convert user to their address
  18. $this->origin = $this->get_user_address($user1_login);
  19. $this->destination = $this->get_user_address($user2_login);
  20. //get distance from db
  21. $this->dist = $this->get_distance_from_db();
  22. if ($this->dist == false){
  23. //get distance from google map
  24. $response = $this->get_google_direction($this->origin, $this->destination);
  25. if ($response == false){
  26. //mark error
  27. $this->error == true;
  28. }else{
  29. $this->json = $response;
  30. $this->dist = $this->convert_response_to_dist_in_meter($response);
  31. }
  32. //store it in db for future use
  33. $this->update_db();
  34. }
  35. }
  36. public function has_error(){
  37. return $this->error;
  38. }
  39. public function get_error_msg(){
  40. if($this->error)
  41. return "Cannot get distance getween $this->user1->display_name and $this->user2->display_name";
  42. else
  43. return "";
  44. }
  45. public function get_dist(){
  46. if ($this->error)
  47. return 0;
  48. return $this->dist;
  49. }
  50. public function get_dist_in_km(){
  51. if ($this->error)
  52. return 0;
  53. return $this->dist/1000;
  54. }
  55. private function update_db()
  56. {
  57. global $wpdb;
  58. $table_name= $this->get_table_name();
  59. $sql = "INSERT INTO $table_name (origin,destination,response,distance) VALUES ('%s', '%s', '%s', %d) ;";
  60. $query = $wpdb->prepare($sql, array(
  61. $this->origin,
  62. $this->destination,
  63. json_encode($this->json),
  64. $this->dist,
  65. ));
  66. $results = $wpdb->get_results($query);
  67. if ( $results == false) {
  68. //failed
  69. $lastid = $wpdb->insert_id;
  70. $msg = sprintf("Insert into address distance db error: date=%s, user1_id=%s, user2_id=%s, orign=%s, dest=%s, resp=%s, dist=%d, db_error=%s",
  71. date('Y-m-d, H:i:s'),
  72. $this->user1->ID,
  73. $this->user2->ID,
  74. $this->origin,
  75. $this->destination,
  76. json_encode($this->json),
  77. $this->dist,
  78. $wpdb->last_error
  79. );
  80. $error_log = plugin_dir_path(__FILE__) . "/logs/addr.log";
  81. file_put_contents($error_log, $msg, FILE_APPEND);
  82. }
  83. }
  84. private function get_user_address($login){
  85. $user = get_user_by('login', $login);
  86. $addr = get_user_meta($user->ID, 'address', true);
  87. return $addr;
  88. }
  89. private function get_table_name()
  90. {
  91. global $wpdb;
  92. $table_name = $wpdb->prefix . 'acare_addr_distance';
  93. return $table_name;
  94. }
  95. private function get_distance_from_db(){
  96. global $wpdb;
  97. $table_name = $this->get_table_name();
  98. $sql = "SELECT * FROM $table_name WHERE origin='%s' and destination='%s' ";
  99. $query = $wpdb->prepare($sql, array(
  100. $this->origin,
  101. $this->destination,
  102. ));
  103. $row = $wpdb->get_row($query);
  104. if ( $row == null )
  105. return false;
  106. return (int) $row->distance; //in meters;
  107. }
  108. private function super_json_decode($json, &$value, $assoc = false, $depth = 512, $options = 0) {
  109. $pValue = false;
  110. $result = json_decode($json, $assoc, $depth);
  111. if(json_last_error() == JSON_ERROR_NONE) {
  112. $value = $result;
  113. return true;
  114. }
  115. return false;
  116. }
  117. private function convert_response_to_dist_in_meter($resp){
  118. $distance = -1;
  119. if (count($resp->routes) >0)
  120. {
  121. $legs = $resp->routes[0]->legs;
  122. foreach ($legs as $l){
  123. $distance += $l->distance->value;
  124. }
  125. }
  126. return $distance;
  127. }
  128. private function get_google_direction($origin, $destination) {
  129. $data = "";
  130. $arg = array(
  131. 'origin' => $origin,
  132. 'destination' => $destination,
  133. 'key'=>'AIzaSyDMRjRh3wu0zTQneYboTk5VgQio3XABz_o',
  134. );
  135. $ch = curl_init("https://maps.googleapis.com/maps/api/directions/json?". http_build_query($arg));
  136. curl_setopt_array($ch, array(
  137. CURLOPT_CONNECTTIMEOUT => 10,
  138. CURLOPT_RETURNTRANSFER => true,
  139. CURLOPT_TIMEOUT => 60,
  140. ));
  141. $response = false;
  142. if($this->super_json_decode(curl_exec($ch), $data)) {
  143. $response = $data;
  144. }
  145. $info = curl_getinfo($ch);
  146. if($info['http_code'] !== 200) {
  147. return false;
  148. }
  149. return $response;
  150. //var_dump($response);
  151. }
  152. }